Wednesday, August 28, 2013

Git - Using SSH key to push code without entering password

I had to enter password every time I push my commits to git server. I can avoid doing it again and again by using SSH key:

1. In my computer, generate SSH Key:

$ cd ~/.ssh
$ ssh-keygen -t rsa -C "dangtrinhnt@gmail.com"

The above command will create 2 files, id_rsa and id_rsa.pub. id_rsa.pub is your public key. Send it to the git server.


2. Copy the public key to the remote server:

$ ssh-copy-id -i ~/.ssh/id_rsa.pub myusername@remotehost

The command will prompt me to enter password of my user account in the remotehost. Enter and done.

+ After that, if I can ssh to the remote server without error, I can assume that it works:

trinh@mylocalpc$ ssh myusername@remotehost
myusername@remotehost$

+ If I get the error "Agent admitted failure to sign using the key", run the following command and try to ssh again:

$ ssh-add



From now on, whenever I push code to remote git server, I don't have to input my password anymore.


References:
http://git-scm.com/book/en/Git-on-the-Server-Generating-Your-SSH-Public-Key
http://git-scm.com/book/en/Git-on-the-Server-Setting-Up-the-Server