Posted October 9, 2009 by Spyros in Linux Tips

Edit The SSH Config File to Keep The Connection Open And More


Using the powerful SSH command is really something you should get your hands on. Remote shell access is everywhere nowadays and even if you have a simple server running online, you would definately need to set SSH up in order to use a shell. If you are already using SSH and provide a password every time you want to login to your server(s), you may want to check this post about setting up SSH keys for authentication so that you won’t ever have to enter your password again.

If you tend to use SSH quite a lot, you may have noticed that it is quite unpleasing that after some idle time, the SSH connection suddenly drops. This is actually a security feature, but it is really unpleasing for the casual user. Well, ok, there could be a change that somebody is around there and when you go grab some coffee, takes over your connection and poses a serious security threat.

However, in the vast majority of cases, you are really the only user of your own machine. Therefore, there is no real need that you use that security feature and it would be nice if you could set your machine up so that it never loses the SSH connection.

Another scenario is that you want to connect to a webserver using something like “ssh -p 10000 hthought@domain.com”. While this is not difficult to remember of course, it is a bit tedious to type. Wouldn’t it be much easier to type “ssh domain” and get the absolute same result ? These are the things we will be talking about in this post.

How to Keep The SSH Connection Alive ?

The first thing you need to do is create the file ~/.ssh/config at your home folder (in your client machine). Then, you only need to include these two lines :

Host *
ServerAliveInterval 60

This means that for every host that you specify on this file, the client will send a message to the server at an interval of 60 seconds, to inform it that the connection is still alive. This would essentially mean that the connection will never close. There is also the option of doing the same thing in the server using the command ClientAliveInterval in the same way it’s used here.

How to Make SSH Connections Simpler ?

Let’s suppose that we want to connect to domain.com, as a user named hthought using the ssh port 2000. Doing that is very simple and once more you need to change your client’s machine ~/.ssh/config file to do so :

Host codercaste
User hthought
HostName codercaste.com
Port 2222

This actually specifies that whenever we use “ssh codercaste”, we would be connecting to codercaste.com under the name hthought and at ssh port 2222. Save the file and try that and see now that you can ssh without having to type the whole thing. Hope that saves you some time.