Making your life easier in Terminal: SSH Setups

I spend most of my time in Ubuntu on my dual boot laptop and do a lot of hopping in and out of a few different Linux based servers. The problem I have with that is that each server has a different username and a different password!

So it wasn’t unusual for me to use either the wrong username or password for a server and get annoyed because I couldn’t get in.
Enter SSH Config Files!

The purpose of config files (simply put) is to give you a simple name that you will remember for individual systems to connect to via SSH. Very easy to setup, the file you need is /home/username/.ssh/config and will end up looking like this

Host XXX
        Hostname        host-ip
        User            username_here

Host YYY
        Hostname        host-ip
        User            username_here

Host AAA
        Hostname        host-ip
        User            username_here

NB you can use a qualified hostname such as servername.foo.com but this is reliant on your domain DNS being setup correctly prior to attempting to use the host file. Once everything is configured correctly, this setup gives the end result of being able to simply type:

ssh AAA

instead of

ssh username_here@host-ip

a minor convenience, but extremely useful and potentially a big time saver for people accessing multiple servers regularly.

To make life even easier with ssh logins, particulary inside your own network or to server you regularly acceess there is a very easy addon setup to the ssh configuration on your machine that will allow you to ssh to machines without the need to enter any passwords.

Password-less SSH login: The Method

First you need to create an ssh key for remote machines.

philtann@Aeryn-Ubuntu:~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/philtann/.ssh/id_rsa): 
Created directory '/home/philtann/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/philtann/.ssh/id_rsa.
Your public key has been saved in /home/philtann/.ssh/id_rsa.pub.

Then transport the id_rsa.pub key to the server that you’re wanting to access via ssh without password entry. The key needs to be entered into the ~/.ssh/authroized_keys file on the destination machine which can be achieved using the cat command. The below example is an actual case I have completed on a machine I have called “usa” in my ssh config file that we earlier setup.

philtann@Aeryn-Ubuntu:~/.ssh$ cat ~/.ssh/id_rsa.pub | ssh usa 'cat >> ~/.ssh/authorized_keys'
philtann@usa.philtann.com's password:

As long as no error has been returned when this command is completed, you should now be able to ssh into the server without the need to enter a username, host ip or password.

philtann@Aeryn-Ubuntu:~/.ssh$ ssh usa
Welcome to Ubuntu 11.04 (GNU/Linux 2.6.32-pony6-3 i686)

 * Documentation:  https://help.ubuntu.com/
Last login: Mon Nov 19 06:27:17 2012 from 202-6-150-199.static.adam.com.au
philtann@epicbox:~$

This is certainly not something that is essential to operation of any machines, networks or daily life in any distro of Linux but it adds convenience to regular use of ssh and hopefully will save people a bit of time.

1 Comment

Comments are closed