Introduction

This lesson will cover how to switch to other accounts using sudo command.

The sudo - Super User Do

Another way to switch users or execute commands as others is to use the sudo command. The syntax for sudo is:

sudo command

Thesudo allows you to run programs with the security priviledges of another user. Like su, if no username is specified, it assumes that you were trying to run commands as the superuser. This why sudo is referred to as superuser do.

It is commonly used to install, start and stop applications that require root priviledges. One of the advantages of using sudo over the su command is that you don't need to know the password of the other user. This could eliminate some issues that arise from using shared passwords in generic accounts. When you execute the sudo command, you are prompted for your password. If the sudo configuration permits access, that command is executed.

The sudo configuration is typically controlled by the system administrator and requires root access to change. Of course, on your personal system, you have access to the root account, and you are effectively the system administrator as well.

Using sudo

To see the commands that are available for you to run with sudo, use sudo -l. To run a command as the root user, use sudo command. You can specify a user with -u, for example sudo -u root command is the same as sudo command. However, if you want to run a command as another user, you need to specify that with -u. So, for example sudo -u nikki command.

Commands Meaning
sudo -l List available commands.
sudo command Run command as root.
sudo -u root command Run command as root.
sudo -u user command Run command as user.

You can use sudo su to switch to the superuser account. You can use sudo su - to switch to the superuser account with root's environment. The sudo su - username would switch to that username's account with an environment that you would expect to see when you logged in as that user.

Commands Meaning
sudo su Switch to the superuser account.
sudo su - Switch to the superuser account with root's environment.
sudo su - username Switch to the username's account with the username's environment.

Another way to switch to another account with sudo is to use the -s option. If you run sudo -s that will start a shell as root. You can specify a user with the -u option.

Commands Meaning
sudo -s Start a shell as root
sudo -u root -s Same as above.
sudo -u user -s Start a shell as user.

Examples

Example 1: What command to run with sudo.

You can run the following to see what command can be run with sudo:

$ sudo -l
Matching Defaults entries for robin on robin-ThinkPad-T410:
  env_reset, mail_badpass,
  secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin
 
User robin may run the following commands on robin-ThinkPad-T410:
  (ALL : ALL) ALL

In this case, user robin can run any command.

Example 2: Run a command with sudo

Run a command with sudo

$ sudo /etc/mongodb start
mongodb started
Example 3: Run a command as the other user

To run a command as the other user:

$ sudo -u nikki /etc/nikkiapp/bin/start
Nikki's app started
Running as user: nikki
Example 4: Change to root account

To change to root account:

$ sudo -s
$ whoami
root

Changing the sudo Configuration

If you need to modify the sudo configuration, use the command visudo. It effectively starts the vi editor and edits as the /etc/sudoers file. The visudo command has to be executed with approved priviledges. This means that you need to switch to the root account, for example: su root visudo or run sudo visudo as your own account.

The sudoers file contains a list of users, and what commands that those users can run and as what users those commands can be run as.

There are many options to the sudoers file, but the most simple format and one that you will see quite often is a line that gives a specific user a set of commands that he or she can run. The format is like

user host=(users) [NOPASSWORD:]commands

Here are 2 examples:

admin ALL=(ALL) NOPASSWORD:ALL
robin servername=(root) /etc/init.d/apps

The su command

Another way to switch user is to use su command. The details of su is covered in su command.

References & Resources

  • N/A