Can sudo be configured for specific commands?

QuestionsQuestions8 SkillsProUser Account ManagementOct, 17 2025
0134

Yes, sudo can be configured to allow specific users or groups to execute only certain commands with elevated privileges. This is done by editing the /etc/sudoers file, which defines the permissions for sudo.

Here’s how you can configure sudo for specific commands:

  1. Edit the sudoers file: Use the visudo command to safely edit the /etc/sudoers file. This command checks for syntax errors before saving changes.

    sudo visudo
  2. Define User Permissions: You can specify which commands a user or group can run. The syntax is as follows:

    username ALL=(ALL) /path/to/command

    For example, to allow the user joker to run the systemctl restart apache2 command:

    joker ALL=(ALL) /bin/systemctl restart /usr/bin/apache2
  3. Allow Group Permissions: You can also allow a group to run specific commands. For example, to allow all users in the admin group to run the shutdown command:

    %admin ALL=(ALL) /sbin/shutdown
  4. Multiple Commands: You can specify multiple commands by separating them with commas:

    joker ALL=(ALL) /bin/systemctl restart /usr/bin/apache2, /usr/bin/apt-get update
  5. No Password Prompt: If you want to allow a user to run a command without being prompted for a password, you can add NOPASSWD: before the command:

    joker ALL=(ALL) NOPASSWD: /bin/systemctl restart /usr/bin/apache2

By configuring sudo in this way, you can enhance security by limiting the commands that users can execute with elevated privileges, reducing the risk of accidental or malicious changes to the system.

0 Comments

no data
Be the first to share your comment!