What is Sudo ?
On this page
Sudo stands for “superuser do”. It’s a Linux command that allows authorized users to execute specific commands with root privileges without needing to log in as the root user.
We use sudo for:
- Security: Users don’t need to know the root password.
- Control: You can specify exactly which commands a user can run as root.
- Accountability: All
sudoactions are logged, so you can track who did what. - Safety: Limits potential damage from mistakes or malicious commands, since users get only the permissions they truly need.
The goal of this lesson is to configure and test the sudo system on a Linux server and to safely grant limited administrative privileges to a normal user so it can perform system monitoring tasks without full root access.
There are two ways to give a user sudo privileges on Linux, the first one is by using an editing the /etc/sudoers file, this is the main configuration file for sudo.
sudo visudoThe second and better way to do it is by using the /etc/sudoers.d/ directory. You create a separate file inside /etc/sudoers.d/ for each user or group.
sudo nano /etc/sudoers.d/userStep 1: Install the Required Packages
Run these commands as root.
apt update
apt install -y sudo monitoring-plugins-basic net-toolsIf the user doesn’t exist, create it.
adduser checkWhat this does:
- adduser = creates a new user
- You should set a password when prompted
Step 3: Configure sudo Access for the User check
Create and edit this file.
nano /etc/sudoers.d/checkFile contents for /etc/sudoers.d/check
# Allow user 'check' to run specific commands as root without password
check ALL=(ALL) NOPASSWD: /usr/lib/nagios/plugins/check_apt
check ALL=(ALL) NOPASSWD: /usr/sbin/arpSet the correct permissions.
chmod 440 /etc/sudoers.d/check
chown root:root /etc/sudoers.d/checkValidate the configuration.
visudo -cOutput.
root@angelo:/# visudo -c
/etc/sudoers: parsed OK
/etc/sudoers.d/check: parsed OKStep 4: Verify the Configuration
Now switch to the check user and test.
su - checkWhat this does:
- su = switch user
- - = start a login shell
The first command should work without a password.
sudo /usr/sbin/arp -nOutput.
check@angelo:/root$ sudo /usr/sbin/arp -n
Address HWtype HWaddress Flags Mask
193.191.177.5 ether b6:5c:06:e0:89:08 CThe second command should also work without a password.
sudo /usr/lib/nagios/plugins/check_aptOutput.
check@angelo:/root$ sudo /usr/lib/nagios/plugins/check_apt
APT OK: 0 packages available for upgrade (0 critical updates).Disallowed command should ask for a password.
sudo ip neighbor showExit back to root.
exit