Sudo Command in Linux 2024

Learn how to use the sudo command in this tutorial. With sudo, you can run programs as another user, such as the root user, making it a frequently used command for those who spend a lot of time on the command line. Not only is using sudo more convenient, but it also enhances security as it allows you to assign limited administrative privileges to specific users without revealing the root password.

Follow our step-by-step guide to master the sudo command.

Sudo Command in Linux

Installing Sudo (sudo command not found)

The sudo package comes preinstalled on the majority of Linux distributions. You can verify whether it is installed on your system by launching your console, typing sudo, and hitting Enter. In the event that sudo is already installed, the system will present a brief help message; otherwise, you will receive an error message saying that the sudo command not found.

In case sudo is not installed on your system, it can be easily installed by utilizing your distro’s package manager.

Install Sudo on Ubuntu and Debian

apt install sudo

Install Sudo on CentOS and Fedora

yum install sudo

Adding User to Sudoers

On most Linux systems, you can give someone sudo access by adding them to the sudo group in the sudoers file. This group lets its members run any command as the root user. The name of the sudo group can vary between different Linux distributions.

If you’re using a RedHat-based distribution like CentOS or Fedora, the sudo group is called wheel. To add a user to the sudo group, just run a command:

usermod -aG wheel username

In Ubuntu, the root user account is turned off for safety. People are told to use sudo to do system tasks instead. The first user made during installation can already use sudo. This means the user logged in likely already has sudo powers.

If you want a user to use only certain programs with sudo, don’t add them to the sudo group. Instead, put their name in the sudoers file. For instance, if you want trioguide to only run mkdir with sudo, do this:

sudo visudo

and append the following line:

trioguide  ALL=/bin/mkdir

When you type visudo on your computer, it opens the /etc/sudoers file in vim.

Additionally, you have the option to let users use sudo commands without entering their password:

trioguide  ALL=(ALL) NOPASSWD: ALL

How to Use Sudo

Here’s how you use the sudo command:

sudo OPTION.. COMMAND

There are lots of ways to use the sudo command, but most of the time, people use it in the simplest way. You can use sudo by just putting sudo before the command:

sudo command

If you want to use sudo for a command, specify the command name. Sudo will look at the /etc/sudoers file to see if you are allowed to use sudo. When you use sudo for the first time in a session, you will be asked to enter your password, and then the command will be executed with root privileges.

To illustrate, if you want to see all the files in the /root folder, you can use:

sudo ls /root

Output:
[sudo] password for trioguide:
.  ..  .bashrc	.cache	.config  .local  .profile

Password Timeout

When you use sudo, it will usually ask for your password again after five minutes if you haven’t used it. You can adjust this time by modifying the sudoers file.

To do this, you need to open the file using visudo:

sudo visudo

Add the following line to set the default timeout to 10 minutes:

Defaults  timestamp_timeout=10

To modify the timestamp solely for a particular user, include the subsequent line, substituting user_name with the corresponding user’s name:

Defaults:user_name timestamp_timeout=10

Run a Command as a User Other than Root

Many people think that the only purpose of the sudo command is to grant a regular user root privileges. However, it is worth noting that you can also utilize sudo to execute a command as any user by employing the -u option.

To illustrate, consider the following scenario where we use sudo to execute the “whoami” command as the user “trioguide”:

sudo -u trioguide whoami

The command whoami will display the username of the user executing the command.

Output:

trioguide

How to Redirect with Sudo

When attempting to redirect a command’s output to a file without the necessary write permissions for your user account, an error message will be displayed stating “Permission denied.”

sudo echo "test" > /root/file.txt

Output:

bash: /root/file.txt: Permission denied

The reason for this occurrence is that when the output redirection “>“, it is executed with the privileges of the logged-in user instead of the user specified with sudo. This happens before the sudo command is executed.

To solve this problem, one option is to execute a new shell as root using the command sudo sh -c:

sudo sh -c 'echo "test" > /root/file.txt'

You could also use the tee command to pipe the output as a regular user, illustrated in the example below:

echo "test" | sudo tee /root/file.txt

Baseline

Mastering the use of the sudo command and creating new users with sudo privileges is crucial for efficient and secure system management. By following the steps outlined in this article, you can confidently execute administrative tasks without compromising system security.

If you need further clarification or have any queries regarding this topic, don’t hesitate to leave a comment below. Stay informed and keep your system secure with the right knowledge and skills.

Alex

Alex

Hey there! My name is Alex and I'm a professional content writer. I'm also lucky enough to be part of an amazing trio team! I absolutely love what I do and I'm passionate about creating content that engages, informs, and entertains. Whether it's writing blog posts, website copy, or social media content, I always strive to deliver high-quality work that exceeds expectations.

We will be happy to hear your thoughts

Leave a reply

TrioGuide
Logo