The difference between ~. (tilde dot) and “exit” when exitting an SSH connection

exitsshunix

Whenever I have a backgrounded X11 connection in an SSH session, I must use ~. and not exit to exit the ssh session.

What is the difference here? Can I always use the escape sequence ~. ?

Best Answer

exit tells your current shell to quit. Since (when not having a tunneled connection, e.g. X11, active) that means that the process that sshd invoked on the server side exited, SSH then disconnects.

~. is the "magic" escape sequence that SSH captures and immediately disconnects (including the X11 tunnel).

So, no, these aren't interchangeable. If you want your remote shell (or whatever program is running remotely) to gracefully exit, you should end it accordingly.