This question already has an answer here:

I have setup an SSH connection to run a program on a remote server. The program prints debug information on the terminal every 10 seconds. If I leave the SSH windows open for a long time (say 10 hours), does the SSH connection become inactive? How is activity defined in an SSH session? Should I type/run command every x seconds to keep the session alive?

marked as duplicate by mdpc, Rui F Ribeiro, Philippos, Romeo Ninov, Archemar Jan 16 '18 at 11:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    See openssh.com/faq.html#2.12 – cuonglm Feb 15 '16 at 4:55
  • Why not redirecting the errors to a file? – Rui F Ribeiro Feb 15 '16 at 9:04
  • 1
    @RuiFRibeiro As I wrote in my answer, that only works well if the program doens't require input every once in a while. – Anthon Feb 15 '16 at 11:18

You should not have to do anything special, SSH does not terminate a connection because of inactivity. So there is no inactivity period defined within SSH.

However one of the devices on the network route between you and your server might lose the route and for that activity from one side usually is enough, but not always (there are "arrival confirmation packages", acknowledgements, going back to the server from the client when using TCP).
Traffic from both sides is no guarantee for continued connectivity, if you have a DSL modem and your provider decides that you get a new IP address every day, the connection will be broken.

You can have your client sent some packets on a regular basis by inserting

ServerAliveInterval 5

in /etc/ssh/ssh_config or your ~/.ssh/config to have some client to server traffic every 5 seconds in addition to the traffic coming from the server every 10 seconds.

The simpler "solution" to DSL modem resetting a connection is running your server side software in tmux or screen: if the connection is broken, you just SSH into the server again, issue tmux attach or screen -r and you can continue to view the uninterrupted server program.

Using tmux/screen is especially useful if you are not so much worried about losing the connection, but of the consequence that the server program stops if you do. For that you can also redirect the output of the original program to file and use tail -f, but that doesn't allow you to easily interact with the server program (if that would be necessary). tmux and screen (in their basic forms) are easy to use, but there are some side-effects like not being able to scroll back using the sliders on your graphical terminal (you have to use keyboard shortcuts to have the server scroll back in its buffer for that).

A more flexible solution around devices breaking your connection is to use mosh. This uses SSH to exchange some secret info and then allows for reattachment even if one or both of the IP addresses changes. This is however more difficult to set up and get to work.

In your case I would start with using tmux and manually reattach when the connection indeed deactivates.

  • I see. Since SSH uses TCP to exchange data between client/server, the inactivity causes the TCP connection to break and hence disconnect SSH client/server. – ManiAm Feb 15 '16 at 6:02
  • 1
    @ManiAm Correct, but even with activity a connection might be broken. E.g. if your ISP decides to breaks the connection every Y hours to prevent you from using a server at home with a more or less fixed IP. – Anthon Feb 15 '16 at 6:07

also, really helpful to use SCREEN command. With that you can run any command and don't worry about your ssh connection.

  • 2
    Also? I already mentioned screen in my answer. – Anthon Feb 15 '16 at 7:26

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