Least-privilege sudo: restrict service restarts and test the boundary
When several sites serving an international business share a dedicated server, an on-call account may need nothing more than the ability to restart one background service. Yet that account is often added directly to an administrator group. Access granted to troubleshoot a single issue then becomes permanent, full control over the system. A safer approach is to list the actions that are actually needed, then prove that everything else is denied—not merely check that “the restart worked.”
This walkthrough uses Debian 12 and a separate test service. It does not restart production services or change network settings. After paying for a Tbps dedicated server, you can view its credentials in the control panel and connect to Linux over SSH. The account and service names below are chosen for this exercise; they are not provisioning defaults.
Define the permission boundary first
Prepare an existing account named svcops that can already log in successfully, along with a separate administrator session that retains full privileges. The delegated account must be able to authenticate with sudo according to your organization’s policy. This walkthrough does not configure passwordless access or change SSH authentication.
The administrator first checks the current setup:
id svcops
command -v sudo visudo systemctl
sudo -l -U svcops
Confirm that the tools come from trusted system packages and that the actual path to systemctl is /usr/bin/systemctl. If the path differs, verify the file and adjust the rule accordingly rather than copying it unchanged.
If the account already has full privileges through an administrator group, other sudoers files, or centralized authorization, adding a narrow rule will not remove those existing privileges. Review the current grants first, then have an administrator adjust them. Do not experiment with revoking privileges on your only administrator account.
This exercise permits just two actions: restarting permission-probe.service and checking whether it is active. It does not permit stopping the service, editing its unit, reloading the service manager, or launching a command interpreter.
Sudo permissions are additive. A narrow rule cannot cancel out a broader rule, and correct command matching is no substitute for checking file write permissions.
Create a separate test service
Using a trusted editor, the administrator creates /etc/systemd/system/permission-probe.service with the following contents:
[Unit]
Description=Privilege boundary probe
[Service]
Type=oneshot
DynamicUser=yes
ExecStart=/usr/bin/true
RemainAfterExit=yes
This service only runs a program that exits successfully. It does not access production data or listen on the network. RemainAfterExit=yes keeps the service active after the program finishes, making its state easy to check. This is a permission test; it does not demonstrate that a real application has a working health check.
The administrator runs:
sudo chown root:root /etc/systemd/system/permission-probe.service
sudo chmod 0644 /etc/systemd/system/permission-probe.service
sudo systemd-analyze verify /etc/systemd/system/permission-probe.service
sudo systemctl daemon-reload
sudo systemctl start permission-probe.service
sudo systemctl is-active permission-probe.service
If verification or startup fails, investigate the error before granting access. Do not run enable here: the test service should not be added to system startup. Subsequent restarts of this service will not touch any real background processes.
Write an exact authorization rule
The administrator edits the rule with sudo visudo -f /etc/sudoers.d/svcops-probe. The filename must not contain a dot or a backup suffix, which could cause it to be skipped when the include directory is read.
svcops ALL=(root) PASSWD: /usr/bin/systemctl restart permission-probe.service, /usr/bin/systemctl is-active permission-probe.service
This matches the full command path and fixed arguments, without wildcards. Do not broaden it to allow all of systemctl, or let the caller supply arbitrary service names. Treat service aliases, different argument ordering, and additional options as new authorization requirements—not as reasons to keep expanding the rule to accommodate calling habits.
After saving, check the file permissions and the complete configuration:
sudo chown root:root /etc/sudoers.d/svcops-probe
sudo chmod 0440 /etc/sudoers.d/svcops-probe
sudo visudo -cf /etc/sudoers
sudo -l -U svcops
Checking the complete configuration can reveal syntax errors in other included files. Keep the administrator session open, and test with the delegated account only after the checks pass. The ALL in this rule occupies the host-matching field; it does not mean that all commands are allowed.
This example does not authorize systemctl status: interactive output may open a pager, expanding the scope of the security review. If you only need a state result, is-active is sufficient. An administrator should read detailed logs.
Test both allowed and denied operations
Open a separate login session as svcops, clear any cached authentication, and run the exact commands:
sudo -k
sudo /usr/bin/systemctl restart permission-probe.service
sudo /usr/bin/systemctl is-active permission-probe.service
The restart should succeed, and the reported state should be active. Record exit statuses rather than capturing just one line of output. When the service is not running, is-active returns a nonzero status; that is a service-state result, not an authorization denial from sudo.
Next, check the policy without performing the target actions. The -l option below queries permissions; it does not actually stop the service or open a command interpreter:
sudo -l /usr/bin/systemctl stop permission-probe.service
sudo -l /usr/bin/systemctl restart permission-probe.service --no-block
sudo -l /usr/bin/systemctl daemon-reload
sudo -l /bin/sh
All four checks should report that the command is not permitted. The second deliberately adds an argument to verify that the rule has not been broadened to accept arbitrary arguments. If any check is allowed, return to the administrator session and review every source of authorization. Do not simply modify the file created for this exercise and declare the test passed.
Inspect write access throughout the execution chain
Before applying this approach to a real service, the administrator must also inspect its definition:
sudo systemctl cat permission-probe.service
sudo systemctl show permission-probe.service \
-p FragmentPath -p DropInPaths -p User -p ExecStart
namei -l /etc/systemd/system/permission-probe.service
For a real service, continue tracing startup scripts, files loaded by interpreters, environment files, and related configuration. If the delegated account can replace a program that a privileged service will execute, even restart-only access may indirectly allow privilege escalation. A writable parent directory can also allow a file to be replaced, so checking only the file’s own permission bits is insufficient. Where ACLs are present, check the effective ACL permissions as well.
Do not try to fix this by recursively applying uniform permissions to directories. Check each path individually to establish what read and write access the application actually needs.
Keep evidence and revoke access
The acceptance record should include, at minimum, the account’s group memberships, its complete sudo permission list, the rule contents, syntax-check results, allowed and denied test results, and the findings from the service’s file-write-chain review. Record the specific operations and exit statuses, but do not record passwords or complete sensitive configurations.
The administrator can look for sudo invocation records in the system journal:
sudo journalctl _COMM=sudo --since "15 minutes ago" --no-pager
The log destination depends on the host’s logging configuration. If the query returns nothing, check where records are actually sent; do not conclude that no actions occurred. Sudo invocation records are also not the same as a full session recording. The two must not be treated as interchangeable.
After the exercise, the administrator removes the authorization and checks again:
sudo rm /etc/sudoers.d/svcops-probe
sudo visudo -cf /etc/sudoers
sudo -l -U svcops
sudo systemctl stop permission-probe.service
sudo rm /etc/systemd/system/permission-probe.service
sudo systemctl daemon-reload
The delegated account should query permissions for the original commands again; they should no longer be allowed. Revoking access does not terminate processes that have already started. If this involves an actual security incident, inspect running tasks separately. The final acceptance criterion is not “the account can restart the service.” It is that permitted actions work, additional actions are denied, the delegated account cannot modify the execution chain, and the authorization can be revoked.
Frequently asked questions
Does allowing only one service restart eliminate escalation risks?
No. If the delegated account can modify the unit, executable, scripts, or configuration read by a privileged process, restarting it may execute additional code. Check parent-directory permissions as well.
Why does the example omit systemctl status?
Interactive status output may launch a pager and expand the authorization surface. An exact systemctl is-active rule is enough to check the named service; an administrator handles detailed diagnostics.
AMD EPYC 7543P Dedicated Server
From $463/mo