Linux

How to Use sudo in Linux: A Complete Guide for Beginners

This guide will help you master the sudo command in Linux: from basic usage to security configuration. You will learn to perform root tasks without risk.

Updated at February 17, 2026
15-20 minutes
Easy
FixPedia Team
Применимо к:Ubuntu 20.04+Debian 10+CentOS 7+Fedora 35+

Introduction / Why This Matters

The sudo command is a key tool for Linux administration. It allows you to perform operations requiring superuser (root) privileges using your regular user account's password. This significantly improves system security because:

  • All sudo commands are logged.
  • There's no need to work as root constantly.
  • Permissions can be easily revoked from specific users.

After completing this guide, you'll be able to use sudo confidently, configure permissions, and avoid common mistakes.

Requirements / Preparation

Before you begin, ensure:

  1. You have access to a Linux terminal (Ubuntu, Debian, CentOS, Fedora, or any other distribution).
  2. Your user account has sudo privileges (usually the account created during system installation).
  3. The sudo package is installed (it's included by default in most modern distributions).

If you're unsure about your privileges, proceed directly to Step 1.

Step 1: Checking Current sudo Privileges

First, check if you can use sudo:

sudo -l

What the command does: The -l (list) flag shows the list of commands you can run with sudo. If you see the message sudo: a password is required, enter your user account's password. If you get the error user is not in the sudoers file, your account lacks sudo privileges—proceed to Step 3.

Step 2: Basic sudo Usage

To run any command as root, simply prepend sudo:

# Update package list (Ubuntu/Debian)
sudo apt update

# Restart the nginx web server
sudo systemctl restart nginx

# Create a file in a protected directory
sudo touch /root/test.txt

Important: The first time you use sudo in a session, the system will prompt for your user account's password (not the root password). After entering it, the password is cached for 15 minutes (by default), and subsequent sudo commands won't ask for it.

Step 3: Configuring the sudoers File

If your account doesn't have sudo privileges, add it to the sudo group (for Debian/Ubuntu) or wheel group (for RHEL/CentOS/Fedora). This requires root privileges (e.g., by logging in as root or using another account with sudo).

# For Debian/Ubuntu
sudo usermod -aG sudo your_username

# For RHEL/CentOS/Fedora
sudo usermod -aG wheel your_username

After running the command, it's crucial to log out and log back in for the changes to take effect.

Advanced configuration via visudo:
The /etc/sudoers file manages detailed rules. Always edit it using visudo, which checks syntax before saving:

sudo visudo

Example of adding a rule for a specific user (replace username):

username ALL=(ALL:ALL) ALL

This grants full sudo privileges. To restrict (e.g., only restart nginx), specify:

username ALL=(ALL) /usr/bin/systemctl restart nginx

Step 4: Security and Best Practices

  1. Avoid sudo -i for daily tasks. This command gives a full root session, which is dangerous. Instead, use sudo for each specific command.
  2. Adjust the timeout: To reduce the time sudo doesn't ask for a password, edit /etc/sudoers via visudo:
    Defaults timestamp_timeout=5
    
    The value is in minutes (here, 5). Set to 0 to always require a password.
  3. Be cautious with NOPASSWD: The NOPASSWD directive in sudoers allows running commands without a password. Use it only for automated scripts and strictly defined commands.
  4. Always verify commands: Before pressing Enter with sudo, re-read the command, especially if it involves rm, dd, mv, or modifying system files.

Step 5: Troubleshooting Common Issues

ProblemSolution
sudo: command not foundInstall the sudo package: apt install sudo (Debian/Ubuntu) or yum install sudo (RHEL/CentOS).
user is not in the sudoers fileAdd the user to the sudo/wheel group (see Step 3) and log out/in.
sudo: no tty present and no askpass program specifiedThis occurs when running sudo non-interactively (e.g., in a script). Use sudo -S to pass the password via stdin or configure NOPASSWD for the specific command.
Syntax error in sudoersAlways edit via sudo visudo. It checks syntax before saving.

Verifying the Result

Ensure sudo works correctly:

  1. Run a command requiring root privileges, for example:
    sudo whoami
    
    Expected output: root.
  2. Check that your account is in the sudo group:
    groups
    
    The list should include sudo or wheel.
  3. Try updating the system (if Debian/Ubuntu):
    sudo apt update
    
    The command should execute without access errors.

Potential Issues

Issue: After adding to the sudo group, commands still don't work.
Solution: Log out of the system and log back in (or reboot). Group memberships update only at the start of a new session.

Issue: sudo asks for a password, but the system says it's incorrect.
Solution: Ensure you're entering your user account's password, not root's. Check if Caps Lock is enabled.

Issue: When editing sudoers via visudo, you get >>> sudoers file: syntax error.
Solution: visudo won't save a file with errors. Return to the editor (usually vi), find the line marked with >>>, and fix the syntax. Common errors: missing colons, incorrect permissions.

Issue: You need to run a long command (e.g., copying a large file), and the sudo timeout expires.
Solution: In another terminal, run sudo -v to refresh the timeout. Or configure a longer timestamp_timeout in sudoers.

F.A.Q.

What is sudo and why is it needed in Linux?
How to add a user to the sudo group?
How does sudo differ from su?
How to use sudo safely to avoid harming the system?

Hints

Check current sudo privileges
Basic sudo usage
Configuring the sudoers file
Security and best practices
Troubleshooting common issues

Did this article help you solve the problem?

FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community