sudo (Superuser Do) allows authorized users to execute commands as root or another user while keeping system security intact. The /etc/sudoers file controls who can use sudo and what they can do.
This guide explains configuring sudo permissions properly using the visudo command.
On Ubuntu/Debian, users in the sudo group have admin privileges by default.
1οΈβ£ Add a user (nikhil) to the sudo group
sudo usermod -aG sudo nikhil2οΈβ£ Verify with:
groups nikhil3οΈβ£ Test sudo access:
su - nikhil
sudo whoamiβ Output:
root
π For RHEL/CentOS, use the wheel group instead:
sudo usermod -aG wheel nikhilAlways use visudo to edit /etc/sudoers to avoid syntax errors.
sudo visudoFind the line:
root ALL=(ALL:ALL) ALL
Below it, add:
nikhil ALL=(ALL:ALL) ALL
πΉ This allows nikhil to run any command as root.
To allow nikhil to run sudo without entering a password, modify:
nikhil ALL=(ALL) NOPASSWD: ALLπ Use Case: Helpful for automation and scripts.
To allow nikhil to only restart Apache:
nikhil ALL=(ALL) NOPASSWD: /bin/systemctl restart apache2β
Now nikhil can run:
sudo systemctl restart apache2β But cannot run sudo rm -rf /.
Instead of adding individual users, grant sudo access to an entire group:
%developers ALL=(ALL:ALL) ALLβ
All users in the developers group can use sudo.
To add a user (john) to the group:
sudo usermod -aG developers johnTo simplify management, define command aliases:
Cmnd_Alias WEB_CMDS = /bin/systemctl restart apache2, /bin/systemctl reload nginx
Cmnd_Alias NET_CMDS = /sbin/ifconfig, /bin/pingAssign them:
nikhil ALL=(ALL) NOPASSWD: WEB_CMDS, PASSWD: NET_CMDSβ
nikhil can restart Apache/Nginx without a password
β
But must enter a password to run networking commands
To block dangerous commands (like rm -rf or fdisk):
nikhil ALL=(ALL) ALL, !/bin/rm, !/sbin/fdiskβ
nikhil can run all commands except rm and fdisk.
Enable logging to track sudo usage:
Defaults logfile="/var/log/sudo.log"β
All sudo commands will be logged in /var/log/sudo.log.
To view logs:
cat /var/log/sudo.logDefaults:nikhil !authenticateβ
nikhil can run sudo without being asked for a password.
To prevent users from running sudo su or sudo -i (full root shell):
Defaults !root_sudoβ Users cannot escalate to full root privileges.
To remove sudo access from a user:
sudo deluser nikhil sudo # Ubuntu/Debian
sudo gpasswd -d nikhil wheel # RHEL/CentOSβ
nikhil can no longer run sudo.
| Configuration | Entry in /etc/sudoers |
Effect |
|---|---|---|
| Full sudo access | nikhil ALL=(ALL) ALL |
nikhil can run all commands |
| No password required | nikhil ALL=(ALL) NOPASSWD: ALL |
nikhil can run sudo without password |
| Specific command only | nikhil ALL=(ALL) NOPASSWD: /bin/systemctl restart apache2 |
Can restart Apache but nothing else |
| Restrict commands | nikhil ALL=(ALL) ALL, !/bin/rm, !/sbin/fdisk |
Cannot run rm or fdisk |
| Allow group sudo | %developers ALL=(ALL) ALL |
All developers group members can use sudo |
| Disable root escalation | Defaults !root_sudo |
Prevents sudo su |
| Log sudo commands | Defaults logfile="/var/log/sudo.log" |
Tracks sudo usage |