10

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:~$
Volker Siegel
  • 12,820
  • 5
  • 48
  • 65
Joshua Zitting
  • 103
  • 1
  • 1
  • 6
  • 6
    "Almost all of us do it.. " I doubt that. I maintain several machines and have all the passwords and I never ever use "sudo su" on debian based systems. And I expect most of us to not use it. – Rinzwind Sep 11 '15 at 17:44
  • 1
    Don't use `sudo su`, it's pointless and just launches an extra process. Use `sudo -i`, if you have to, instead. – terdon Sep 11 '15 at 17:52
  • I use `sudo -i` to obtain a root shell. As to ssh, you can configure ssh to allow root logins, personally I use the "without-password" options for root and either ssh keys er kerberos. This keeps the root account locked to passwords. – Panther Sep 11 '15 at 17:52
  • I do not quite get what does not work. First `exit` exits `su`, the second one logouts. Isn't it what you wanted to do? – Pilot6 Sep 11 '15 at 17:59
  • @bodhi.zazen I know you can allow root login.. but "WE cant have root ssh access enabled" – Joshua Zitting Sep 11 '15 at 17:59
  • Try to run `exit` that another `exit`. It does the same as `exit && exit`. – Pilot6 Sep 11 '15 at 18:00
  • 1
    @Pilot6 have you tested that code?? I just did and it does not work.. that is JUST what I want to do.. But it doesnt work.. – Joshua Zitting Sep 11 '15 at 18:01
  • we can allow root logins, your IT department may not allow this, you should be more specific in your questions and avoid such general terms. Us `killall --user $user` where user is your login user – Panther Sep 11 '15 at 18:06
  • Of course `exit && exit` fails - first `&&` evaluates the LHS (Left Hand Side) of the expression and exits. Second, but it has already exited, and returned control to the parent. – waltinator Sep 12 '15 at 05:18
  • @JoshuaZitting You can also hit Ctrl+D instead of typing `exit`, which is a lot quicker in my opinion. In a situation such as the one you describe I would simply hit Ctrl+D twice to exit both shells. You don't even have to let go of the Ctrl key; simply hold it down and hit D twice. Super fast. :) – Malte Skoruppa Sep 12 '15 at 11:25
  • 1
    Hold Ctrl+d to exit. – Cyrus Sep 12 '15 at 12:31

3 Answers3

15

Just do

exec sudo -i

Now the root shell is replacing the default one, and when you exit, you exit "both" (incorrectly worded, since the first shell stop existing with the exec).

Look:

[romano:~] % ssh pern
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.19.0-28-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

[romano@pern:~] % exec sudo -i
[sudo] password for romano: 
root@pern:~# whoami
root
root@pern:~# pstree -a -s -l -p -u $$
init,1
  └─sshd,1140 -D
      └─sshd,17450 
          └─sshd,17570,romano  
              └─sudo,17571,root -i
                  └─bash,17665
                      └─pstree,17678 -a -s -l -p -u 17665
root@pern:~# exit
logout
Connection to pern.XXX.XXX.XXX closed.
[romano:~] % 

I use it a lot to have a ssh-ed terminal: use exec ssh whatever and when you exit, the terminal closes.

Rmano
  • 31,627
  • 16
  • 118
  • 187
9

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!

Jakuje
  • 6,505
  • 7
  • 30
  • 37
1

when you are becoming root user just type::

sudo -s && exit

when you will exit from root you shell will automatically close. you can export this command to make it permanent.

echo "alias mysudo='sudo -s; exit'" >> ~/.bashrc && source ~/.bashrc