70

From a Linux SSH shell, type /etc/init.d/network restart to restart the network service.

I expect my SSH connection to die since the network service goes down. But it doesn't. Very cool. But how does Linux achieve this? How does it keep my SSH connection alive across the service restart?

0

2 Answers 2

78

It does this by doing nothing special. The network restarts in less time than the TCP connection takes to time out, so the TCP connection survives the "outage" the same way it would survive any transient network outage.

The only reason Windows doesn't do the same thing is because Windows specifically resets TCP connections when a network interface goes down. This is, at least arguably, a pretty boneheaded thing to do because TCP was specifically designed to survive transient network outages.

3
  • 1
    David, then it means that the TCP/IP stack is not restarted, right? Because if it were, the server would have lost track of the SSH socket and the sshd child process (thanks Nils) would terminate. Which brings the next question: Which service manages the sockets? TIA. Dec 26, 2011 at 8:04
  • @David Schwartz - what parameters can I use to change the timeout? How do I find out current timeout value? And is it configured on server side, or client side ? Nov 11, 2016 at 8:46
  • @MartinVegter You really don't want to change the TCP connection's timing parameters as they have to coordinate on both sides and messing with them could affect TCP's ability to handle packet loss. You're better off designing the system as a whole to tolerate TCP connection loss seamlessly by recovering the connection. Nov 11, 2016 at 16:06
17

SSHD forks a child process on connection. This child process will not die if either SSHD or the whole network is restarted. This is the reason why you can update ssh and/or its configuration, do a service sshd restart and still keep connected to your old ssh-session with the old settings. Apart from that ssh recovers well from small network outages.

10
  • 3
    SSH doesn't even know about 'small network outages', for the reason given by David Schwartz. It's not a property of SSH specifically.
    – user207421
    Dec 26, 2011 at 21:40
  • 3
    As explained by EJP, your answer is true but irrelevant. Dec 26, 2011 at 23:21
  • 5
    This answer was useful to me because it added the insight about what sshd does to avoid going down on service sshd restart. If you update sshd (security update etc), the child processes would still be running on the original code (pre-update), but would continue to provide service, rather than being interrupted. I guess the availability of the listening socket for the main sshd process is very briefly interrupted, however, when the first child process ends and the network interface momentarily indicates the port as closed. Dec 14, 2013 at 5:03
  • 1
    @allquixotic This interruption on restart does not matter for already existing connections - they stay connected. The "main" sshd listens then again for NEW incoming requests. Or with other words: upon fork the socket gets personalized as well for a specific connection.
    – Nils
    Dec 14, 2013 at 22:02
  • 1
    @DanielAlder Read the second sentence of my answer carefully.
    – Nils
    Nov 27, 2014 at 11:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .