When my connection has been idle for some time, the remote host closes the connection. I try to type a new command, it hangs, then closes the connection. How can I keep the ssh connection active for longer?

$ ssh root@host.com
Last login: Tue Jan  3 03:09:39 2017 from c-99-99-99-99.hsd1.xx.comcast.net
[root@ip-172-99-99-99 ~]# groups
root bin daemon sys adm disk wheel
[root@ip-172-99-99-99 ~]# users
root root
[root@ip-172-99-99-99 ~]# less /etc/passwd
[root@ip-172-99-99-99 ~]# Connection reset by port 22

The remote is CentOS on Amazon (AWS) and local is Cygwin.


Because your last login was from comcast.net I am guessing that you are connecting from a home system where you are behind a router which is doing NAT. A problem that a system doing NAT has is knowing when the connection is no longer wanted. If the connection is taken down cleanly then there is no problem but if the two machines just reboot then there is no signal to the NAT box that this has happened. Therefore your typical NAT box has timers which say "if there is no traffic for an hour then the two endpoints clearly are not going to send anything more" and so recycles the resources that the NAT was taking.

So the thing to do is send some traffic every once in a while. The simplest way assuming you are using the openssh implementation on your cygwin machine is to enable TCPKeepAlive. replace

ssh root@host.com


ssh -o TCPKeepAlive=true root@host.com

For long term use you are better setting up a ~/.ssh/config file.

 host fred
     hostname host.com
     user root

and then you will be able to say just ssh fred to connect to the machine with the needed option.

  • Excellent description and inference! debug1: /home/Chloe/.ssh/config line 1: Applying options for host.com
    – Chloe
    Jan 3 '17 at 20:42
  • 1
    Sometimes the connection is reset even when it hasn't been idle. It says it was reset by the IP address of the remote host, but when I log back in, the machine hasn't been restarted. I'm the only user.
    – Chloe
    Jan 4 '17 at 21:53
  • 1
    The thing I was suggesting was resetting the connect was the box doing NAT, i.e. your cable modem. However because of the way that TCP/IP works it is just possible that you are seeing en.wikipedia.org/wiki/TCP_reset_attack
    – icarus
    Jan 4 '17 at 22:48

It is using your last saved session SSH keys.

You will have to delete it and then ssh will add new keys for the IP address you are trying to access.

ssh-keygen -f "/home/asagarwal-dev/.ssh/known_hosts" -R

The above command solved my problem (where is the IP address which I was trying to ssh from host).

  • This seems to be answering a different question.
    – icarus
    Apr 5 '20 at 11:37

As ssh client configure it below here :

Host *
ServerAliveInterval 60

sudo source ~/.ssh/config

All remote host configure it below here :


ServerAliveInterval 60
  • 1
    What the heck should sudo source ~/.ssh/config do?
    – Jakuje
    Jan 3 '17 at 8:38
  • Just create config file in ssh client with host * and ServerAliveInterval 60
    – supriady
    Jan 3 '17 at 8:42
  • @Jakuje,joking with 9853 reputation.Did you earn your reputation from corruption? Voted my answer minus.
    – supriady
    Jan 3 '17 at 9:01
  • I followed my linux/Unix forum.I didnt follow linux/Unix forum like this forum.Showing their arrogancy to many user here.I dont mind to give my all reputation to you.Dont need to vote my answer minus.
    – supriady
    Jan 3 '17 at 9:07
  • 2
    Hello. This is not a forum, but Q&A. Good answers go up and bad down. I am pointing out what is wrong in your answer (by simple question) and letting you fix it if you dare. Good answers also explain why is the problem happening, not just pasting snippets of configuration files and commands without explanation.
    – Jakuje
    Jan 3 '17 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.