Linux – why does sudo su; su tomcat doesn’t switch to tomcat user

commandlinuxsusudo

In Redhat linux. from my user if I issue command
sudo su -> changes to root
su tomcat –> changes to tomcat user

Why does tailing the commands in single line doesn't work?
sudo su; su tomcat –> stil in root. Doesn't switch to tomcat

Best Answer

The commands

sudo su; su tomcat

are two commands. The shell will run the first and when it completes it will run the second. Note that the shell does not pass the second command as an argument to the first command.

When you exit the first command it will run the second

sudo su ; su tomcat
[sudo] password for iain:
root@host:iain]$ exit
exit
Password: <---- this is the prompt from the second su command.

You can use sudo to transition to the tomcat user directly

sudo -u tomcat <some command>

or use -s to get a shell e.g.

whoami
iain
sudo -s -u tomcat 
whoami
tomcat