brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: command= in authorized_keys locks an SSH key to one job

Network and access
~/tips/tip-ssh-authorized-keys-forced-command

restrict,command="/usr/local/bin/backup.sh" ssh-ed25519 AAAAC3Nza... backup@example

Give a backup or deploy key exactly one thing it may do on the server.

A key for an automated job does not need a shell. In authorized_keys you can tie it to one command, and the server runs that command whatever the client asks for.

Put the options in front of the key, on the same line:

restrict,command="/usr/local/bin/backup.sh" ssh-ed25519 AAAAC3Nza... backup@example

command= forces the command. restrict switches off port, agent and X11 forwarding, PTY allocation and ~/.ssh/rc, and the man page says future restrictions get added to it too. I’d always pair the two, because the man page also points out that a forced command alone still allows forwarding unless you block it.

The command the client asked for is not lost. sshd puts it in SSH_ORIGINAL_COMMAND, so a small wrapper script can allow a few known commands and refuse the rest:

#!/bin/sh
case "$SSH_ORIGINAL_COMMAND" in
  "rsync --server"*) exec $SSH_ORIGINAL_COMMAND ;;
  *) echo "not allowed" >&2; exit 1 ;;
esac

That wrapper is now part of your attack surface. Keep it short, never pass the variable through eval, and test what happens with odd input before you trust it.

Further reading: sshd man page, AUTHORIZED_KEYS FILE FORMAT

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link