3

E.g. when I SSH into a server and then sudo bash and then su <user> to do some work, I want a quick way to exit all shells back to my local terminal.

going in

When I have to do some work on a server as jim.

local > ssh me@server
server:me > sudo bash
server:root > su jim
server:jim > // do stuff as jim

going out

When my work is complete but I don't want to leave the shells logged in.

server:jim > exit
server:root > exit
server:me > exit
local > // after 3 commands

preferred way out

Here's how I'd like to do it.

server:jim > exitall // for example
local > // after one command
Josh M.
  • 243
  • 3
  • 10
  • 2
    Does `ENTER ~ .` meet your requirements? – icarus Aug 07 '19 at 20:37
  • 4
    Do you need to do anything as root? If not you could run `exec sudo su - jim` as the command on the server, and then `exit` will take you all the way out. – icarus Aug 07 '19 at 20:41
  • I don't think Linux does this, but `stty 0` on some systems will drop your connection. – Mark Plotnick Aug 07 '19 at 21:21
  • Also https://unix.stackexchange.com/questions/507011/exit-shell-with-shortcut-not-typing-exit-that-closes-session-properly – muru Aug 08 '19 at 00:45
  • @icarus Thanks for the tip to skip root -- very obvious now that you mention it. :-) – Josh M. Aug 12 '19 at 11:36

2 Answers2

4

If you're open to a proactive (vs retroactive) solution, consider using exec for the intermediate steps that you want to bypass on the way out:

going in (modified):

local > ssh me@server
server:me > exec sudo bash
server:root > exec su jim
server:jim > // do stuff as jim

going out

server:jim > exit
local > // after 1 command

Using exec replaces your current shell with the listed command; as a result, when you exit out of jim's shell, your root shell exits, which then causes the sudo bash shell to exit, leaving you back where you started.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
3

I found the fastest way is to use Ctrl + D as many times as needed which will terminate each bash session

user3508766
  • 189
  • 9