Command - Cmnd_Alias

What is Cmnd_Alias?

Cmnd_Alias (Command Alias) is a feature in /etc/sudoers (and files in /etc/sudoers.d/) that lets you group multiple commands under a single, easy-to-read name.

Instead of repeating long command paths many times, you define the group once and then reference the alias name in your user permission rules. This makes the sudoers configuration:

It is one of four main alias types in sudoers:

Basic Syntax

Cmnd_Alias  ALIAS_NAME = /full/path/to/command1, \
                       /full/path/to/command2 arg1 arg2, \
                       /full/path/to/command3

Rules:

Example for Your api.service (Recommended Version)

Create or edit the file with sudo visudo -f /etc/sudoers.d/deploy-api:

# Command alias for managing the api.service safely
Cmnd_Alias API_SERVICE_CMDS = /usr/bin/systemctl start api.service, \
                             /usr/bin/systemctl stop api.service, \
                             /usr/bin/systemctl restart api.service, \
                             /usr/bin/systemctl status api.service

# Grant the deploy user passwordless access to only these commands
deployuser ALL=(ALL) NOPASSWD: API_SERVICE_CMDS

This is cleaner than listing the four commands directly on the user line.

More Flexible Example (Allow Any Action on the Specific Service)

If you want the deploy user to run any systemctl action on api.service (start, stop, restart, status, reload, enable, etc.):

Cmnd_Alias API_SERVICE_CMDS = /usr/bin/systemctl * api.service

deployuser ALL=(ALL) NOPASSWD: API_SERVICE_CMDS

The * acts as a wildcard for arguments. Be careful — this is slightly broader but still restricted to only the api.service unit.

Even Better: Using Wildcards Safely

You can also allow common patterns:

Cmnd_Alias SYSTEMD_SERVICE = /usr/bin/systemctl start api.service, \
                            /usr/bin/systemctl stop api.service, \
                            /usr/bin/systemctl restart api.service, \
                            /usr/bin/systemctl reload api.service, \
                            /usr/bin/systemctl status api.service

# Or more permissive but still limited:
Cmnd_Alias SYSTEMD_SERVICE = /usr/bin/systemctl * api.service

How to Use It in Your Setup

sudo visudo -f /etc/sudoers.d/10-deploy-api
sudo chmod 0440 /etc/sudoers.d/10-deploy-api
sudo -u deployuser sudo -l
  1. You should see the alias listed.

Then update your PowerShell script to use deployuser (instead of root).

Why This Is Better Than the Original Line

Your original line:

deployuser ALL=(ALL) NOPASSWD: /usr/bin/systemctl stop api.service, /usr/bin/systemctl restart api.service, /usr/bin/systemctl status api.service

Works fine, but using Cmnd_Alias is preferred when:


Revision #1
Created 6 April 2026 15:33:07 by Steve Ling
Updated 6 April 2026 15:37:04 by Steve Ling