setting up ssh-copy-id on ubuntu

14,153

Solution 1

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

Solution 2

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:~$
Share:
14,153

Related videos on Youtube

Samuel Muiruri
Author by

Samuel Muiruri

Updated on September 18, 2022

Comments

  • Samuel Muiruri
    Samuel Muiruri over 1 year

    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 [email protected]
    
    /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 [email protected]
    /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
    [email protected]'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?

    • Yaron
      Yaron almost 7 years
      please edit your question and add the result of ls -lsa ~/.ssh
    • muru
      muru almost 7 years
      WTF don't you have permissions to your own ~/.ssh?
    • Ravexina
      Ravexina almost 7 years
      sudo ls -lsa ~/.ssh might be better. did you create your keys using root user?
    • Ravexina
      Ravexina almost 7 years
      Can you add the output of: ls -ld ~/.ssh too please?
    • Samuel Muiruri
      Samuel Muiruri almost 7 years
      added the code @Ravexina
    • Ravexina
      Ravexina almost 7 years
      use chmod u+x ~/.ssh then try again.
  • Samuel Muiruri
    Samuel Muiruri almost 7 years
    thanks it worked. Can I use chmod 400 foldername on the other folder... where I store images I can't write data to is using sftp previously could.
  • Yaron
    Yaron almost 7 years
    You can't execute chmod 400 on folders, as it will remove the option to access the files in the directory (e.g. using change dir), you can set chmod 700 dir-name
  • Samuel Muiruri
    Samuel Muiruri almost 7 years
    I tried chmod 700 on the folder now when I try to go into it using filezilla I get Command: cd "gallery" Error: Directory /home/sammy/revamp/gallery: permission denied Error: Failed to retrieve directory listing
  • Yaron
    Yaron almost 7 years
    @SamuelMuiruri - I think that you are asking a new question, if so, please open a new question, and provide all relevant details (e.g. I'm trying to copy files using filezilla, from computerA to computerB, from folderA to folderB, the directory/file permissions on folderA is ... ls -lsa folderA. I get the following error from filezilla... etc...
  • newbie
    newbie over 4 years
    @Yaron, is there any flag to tell ssh-copy-id to change correct the permissions as it runs?
  • Yaron
    Yaron over 4 years
    @newbie - ssh-copy-id manual doesn't include such flag
  • Shadab K
    Shadab K about 4 years
    it didn't fix my issue