I am working on remote machine through SSH.

In SSH session, I logged in as root user and doing some work and rebooting it. Every time I reboot the remote machine, my local terminal, from which I had taken session, get stuck. I had to close that terminal. Open new terminal and again had to take SSH session once remote machine booted up.

Is there any way to tell SSH to try establish connection once it get disconnected ?

If I am in SSH session and that SSH server get rebooted, then local terminal get stuck. How do I avoid this ?

  • You can always use screen and detach from it prior to rebooting. The session should still be active and you would just need to re attach to it
    – ryekayo
    Jun 10, 2016 at 14:54
  • What if someone else rebooted the machine ?
    – SHW
    Jun 10, 2016 at 14:56
  • I'm not exactly how to avoid the dead connection in your terminal, but there is a key sequence you can use to disconnect the ssh session on the client side: Hit enter a few times, then type ~. - that should cause your ssh client to drop the connection.
    – Liczyrzepa
    Jun 10, 2016 at 15:00
  • The screen sessions should still be active after someone else reboots unless if they wipe all screen sessions beforehand
    – ryekayo
    Jun 10, 2016 at 15:06
  • It should not. What is the remote system, openssh version and so? I never noticed this behaviour. When host gets rebooted (gracefully, the ssh sessions should get cleanly closed).
    – Jakuje
    Jun 10, 2016 at 15:59

1 Answer 1


If the server is rebooted, all the processes that were running there are killed. You won't be able to reconnect to them, they're gone. period. All you can do is open a new session to the server and run new processes.

If an SSH session gets stuck, you can press Enter ~ . to disconnect it, you don't need to close the terminal.

You can use autossh to automatically reconnect to the server if the connection drops. Once again, if the connection drops because the server rebooted, you'll get a new shell, your old processes are gone.

If you see sessions getting stuck, then autossh won't reconnect automatically, because a stuck session means that the client machine hasn't noticed that the server is gone. If the server had closed the session properly, you wouldn't get a stuck session, you'd get a message like “Connection to darkstar.example.com closed”. You can set ServerAliveInterval to a small value to detect abrupt server disconnection faster, at the expense of a higher risk that the connection will be closed due to a temporary network glitch even though the server is still alive.

  • Sounds like the example server admin chose a default hostname for a Slackware install -- that's a blast from the past!
    – Jeff Schaller
    Jun 14, 2016 at 3:03

You must log in to answer this question.

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