How to Create and Use Linux sudo Command

The sudo command in Linux stands for “superuser do“, which allows an approved user to run a command as the superuser or another user, as specified by the security policy.

This is especially useful for performing tasks that require administrative privileges without logging in as the root user.

Setting Up sudo User in Linux

Before using sudo, you need to ensure it is set up correctly. Typically, sudo is pre-installed on most Linux distributions. If it’s not installed, you can install it using your package manager.

sudo apt install sudo         [On Debian, Ubuntu and Mint]
sudo yum install sudo         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/sudo  [On Gentoo Linux]
sudo apk add sudo             [On Alpine Linux]
sudo pacman -S sudo           [On Arch Linux]
sudo zypper install sudo      [On OpenSUSE]    
sudo pkg install sudo         [On FreeBSD]

To allow a normal regular existing user to use sudo, you must add them to the sudo group.

sudo usermod -aG sudo username   [On Debian systems]
sudo usermod -aG wheel username  [On RedHat systems]

Alternatively, you can create a new sudo user by using the adduser or useradd command.

Make sure to replace new_username with the actual username you want to grant sudo privileges to.

sudo adduser new_username
OR
sudo useradd new_username
sudo passwd new_username
Create Sudo User
Create Sudo User

Once created, add the new user to the sudo group.

sudo usermod -aG sudo username   [On Debian systems]
sudo usermod -aG wheel username  [On RedHat systems]

Switch to the new user and check if they have sudo access.

su - new_username
sudo whoami
Add User to Sudo Group
Add User to Sudo Group

How to Use sudo in Linux

Once a user is added to the sudo group, they can use the sudo command to perform administrative tasks.

Basic sudo Usage

To use sudo, simply prepend it to the command you want to run with superuser privileges.

sudo apt update
Run Command as Sudo
Run Command as Sudo

When you run this command, you’ll be prompted to enter your user password. After entering the password, the command will execute with elevated privileges.

Running a Command as Another User

You can also use sudo to run a command as another user using the -u option followed by the username.

For example, to list files as the user ravi:

sudo -u ravi ls -l /home/ravi
Running Command as Another User
Running Command as Another User

Editing Files with sudo

To edit a system file with a text editor, you often need sudo privileges.

sudo nano /etc/hosts
Editing Files with sudo
Editing Files with sudo

Advanced sudo Configuration

The sudo command is highly configurable. You can customize its behavior by editing the /etc/sudoers file. It’s crucial to edit this file correctly to avoid configuration issues.

To edit /etc/sudoers file, always use the visudo command.

sudo visudo
Editing Sudo Configuration File
Editing Sudo Configuration File

Granting Specific Permissions

You can grant specific permissions to users or groups in the /etc/sudoers file. For example, to allow the user ravi to restart the Apache service without a password prompt, add the following line.

ravi ALL=(ALL) NOPASSWD: /usr/sbin/service apache2 restart

Restrict Access to Commands for Multiple Users

User aliases allow you to specify a list of users who share a common set of privileges, which is particularly useful when you want to grant the same level of access to multiple users.

For example, if you have a group of developers who need access to certain administrative commands, you can create a user alias for them.

User_Alias DEVELOPERS = user1, user2, user3

With this alias defined, you can then grant sudo privileges to all users in the DEVELOPERS alias.

DEVELOPERS ALL=(ALL) /usr/bin/apt

This line allows all users in the DEVELOPERS alias to run the apt command with sudo privileges.

Conclusion

The sudo command is an essential tool for managing a Linux system. It provides a secure way to perform administrative tasks without logging in as the root user.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

5 Comments

Leave a Reply
  1. Actually, the correct way to edit files with sudo is the command `sudoedit file` or `sudo -e file`. It needs the EDITOR environment variable to be set.

    It runs your favorite editor (nano, kate, neovim, etc.) as an ordinary user but allows you to modify the root file in a secure way.

    When you use `sudo nano`, you are running the editor as the root user, and that’s not good at all.

    Reply
  2. If you do not have ‘sudo’ pre-installed on your system, how can you use it to install ‘sudo” or to add a user to the sudo group?

    Reply
    • @dragonmouth,

      If you do not have sudo pre-installed on your system, you can still manage to install it or add a user to the sudo group by logging in as the root user.

      su -
      apt-get update
      apt-get install sudo
      usermod -aG sudo username
      sudo whoami
      
      Reply
      • Yes, I realize this but the install commands as given in the article will not work:

        sudo apt install sudo
        

        will generate a sudoers file error.

        On the other hand, if you are using the root account to install sudo, the leading ‘sudo‘ in the command will generate a “sudo command not found” error.

        Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.