Linux - sudo Privileges in Linux

In Linux, sudo (short for “superuser do”) allows a regular user to run specific commands with root (administrator) privileges. It is a safe way to provide administrative access without giving full control of the entire system.


Why sudo Is Important

  • It prevents users from logging in directly as root, improving security.

  • It allows controlled access for system administration tasks.

  • It keeps logs of who executed what command using sudo.


Basic sudo Usage

sudo command

Runs a command with administrative privileges.

Example:

sudo apt update

How sudo Privileges Are Granted

sudo privileges are controlled through the sudoers file.

1. Adding a user to sudo group

(For Debian/Ubuntu)

sudo usermod -aG sudo username

(For CentOS/RHEL)

sudo usermod -aG wheel username

Once added, the user can run sudo commands.


Editing sudoers File

To safely edit the sudo configuration:

sudo visudo

Inside the file, entries can be defined like:

username  ALL=(ALL:ALL) ALL

This gives full sudo access to a user.


Restricting sudo Access

You can give permission for specific commands only:

Example:

username ALL=(ALL) /usr/bin/systemctl, /usr/bin/reboot

The user can run only those commands using sudo.


Common sudo Errors

1. User not in sudoers file

Happens if the user does not have sudo privileges.

2. Incorrect permissions

The user may need to be added to the correct admin group.


Check sudo privileges

sudo -l

Shows which commands the user is allowed to run.