{"id":688,"date":"2013-06-26T09:28:55","date_gmt":"2013-06-26T09:28:55","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=688"},"modified":"2013-06-26T09:28:55","modified_gmt":"2013-06-26T09:28:55","slug":"unix-find-more-ssh-power-with-ssh-config","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/unix-find-more-ssh-power-with-ssh-config\/","title":{"rendered":"Unix – find more SSH power with ssh config"},"content":{"rendered":"
<\/p>\n
Do you want to use multiple key pairs for github and servers ? Ever wondered how people just type ‘ssh myserver’ and it automagically\u00a0connects ? you will know some cool methods after next few minutes<\/p>\n
<\/p>\n
Normally, you can use alias to connect to server with fewer keystrokes<\/p>\n
\nalias myserver='ssh user@yourhost.com -p 37000'\n# To connect using this alias, just type\nmyserver\n<\/pre>\nThis works fine. you can have a list of shortcut if you add aliases for all your servers in .bashrc (or something like that if you use other shell)<\/p>\n
But this is not the ssh way, using this method, you can’t achieve 1st goal : ‘use multiple key pairs for github and servers’. you can achieve 1st goal and have a nicer approach for 2nd goal with ssh config<\/p>\n
Create an SSH config file<\/strong>
\nTo create an SSH config file, open your terminal and issue the following command:<\/p>\n\nvi ~\/.ssh\/config\n<\/pre>\nIf you\u2019re a little unsure of vi, replace it with your favourite editor, such nano.<\/p>\n
In the same file, copy and paste the following lines:<\/p>\n
\nHost myserver\n HostName myserver.com\n User root\n<\/pre>\nYou\u2019re done! Save the file and open your new SSH session by typing:<\/p>\n
\nssh myserver\n<\/pre>\nAt this point, you have a nicer ssh shortcut, but how to connect to github with multiple keys ? we will need another key pair first<\/p>\n
generate new ssh key<\/strong><\/p>\n
\nssh-keygen -t rsa -f ~\/.ssh\/id_rsa_deploy -C "comment for this key"\n<\/pre>\nthen add these line to you SSH config file<\/p>\n
\nHost github.deploy\nUser git\nHostName github.com\n#use specific ssh key file for this Host (github.deploy) - so we can use it like this : git clone git@github.deploy:[repos name]\nIdentityFile ~\/.ssh\/id_rsa_deploy\n\nHost github.com\nUser git\nIdentityFile ~\/.ssh\/id_rsa\n#Port 22 # you can specify port for your host too\n<\/pre>\nWhich means that if I want to clone a repository using my deploy credentials, I would use the following:<\/p>\n
\ngit clone git@github.deploy:orgname\/some_repository.git\n<\/pre>\ncongratulation, you have done it!<\/p>\n
Note for Mac OS X<\/strong><\/em> : you will need to killall ssh-agent in order to reload config. May be you will need to use this command to see what you need to kill actually (ssh-agent or sshd)<\/p>\n
\nlaunchctl list\n<\/pre>\nthen use this command to kill process<\/p>\n
\nkillall ssh-agent\n<\/pre>\n","protected":false},"excerpt":{"rendered":"Do you want to use multiple key pairs for github and servers ? Ever wondered how people just type ‘ssh myserver’ and it automagically\u00a0connects ? you will know some cool methods after next few minutes Normally, you can use alias to connect to server with fewer keystrokes alias myserver=’ssh user@yourhost.com -p 37000′ # To connect…<\/p>\n