How to Run Commands as Another User in Linux

Linux is the best and most-used open-source operating system. Linux users have a large number of options to choose operating systems. You have options to choose a desktop for your Linux systems. But still, Linux professionals love the command line to work. Mainly the Linux server editions come with a command-line option only, which create them lighter and faster.

The Linux users use a shell to interact with operating systems. In which the Bash (Bourne Shell) is the most used shell and available default on most systems. Nowadays Zsh (Z Shell) is also getting popularity among the users due to its features.

In this tutorial, you will learn how to run commands as another user in Linux/Unix systems.

Running Command as Another User with “su”

su (Switch User) command is used to run a shell as another user. This command switches to the new user and loads 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 commands as another user without switching to them. To do this, check the below example.

Related:  How To Find Files Modified in Last 30 Days in Linux

I am currently logged in as a root user. You can pass your command with -c parameters. Now, the below example will run the “ls” command as user mytechmint without switching to the user.

su - mytechmint -c "pwd" 

Output:

/home/mytechmint

You can also run multiple commands as another user in Linux, like:

su - mytechmint -c "pwd; mkdir hello && cd hello; pwd" 

Output:

/home/mytechmint /home/mytechmint/hello

In the above command, first prints present working directory with pwd, then create and switches to a new directory named “hello”. Again prints the present working directory. All commands are separated with a semicolon (;) as we do in general.

Run Command as Another User with “sudo”

This is generally used to run commands as a 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 the user to which you want to run the command. After that specify the command to run as a defined user.

Related:  How to Reset MySQL "root" Password in Linux

Syntax:

sudo -u username [commands...]

For example, you are writing a shell script, which is required to run as a non-root user. But you need to restart the apache2 service. In that case, you can use sudo to run the command as the 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 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 – run a command as user “mytechmint” and create a directory in his home directory.

runuser - mytechmint -c 'mkdir -p ~/hello' 

Then list files under the home directory of user ‘mytechmint’.

runuser - mytechmint -c 'ls -l' 

Output:

total 16 
-rw-r--r-- 1 mytechmint mytechmint 8980 Feb 15 2020 examples.desktop 
drwxr-xr-x 2 mytechmint mytechmint 4096 Dec 21 15:55 hello

You can also execute booth commands in a single command. Just add multiple commands with semicolon-separated.

runuser - mytechmint -c 'mkdir -p ~/hello; ls -l' 

In this tutorial, we have learned to run commands as another user in a Linux system. You have learned to run commands as another user with the help of su, sudo and runuser Linux commands. Feel free to use the comment box in case you have any query 😎

Leave a Comment