Sudo is one of the most important commands on Linux, so it’s also one of the first commands every Linux user must learn. Modern Linux distributions can provide easy-to-navigate work environments such as Windows or macOS, but there are still many of the best tasks left in the terminal. Because you might experience permission issues in the terminal, it’s important to learn how to use sudo on Linux.
What is Sudo’s Order?
Sudo is a command that exists to deal with permission issues on 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 fastest way to resolve the issue.
Initially, sudo is short for superuser do, because it was intended to allow each user to execute commands as if they were superuser. It can also stand up to substitute users do, because the same command can allow each user to execute the command as a superuser or other limited user.
To run the sudo command, you must enter a password. Your user account also needs to have permission to use sudo. If not, and you need to change the sudoers configuration file to give yourself permission or ask the system administrator to do it if you don’t have permission.
When to Use Sudo Commands
When using a terminal on Linux, you can enter commands and receive error messages that are denied permission. There are several solutions to that problem, such as switching to superuser, but the simplest is to reenter the command with sudo added to the front.
One example of a command that usually requires sudo to work is apt-get. This is an example where sudo will be useful:
In this example, you simply add sudo to the command that failed:
sudo apt-get install libreoffice
Putting it in a terminal will cause the command to run as a superuser and get rid of your permission problems:
Linux might ask you to enter a password before allowing you to execute the sudo command, in which case you can just type the password for your account and press Enter.
How to Use Other Sudo Options:
Most users don’t need to use the sudo command outside the basic scenario outlined above, which is a simple solution to permission errors. However, there are many options you can add to the sudo command to subtly change how 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 screen of useful information about all the sudo options and what they do. If you don’t have this article, try the -h option and see if it helps.
- Option -u: Typing sudo -u username will execute the next command as the specified user and not as yourself or the superuser.
- Option -k: Typing sudo -k will reset your timestamp and cancel your credentials. The next time sudo is used, it will ask for a password.
- Option -v: Typing sudo -v will update your timestamp and extend your sudo timeout by five minutes, or the amount of time specified in the sudoers file.
- Option -s: Typing -s launches a new shell as specified by the environment variable or in the passwd file. This is useful if you need to run as many commands as root but don’t want to use the su command.
- Option -l: Typing sudo -l will print a list of commands that are allowed by the current user and a list of prohibited commands.
What If Sudo Won’t Run: How To Install Sudo
Depending on your Linux distribution, you might find sudo not installed by default. When that happens, you will see an error message like this:
sudo command not found.
If you receive this message, you will not be able to use sudo. The solution is to install sudo, which is a different process depending on the Linux distribution you are using. We will show you how to install sudo on the four most popular Linux distributions here.
To install sudo if you are using Ubuntu or Debian, open a terminal and enter this command:
apt-get install sudo
To install sudo if you are using CentOS or Fedora, open a terminal and enter this command:
yum install sudo
How to Give Yourself permission to Use Sudo
Because sudo is designed to allow ordinary users to act as superusers, there must be a way to control which system users have access to it. To achieve this, Linux uses the sudoers file, which is a file that determines which users are allowed to use sudo.
Adding yourself to sudoers files is slightly different depending on your Linux distribution. To add yourself to Debian, Ubuntu, and other similar distributions, open a terminal window and enter:
usermod -aG sudo username
Replace the username with the actual username that you want to add to the sudoers file, and that username will be given access to sudo.
To add yourself to CentOS or Fedora, open a terminal window and enter:
usermod -aG wheel username
Replace the username with the username you want to add to the sudoers file, and that will be given access to sudo.