Unix: Making ssh connections easier

Need a way to make connecting to lots of servers using different settings and usernames easier? Look no further than the ssh config file.

By  

I rely on ssh for connecting to dozens of servers. The problem is that my username is often different from one system to the next and I often have trouble remembering which username goes with which system. One easy way to keep them straight is to take advantage of a not-so-obvious SSH feature -- the SSH config file. By creating and configuring a ~/.ssh/config file, you can create a profile for each of the servers that you use and make connecting to those systems quite a bit easier.

For a basic example of how this works, say that I use a system named boson in the domain elemparticles.org and that I log into this server with the username higgs. I could use the command ssh higgs@boson.elemparticles.org. Alternately, I could create a profile for the server that looks like this and then get by with just using the command ssh boson to log in.

Host boson
  HostName boson.elemparticles.com
  User higgs

If you don't have a .ssh directory or an ssh config file, you can start with commands like these and then edit the config file you have just created.

mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config

Once you've tested your first ssh login profile with a simplified ssh command such as ssh boson command, you can add as many additional profiles as suit your needs. In the example below, I've set up profiles for four separate systems, each with a different username.

Host boson
  HostName boson.elemparticles.com
  User higgs

Host fermion
  Hostname fermion.elemparticles.com
  user dirac

Host dweebs
  Hostname linuxdweebs.example.org
  User sandra

Host solaris
  Hostname sug.history.net
  User shs

You might want to add other options to your profiles as well. The ServerAliveInterval and ServerAliveCountMax settings will allow you to establish keep alives for connections to those servers.

Photo Credit: 

flickr / jazbeck

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

Ask a Question