4

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

14

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
5

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
  • 2
    And you might want to make sure you inherit tomcat's environment as well; try sudo su - tomcat. – MadHatter May 14 '14 at 15:35
  • @MadHatter: Yes, of course ;) - I changed my answer – bmk May 14 '14 at 15:36
  • You already had my +1, can't add another - sorry! – MadHatter May 14 '14 at 15:44

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.