/etc/sudoers file using visudo. Add a line specifying the username, host, and full path to the executable. This method grants precise privileges while maintaining system security by avoiding global sudo access.
Understanding the Sudoers Configuration
The /etc/sudoers file is the configuration file that defines who can use the sudo command and what commands they are allowed to execute. It is critical to understand that this file controls privilege escalation on Linux systems. Modifying it incorrectly can lock you out of administrative functions or create significant security vulnerabilities. The standard and safest way to edit this file is by using the visudo command. This utility performs a syntax check before saving changes, preventing broken configurations that could render the system unusable.
By default, members of the sudo group have full root access. However, in many scenarios, such as running a web server script or managing services, you only need to grant permission for a single specific command. Granting NOPASSWD access to a single binary is far more secure than giving a user general root capabilities. This approach follows the principle of least privilege, ensuring that even if an attacker compromises the user account, their ability to harm the system remains limited to that specific action.
Identifying the Command Path
Before editing the sudoers file, you must know the absolute path to the command you wish to authorize. Sudo requires the full path because it does not rely on the user's $PATH environment variable for security reasons. To find the path, use the which command followed by the program name. For example, to find the location of the systemctl command, you would run:
which systemctl
This typically returns /usr/bin/systemctl. It is important to note that some commands might be aliases or shell built-ins. Sudo can only execute external binaries. If the command is a shell function or alias, you must either call the underlying binary directly or wrap the execution in a script. For service management, /usr/bin/systemctl is the standard target. Always verify the path to ensure accuracy, as typos here will result in permission denied errors.
Editing the Sudoers File
Open the sudoers file with elevated privileges using the visudo command. This ensures that the file is locked against concurrent edits and checked for syntax errors upon exit:
sudo visudo
Scroll to the bottom of the file or locate the section dedicated to user specifications. The syntax for a custom rule is as follows:
username ALL=(ALL:ALL) NOPASSWD: /path/to/command
Let us break down these components. The first field is the username, which should match the login name of the user requiring access. The second field, ALL, specifies from which hosts the user can run the command; typically, this is left as ALL for local servers. The third part, (ALL:ALL), indicates that the command runs as any user and any group, which is standard for most administrative tasks. The keyword NOPASSWD tells sudo not to prompt for a password. Finally, the absolute path to the command must be specified exactly.
For our example, allowing a user named deploy to start services without a password, the line would look like this:
deploy ALL=(ALL:ALL) NOPASSWD: /usr/bin/systemctl
Save the file and exit. Visudo will automatically validate the syntax. If there is an error, it will warn you and ask if you want to abort the changes. Do not ignore these warnings. Correct any syntax issues before proceeding.
Refining Command Arguments
In some cases, you may want to restrict the user to only start services, but not stop them or restart them. You can do this by appending specific arguments to the command path. For instance, to allow only starting services:
deploy ALL=(ALL:ALL) NOPASSWD: /usr/bin/systemctl start *
Note that wildcards are allowed in argument lists, but they must be quoted or escaped properly depending on the shell interpretation. However, using wildcards can sometimes introduce security risks if not carefully managed. A safer approach is often to list each allowed subcommand explicitly, such as /usr/bin/systemctl start * /usr/bin/systemctl status *. This provides granular control over what the user can achieve with the tool.
Testing the Configuration
After saving the changes, test the configuration by switching to the target user or using su. Attempt to run the command using sudo. Since NOPASSWD is set, you should not be prompted for a password. If you receive a "permission denied" error, double-check the username spelling and the command path. Also, ensure that the user is not being overridden by a more general rule higher up in the file. Sudo reads rules in order, and the last matching rule usually takes precedence, but specific user definitions generally override group memberships.
For additional context on securing your Ubuntu server, consider reviewing Securing an Ubuntu Server for Development Purposes. Properly configured sudo rules are just one layer of defense in a comprehensive security strategy.
Frequently Asked Questions
Can I use wildcards for the command path?
No, you cannot use wildcards in the command path itself. The path must be an exact match to the binary location. Wildcards are only permitted in the arguments section after the path. Using a wildcard in the path will cause the rule to fail or behave unpredictably.
What happens if I make a syntax error in sudoers?
If you use visudo, the editor will detect syntax errors before saving. If you bypass visudo and edit the file directly, you may lock yourself out of sudo access. In such cases, you must boot into recovery mode or use a live USB to fix the file manually.
Does NOPASSWD affect other commands?
No, the NOPASSWD tag applies only to the specific command listed in that line. The user will still need to enter their password for any other sudo commands not explicitly listed with NOPASSWD. This keeps the rest of the system secure.
How do I remove a sudoers rule?
Edit the file again using visudo and delete the line corresponding to the user and command. Save the file and exit. The changes take effect immediately without needing a reboot. Always verify the change by testing the command as the user.
Is it safe to allow systemctl without arguments?
Allowing /usr/bin/systemctl without argument restrictions gives the user full control over all systemd services. They can stop critical services, enable malware persistence, or disrupt system functionality. It is highly recommended to restrict arguments to only the necessary actions like start or status.