skip to content

In OpenSSH's authorized_keys file, how do the command=, no-pty and expiry-time= options limit what one key can do on a shell host?

level: middleimportance: should knowfreq 26%

answer

  1. options field before the key type
  2. the client's command is ignored
  3. SSH_ORIGINAL_COMMAND
  4. forwarding is a separate switch
  5. a date after which it is refused

basics

~20 s

Options written before a key in authorized_keys apply only to logins made with that key: command= runs a fixed command instead of whatever the client asked for, no-pty refuses a terminal, and expiry-time= makes sshd refuse the key after a date.

solid answer

~50 s

Each `authorized_keys` line may begin with comma-separated options that sshd applies only to logins made with that key. `command="..."` replaces whatever the client asked to run, whether a shell, a command or a subsystem, with a fixed command; the original request survives only in the `SSH_ORIGINAL_COMMAND` environment variable, which a wrapper script can inspect. `no-pty` makes any terminal request fail, which also keeps the channel 8-bit clean for a data stream. `expiry-time="YYYYMMDD"`, or a full time with `Z` for UTC, makes sshd stop accepting the key afterwards, so a temporary grant ends without anyone editing the file. These options restrict what the key may do once it has proved possession; they add no proof of identity. And `command=` alone does not stop TCP or X11 forwarding: add `no-port-forwarding` and its siblings, or the `restrict` bundle, for that.

go deeper

for a junior

Recall that options go in front of the key on its authorized_keys line and only affect logins made with that key: a forced command, no terminal, an end date.

for a middle

Explain the mechanics: the forced command replaces shell, exec and subsystem requests, SSH_ORIGINAL_COMMAND carries what was asked, and forwarding stays open unless separately forbidden.

for a senior

Show how you would scope an automation key in production: one forced command or a strict wrapper, no-pty, forwarding switched off, an expiry, and awareness of how ForceCommand and certificate force-command override it.

for a principal

Weigh per-key options against central controls: they are precise but live in files users can edit, so decide where policy belongs as the number of keys and hosts grows.

## Where the options live In OpenSSH, each line of `~/.ssh/authorized_keys` (or whatever `AuthorizedKeysFile` names) has the shape **options, key type, base64 key, comment**, and the options field is optional. Options are comma-separated, with no spaces except inside double quotes, and their keywords are case-insensitive. sshd(8) applies them **only when that particular key authenticates the login**, so one account can hold an unrestricted key for its owner and a tightly limited key for an automated job. Options do not change what the publickey method proves. sshd first verifies the signature as RFC 4252 requires; the options then decide what the authenticated session may do. A typical case on a shell host: a monitoring server needs to trigger one snapshot script on `db1.internal.example` as `deploy`, and nothing else. The key it uses gets a line such as `command="/usr/local/bin/snapshot",no-pty,expiry-time="20261231Z" ssh-ed25519 AAAA... monitor`, while the account's other keys stay as they are. ## command=: one remote action per key 1. The client authenticates with the key and asks for a shell, an exec command or a subsystem. 2. sshd ignores what was asked and runs the command from the option instead. The man page states it applies to shell, command **and subsystem** execution, so an `sftp` request on that key runs the forced command too. 3. The client's original request is placed in **`SSH_ORIGINAL_COMMAND`**. A wrapper script can read it and allow a short list of verbs; the wrapper then becomes the security boundary and must validate strictly. 4. The command runs on a pty only if the client requested one. `command=` is the usual way to let automation trigger exactly one action without being able to open a shell. ## no-pty `no-pty` makes any pseudo-terminal request fail. An interactive shell becomes impractical, and the channel stays **8-bit clean**: the man page recommends not requesting a pty, or setting `no-pty`, when a forced command streams binary data, because a terminal would translate some bytes. ## expiry-time= `expiry-time="timespec"` gives a time after which the key is **not accepted**. The format is `YYYYMMDD` or `YYYYMMDDHHMM[SS]`, read in the system time zone unless suffixed with `Z` for UTC. It suits a contractor's or an incident responder's temporary key: the grant ends on its own even if nobody remembers to delete the line. Because the check happens when the key is offered, it stops new logins after the time; it does not reach into sessions that are already open. ## What these options do not do - **They do not block forwarding.** The man page warns that with `command=` the client may still request TCP and X11 forwarding unless they are explicitly prohibited, e.g. with `no-port-forwarding`, `no-X11-forwarding`, `no-agent-forwarding`, or the `restrict` keyword that turns all of them off along with pty allocation. - **They do not add a factor.** A restricted key is still one key; demanding a second method is the job of `AuthenticationMethods`. - **They cannot be bypassed by the client.** Passing another command on the `ssh` command line, or in a client configuration option, changes only `SSH_ORIGINAL_COMMAND`. - **They live with the key.** Anyone who can edit the account's `authorized_keys` can remove them, which is why central policy usually sits in server configuration or in certificates instead. Restricting the forwarding destinations a key may open or listen on belongs to the material on SSH port forwarding; limiting a file-transfer partner's key to its source addresses belongs to the material on SFTP security. ## Precedence with other forced commands | Source of the command | Where it is set | How it combines | |---|---|---| | `command=` | one line of `authorized_keys` | runs for logins with that key only | | `ForceCommand` | sshd_config, often inside a `Match` block | may supersede the key's `command=` | | `force-command` | a critical option inside a user certificate | if the key line also has `command=`, the certificate is accepted only when both commands are identical | The certificate rule is deliberately strict: a mismatch refuses the login rather than picking one of the two commands. ## Reading a line back When reviewing a key line, read it left to right: which options apply, whether a forced command is set and what it does with `SSH_ORIGINAL_COMMAND`, whether forwarding is still open, and when the key stops being accepted. Everything else on the line is the key itself, and the comment field is not used for anything.

  • With OpenSSH, a key line has command= set and the client asks for the sftp subsystem. What runs?
    The forced command. sshd(8) says command= applies to shell, command or subsystem execution, so the SFTP server does not start and the client sees a protocol error. A wrapper script could read SSH_ORIGINAL_COMMAND and decide what to allow, but then that script is the security boundary and must validate strictly.
  • How does an authorized_keys command= interact with a force-command inside an OpenSSH user certificate?
    If both apply to one login, sshd accepts the certificate only when the two commands are identical; a mismatch refuses the login. Separately, a ForceCommand in sshd_config may supersede a key's command= altogether.

  • command= also turns off port, X11 and agent forwarding for that key.
  • The client can override a forced command by naming its own command.
  • expiry-time= closes sessions already open when the date passes.
  • authorized_keys options add a second authentication factor to the key.
  • no-pty stops the client from running any command at all.