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.
Running Command As Another User with Su
su (Switch User) command is used to run shell as another user. This command switches to the new user and load their environment.
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"
You can also run multiple commands as another user in Linux, like:
su - rahul -c "pwd; mkdir hello && cd hello; pwd"
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.
Run Command As Another User with 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 specificy the command to run as defined user.
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'
Run Command As Another User with Runuser
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.
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'
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.