Monday, July 23, 2012

HOW TO SETUP AND CONFIGURE 'ssh' ON LINUX (UBUNTU)

SSH (Secure Shell) is a network protocol secure data communication, remote shell services or command execution and other secure network services between two networked computers that it connects via a secure channel over an insecure network. The ssh server runs on a machine (server) and ssh client runs on another machine (client).ssh has 2 main components :1- ssh : The command we use to connect to remote machines - the client. 2- sshd : The daemon that is running on the server and allows clients to connect to the server.ssh is pre-enabled on Linux, but in order to start sshd daemon, we need to install ssh first. Use this command to do that :$ sudo apt-get install sshThis will install ssh on your machine. In order to check if ssh is setup properly do this :$ which sshIt will throw this line on your terminal/usr/bin/ssh$ which sshdIt will throw this line on your terminal/usr/bin/sshdSSH uses public-key cryptography to authenticate the remote computer and allow it to authenticate the user, if necessary. Well, there are numerous post and links that explain about ssh in much detail. You can just google ssh if you want to learn about it. I'll now show the steps required to configure ssh.1- First of all create a ssh-keypair using this command :$ ssh-keygen -t rsa -P ""Once you issue this command it will ask you for the name of directory where you want to store the key. Simple hit enter without giving any name, and your key will be created and saved to the default location i.e /.ssh directory inside your home directory. (Files and directories having names tarting with a dot (.) are hidden files and directories in Linux. To see these files and directories just go to your home folder and press Ctrl+h).

cluster@ubuntu:~$ ssh-keygen -t rsa -P ""

Generating public/private rsa key pair.

Enter file in which to save the key (/home/cluster/.ssh/id_rsa):

Hit enter, and you will see something like this :

Your identification has been saved in /home/cluster/.ssh/id_rsa.Your public key has been saved in /home/cluster/.ssh/id_rsa.pub.The key fingerprint is:66:4f:72:26:2b:18:57:43:64:4f:3e:5a:58:d1:2c:30 cluster@ubuntuThe key's randomart image is:+--[ RSA 2048]-----+| .E.++ || o B. o || + =. || . + . || . . S + || + o O || . . . . || . || |+---------------------+cluster@ubuntu:~$Your keypair has been created now. The keypair contains the keys in 2 different files present under the .ssh directory. These are id_rsa (the private key) and id_rsa.pub (the public key). To connect to a remote machine, just give ssh command along with the hostname of that machine. NOTE : Hostname of the machine to which you want to ssh must be present in your /etc/hosts file along with its IP address.For example, if you want to connect to machine called 'client', do this :cluster@ubuntu:~$ ssh localhost cluster@localhost's password:Enter the password of the client machine to login. Now, you are at the terminal of the client machine. Just use a few commands like ls or cat to cross check. Once you give the password and hit enter you will see something like this on your terminal :cluster@ubuntu:~$ ssh clientcluster@client's password: Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-26-generic x86_64) * Documentation: https://help.ubuntu.com/90 packages can be updated.10 updates are security updates.Last login: Fri Jul 20 01:08:28 2012 from clientcluster@client:~$NOTE : In some cases you may want to use passwordless ssh (for example while working with Apache Hadoop). To do that you just have to copy the public key, i'e the content of your id_rsa.pub file to the authorized_keys in the .ssh directory of the client machine. Use the following command to do that :cluster@ubuntu:~$cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keysNow, if you do ssh client, you won't be asked for any password.