Background:
In a Linux system, managing user privileges is essential for ensuring security and proper system administration. The sudo command allows users to execute commands with elevated privileges, such as root access, without granting full root access. The sudoers file, located in /etc/sudoers, is where system administrators define and control which users can execute specific commands with sudo.
Scenario:
As a regular user, you need to add a new user to the system. Since adding users requires administrative privileges, you would use the sudo command to execute the adduser command with root access.
Example:
$ sudo adduser new_userTo run a command as a user other than root, use the -u option:
$ sudo -u username commandThis gives administrators fine-grained control over who can execute which commands and as which user.
Scenario:
As an administrator, you need to control which users have sudo access and what commands they can execute. The sudoers file is where these configurations are made.
Steps:
- View the file's permissions:
$ ls -l /etc/sudoers -r--r----- 1 root root 755 Jan 18 2018 /etc/sudoers
- Edit the file using
visudofor safety, as it prevents syntax errors.
Sample Configuration:
# Root can execute any command
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Members of the sudo group may execute any command
%sudo ALL=(ALL:ALL) ALLScenario:
You need to check what commands the user durgasoft is allowed to run with sudo.
Steps:
- List commands for the current user:
$ sudo -l
- List commands for a specific user:
$ sudo -l -u durga1
Background:
In a mid-sized organization, a Linux-based server is shared by multiple teams. Each team has specific administrative requirements, but unrestricted root access cannot be granted for security reasons. To ensure proper privilege management, the system administrator must use the sudo command and configure the sudoers file for fine-grained access control.
- Enable Team A to install and update software.
- Allow Team B to manage network configurations.
- Restrict general users from accessing sensitive commands.
- Ensure all actions performed with
sudoare logged for auditing purposes.
First, create user groups for Team A and Team B to streamline privilege assignments.
# Create groups for each team
$ sudo groupadd team_a
$ sudo groupadd team_b
# Add users to respective groups
$ sudo usermod -aG team_a user1
$ sudo usermod -aG team_b user2Use the visudo command to safely edit the /etc/sudoers file and define custom privileges.
$ sudo visudoAdd the following rules:
# Allow Team A to run package management commands
%team_a ALL=(ALL) /usr/bin/apt-get, /usr/bin/yum
# Allow Team B to manage network settings
%team_b ALL=(ALL) /sbin/ifconfig, /sbin/ip
# Prevent all users from editing the sudoers file directly
ALL ALL=(ALL) !/bin/vi /etc/sudoers-
Check if a user from Team A can install software:
$ sudo apt-get install nano
-
Attempt to execute a restricted command:
$ sudo passwd root
Expected Result: Access denied.
-
Verify network configuration access:
$ sudo ifconfig
-
Test a restricted command:
$ sudo apt-get update
Expected Result: Access denied.
To monitor actions performed with sudo, check the system log file.
# View the sudo log entries
$ sudo tail -f /var/log/auth.logSample Output:
Jan 14 10:15:00 server1 sudo: user1 : TTY=pts/0 ; PWD=/home/user1 ; USER=root ; COMMAND=/usr/bin/apt-get install nano
Jan 14 10:20:00 server1 sudo: user2 : TTY=pts/0 ; PWD=/home/user2 ; USER=root ; COMMAND=/sbin/ifconfig
Some users need frequent access to specific commands without typing a password each time.
# Add this rule to the sudoers file for Team B
%team_b ALL=(ALL) NOPASSWD: /sbin/ifconfigAllow Team A access only during business hours (e.g., 9 AM to 6 PM).
# Add this rule to the sudoers file
%team_a ALL=(ALL) ALL :!/(ALL) ALL, !time=9:00-18:00-
Test all configurations:
$ sudo -l
-
Remove a user from a group if privileges are no longer needed:
$ sudo gpasswd -d user1 team_a
-
Backup the
sudoersfile:$ sudo cp /etc/sudoers /etc/sudoers.bak
By leveraging the sudo command and configuring the sudoers file effectively, the administrator achieved:
- Role-based access control for different teams.
- Prevention of unauthorized access to critical commands.
- Enhanced security through detailed auditing and logging of actions.