I typically have four or five terminal windows open, and I’m almost always logged in to at least three servers (my dev box, production box, and database server). It’s a huge pain to log back into all these sessions whenever my connection is dropped. To keep myself sane, I use a couple of tricks to keep timeouts from occurring, and to streamline the login process when they do.

Keep connections alive

My home network consists of a cheap NAT firewall/wireless access point connected to a cable modem. In order to route incoming traffic properly, NAT devices keep a table of active connections in memory. As a result, NAT firewalls have a nasty habit of timing out idle sessions to keep their state tables clean. Thankfully, SSH has a built in keepalive mechanism that solves the problem.

You can turn SSH keepalives on at the system level, or on a per user basis. Single user configuration options are stored in ~/.ssh/config. The system wide configuration options are typically found in /etc/ssh_config (on Debian systems, it’s /etc/ssh/ssh_config). To enable keepalives, open the config file in your favorite text editor and add the following lines:

Host *
  ServerAliveInterval 60

The numeric argument specifies the number of seconds between keepalive requests on idle connections. The Host line lets you restrict declarations to a particular host, or group of hosts. A single ‘*’ is a wildcard pattern that matches any host, so keepalive requests will be sent for all sessions.

Password free login

Even with keepalive requests turned on, your session will time out occasionally (e.g., when you lose your internet connection). You can save yourself a bit of time by adding your workstation’s SSH key to the authorized_keys file on each remote system you login to.

First, generate an SSH public/private key pair on your local system (if you already have a key pair you can skip this step). When prompted for a passphrase, leave it blank.

local$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/mmalone/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/mmalone/.ssh/id_rsa.
Your public key has been saved in /Users/mmalone/.ssh/id_rsa.pub.
The key fingerprint is:
4d:d0:f4:f2:6c:3a:ac:b4:dc:c7:71:2b:b8:b7:5a:7c
mmalone@michael-malones-computer.local

Next, copy the public key from your local system to the system you’re logging into (if the ~/.ssh directory does not exist on the remote system, you may have to create it: mkdir ~/.ssh).

local$ cat ~/.ssh/id_rsa.pub | ssh mmalone@immike.net \\
> 'cat >> .ssh/authorized_keys'
Password:

All done. Now you can log into your remote system without a password:

local$ ssh mmalone@immike.net
Linux www 2.6.18-3-686 #1 SMP Sun Dec 10 19:37:06 UTC 2006 i686

mmalone@www:~$

The same public key can be used on any number of remote servers, so you can repeat steps two and three on any other servers you regularly use.