How to Use the Sudo Command in Linux

Get past 'permission denied' messages... and more!

Sudo is one of the most important commands in Linux, so it's also one of the first commands every Linux user should learn how to use. Modern Linux distributions can provide a working environment that's just as easy to navigate as Windows or macOS, but there are still a lot of tasks best left to the terminal. Since you're likely to run into permissions issues in the terminal, it's important to learn how to use the sudo command in Linux.

A man works in Linux using the sudo command.
Maskot / Getty

What Is the Sudo Command?

Sudo is a command that exists to deal with permissions problems in Linux. If you try to run a terminal command, and you get an error message that you don't have permission to run that command, sudo is the quickest way around the problem.

Originally, sudo stood for superuser do, because it was intended to allow any user to execute a command as if they were the superuser. It can also stand for substitute user do, as the same command can allow any user to execute a command as either the superuser or any other restricted user.

To execute the sudo command, you have to enter your password. Your user account also needs to have permission to use sudo. If it doesn't, and you need to alter the sudoers configuration file to give yourself permission or ask the system administrator to do so if you don't have permission.

When to Use the Sudo Command

When using the terminal in Linux, you may enter a command and receive a permission denied error message. There are several solutions to that problem, like switching to the superuser, but the simplest is to just reenter the command with sudo appended to the front.

One example of a command which typically requires sudo to work is apt-get. Here's an example where sudo would come in handy:

A screenshot of the permission denied message in a Linux terminal.

In this example, you would simply append sudo to the failed command:

sudo apt-get install libreoffice

Entering that into the terminal will cause the command to run as the superuser and get rid of your permissions problem:

A screenshot of the Linux terminal window with the sudo command executed.

Linux may ask you to enter your password before allowing you to execute the sudo command, in which case you simply type the password for your account and press Enter.

How to Use Other Sudo Options:

Most users won't ever need to use the sudo command beyond the basic scenario outlined above, where it's a simple workaround for permissions errors. However, there are a lot of options you can add to the sudo command to subtly change the way it works and what happens when you enter it.

Here are the most important sudo options and what they do:

  • The -h option: Typing sudo -h will bring up a helpful screen of information about all of the sudo options and what they do. If you don't have this article handy, try the -h option and see if that helps.
  • The -u option: Typing sudo -u username will execute the next command as the specified user rather than as yourself or the superuser.
  • The -k option: Typing sudo -k will reset your timestamp and invalidate your credentials. The next time sudo is used, it will prompt for a password.
  • The -v option: Typing sudo -v will update your timestamp and extend your sudo timeout for five minutes, or an amount of time specified in the sudoers file.
  • The -s option: Typing -s launches a new shell as specified by the environment variable or in the file passwd. This is useful if you need to execute a lot of commands as root but don't want to use the su command.
  • The -l option: Typing sudo -l will print out a list of commands that the current user is allowed to use and a list of the commands that are forbidden.
A screenshot of the sudo -l command.

What If Sudo Won’t Run: How to Install Sudo

Depending on your Linux distribution, you may find sudo isn't installed by default. When that happens, you will see an error message like this:

sudo command not found.

If you receive this message, then you won't be able to use sudo. The solution is to install sudo, which is a process that differs depending on which distribution of Linux you are using. We'll show you how to install sudo on four of the most popular Linux distributions here.

To install sudo if you're using Ubuntu or Debian, open a terminal and enter this command:

apt-get install sudo

To install sudo if you're using CentOS or Fedora, open a terminal and enter this command:

yum install sudo

How to Give Yourself Permission to Use Sudo

Since sudo is designed to allow regular users to act as the superuser, there has to be a way to govern which users of a system have access to it. To accomplish this, Linux uses the sudoers file, which is simply a file that specifies which users are allowed to use sudo.

Adding yourself to the sudoers file is slightly different depending on your Linux distribution. To add yourself on Debian, Ubuntu, and other similar distributions, open a terminal window and enter:

usermod -aG sudo username

Replace username with the actual username you want to add to the sudoers file, and that username will be granted access to sudo.

To add yourself on CentOS or Fedora, open a terminal window and enter:

usermod -aG wheel username

Replace username with the username you want to add to the sudoers file, and it will be granted access to sudo.