3

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?

6
  • 2
    please edit your question and add the result of ls -lsa ~/.ssh
    – Yaron
    Jul 20, 2017 at 6:58
  • WTF don't you have permissions to your own ~/.ssh?
    – muru
    Jul 20, 2017 at 7:00
  • sudo ls -lsa ~/.ssh might be better. did you create your keys using root user?
    – Ravexina
    Jul 20, 2017 at 7:02
  • Can you add the output of: ls -ld ~/.ssh too please?
    – Ravexina
    Jul 20, 2017 at 7:06
  • added the code @Ravexina Jul 20, 2017 at 7:20

3 Answers 3

3

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
  • 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. Jul 20, 2017 at 7:27
  • 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
    – Yaron
    Jul 20, 2017 at 7:33
  • 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 Jul 20, 2017 at 7:42
  • @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...
    – Yaron
    Jul 20, 2017 at 7:46
  • @Yaron, is there any flag to tell ssh-copy-id to change correct the permissions as it runs?
    – newbie
    Oct 4, 2019 at 20:52
1

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:~$
-1

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
  • it didn't fix my issue
    – Shadab K
    Feb 15, 2020 at 21:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .