Prevent your SSH connection from freezing
Recently I was finding that my SSH connection to a Media Temple (dv) Centos 5 server was freezing. Creating a local config file fixed it. For anyone else suffering the same problem here's how to do it.
The Issue ¶
I’m on Mac OSX 10.5 connecting to a Centos 5 box using Terminal. I’m using shared keys and the connection was fine and working well until I left the Terminal window idle for over 15 seconds. I was also finding that SFTP sessions via Transmit would regularly freeze. All of this was very annoying and frustrating.
The solution ¶
I discovered that through creating a local config file for ssh you can declare settings that fix this and many other issues. Remember I’m on a Mac OSX 10.5 here. The usual disclaimer applies - this article is unsupported and you make changes at your own risk.
Open terminal and get going:
cd /Users/yourname/.ssh
# If you get ""-bash: cd: .ssh: No such file or directory"
# You need to create it
mkdir /Users/yourname/.ssh
# Set security on the folder so only you can read, write and execute
chmod 700 /Users/yourname/.ssh
# Change directory
cd .ssh
# Create the config file
touch config
# Edit the config file
vi config
# When in vi hit i to enter insert mode and add the following lines to the config file
ServerAliveCountMax 3
ServerAliveInterval 10
# Save and quit by hitting :wq
# Set correct permissions on the config file
chmod 644 /Users/yourname/.ssh/config
Now try connecting via SSH and this should stop your SSH sessions freezing. This instantly fixed the issue for me anyway. Hopefully it might help a few other people.
Other uses ¶
Through reading the documentation I discovered it is also possible to set configuration on a per user basis for SSH. You can set compression, the port number you are connecting to, specific settings for hostnames and much more.
If you are using SSH on a daily basis having a quick read of the documentation - is likely to throw up a few useful things you can put into a config file.
Further reading ¶
Tags
Can you help make this article better? You can edit it here and send me a pull request.
See Also
-
Securing Plesk on a Media Temple (dv)3 server
By taking a few simple steps with SSH, FTP and the firewall you can make your Plesk server more secure than the default configuration. -
What's wrong with Plesk
Increasingly I'm growing disillusioned with Plesk and SWsoft the company behind it. For me it is a stagnant product and a lack of competition is the root cause. -
Automating backups with Amazon S3 on Linux
A tutorial on how to back up files on Linux using Amazon's S3 service. The end result is a cost-effective synchronised remote backup service.