1

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
  • 1
    Source the script: . ./test.sh. – muru Jun 19 '15 at 9:13
  • 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 – LnxSlck Jun 19 '15 at 9:13
  • 1
    Again: source the script. . ./test.sh. – muru Jun 19 '15 at 11:58
5

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.

1

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
  • Why would you do sudo su -c "command" instead of sudo command directly? – muru Jun 19 '15 at 9:59
  • @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 – Sergiy Kolodyazhnyy Jun 19 '15 at 10:08
  • 1
    the whole process seems to be to get an interactive shell, so that OP can run arbitrary commands. As for other users: sudo -u. – muru Jun 19 '15 at 10:09
  • I forgot about that flag. Good point ! I'll edit my answer in a second – Sergiy Kolodyazhnyy Jun 19 '15 at 10:12
1

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
  • Any ideas of how to quit the login shell from a real child script? – Velkan Dec 3 '18 at 7:26
  • @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. – muru Dec 6 '18 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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