- How to Run Linux Commands as Another User?
- Method 1: Using the “runuser” Command
- Syntax
- Example 1: Switch the User and then Run Commands
- Example 2: Run the Linux Commands as Another User from the Same Shell
- Method 2: Using the su Command
- Example 1: Switch the User and then Run Commands
- Example 2: Run the Linux Commands as Another User from the Same Shell
- Method 3: Using the “pkexec” command
- Syntax
- Example
- Conclusion
- su options — running command as another user
- 2 Answers 2
- How to run a command as a specific user in an init script?
- 6 Answers 6
- Linux Run Commands As Another User
- Running Command As Another User with Su
- Run Command As Another User with Sudo
- Run Command As Another User with Runuser
- Conclusion
How to Run Linux Commands as Another User?
Sometimes, there is a need to run a Linux command from another user account, and to do so, the conventional approach is to switch to that user and then run the command. However, Linux distributions also support a feature to run a command as another user without logging out of the current user.
This blog will explore different approaches to run the Linux commands as another user.
Method 1: Using the “runuser” Command
The “runuser” command allows running the command as the substitute user in Linux distributions. The general syntax of using the “runuser” command to run Linux commands as another user:
Syntax
$ sudo runuser -[options] [username] [command]
According to the above general usage of the runuser command, we can use this command as the root user or with super-user privilege by using the “sudo”.
Then, we can use the different options of the runuser, which can be explored with the command:
The options and their application will be displayed on the screen.
Coming back to the general syntax of the “runuser” command, after specifying options, you have specified the user name to which you want to use and then the command.
Example 1: Switch the User and then Run Commands
For example, we will log in to another user, “john,” by using the runuser command’s option “-l”:
Now, we can run the multiple commands in the user john and then exit the shell by using the command:
The shell of john user has been logout.
Example 2: Run the Linux Commands as Another User from the Same Shell
If you want to run the command as John user from the same shell:
The contents of the john user directory have been displayed.
Method 2: Using the su Command
Next is switching the specified user account using the “su” command. The general syntax of using the “su” command:
$ su [options] - [username] [arguments]
The above general syntax of switching to another account is simple to understand. Use the su command with the “-” sign and specify the username. It will ask you for the user account’s password and log in to another user account.
Example 1: Switch the User and then Run Commands
To understand it more clearly, we will use the su command to log in to the “john” user account:
After asking for the password, it is logged in to the john user shell. Run the ls command in the new shell:
To go back to the “itslinux” user, again use the su command:
We are back to the shell of our user account.
Example 2: Run the Linux Commands as Another User from the Same Shell
To run the command without logging into another user shell, use the command:
First, it asks for a password, and then the result of the command is printed.
Method 3: Using the “pkexec” command
The “pkexec” command can also be used to run Linux commands by another user. The usage of the pkexec command is visualized in the following syntax:
Syntax
$ sudo pkexec --user [username] [command]
Example
Following the above general syntax, we will log in to the john account and run the command:
The command has been executed in the “john” user account.
Conclusion
To run Linux commands as another user, we can use the “runuser” command and “pkexec” command and can log in to another user with the “su” command. All these commands switch to another user (either while execution or before execution) to run the command from a specific user’s account. This post has briefly explained the methods to run Linux commands as another user.
su options — running command as another user
I was wondering how to run a command as another user from a script. I have the script’s owner set as root. I also have the following command being run within the script to run the command as the hudson user:
For other googlers: some users might have this ability disabled on purpose. You can run sudo cat /etc/passwd | grep user-abc . If you see something like this: user-abc:x:994:994::/home/user-abc:/bin/false then it won’t work. That’s because the last part » /bin/false » means that there is no shell for that user.
2 Answers 2
$ su --help Usage: su [options] [LOGIN] Options: -c, --command COMMAND pass COMMAND to the invoked shell -h, --help display this help message and exit -, -l, --login make the shell a login shell -m, -p, --preserve-environment do not reset environment variables, and keep the same shell -s, --shell SHELL use SHELL instead of the default in passwd
And some testing (I used sudo as I don’t know the password for the nobody account)
$ sudo su -c whoami nobody [sudo] password for oli: nobody
When your command takes arguments you need to quote it. If you don’t, strange things will occur. Here I am —as root— trying to create a directory in /home/oli (as oli) without quoting the full command:
# su -c mkdir /home/oli/java oli No passwd entry for user '/home/oli/java'
It’s only read mkdir as the value for the -c flag and it’s trying to use /home/oli/java as the username. If we quote it, it just works:
# su -c "mkdir /home/oli/java" oli # stat /home/oli/java File: ‘/home/oli/java’ Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 811h/2065d Inode: 5817025 Links: 2 Access: (0775/drwxrwxr-x) Uid: ( 1000/ oli) Gid: ( 1000/ oli) Access: 2016-02-16 10:49:15.467375905 +0000 Modify: 2016-02-16 10:49:15.467375905 +0000 Change: 2016-02-16 10:49:15.467375905 +0000 Birth: -
How to run a command as a specific user in an init script?
I’m writing an init script which is supposed to execute a single command as a user different than root. This is how I’m doing it currently:
sudo -u username command This generally works as expected on Ubuntu/Debian, but on RHEL the script which is executed as the command hangs.
Is there another way to run the command as another user?
(Note that I can’t use lsb init functions as they’re not available on RHEL/Centos 5.x.)
Notice that this question is about something set up exclusively by the administrator (typically, a daemon that runs as some user for security). A slightly different case is users setting up on their own commands to run at boot, with their user crontab. See askubuntu.com/questions/260845/…
6 Answers 6
On RHEL systems, the /etc/rc.d/init.d/functions script is intended to provide similar to what you want. If you source that at the top of your init script, all of it’s functions become available.
The specific function provided to help with this is daemon . If you are intending to use it to start a daemon-like program, a simple usage would be:
daemon --user=username command
If that is too heavy-handed for what you need, there is runuser (see man runuser for full info; some versions may need -u prior to the username):
/sbin/runuser username -s /bin/bash -c "command(s) to run as user username"
Linux Run Commands As Another User
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:
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.
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.
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'
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.