Reputation: 31
I need to run two or more commands as another user in Linux. For example, using "root", I need to sudo to another user and do something like "cd /tmp/; ls -ltr".
If I do: "sudo -i -u john.smith whoami" without the double quotes, it will tell me I am john.smith.
Now I want to expand that do whoami, change directories, and execute an ls command while using "root" to sudo as john.smith.
Upvotes: 3
Views: 4558
Reputation: 183602
Bash supports a -c
flag that lets you specify the command to run as a command-line argument — basically an inline Bash script. That means you can easily combine multiple commands into a single call to bash
, which is then easily sudo
-ed:
sudo -i -u john.smith bash -c 'whoami ; cd /tmp/ ; ls -ltr'
or
sudo -i -u john.smith \
bash -c ' whoami
cd /tmp/
ls -ltr
'
(Other shell languages have the same feature.)
Upvotes: 5
Reputation: 10559
You can make small shell script file and start it with sudo.
Another thing I often useon Ubuntu is to make "sudo su" - this give me root shell and I proceed from there.
Upvotes: 0