In Ubuntu, how to give a user sudo but limit sudo to a few commands he really need.
- User
In Ubuntu, how to give a user sudo but limit sudo to a few commands he really need.
Assistant
You can limit a user's sudo access to specific commands by editing the sudoers file. Here's how to do it safely:
Edit the sudoers file
Use
visudoto edit the sudoers configuration:sudo visudoAdd a rule for the specific user
Add a line like this at the end of the file:
username ALL=(ALL) /path/to/command1, /path/to/command2, /path/to/command3Example scenarios
Allow only system service management:
john ALL=(ALL) /bin/systemctl start, /bin/systemctl stop, /bin/systemctl restart, /bin/systemctl statusAllow file operations in specific directories:
jane ALL=(ALL) /bin/cp /home/shared/*, /bin/mv /home/shared/*, /bin/rm /home/shared/*Allow package management:
bob ALL=(ALL) /usr/bin/apt update, /usr/bin/apt upgrade, /usr/bin/apt installImportant considerations
- Use full paths to commands (find them with
which command) - Be careful with wildcards - they can be security risks
- Test the configuration after saving
- The user won't need to enter a password if you add
NOPASSWD:before the command list
Alternative: Create a custom group
You can also create a group with specific sudo privileges:
sudo groupadd limited-sudo sudo usermod -a -G limited-sudo usernameThen in visudo:
%limited-sudo ALL=(ALL) /specified/commands/hereThis approach makes it easier to manage multiple users with the same restricted permissions.
- Use full paths to commands (find them with
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.