MacOS – Permission denied adding SSH key to .ssh/config

macospermissionsshterminal

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

Best Answer

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).