authorized_keys
Quick Reference
Command Name:
authorized_keys
Category:
networking
Platform:
Linux/Unix
Basic Usage:
Common Use Cases
Syntax
Located at: ~/.ssh/authorized_keys
Options
Option | Description |
---|---|
command="command" | Forces a specific command to be executed when this key is used for authentication |
from="pattern-list" | Restricts logins with this key to come from specified host names or IP addresses |
no-port-forwarding | Prevents TCP port forwarding when authenticated with this key |
no-agent-forwarding | Disables authentication agent forwarding for this key |
no-X11-forwarding | Prevents X11 forwarding when authenticated with this key |
no-pty | Prevents allocation of a pseudo-terminal when authenticating with this key |
no-user-rc | Prevents execution of ~/.ssh/rc by ssh when authenticating with this key |
permitopen="host:port" | Restricts port forwarding to specified destination only |
environment="NAME=value" | Sets environment variables when this key is used for login |
Examples
How to Use These Examples
The examples below show common ways to use the authorized_keys
command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
Basic Structure:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQ... user@hostname
A standard entry contains a public key (typically RSA, DSA, ECDSA, or Ed25519) and an optional comment.
With Options:
command="only-this-command",from="trusted-host.example.com" ssh-rsa AAAAB3NzaC1yc2EAAAADAQ... restricted-access
You can prefix keys with options to restrict access or force specific commands.
Adding a Key Manually:
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQ... user@hostname" >> ~/.ssh/authorized_keys
Manually append a public key to the authorized_keys file.
Setting Permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Set the proper restrictive permissions for the .ssh directory and authorized_keys file.
Using ssh-copy-id:
ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote-host
A safer way to add your key to a remote host's authorized_keys file.
Restricting to Specific Commands:
command="rsync --server -logDtprze.iLsfx . /backup/" ssh-rsa AAAAB3NzaC1yc2EAAAADAQ... backup-only
Restrict the key to only execute a specific command when used for login.