2

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
  • 2
    Does ENTER ~ . meet your requirements? – icarus Aug 7 '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 7 '19 at 20:41
  • I don't think Linux does this, but stty 0 on some systems will drop your connection. – Mark Plotnick Aug 7 '19 at 21:21
  • @icarus Thanks for the tip to skip root -- very obvious now that you mention it. :-) – Josh M. Aug 12 '19 at 11:36
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.

  • Cool, I'll give it a go! – Josh M. Aug 12 '19 at 11:37
2

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

  • Great, thanks! I'll try it out. – Josh M. Aug 12 '19 at 11:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.