I’ve been using Linux as my primary operating system for over five years, and spent a good deal of time with it before that time. It’s only been within the last year or so that I’ve really begun to explore and enjoy working from the Linux/Unix command line. It’s greatly simplified my work flow when it comes to many tasks, and it seems I spend the majority of my time either in a command line or web browser now a days. Now that nearly 100% of the time when I’m working on a remote system, whether it is a server, Linux/Unix PC, or networking equipment, I’m usually connecting with SSH.
SSH is an incredibly powerful tool that can be used for everything from remote terminal access, to running entire graphical programs over a network. For me though remembering and having to type in all the IP Addresses, DNS Names, usernames and port numbers for all the SSH connections I use daily can be a real pain. That’s why setting up your own SSH config file can save huge amounts of time and effort in your day-to-day work.
This tutorial will be much easier to follow if you have a basic understanding of how to use SSH.
The steps in this tutorial were performed on Ubuntu 11.10. You may need to modify some steps depending on what system you are running.
1. Open Gedit (or your editor of your choice)
2. Click File->Save
Go to your home folder, and display hidden files by hitting CTRL+H on your keyboard.
Open the .ssh folder and save your new file as config
Command Line Version
1. First open up the terminal on your Linux/Unix based system.
2. Use vi (or any editor of your choice)
3. There are many options that you can set in a config file, but we will go over the settings that I usually use. Here’s an example:
Host myServer HostName example.com User root Port 222 Host myDesktop Hostname 192.168.1.100
This example shows two different SSH connections and their different options. The first is an example server that we want to connect to at example.com, with the username root, and where the SSH service is running on the non-standard port 222. The second is an example of a computer on your local network. As you can see we used a local IP address instead of a DNS name, also we didn’t bother setting a port number since it is running on the standard port 22. Also because in this example I’m assuming your connecting from another computer you own at the location there is no username set since I’m assuming you are using the same username on both computers you’re using for the connection.
4. Other options that I often use would be ServerAliveInterval, and ServerAliveCountMax.
Host myServer HostName example.com User root Port 222 ServerAliveInterval 30 ServerAliveCountMax 120 Host myDesktop Hostname 192.168.1.100 ServerAliveInterval 30 ServerAliveCountMax 120
This example would keep your SSH connection live for an hour by refreshing the connection every 30 seconds. This can be handy if you don’t want to be logged out every time you step away from your keyboard.
With your new SSH Config file instead of having to type in:
ssh -p 222 email@example.com
You can just use:
Hope this little trick saves you as much time as it has saved me.