In order to restrict executable SSH commands with authorized keys, you can use the SSH feature called forced command
within the authorized_keys
file.
As the command is bound to an SSH key, when the user try to execute a command, the only output will be the one of the command configured previously.
The use of a non-interactive will be helpful when the user is a daemon or can’t answers on the terminal, especially if SSH_ORIGINAL_COMMAND
is used: this variable contains the original command line if a forced command is executed and it can be used to extract the original arguments.
Restrict executable SSH commands with authorized keys
On a serverA and serverB, you have a same user account.
Deals keys
You need in first time deals the user keys with servers.
Deals users keys with servers :
# ssh-keygen /home/user01/.ssh/id_rsa |
Now your user01 can connect to the other server without enter passwd and launch all cmd (and connect to serverB) :
[user01@serverA ~]$ ssh serverB ls -la /tmp |
Limit to only one command
On the serverB, restrict a command in the user01 authorized_keys files :
# vi /home/user01/.ssh/authorized_keys |
And add the chosen command (date here) :
command="date" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCv6Ytv+ugm3dd3ALt/aJg2E7Mj083[....]= user01@serverA |
On the serverA, the user01 can test all command but the only output will be for “date”, which set as the forced command
:
[user01@serverA ~]$ ssh serverB date |
Limit to whitelist command
Instead of add a single command in a .ssh/authorized_keys
. You can create a whitlist command in a bash script.
Simply add the path to your scrip :
command="/usr/bin/authorized_cmd.sh" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCv6Ytv+ugm3dd3ALt/aJg2E7Mj083[....]= user01@serverA |
You can add more security to your /var/lib/user01/.ssh/authorized_keys
file with adding 5 useful parameters before the key:
no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,command="/usr/bin/authorized-cmd.sh" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC9EXZT3nctHP7AdcSU8c6zW/8vQZJNOULtmU6450cgJVTwi4F/rvdozM4YHsxbpHP3X/KihZb[...]+1ICBaaV/jmcBjREeUZV85BBMxw24GU5qLnWh8zhLhfBRtsG2UuGjRQ4QPHk/3klcHK/k= [email protected] |
Five security layers had been added here:
• no-port-forwarding
: no remote access with port forwarding is possible
• no-X11-forwarding
: no graphical X11 forwarding is possible
• no-agent-forwarding
: no SSH transfer agent is possible
• no-pty
: no terminal connexion is possible
• command="/usr/bin/authorized-cmd.sh"
: the whitelist commands script
Example of custom bash whitelist commands
Simple version
#!/bin/sh |
Artistic version
#!/bin/sh |
The use of SSH ORIGINAL COMMAND
This script is non-interactive, helpful when the user is a daemon or can’t answers on the terminal.
The goal is to use the SSH_ORIGINAL_COMMAND
environment variable which contains the original command line if a forced command is executed. It can be used to extract the original arguments.
Manual:
SSH_ORIGINAL_COMMAND
This variable contains the original command line if a forced command is executed. It can be used to extract the original arguments.
The script in /usr/bin/authorized-cmd.sh
with SSH_ORIGINAL_COMMAND
contains:
#!/bin/sh |
You can add some log files:
#!/bin/sh |
Documentation
https://superuser.com/questions/641275/make-linux-server-allow-rsync-scp-sftp-but-not-a-terminal-login
https://superuser.com/questions/1507366/securing-ssh-original-command-on-a-ssh-proxy-server
https://www.ibm.com/docs/en/zos/2.2.0?topic=socrlp-environment-variables
https://unix.stackexchange.com/questions/324727/openssh-prevent-globbing-on-ssh-original-command
https://linuxcommand.org/lc3_man_pages/ssh1.html