Generating the SSH key
ssh-keygen -t rsa
[[email protected]]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/droidzone/.ssh/id_rsa): /home/droidzone/.ssh/id_rsa_hostgator_ubuntu Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/droidzone/.ssh/id_rsa_hostgator_ubuntu. Your public key has been saved in /home/droidzone/.ssh/id_rsa_hostgator_ubuntu.pub. The key fingerprint is: 1a:24:ff:8d:f0:8a:64:c4:7a:de:d1:8f:15:5a:55:c5 [email protected] The key's randomart image is: +--[ RSA 2048]----+ +-----------------+
Now, you can view your public key with:
Logging in to the server
You can login to the server using your password with:
ssh -p 2222 [email protected]
Here, myusername is my user name (obviously), and 184.108.40.206 is the ip address of the host.
[[email protected]]$ ssh -p 2222 [email protected] [email protected]'s password: Last login: Thu Sep 2 12:06:19 2012 from 220.127.116.11 [hostgator ~]$
Adding the key to the remote server
Now, that you can confirm the server credentials and generated a public/private key pair, you wish to create a passwordless login to access the server quickly from your machine.
You have already created the pair, so the next step is to transfer your public key to the server. You can do this by manually adding the public key to your server’s ~/.ssh/authorized_keys2 or ~/.ssh/authorized_keys (newer). Just do a:
ls -l ~/.ssh
and see which file your server uses.
You should now copy the entire text shown by:
cat /home/droidzone/.ssh/id_rsa_hostgator_ubuntu.pub[/code] and append it to the end of the remote server's ~/.ssh/authorized_keys2 or ~/.ssh/authorized_keys. For this you can open the relevant file in emacs.emacs ~/.ssh/authorized_keys2
At the end of the current last line (last letter), press Enter.
Paste with Ctrl-Shift-V (i.e paste the new public key as the last line).
Save the file with Ctrl-X Ctrl-C.
Now, you can do a passwordless login to the server with:ssh -i ~/.ssh/id_rsa_hostgator_ubuntu -p 2222 [email protected]
Here, id_rsa_hostgator_ubuntu is your private key file. You can of course automate this with this a bash script.
Adding a ssh key to remote server with a single command:
Generate key:ssh-keygen -t rsa
Add the key:cat ~/.ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'