13

I use a ProxyJump command for a number of ssh sessions I use daily, and also switch users a lot on these sessions and having to type exit 3 or 4 times in a row isn't too fun.

I am aware of newline + ~ + . to terminate an ssh session, I still have to check if it terminates it amicably like an exit would, but how do you exit all sessions in the current shell with a single command or shortcut such that typing exit 3 or 4 times in my case becomes a one-time thing?

tsujp
  • 587
  • 2
    ~. just disconnects your SSH client (it's useful when the network has disappeared, for example). Normally, the SSH daemon will recognise that and signal its session with SIGHUP, so it's not quite the same as quitting the shell normally (but it's useful if you can't get any response from the shell). – Toby Speight Mar 18 '19 at 17:44

2 Answers2

30

Ctrl-D will exit a shell in many cases. It is quicker than typing exit Enter. It's still not a single command to terminate everything, but holding Ctrl and hitting D several times is easier and faster. Not sure how valuable this is for your use case.

Discussed in detail here.

DopeGhoti
  • 76,081
0xSheepdog
  • 2,750
  • 2
    The next stop is https://unix.stackexchange.com/a/182071/5132 , and the stop after that is the further reading there. (-: – JdeBP Mar 18 '19 at 16:24
  • 3
    Be careful with CTRL+D. In bash, if you have input any characters in your prompt, hitting CTRL+D will execute it the same way Enter would. In other words, don’t type out an rm command and then hit CTRL+D if you didn’t want to execute it. I don’t see this behavior in ksh. – Peschke Mar 18 '19 at 18:28
  • 2
    @Peschke On every system I've ever used Ctrl+D does nothing if I've typed something. I've never seen the behavior you describe. – John Kugelman Mar 18 '19 at 19:19
  • 1
    @JohnKugelman I tested and confirmed the behavior at the time of my comment with bash on RHEL 7.5. – Peschke Mar 18 '19 at 19:21
  • @Peschke I can't confirm this on CentOS 7.3. Maybe it's depending on which terminal you use? – mtak Mar 19 '19 at 09:28
  • 3
    Peschke is using the vi terminal input bindings. – JdeBP Mar 19 '19 at 11:20
  • That's fine I can just mash CTRL+D a few times and get the job done. Thank you. – tsujp Mar 19 '19 at 12:49
  • @JdeBP: That’s true. I forgot about other input bindings. I still don’t see this issue in ksh. Must be on bash only with vi input bindings. – Peschke Mar 19 '19 at 15:36
6

Typing ~. actually does the trick of closing all the "nested" connections through to the deepest.

It does so with the amicable exit you want as long as all your shells are configured to handle SIGHUP as a graceful death. Bash does so by default. You can easily check the amicable exit for your set-up by e.g. seeing whether the .bash_history files of your deeper sessions get properly updated after the ~., or if you set your bash-es not to update .bash_history upon exit then you might set a trap on the EXIT event (or in your ~/.bash_logout) with a command that writes something somewhere and later see whether it got executed.

Besides, I’m not sure what you mean by "exit all sessions in current shell". Each interactive bash is just one session, and the commands you type go always only to the deepest shell of the ProxyJump chain. If you meant "the background jobs in the current shell", bash does send SIGHUP to all its jobs on receiving SIGHUP by the ssh daemon upon the ~. , therefore such exit should be fully amicable if those running jobs react properly to a SIGHUP.

LL3
  • 5,418