Thursday, June 5, 2014

Simplify SSH Login

On a Linux machine, visiting a remote Unix/Linux machine is usually running command like 'ssh foo@hpc.example.com' and then typing in password once prompted. This process is trivial but  a little bit of annoying that we need to type in the whole lengthy address and password every time. It would be nice if we can simplify the login process so that we access the remote server without typing in the full address, user id and password. Here is a solution (note that all the following procedures are done on the local machine).

First of all, edit/add the SSH configuration file $HOME/.ssh/config with content like the following:
Host hpc
    HostName hpc.example.com
    Port 21
    User foo
Set file mode so that only the current user can read/write this configuration file:
chmod 600 $HOME/.ssh/config

Here we have set up an alias 'hpc' for the full remote machine address and 'ssh hpc' can initialize the login process without using the lengthy one 'ssh foo@hpc.example.com'. But we still need to type in password to get access.

Let us next set up passwordless ssh login.

Make a pair of private and public keys by:
ssh-keygen -t rsa

Note that passphrase should be left empty when prompted. By default, two files id_rsa.pub (the public key) and id_rsa (the private key) will be generated in the folder ~/.ssh/.

Copy the public key to the remote machine and then append its content to file ~/.ssh/authorized_keys:
ssh hpc cat id_rsa.pub >>~/.ssh/authorized_keys <~/.ssh/id_rsa.pub

Finally, change the file mode of the private key file so that other users can not meddle with it:
chmod 600 ~/.ssh/id_rsa

Now everything is set and we should be able to access the remote server without a password:

ssh hpc




No comments: