why does sudo su; su tomcat doesn't switch to tomcat user?

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


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

When you divide your sudo and su commands with a ; sign the su will be executed after the sudo has finished. I.e. in that case you execute the su as your normal user.

To achieve your desired result try to execute the whole in one command:

sudo su - tomcat