Friday, November 15, 2024
Google search engine
HomeSecurity & TestingHow to Use the sudo Command in Linux

How to Use the sudo Command in Linux

Introduction

Sudo stands for SuperUser DO and is used to access restricted files and operations. By default, Linux restricts access to certain parts of the system preventing sensitive files from being compromised.

The sudo command temporarily elevates privileges allowing users to complete sensitive tasks without logging in as the root user. In this tutorial, learn how to use the sudo command in Linux with examples.

tutorial explains how to use the sudo command in linuxtutorial explains how to use the sudo command in linux

Prerequisites

  • A system running Linux
  • Access to a command line/terminal window (Activities > Search > Terminal)
  • A user account with sudo or root privileges

How to use the sudo Command

sudo was developed as a way to temporarily grant a user administrative rights. To make it work, use sudo before a restricted command. The system will prompt for your password. Once provided, the system runs the command.

Syntax

To start using sudo, use the following syntax:

sudo [command]

When the sudo command is used, a timestamp is entered in the system logs. The user can run commands with elevated privileges for a short time (default 15 minutes). If a non-sudo user tries to use the sudo command, it is logged as a security event.

Options

sudo can be used with additional options:

  • -h – help; displays syntax and command options
  • -V – version; displays the current version of the sudo application
  • -v – validate; refresh the time limit on sudo without running a command
  • -l – list; lists the user’s privileges, or checks a specific command
  • -k – kill; end the current sudo privileges

Additional options can be found under the -h option.

Options related to the sudo command in Linux.Options related to the sudo command in Linux.

Note: Staying logged in as an administrator compromises security. In the past, admins would use su (substitute user) to temporarily switch to an administrator account. However, the su command requires a second user account and password, which isn’t always feasible.

Granting sudo Privileges

For most modern Linux distributions, a user must be in the sudo, sudoers, or wheel group to use the sudo command. By default, a single-user system grants sudo privileges to its user. A system or server with multiple user accounts may exclude some users from sudo privileges.

We recommend to only grant privileges that are absolutely necessary for the user to perform daily tasks.

The following sections explain how to add a user to the sudoers group.

RedHat and CentOS

In Redhat/CentOS, the wheel group controls sudo users. Add a user to the wheel group with the following command:

usermod -aG wheel [username]

Replace [username] with an actual username. You may need to log in as an administrator or use the su command.

Debian and Ubuntu

In Debian/Ubuntu, the sudo group controls sudo users. Add a user to the sudo group with the following command:

usermod -aG sudo [username]

Replace [username] with an actual username. You may need to log in as an administrator or use the su command.

Using visudo and the sudoers Group

In some modern versions of Linux, users are added to the sudoers file to grant privileges. This is done using the visudo command.

1. Use the visudo command to edit the configuration file:

sudo visudo

2. This will open /etc/sudoers for editing. To add a user and grant full sudo privileges, add the following line:

[username] ALL=(ALL:ALL) ALL
Image of the visudo configuration file in Linux.Image of the visudo configuration file in Linux.

3. Save and exit the file.

Here’s a breakdown of the granted sudo privileges:

[username] [any-hostname]=([run-as-username]:[run-as-groupname]) [commands-allowed]

Note: It’s easier to simply add a user to the sudo or wheel group to grant sudo privileges. If you need to edit the configuration file, only do so using visudo. The visudo application prevents glitches, bugs, and misconfigurations that could break your operating system.

Examples of sudo in Linux

Basic Sudo Usage

1. Open a  terminal window, and try the following command:

apt-get update

2. You should see an error message. You do not have the necessary permissions to run the command.

example of an action in Linux that requires elevated permissionsexample of an action in Linux that requires elevated permissions

3. Try the same command with sudo:

sudo apt-get update

4. Type your password when prompted. The system executes the command and updates the repositories.

system updating the official repositories when sudo invokedsystem updating the official repositories when sudo invoked

Run Command as a Different User

1. To run a command as a different user, in the terminal, enter the following command:

whoami

2. The system should display your username. Next, run the following command:

sudo -u [different_username] whoami

3. Enter the password for [different_username], and the whoami command will run and display the different user.

Using the sudo -u command to run a command as another userUsing the sudo -u command to run a command as another user

Switch to Root User

This command switches your command prompt to the BASH shell as a root user:

sudo bash

Your command line should change to:

root@hostname:/home/[username]

The hostname value will be the network name of this system. The username will be the current logged-in username.

Using the sudo bash command to switch root userUsing the sudo bash command to switch root user

Execute Previous Commands with sudo

The Linux command line keeps a record of previously executed commands. These records can be accessed by pressing the up arrow. To repeat the last command with elevated privileges, use:

sudo !!

This also works with older commands. Specify the historical number as follows:

sudo !6

This example repeats the 6th entry in history with the sudo command.

To learn about how to efficiently use history command, check out our article on sudo history command with examples.

Run Multiple Commands in One Line

String multiple commands together, separated by a semicolon:

sudo ls; whoami; hostname
Running multiple sudo commands.Running multiple sudo commands.

Add a String of Text to an Existing File

Adding a string of text to a file is often used to add the name of a software repository to the sources file, without opening the file for editing. Use the following syntax with echo, sudo and tee command:

echo ‘string-of-text’ | sudo tee -a [path_to_file]

For example:

echo "deb http://nginx.org/packages/debian `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list
Invoking the sudo command to add the Nginx repositoryInvoking the sudo command to add the Nginx repository

Note: This would add the Nginx software repositories to your system.

Conclusion

You should now understand the sudo command, and how to use it. Next, learn the difference between the sudo and su command.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments