Linux is the best and most-used open source operating system. The Linux users have a large number of options to choose operating system. You have options to choose desktop for your Linux systems. But still Linux professionals love the command line to work. Mainly the Linux server editions comes with command line option only, which create them lighter and faster.
The Linux users uses shell to interact with operating systems. In which the Bash (Born Shell) is the most used shell and available default on most systems. Now a days Zsh (Z Shell) is also getting popularity between the users due to its features.
In this tutorial you will learn how to run command as another user in Linux/Unix systems.
Using su Command
The su
(substitute user) command is used to switch to another user account. By default, it switches to the root account.
The basic su
command looks like below:
su - username
The above command which you to another user, where you can run commands with that user. But our aim is to run Linux command as another user without switching to them. To do this, check below example.
I am currently logged in as root
user. You can pass your command with -c
parameter. Now, the below example will run “ls” command as user rahul without switching to the user.
su - rahul -c "pwd"
Output: /home/rahul
You can also run multiple commands as another user in Linux, like:
su - rahul -c "pwd; mkdir hello && cd hello; pwd"
Output: /home/rahul /home/rahul/hello
In the above command, first prints present working directory with pwd, then create and switches to new directory named “hello”. Again prints the present working directory. All commands are separated with semicolon (;) as we do in general.
Using Sudo
This is generally used to run command as root user, but you can also use it with other users. Here you don’t need to use any command line switches. Enter the name of user to which you want to run command. After that specific the command to run as defined user.
Syntax:
sudo -u username [commands...]
For example, you are writing a shell script, which required to run as non-root user. But you need to restart apache2 service. In that case you can use sudo to run command as root user. Like:
sudo -u root 'systemctl restart apache2'
Using runuser
Command
You can also use runuser
to run commands as another user in Linux systems. This is lesser known commands by the Linux users. Use runuser
command to execute commands with the effective user ID and group ID of the defined user.
Syntax:
runuser - username -c [commands...]
Example: for example, run a command as user “rahul” and create directory in his home directory.
runuser - rahul -c 'mkdir -p ~/hello'
Then list files under home directory of user ‘rahul’.
runuser - rahul -c 'ls -l'
Output:> total 16 -rw-r--r-- 1 rahul rahul 8980 Feb 15 2020 examples.desktop drwxr-xr-x 2 rahul rahul 4096 Dec 21 15:55 hello
You can also execute booth commands in a single command. Just add multiple commands wit semicolon separated.
runuser - rahul -c 'mkdir -p ~/hello; ls -l'
Conclusion
In this tutorial, you have learned to run commands as another user in Linux system. You have learned running commands as another user with the help of su, sudo and runuser Linux commands.