In another article How to keep idle ssh connection alive, I described ways how to keep ssh connection alive, now here is quick examples to set timeout for idle connection.

Neither of ways below works for any single ssh connection session.

Server side setting

In /etc/ssh/sshd_config, set

ClientAliveInterval 1200
ClientAliveCountMax 3

Restart sshd service, run

/etc/init.d/sshd restart

or

service sshd restart

The setting will give you an timeout of 60 minutes, 1200*3 seconds.

Note: If the connection across a firewall, where there is conection idle time setting, you may want to decrease the interval value, while increase the max count to get the same idle timeout.

Client side setting

ServerAliveInterval 1200
ServerAliveCountMax 3

You can apply the setting in 3 ways

1. command line options(apply to session only)
2. user-specific file (~/.ssh/config, applies to the user only)
3. system-wide file (/etc/ssh/ssh_config)

The setting will give you an timeout of 60 minutes, 1200*3 seconds.