logout current user from script

20,801

Solution 1

Perhaps all you want to do is:

exec ./test.sh

This replaces your current shell process by the script, so that when it finishes there is nothing left to run.

Solution 2

Typically, to run a command as root, sudo is sufficient.

#!/bin/bash
echo test
sudo whoami

If you insist on using su,for whatever reason, you can use -c flag :

#!/bin/bash
echo test
sudo su -c whoami

And here's the result, same in both cases:

$ ./test.sh                                                                                                                           
test
[sudo] password for xieerqi: 
root

If you want to enter root shell, use sudo -i

serg@ubuntu[/home/xieerqi] 
$ sudo -i
# date
Fri Jun 19 13:16:15 EEST 2015
# whoami
root
# exit
serg@ubuntu[/home/xieerqi] 
$ 

With su :

serg@ubuntu[/home/xieerqi] 
$ sudo su -c /bin/bash
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
root@anaconda:/home/xieerqi# date
Fri Jun 19 12:41:06 EEST 2015
root@anaconda:/home/xieerqi# echo blah
blah
root@anaconda:/home/xieerqi# exit
exit
serg@ubuntu[/home/xieerqi] 
$ whoami
xieerqi

If you want to run commands as another user, not only root, you can use sudo -u username command

$ sudo -u testuser whoami
testuser

Same idea with su:

serg@ubuntu[/home/xieerqi] 
$ sudo su -c bash testuser
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
testuser@anaconda:/home/xieerqi$ whoami
testuser
testuser@anaconda:/home/xieerqi$ exit
exit
serg@ubuntu[/home/xieerqi] 
$ whoami
xieerqi

Solution 3

A logout in a script doesn't affect anything, since logout only works for login shells, and scripts aren't typically run as login shells.

It seems to me that you're logging on via SSH (or something), running as another user and immediately logging out. In that case, just source the script, so that the commands are run in your current shell. Then, logout will work.

Or, you can use exec, but in that case, it isn't logout that's taking effect - logout will fail, and the end of the script simply causes it to exit. Distinction without a difference, I suppose.

Or, if you find it cumbersome to use . ./script, define a function with braces:

switchuser () {
  echo "test script"
  sudo su -
  exit
}

Commands in braces run in the current shell, and commands in parentheses are run in a sub-shell. Add this to your .bashrc, then you can simply do:

switchuser
Share:
20,801

Related videos on Youtube

Wolfy
Author by

Wolfy

Updated on September 18, 2022

Comments

  • Wolfy
    Wolfy over 1 year

    I would like to create a script that:

    1. run some commands
    2. execute sudo su - [some other username]
    3. after I exit from [some other username] -> logout

    Example: test.sh

    echo "test script"
    sudo su
    logout
    

    If I run a script like this:

    echo "test"
    sudo su
    whomai
    

    I get the wanted result:

    [wolfy@ubuntusrv ~ ]# ./test.sh
    test
    [root@ubuntusrv wolfy]# date
    Fri 19 Jun 10:56:52 CEST 2015
    [root@ubuntusrv wolfy]# echo bla
    bla
    [root@ubuntusrv wolfy]# exit
    wolfy
    [wolfy@ubuntusrv ~ ]#
    

    As you can see, the script runs my echo command and then I sudo su to root, where I can do what I need to do and after I quit root session the script execute the last (whoami) command.

    My problem is that I can't logout myself at the end of this script.

    I need a way to do this without root privileges.

    Can someone help me?

    Additional explanation:

    1. when I run sudo su - username (userA) I need to provide my password (wolfy) to login as the other user (userA). Then I work for the whole session with that user (userA) and when I logout from that user (userA) I would like to automatic logout from my (wofly) user.
    2. using sudo command is not an option
    • muru
      muru almost 9 years
      Source the script: . ./test.sh.
    • LnxSlck
      LnxSlck almost 9 years
      You don't need to go to root to do some commands, instead of sudo su - just sudo command, after that the logout should work
    • muru
      muru almost 9 years
      Again: source the script. . ./test.sh.
  • muru
    muru almost 9 years
    Why would you do sudo su -c "command" instead of sudo command directly?
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy almost 9 years
    @muru I wouldn't. OP wanted to run shell and several commands as another user, so that's what I've given him. The title says "current user", so I assume OP doesn't just want root, but other users as well
  • muru
    muru almost 9 years
    the whole process seems to be to get an interactive shell, so that OP can run arbitrary commands. As for other users: sudo -u.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy almost 9 years
    I forgot about that flag. Good point ! I'll edit my answer in a second
  • Velkan
    Velkan over 5 years
    Any ideas of how to quit the login shell from a real child script?
  • muru
    muru over 5 years
    @Velkan kill the parent shell from a script? You'd probably have to get that shell's PID (easily found using ps -o ppid= -o $$), and kill -TERM or kill -KILL that PID. I don't think there's a clean way.