How do I configure sudo privileges for specific users on Linux systems?

You configure sudo privileges for specific users by editing the sudoers file or adding a custom file under /etc/sudoers.d. Here’s how:


Step 1: Edit the sudoers File

  1. Open a terminal and log in as a user with administrative privileges.
  2. Use visudo to safely edit the sudoers file. This command checks syntax and prevents lockouts:
    sudo visudo
  3. Find the section where user or group privileges are defined. You’ll see lines like:
    root ALL=(ALL:ALL) ALL
  4. Add a new entry for the user. To grant user1 full sudo privileges:
    user1 ALL=(ALL:ALL) ALL
  5. ALL=(ALL:ALL) ALL means the user can run any command as any user or group on any host.

Step 2: Grant Limited Privileges (Optional)

To restrict a user to specific commands, add a rule like this:

user1 ALL=(ALL) NOPASSWD: /bin/systemctl restart apache2.service

NOPASSWD lets the user run that command without a password. Replace the command path with the full path of the executable you want to allow.


Step 3: Use the /etc/sudoers.d Directory

Instead of editing the main sudoers file, create a custom file for the user:

  1. Create the file with visudo (recommended) or your preferred editor:
    sudo visudo -f /etc/sudoers.d/user1
  2. Add the necessary rules. For full access:
    user1 ALL=(ALL:ALL) ALL
  3. Save and exit. The file is automatically included.

Step 4: Test the Configuration

  1. Switch to the configured user:
    su - user1
  2. Run a command with sudo:
    sudo whoami
  3. If configured correctly, the command executes and returns root.

Important Notes:

  • Always Use visudo: Editing sudoers or sudoers.d files without visudo risks syntax errors that can lock you out.
  • Minimal Privileges: Grant only what’s necessary. Avoid full sudo access unless required.
  • Audit Regularly: Review sudo configurations periodically for security and compliance.
  • Log Monitoring: Check /var/log/secure (RHEL/CentOS) or /var/log/auth.log (Debian/Ubuntu) for sudo usage.

Liked this briefing? Get it in your inbox

Every morning: the stories that make tech leaders act, with official clips and primary sources. Free, 5-minute read.

Free. One click to unsubscribe. We never share your email. Privacy

How do I configure sudo privileges for specific users on Linux systems?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top