January 21, 2016

SSH time saver

If you use SSH frequently then this might be useful. And if you use ssh infrequently then this might be even more useful. SSH is one of the most common ways to get access to a unix based server. If you are in IT or development you likely have access to several servers that all have their own dedicated functions. On top of that, a lot of servers you use might not be under a DNS and their is no convient domain name to use when logging in; instead there is just an IP.

If you are comfortable on the linux cli you have likely come across how to make and add aliases to your environment. This is one way to save your credientials. For example adding this:

alias ll='ls -al'
alias myserver='ssh root@192.168.0.1'
to your bashrc file (~/.bashrc) will let you log into this server by just typing myserver.

The first alias here shows how you can alias any command. The second here we have alias myserver to now execute the following command and therefore log us in via ssh.

This works well for a lot of bash commands and even ssh commands. However once you have a lot of ssh options and parameters it can become a bit unwieldy.

SSH Config

The solution is simple. This file ~/.ssh/config can store all our connections and settings. Here is how:

Host prod
  HostName myserver
  Port 22
  User root

Now we can reference the name of our host to login: ssh prod and we should be logged in.

Even more

There is a lot more you can do with a ssh config file. This is just the basics. To get more I would suggest you checkout the man pages for ssh_config man ssh_config.
Tags: linux ssh