06. Changing Identities su, sudo, -l, -c

Sometimes we need to take on someone else's identity to carry out a specific task.

Let's begin by looking at su, which is used to assume the identity of another user. Then we'll discuss sudo, which is used to execute a single authorized command.

Using su

The su command (aka switch user), allows you to switch to any user account, as long as you know the password.

For example, to switch to the user bob, simply pass bob as the argument to su.

$ su bob
Password:

Don't worry if the password isn't showing any characters as you type - the shell is still reading what you type into the buffer.

Loading the user's environment with -l

If you'd like to load the user's entire environment, you can do so with the -l option.

$ su -l bob
Password:
# Environment gets loaded
# Working directory is bob's home directory

To switch to the superuser, simply use a single hyphen -.

$ su - 

Actually, the -l option is interchangable with using -. Keep in mind that with no argument after the hyphen, the shell assumes the superuser.

Executing a single command as another user

In case you just want to execute a single command as the user, use the -c option.

$ su bob -c 'dougie'

This code will execute the command dougie under the user bob. Make sure to enclose your command in single quotes to prevent expansion!

To exit and return to your account, use the exit command.

$ exit

Using sudo

The sudo command allows a permitted user to execute a command that he or she is entitled to.

The list of who can perform what is kept in a file called /etc/sudoers, and is maintained only by the root adminstrator.

The administrator is able to use this file to allow access to different users in a controlled way. With this, users may be restricted to one or more specific commands and no others.

Unlike su, sudo does not start a new session or load any environmental configurations, and when using sudo, the user enters his own password - not the superusers's.

To use sudo, simply pass in the command after the sudo.

$ sudo vim /etc/hosts
Password:
# Now you can edit this secure file

Simple shortcut for using sudo

Many times you'll input a command that the shell will complain that you don't have sudo permissions. Instead of typing the command again, you can simply call sudo !! to run the same command with sudo permissions.

So what's the main difference? su vs. sudo

su switches you to the root user account (or another user's) and requries a specific password. sudo, on the other hand, runs a single command with root priveleges and does not require a root user password.

/etc/sudoers

The /etc/sudoers file defines who can perform what commands with sudo. To access this file, use the visudo command.

# Defines users
User_Alias ADMINS = user1, user2
 
# Users with ADMINS alias can use sudo to execute commands as root
# First ALL = any host, Second ALL = any command
ADMINS ALL = NOPASSWD: ALL
 
# superuser may also use sudo to run any command on any host. 
# (ALL) means superuse may also run commands as any other user.
root   ALL = (ALL) ALL

Take your Linux skills to the next level!

Linux for Beginners

Take your Linux skills to the next level! Try Linux & UNIX

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad