I am trying to add SSH key to ~/.ssh/config using echo "IdentityFile ~/.ssh/my-ssh-key" >> ~/.ssh/config

It is showing me error -bash: /Users/gaurav/.ssh/config: Permission denied

What i am doing wrong?

Permission to folders

total 64
-rw-r--r--  1 root    staff    85 Apr 15 12:59 config
-rw-------  1 gaurav  staff  1766 Jan  4 23:11 github_rsa
-rw-r--r--@ 1 gaurav  staff   404 Mar 20 09:50 github_rsa.pub
-r--------  1 gaurav  staff  1766 Mar 20 10:05 google-sb-server
-rw-r--r--  1 gaurav  staff   388 Mar 20 10:05 google-sb-server.pub
-rw-------  1 gaurav  staff  3247 Jan  5 10:37 id_rsa
-rw-r--r--  1 gaurav  staff   726 Jan  5 10:37 id_rsa.pub
-rw-r--r--  1 gaurav  staff  2230 Mar 20 12:45 known_hosts

The config file in ~/.ssh/ has the wrong owner/permissions. It's owned by root.

Change the owner to gaurav with:

sudo chown gaurav:staff ~/.ssh/config

enter your password and it will work (if you are a sudoer).

| improve this answer | |
  • It is completed thanks . Now i am able to do . Once more thing giving ownership to current_user is it safe in case of SSH – twister_void Apr 15 '17 at 11:32
  • @twister_void I don't really understand your question in the comment. Please add a second question regarding security issues (of the config file?) on the main (apple.stackexchange) site. – klanomath Apr 15 '17 at 11:35
  • Home directory on the server should not be writable by others: chmod go-w ~/
  • SSH folder on the server needs 700 permissions: chmod 700 ~/.ssh
  • Authorized_keys file needs 644 permissions: chmod 644 ~/.ssh/authorized_keys
  • Make sure that gaurav owns the files/folders and not root: chown user:gaurav authorized_keys and chown user:gaurav ~/.ssh
  • Put the generated public key (from ssh-keygen) in the user's authorized_keys file on the server
  • Make sure that user's home directory is set to what you expect it to be and that it contains the correct .ssh folder that you've been modifying. If not, use usermod -d ~/ gaurav to fix the issue
  • Finally, restart ssh: service ssh restart
  • Then make sure client has the public key and private key files in the local user's .ssh folder and login: ssh gaurav@host.com

Best of Luck!

| improve this answer | |

You must log in to answer this question.

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