Giving someone full root access for a task that needs only one privileged command creates avoidable risk. Linux sudo access to specific commands lets you grant a defined action while keeping unrelated administrative powers out of reach. The key is to treat each rule as a carefully scoped permission, not as a shortcut around role design.
This approach follows the principle of least privilege: give users only the access needed to perform their assigned work. The NIST definition of least privilege describes this principle in the context of restricting access rights.
Start with tasks, not job titles
Before writing a rule, describe the task that requires elevated access. Identify the exact command, the system or service it affects, and the account it needs to run as. A role name such as “developer” or “helpdesk” does not, by itself, explain what permissions are appropriate.
- Helpdesk: May need to check the status of a named service, but not change its configuration or restart unrelated services.
- Developers: Usually should deploy through an established release process rather than receive broad server administration rights. If a privileged action is necessary, consider a narrowly designed, administrator-owned wrapper for that action.
- Application operators: May need to restart or reload a particular service. Their access should not automatically extend to other services or to editing service definitions.
Groups make it easier to assign a defined role to its members, but the group should represent a real, limited permission set. Review existing group memberships and sudo rules first. A narrow new rule does not remove broader access a user already has through another group or rule.

Use a sudoers drop-in and narrow the command
On systems configured to include it, a file in /etc/sudoers.d keeps a role’s rule separate from the main sudoers file. Check that the main configuration includes this directory before relying on a drop-in. The sudoers manual documents the rule format and command matching behavior.
For example, a helpdesk role might need to check one service, while an application-operator role can restart or reload that same service. Replace the sample groups, hosts, service, and command path with values that match your environment. The host names and service below are illustrative.
Example rule: Host_Alias APP_HOSTS = app01.example.internal, app02.example.internal
Example rule: Cmnd_Alias HELP_DESK = /usr/bin/systemctl status inventory-api.service
Example rule: Cmnd_Alias APP_OPS = /usr/bin/systemctl restart inventory-api.service, /usr/bin/systemctl reload inventory-api.service
Example rule: %helpdesk APP_HOSTS = (root) HELP_DESK
Example rule: %app-operators APP_HOSTS = (root) APP_OPS
Use the actual absolute path on each system; it may differ from the example. Confirm it with command -v systemctl. Keep the permitted arguments specific. In sudoers, omitting arguments from a command rule can allow the command to be run with arbitrary arguments, so avoid treating a command path alone as a complete restriction.
Do not add broad wildcards just to make a rule convenient. Argument patterns can match more than intended, especially when they span spaces. If a task requires variable input, consider whether a tightly controlled wrapper is safer than a wide command pattern. Avoid NOPASSWD unless there is a documented operational reason and the associated risk has been reviewed.
Edit and validate rules safely
Use visudo to edit sudoers files. It checks syntax before saving, helping prevent a malformed rule from disrupting sudo access. To edit a drop-in, use:
Command: visudo -f /etc/sudoers.d/app-operators
After making a change, validate the configuration with visudo -c. Then test the result as the intended user: confirm the required command is permitted and an unrelated privileged command is denied. Avoid testing a potentially disruptive action on a production service without following the organization’s change process.
A user can inspect their own effective permissions with sudo -l. An administrator with appropriate access can inspect another account with sudo -l -U username. These checks help reveal permissions granted through multiple rules, rather than just the contents of one drop-in file. The sudo manual describes the listing option.
Check for escape routes and indirect access
An allow-list is not automatically safe just because it names a specific executable. Some programs can launch a shell, run other commands, or modify files that affect privileged behavior. Editors, pagers, interpreters, and tools with extensible features deserve particular scrutiny. Avoid granting access to a general shell or an interactive tool when the task can be expressed as a specific command.
Also check who controls the command’s inputs. A user who can edit a service unit, its startup script, or a privileged configuration file may be able to turn a permitted service action into a path to greater access. Review the ownership and write permissions of the service definition, executable, scripts, and relevant configuration. Keep those files controlled by trusted administrators when a sudo rule can trigger them with elevated privileges.
For broader access-control planning, see our guidance on strengthening Linux security. If the permission depends on consistent server settings, our article on managing Linux server configurations can help frame that operational work.
Document and review each permission
A sudo rule should have a clear reason to exist and someone accountable for it. Keep a record alongside your access or change-management documentation with details such as:
- The user group and the exact commands and arguments permitted.
- The hosts and run-as identity covered by the rule.
- The business purpose and the owner responsible for approving the access.
- The change record or approval that authorized it, plus when it was last reviewed.
- How the rule will be removed when the task ends or a person’s responsibilities change.
Review permissions as part of access changes and at a regular interval appropriate to your organization. Confirm that group membership is still correct, the task still requires elevated access, and the command remains safe with current scripts and configuration. Remove permissions that no longer have an owner or a valid purpose. For related access-removal steps during staff changes, see our guide to revoking SSH access during offboarding.
Frequently asked questions
Should sudo rules go in /etc/sudoers or /etc/sudoers.d?
Use a drop-in when the system’s main sudoers configuration includes that directory. It keeps a role’s rules separate and easier to review. In either location, edit with visudo and validate the complete configuration.
Can sudo restrict command options and arguments?
Yes. Specify the command with the arguments the task requires, and avoid broad patterns. Be especially cautious with wildcards because they can match more input than intended.
How can I see what sudo permissions a user has?
The user can run sudo -l to list their permissions. An administrator can use sudo -l -U username to inspect another account, subject to the required access.
Which allowed commands can provide a route to a root shell?
Interactive editors, pagers, interpreters, shells, and some flexible utilities may be able to launch other commands or modify sensitive files. Assess the behavior of the specific program and its inputs before allowing it through sudo.
Make sudo access deliberate
Command-specific sudo rules work best when they reflect a real task, limit both command and host scope, and are tested for both allowed and denied actions. Document an owner and purpose, then review and remove access when it is no longer needed. If you need help assessing Linux permissions or designing a maintainable access model, contact Linux Consulting Services to discuss your environment.