Search code examples
githubssh-keys

How could I setup a permanent connection to github from server with SSH key?


I followed the guide from https://help.github.com/articles/generating-ssh-keys/ to generate a ssh key used to connect to github from my server.

The problem is if I close the shell and login the server again, I have to repeat below steps to reconnect to github, otherwise the accessing will be denied:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

How can I setup a permanent connection with github?


Solution

  • Since you are logging on to the command-line (presumably using SSH) all applications that you run, including ssh-agent, stop when you log out.

    In my opinion your best bet is to have your CentOS machine start ssh-agent for you each time you log in, then manually use ssh-add to add your key. This can be done by modifying your shell's login script. If you are using bash, this file is called ~/.bash_profile. If you are using another shell, refer to its documentation and choose an appropriate file.

    Edit one of these files and add eval "$(ssh-agent -s)" at the end.

    Now, each time you log in ssh-agent should automatically start, and you can optionally run ssh-add (since you are using a file that ssh-add looks for by default you don't need to pass the file to the command) to add your key, then do Git stuff.

    It sounds like you want to automate the adding of the key as well. This isn't something that I would choose to do since I like adding keys to be explicit, but because you don't have a passphrase on your SSH key you should be able to add ssh-add to the same file we modified above (after we start the agent, of course) and have your key added automatically.