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.
  • 242
  • 1
  • 3
  • 12

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
  • 67,283
  • 35
  • 116
  • 255
3

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