Run as user linux sudo

Run Commands As Another User Via Sudo In Linux

Picture this scenario. You are managing a Linux server that is accessed by multiple users. All users have sudo rights. You want to test certain Linux commands with different users. How would you do that? Log in as one user and test the commands, and then log out and log back in as another user and test commands? Yes, It is one way to do it. However, there is a simple way to run commands as another user via sudo in Linux. Read on to find out.

For the purpose of this guide, I have created two user accounts namely «senthil» and «kumar» in my AlmaLinux 8 test machine. I am going to use «senthil» as current user and «kumar» as the target user.

I want the user «senthil» to be able to run any command as user «kumar» on any hosts, without actually logging in as «kumar». Understood? Well, let me show you an example.

Run Commands As Another User Via Sudo

I currently logged in as user «senthil». Let me show you the currently logged in user with whoami command.

Now I am going to run a simple » echo » command with sudo as the target user «kumar» while logged in as «senthil»:

You will be prompted to enter the password of current user (i.e. «senthil» in this case). Once authenticated, the echo command will be run as user «kumar» and display the username and user id of the «kumar».

Sample output:

What we actually just did is we executed the » echo » command as user «kumar» while we logged in as «senthil». This is why you see the username and user id of kumar, but not senthil’s.

Let us verify if the uid of the user «kumar» is correct with id command.

Yes, the uid of kumar is correct in both outputs.

Here, the -u flag is used to run the given command as another user (i.e. kumar in this case) and bash -c is used to mention the name of the command. You should mention the command in single quote.

If you don’t use bash -c option, the echo command will return the username and uid of the current user instead of the target user.

Similarly, you can run any commands as different user.

Please note that in our previous example command, I didn’t enter sudo password for the target user. Why? Because, the echo command doesn’t require sudo password.

If you run a command that requires sudo permission as another user, you must enter the password. Here is another example.

Sample output:

Did you notice that I entered sudo password for both users? Yes. Because, we run » sudo dnf update «, which requires elevated privileges. That’s why it is required to enter the sudo password for the target user. For normal commands e.g. ls , uname etc., we don’t need to enter the target user’s password.

Читайте также:  Как удалить все принтеры linux

Not just Linux commands, you can apply this method to run scripts as well. Let us say you want to run backup script that is saved in the $HOME directory of another user. You can simply run the script from the current user as other user by using this command:

Run Commands Without Sudo Password

Sometimes, you might be in a situation where you don’t want to enter the sudo password for every commands. In such cases, you can skip the password prompt for specific commands by adding NOPASSWD option in /etc/sudoers file.

Let us say you want to allow a certain users to run any dnf command without sudo password. To do so, first find the path of the dnf command using which or whereis commands.

As you can see, the executable path of dnf command is /usr/bin/dnf .

Now, edit /etc/sudoers files using command:

Heads Up: Please note that you should not manually edit the sudoers file using any text editor. Always use visudo command to safely edit the sudoers file.

Add the following line at the end the file to allow the user called «kumar» to run dnf commands without sudo password.

Save the file and exit.

From now on, the user «kumar» can able to run dnf commands without sudo password.

See? We don’t need to enter sudo password for «kumar».

To revoke this behavior, simply remove the above line.

Disclaimer: You should be very careful while applying this method. This method can be used for both productive and destructive purposes. Say for example, if you allow users to execute ‘rm’ command without sudo password, they could accidentally or intentionally delete important files. Don’t do this unless it is really necessary.


In this brief tutorial, we discussed how to allow users to run commands as another user via sudo in Linux. We also learned how to disable sudo password prompt for certain users when running specific command(s) by modifying sudoers file.


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.

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

Читайте также:  Wordpress кнопка будьте с нами

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:

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.


Example – for example, run a command as user “rahul” and create directory in his home directory.

Then list files under home directory of user ‘rahul’.

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


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.


How to Use the sudo Command in Linux

Home » SysAdmin » How to Use the sudo Command in Linux

Sudo stands for SuperUser DO and is used to access restricted files and operations. By default, Linux restricts access to certain parts of the system preventing sensitive files from being compromised.

The sudo command temporarily elevates privileges allowing users to complete sensitive tasks without logging in as the root user. In this tutorial, learn how to use the sudo command in Linux with examples.

  • A system running Linux
  • Access to a command line/terminal window (Activities > Search > Terminal)
  • A user account with sudo or root privileges

How to use the sudo Command

sudo was developed as a way to temporarily grant a user administrative rights. To make it work, use sudo before a restricted command. The system will prompt for your password. Once provided, the system runs the command.


To start using sudo , use the following syntax:

When the sudo command is used, a timestamp is entered in the system logs. The user can run commands with elevated privileges for a short time (default 15 minutes). If a non-sudo user tries to use the sudo command, it is logged as a security event.


sudo can be used with additional options:

  • -h – help; displays syntax and command options
  • -V – version; displays the current version of the sudo application
  • -v – validate; refresh the time limit on sudo without running a command
  • -l – list; lists the user’s privileges, or checks a specific command
  • -k – kill; end the current sudo privileges

Additional options can be found under the -h option.

Note: Staying logged in as an administrator compromises security. In the past, admins would use su (substitute user) to temporarily switch to an administrator account. However, the su command requires a second user account and password, which isn’t always feasible.

Granting sudo Privileges

For most modern Linux distributions, a user must be in the sudo, sudoers, or wheel group to use the sudo command. By default, a single-user system grants sudo privileges to its user. A system or server with multiple user accounts may exclude some users from sudo privileges.

Читайте также:  Hp laserjet 1536 dnf mfp картридж оригинальный

We recommend to only grant privileges that are absolutely necessary for the user to perform daily tasks.

The following sections explain how to add a user to the sudoers group.

RedHat and CentOS

In Redhat/CentOS, the wheel group controls sudo users. Add a user to the wheel group with the following command:

Replace [username] with an actual username. You may need to log in as an administrator or use the su command.

Debian and Ubuntu

In Debian/Ubuntu, the sudo group controls sudo users. Add a user to the sudo group with the following command:

Replace [username] with an actual username. You may need to log in as an administrator or use the su command.

Using visudo and the sudoers Group

In some modern versions of Linux, users are added to the sudoers file to grant privileges. This is done using the visudo command.

1. Use the visudo command to edit the configuration file:

2. This will open /etc/sudoers for editing. To add a user and grant full sudo privileges, add the following line:

3. Save and exit the file.

Here’s a breakdown of the granted sudo privileges:

Note: It’s easier to simply add a user to the sudo or wheel group to grant sudo privileges. If you need to edit the configuration file, only do so using visudo. The visudo application prevents glitches, bugs, and misconfigurations that could break your operating system.

Examples of sudo in Linux

Basic Sudo Usage

1. Open a terminal window, and try the following command:

2. You should see an error message. You do not have the necessary permissions to run the command.

3. Try the same command with sudo :

4. Type your password when prompted. The system executes the command and updates the repositories.

Run Command as a Different User

1. To run a command as a different user, in the terminal, enter the following command:

2. The system should display your username. Next, run the following command:

3. Enter the password for [different_username] , and the whoami command will run and display the different user.

Switch to Root User

This command switches your command prompt to the BASH shell as a root user:

Your command line should change to:

The hostname value will be the network name of this system. The username will be the current logged-in username.

Execute Previous Commands with sudo

The Linux command line keeps a record of previously executed commands. These records can be accessed by pressing the up arrow. To repeat the last command with elevated privileges, use:

This also works with older commands. Specify the historical number as follows:

This example repeats the 6th entry in history with the sudo command.

To learn about how to efficiently use history command, check out our article on sudo history command with examples.

Run Multiple Commands in One Line

String multiple commands together, separated by a semicolon:

Add a String of Text to an Existing File

Adding a string of text to a file is often used to add the name of a software repository to the sources file, without opening the file for editing. Use the following syntax with echo, sudo and tee command:

Note: This would add the Nginx software repositories to your system.

You should now understand the sudo command, and how to use it. Next, learn the difference between the sudo and su command.


Поделиться с друзьями