setting up ssh-copy-id on ubuntu

I'm trying to setup ssh-copy-id on ubuntu I need a second opinion on this, is this a missing .pub path or wrong settings for ssh

sammy@samuel-pc:~$ cat ~/.ssh/id_rsa.pub
cat: /home/sammy/.ssh/id_rsa.pub: Permission denied
sammy@samuel-pc:~$ ssh-copy-id root@67.205.154.128
/usr/bin/ssh-copy-id: ERROR: failed to open ID file '/home/sammy/.pub': No such file (to install the contents of '/home/sammy/.pub' anyway, look at the -f option)
sammy@samuel-pc:~$ cd .ssh
-bash: cd: .ssh: Permission denied
sammy@samuel-pc:~$ sudo ls -l ~/.ssh/id_rsa.pub
[sudo] password for sammy:
-rwxr-xr-x 1 sammy sammy 397 Jul 12 20:25 /home/sammy/.ssh/id_rsa.pub

I've also tried sshing from my computer, but the authentication was broken and I tried to fix it but it fails

samuel@samuel-pc:~$ ssh-copy-id
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
's password:
sh: 1: cannot create .ssh/authorized_keys: Permission denied

Result from sudo ls -lsa ~/.ssh

sammy@samuel-pc:~$ sudo ls -lsa ~/.ssh
[sudo] password for sammy:
Sorry, try again.
[sudo] password for sammy:
total 28
4 drw-rw-rw- 2 sammy sammy 4096 Jul 13 21:25 .
4 drwxr-xr-x 9 sammy sammy 4096 Jul 19 23:55 ..
4 -rwxr-xr-x 1 sammy sammy 790 Jul 13 21:35 authorized_keys
4 -rwxr-xr-x 1 sammy sammy 1675 Jul 12 20:25 id_rsa
4 -rwxr-xr-x 1 sammy sammy 397 Jul 12 20:25 id_rsa.pub
4 -rwxr-xr-x 1 sammy sammy 1024 Jul 13 12:48 .id_rsa.pub.swp
4 -rwxr-xr-x 1 sammy sammy 222 Jul 12 20:21 known_hosts

and result for ls -ld ~/.ssh

sammy@samuel-pc:~$ ls -ld ~/.ssh
drw-rw-rw- 2 sammy sammy 4096 Jul 13 21:25 /home/sammy/.ssh

How can I get right access for ssh in the server?

6

3 Answers

The following command should fix the permission problem of your ~/.ssh folder

chmod 700 ~/.ssh
  • This will allow read/write/execution to the folder - only for the owner (yourself)
  • execution means - change directory

The following command will set the correct permission of the files inside the ~/.ssh folder

chmod 400 ~/.ssh/*
  • The files in ~/.ssh folder should have owner read-only permission
  • chmod 400 set the owner read-only permission

After you fix the ~/.ssh folder/files permission you can try to execute again the ssh-copy-id command

6

In order to use ssh-copy-id you need to have ssh (id_rsa) key files generated. In my case I accidentally generated key with sudo which resulted in id_rsa and id_rsa.pub files generated under /root/.ssh/ instead of /home/user/.ssh/. So afterwards trying to accomplish ssh-copy-id someuser@somehost I've got:

/usr/bin/ssh-copy-id: ERROR: failed to open ID file '/home/user/.pub': No such file
(to install the contents of '/home/user/.pub' anyway, look at the -f option)

The temptation is to use -f option, however real solution is simple: just generate ssh private key without sudo:

ssh-keygen -t rsa

Typically below ~/.ssh/ file content should work:

user@ubuntu:~$ ll .ssh/
total 28
drwx------ 2 user group 4096 Dec 30 16:17 ./
drwxr-xr-x 15 user group 4096 Dec 30 16:17 ../
-rw------- 1 user group 1773 Dec 30 17:23 authorized_keys
-rw------- 1 user group 1679 Jan 1 2016 id_rsa
-rw-r--r-- 1 user group 399 Jan 1 2016 id_rsa.pub
-rw-r--r-- 1 user group 2436 Dec 30 16:17 known_hosts
user@ubuntu:~$

So your issue is caused by a few errors in your file permissions.

First: Your directory has no executable flag which means your system can not actually change into it and access the files inside of it. This should fix that.

chmod +x ~/.ssh

Afterwards you will need to adjust your permissions of the files of the public keys and private keys, which should only be readable by your user and not anything by any other user. Which you accomplish with:

chmod 400 ~/.ssh/*

This should fix your issue. Sometimes ssh is really picky and can cause still errors if your /home/user is still writable by the group/others. So for safty remove write permission from the home for group and others aswell.

chmod go-w ~/

This should fix your issues.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like