{"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":"

\"keys-banner\"<\/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>\n

This 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>\n

If 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>\n

You\u2019re done! Save the file and open your new SSH session by typing:<\/p>\n

\nssh myserver\n<\/pre>\n

At 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>\n

then 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>\n

Which 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>\n

congratulation, 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>\n

then 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

Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,53,66],"tags":[48,49,92,51,64],"aioseo_notices":[],"views":5,"_links":{"self":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/688"}],"collection":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/comments?post=688"}],"version-history":[{"count":0,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/posts\/688\/revisions"}],"wp:attachment":[{"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/media?parent=688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/categories?post=688"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ndthanh.com\/wp-json\/wp\/v2\/tags?post=688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}