Ubuntu – Exit from both root and user with one command

bashcommand linerootsshsudo

I know about "not using sudo su -" etc. But let's be honest, almost all of us do it.

So, here is the issue. We can't have root logins enabled, so we have to ssh in as our user then su to root.

Here is the process tree:

    1  7897  7826  7826 ?           -1 S     1000   0:00 sshd: josh@pts/0
 7897  7898  7898  7898 pts/0     8182 Ss    1000   0:00  \_ -bash
 7898  7990  7990  7898 pts/0     8182 S        0   0:00      \_ sudo su -
 7990  7991  7990  7898 pts/0     8182 S        0   0:00          \_ su -
 7991  7992  7992  7898 pts/0     8182 S        0   0:00              \_  -su
 7992  8182  8182  7898 pts/0     8182 R+       0   0:00                  \_ ps axjf

I would like to exit from root, then from my user with one command. Is there a way to do this?

BTW exit && exit does not work because it exits the shell and doesnt process the rest of the command

josh@ubuntu:~$ sudo su -
root@ubuntu:~# exit && exit
logout
josh@ubuntu:~$

Best Answer

Technically, no one answered your question. I appreciate that they think their way is better (probably is), but here's another approach (in case you have to su - some time and have the same issue);

  1. [Log into a system]
  2. $ sudo su -;exit
  3. # echo "do things"
  4. # exit

When you exit from root, the original user will also log out since it's continuing it's last command.

Cheers!