Linux commands: sudo

By

Published · Updated

Tagged: CLI

Learn how the Linux sudo command runs a command as root, why sudo with > redirection fails, how long the password is cached, and how to give a user sudo access.

sudo is commonly used to run a command as root.

You must be enabled to use sudo, and once you do, you can run commands as root by entering your user’s password (not the root user password).

The permissions are highly configurable, which is great especially in a multi-user server environment, and some users can be granted access to running specific commands through sudo.

For example you can edit a system configuration file:

sudo nano /etc/hosts

which would otherwise fail to save since you don’t have the permissions for it.

You can run sudo -i to start a shell as root:

Terminal showing sudo -i asking for the password before it starts a root shell

You can use sudo to run commands as any user. root is the default, but use the -u option to specify another user:

sudo -u flavio ls /Users/flavio

The sudo command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

How long does sudo remember my password?

After you type your password, sudo doesn’t ask again for 5 minutes. The cache is per terminal, so a new terminal window asks again.

Three options control this:

sudo -v   # type the password now and extend the cache for another 5 minutes
sudo -k   # forget the password for this session, so the next sudo asks
sudo -K   # remove every cached credential for your user, on every terminal

sudo -k is handy before you walk away from your computer with a terminal open.

Why does sudo echo ... > file fail?

This is the most common surprise with sudo:

sudo echo "127.0.0.1 myapp.local" >> /etc/hosts
# bash: /etc/hosts: Permission denied

The >> is not part of the command sudo runs. Your shell handles the redirection before sudo starts, and your shell runs as you, not as root. So echo runs as root, but writing the file doesn’t.

There are two fixes. Use tee, which writes the file itself, and run tee with sudo:

echo "127.0.0.1 myapp.local" | sudo tee -a /etc/hosts

-a appends. Without it, tee overwrites the whole file, so be careful. tee also prints the line to the terminal, add > /dev/null at the end if you don’t want that.

Or run a whole shell as root, so the redirection happens inside it:

sudo sh -c 'echo "127.0.0.1 myapp.local" >> /etc/hosts'

Edit a system file safely with sudoedit

Instead of sudo nano /etc/hosts, you can use sudoedit (the same as sudo -e):

sudoedit /etc/hosts

sudo copies the file to a temporary file, opens it in your editor running as you, and copies it back when you save. Your editor never runs as root, and it uses your own settings. Set the EDITOR environment variable to pick the editor.

What can I run with sudo?

List what you’re allowed to do on this machine:

sudo -l

You’ll see lines like (ALL : ALL) ALL, which means you can run any command as any user and group.

sudo -i vs sudo -s

Both give you a root shell:

I use sudo -i when I need to do several things as root. Type exit to go back to your user.

Why is my environment variable missing under sudo?

By default sudo runs the command in a minimal environment. Most of your variables are dropped, and PATH is usually replaced with a fixed safe value (the secure_path setting).

So this can fail even if it works without sudo:

export API_URL=http://localhost:3000
sudo node script.js   # script.js doesn't see API_URL

sudo -E asks to keep your environment, but the configuration can refuse it. Passing the variable explicitly is clearer:

sudo API_URL=http://localhost:3000 node script.js

The same reset is why a command installed only in your user’s PATH can give command not found under sudo. Use the full path to the program.

Use sudo in scripts with -n

If a script runs sudo and a password is needed, it stops and waits. Add -n and sudo fails right away with an error instead of prompting:

sudo -n systemctl restart nginx

That’s what you want in cron jobs and CI, where nobody is there to type a password.

”user is not in the sudoers file”

If you see:

flavio is not in the sudoers file.

your user is not allowed to use sudo at all. Someone with root access has to add you.

The usual way is to add the user to the group that has sudo rights. On Debian and Ubuntu that group is sudo. On Fedora and RHEL it’s wheel:

# Debian, Ubuntu
sudo usermod -aG sudo flavio

# Fedora, RHEL
sudo usermod -aG wheel flavio

Don’t forget -a. Without it, usermod -G replaces all the user’s other groups. The user must log out and back in for the new group to apply.

Edit the sudoers file with visudo

The rules live in /etc/sudoers. Never open it with a normal editor. Use visudo:

sudo visudo

visudo locks the file and checks the syntax before saving. A typo in /etc/sudoers can stop sudo from working, and then you can’t use sudo to fix it.

A rule has this shape: who, on which hosts, as which user, which commands.

# user flavio can run any command as any user
flavio ALL=(ALL:ALL) ALL

# anyone in the sudo group can too
%sudo ALL=(ALL:ALL) ALL

# user deploy can restart nginx without a password, and nothing else
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx

% means a group. NOPASSWD: skips the password prompt for those commands. Always use full paths to commands in these rules.

Instead of editing the main file, put your rules in their own file in /etc/sudoers.d/, which the main file includes on most systems (look for an @includedir /etc/sudoers.d line). Create it with:

sudo visudo -f /etc/sudoers.d/deploy

Watch the file name. sudo skips files in that folder whose names contain a . or end with ~, so a file called deploy.conf is silently ignored.

sudo lets you run commands with someone else’s permissions. To change who owns a file instead, see chown.

Want me to talk about your product? You can sponsor this site.

Related posts about cli: