---
title: "Token command matching"
description: "Check how token command rules match submitted lines, wildcards, accounts, and sudo. Use refusal codes and audit details to identify why a command was denied."
---

> Full documentation index: https://docs.alpacax.com/llms.txt

# Token command matching

Use these rules when checking whether a token allows a submitted command. [Configure commands for a service token](/use/iam/applications/service-tokens/command-patterns/) before running an automated job.

## How a command rule is matched

Allowed commands are configured per token. See [an existing personal API token](/use/settings/api-tokens/#acl-rules) and [Service tokens](/use/iam/applications/service-tokens/). A rule must allow the submitted command line, including any `sudo` prefix; permission to elevate on the server is checked separately. These permissions are configured in different places and follow opposite conventions, which is the most common reason an automated deployment stalls here.

- The rule is compared against **the command line you submit**, from beginning to end. A partial match is not enough.
- `*` is the only wildcard. A `*` standing alone at the end of a rule covers all remaining arguments, and a bare `*` covers a command line subject to the first-word limits below. Any other `*` stays inside one word: it does not cover spaces or the characters `$`, `` ` ``, `*`, `?`, `[`, and `{`, so `deploy-*` allows `deploy-web` but not `deploy-web --force`. A rule whose `*` is quoted or escaped matches nothing. If the first word of a rule contains `*`, including a standalone `*`, it never covers a command line that begins with an environment assignment (`NAME=value …`) or a shell prefix (`time`, `!`, `if`, `then`, `else`, `elif`, `while`, `until`, `do`, `{`). Such commands need a rule that spells the prefix literally.
- A `*` that follows literal text in a word never matches a parent-directory segment (`..` followed by `/`, `:`, `,`, whitespace, or the end of the word), so `rm -rf /tmp/build-*` matches `rm -rf /tmp/build-x/sub` but not `rm -rf /tmp/build-x/../../etc`. A `*` that starts a word, as in `cp * /dst`, can match parent-directory segments, and a `..` written in the rule itself, as in `git log v1..*`, is literal.
- If the command line chains several commands with `&&`, `||`, `|`, `;`, `&`, or a newline, each part is matched separately and **every** part must be allowed by some rule.
- A command line with a redirection is rejected, whatever the rules say, a bare `*` included. That covers `>`, `>>`, `<`, `>|`, and forms such as `2>&1`, `2>/dev/null`, and `<<`. A rule cannot say which files a command may read or write. Drop the redirection from the submitted line, or move it into a script on the server that a rule allows by name. Process substitution (`<(...)`) is not a redirection and is checked like any other command.
- A `#` that starts a word begins a comment, as in the shell: the rest of that line is ignored and cannot satisfy a rule, and the next line is matched again. Inside `$(...)` or a subshell `(...)`, such a `#` is not treated as a comment; the whole line is rejected.
- A line whose quoting different shells read differently is rejected rather than guessed at, for example an apostrophe inside `"${x:-...}"`.
- Quotes are removed and extra spacing is collapsed before the comparison, so `echo "hello world"` is matched as `echo hello world`, and `echo  hello` (two spaces) is matched as `echo hello`.
- If nothing matches, nothing runs on the server. On Alpacon Cloud, the command is accepted and then rejected before it runs with `command_acl_denied`. On a self-hosted deployment, the request is refused with `api_token_acl_not_allowed`. See [Denial messages](#denial-messages).

### A trailing wildcard needs an argument

A rule ending in ` *` requires at least one argument, because the space before the wildcard is part of the rule.

| Rule | Allows | Does not allow |
| --- | --- | --- |
| `docker` | `docker` | `docker ps` |
| `docker *` | `docker ps`, `docker compose up -d` | `docker` (no arguments) |
| `systemctl status *` | `systemctl status nginx` | `systemctl restart nginx` |

To allow both the bare command and its arguments, add two rules: `docker` and `docker *`.

### Username and group on a rule

Each rule also pins which system user and group may run it.

| Token type | Username left empty | Username `*` | Username `deploy` |
| --- | --- | --- | --- |
| API access token | Only the token owner | Any user | Only `deploy` |
| Service token | **Any user** | Any user | Only `deploy` |

A service token belongs to an application, not to a person, so there is no owner to fall back on. Leaving the username empty on a service token rule allows **any** system user. Set it explicitly when the rule should be pinned to one account.

## Command rules and sudo policies are opposites

Alpacon has a second place where command patterns are written: a **sudo policy**, which decides who may elevate on which servers. The two look alike, but they are checked at different moments against different text, so the same command needs the opposite pattern in each.

| | Allowed command rule | Sudo policy |
| --- | --- | --- |
| Applies to | one token | Alpacon users on selected servers |
| Checked | before the command is sent to the server | when the server asks Alpacon to approve the elevation |
| Compared against | the line as you wrote it, `sudo` included | the command that runs as root, with `sudo` already removed |
| Pattern for `sudo systemctl restart nginx` | `sudo systemctl restart nginx` | `systemctl restart nginx` |
| A leading `sudo` in the pattern | required | rejected when you save the policy |
| On no match | command refused, nothing runs | elevation refused, `sudo` exits with an error |

Saving a sudo policy whose pattern starts with `sudo` fails with `sudo_policy_pattern_has_sudo_prefix`. That is deliberate: such a pattern could never match anything, and rejecting it as you type is better than a silent denial weeks later.

See [sudo with MFA](/use/work-sessions/terminal/sudo-with-mfa/) for how sudo policies are created and used.

## Denial messages

When elevation is refused, the terminal prints `Alpacon denied this sudo command (CODE).` and `sudo` exits with a non-zero status.

| Code | Meaning | What to do |
| --- | --- | --- |
| `SUDO_RISK_DENIED` | The command was assessed Critical, or above the level your workspace allows, and no person approved it. | Have a person approve the command itself: run it in a work session, where a held command's approval covers its `sudo`. Or split it into narrower commands. |
| `SUDO_COMMAND_NOT_AUTHORIZED` | This sudo cannot be tied to a person on this deployment. | Run it as a signed-in person's authorized command. This is the default self-hosted outcome for service-token sudo. |
| `SUDO_NO_WORKSESSION_POLICY` | No sudo policy matched this command. | Run it in a work session whose sudo patterns cover the command; adding a pattern may need approval. |
| `SUDO_POLICY_MFA_REQUIRED` | A sudo policy matched but requires MFA, which a non-interactive command cannot provide. | Run the command from an interactive terminal where you can complete MFA. |
| `WORK_SESSION_SCOPE_NOT_ALLOWED` | The work session does not include the sudo scope. | Request the sudo scope when creating the session. |

A command the token's allowed-command rules refuse is a different failure: nothing reaches the server, and how the refusal shows up depends on the deployment.

On Alpacon Cloud, the API accepts the command, and it is rejected before it runs with `command_acl_denied`. Open the command in [Commands](/use/audit/events/commands/#understanding-a-rejection) and check its **Rejection reason**.

On a self-hosted deployment, the API responds with `api_token_acl_not_allowed` before the command is sent. The response is the same whatever the cause; the refused command is recorded in [Commands](/use/audit/events/commands/), and its **Denial reason** says which cause applies:

- `Command contains a redirection (>, >>, <); ...`: no rule can allow this line. Remove the redirection, or move it into a script on the server.
- `Command quoting or a comment cannot be read the same way by every shell, ...` or `Command contains shell quoting that could obfuscate substitutions.`: no rule can allow this line either. Simplify the quoting, avoid `$'...'` and `$"..."`, or move the comment out of `$(...)` or `(...)`.
- Any other reason means no allowed-command rule matched the line. Go back to [Write the rule exactly as you run it](/use/iam/applications/service-tokens/command-patterns/#write-the-rule-exactly-as-you-run-it).

The same `api_token_acl_not_allowed` code can describe a server, command, or file access refusal. Check the reported gate and the command's denial detail before changing a rule. A command refusal calls for a matching allowed-command entry; a server or file refusal calls for the corresponding access rule.

## Related docs

- [Allow commands with a service token](/use/iam/applications/service-tokens/command-patterns/)
- [Manage personal API tokens](/use/settings/api-tokens/)
- [alpacon token acl command](/reference/cli/token/acl-command/)
- [Access-control list API](/reference/api/acl/)
- [Command history](/use/audit/events/commands/)

Source: https://docs.alpacax.com/reference/tokens/command-patterns/
