64

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?

70

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.

  • 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. – Serge Wautier Dec 26 '11 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 ? – Martin Vegter Nov 11 '16 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. – David Schwartz Nov 11 '16 at 16:06
11

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.

  • 2
    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 '11 at 21:40
  • I am not sure if there is not also a little bit of programming involved on the ssh-side. Basically all TCP services should be fault-tolerant - many are not. There needs to be an additional retry mechanism involved in the application - then the service can even survive "medium" outages as well. – Nils Dec 26 '11 at 21:59
  • 3
    As explained by EJP, your answer is true but irrelevant. – Gilles 'SO- stop being evil' Dec 26 '11 at 23:21
  • 3
    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. – allquixotic Dec 14 '13 at 5:03
  • 1
    @Nils I know that you wrote that. But this makes the average reader thinking that forking is relevant. But the same thing also happens for programs which don't fork. Your answer is something like: "Because steel is stronger than iron, apples are red" – Daniel Alder Nov 27 '14 at 15:07

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.