SSHauthorized_keys 命令选项:多个命令?

dka*_*eae 22 ssh command

authorized_keys 有一个 command="..." 选项,将密钥限制为单个命令。有没有办法将一个键限制为多个命令?例如,通过在那里使用正则表达式,还是通过编辑其他配置文件?

hfs*_*hfs 37

每个键只能有一个命令,因为该命令是“强制”的。

但是您可以使用包装脚本。被调用的命令获取原始命令行作为环境变量$SSH_ORIGINAL_COMMAND,它可以评估。

例如把这个放在~/.ssh/allowed-commands.sh

#!/bin/sh
#
# You can have only one forced command in ~/.ssh/authorized_keys. Use this
# wrapper to allow several commands.

case "$SSH_ORIGINAL_COMMAND" in
    "systemctl restart cups")
        systemctl restart cups
        ;;
    "shutdown -r now")
        shutdown -r now
        ;;
    *)
        echo "Access denied"
        exit 1
        ;;
esac
Run Code Online (Sandbox Code Playgroud)

然后引用它~/.ssh/authorized_keys

command="/home/user/.ssh/allowed-commands.sh",…
Run Code Online (Sandbox Code Playgroud)

  • 有用。可能值得更清楚的是,是 sshd 本身添加了`SSH_ORIGINAL_COMMAND`(每个`man sshd`),以便它在脚本中可用。还给出了一个允许运行某些“SSH_ORIGINAL_COMMAND”模式的自动化脚本的例子。另见 https://unixwars.blogspot.com/2014/12/getting-sshoriginalcommand.html (5认同)

gf_*_*gf_ 12

在伟大的SSH 中, O'Reilly 所著的The Secure Shell:The Definitive Guide一书,在第八章中,有一个很好的例子,使用如下脚本:

#!/bin/sh

/bin/echo "Welcome!
Your choices are:
1       See today's date
2       See who's logged in
3       See current processes
q       Quit"

/bin/echo "Your choice:"

read ans

while [ "$ans" != "q" ]
do
   case "$ans" in
      1)
         /bin/date
         ;;
      2)
         /usr/bin/who
         ;;
      3)
         /usr/bin/top
         ;;
      q)
         /bin/echo "Goodbye"
         exit 0
         ;;
      *)
         /bin/echo "Invalid choice '$ans': please try again"
         ;;
   esac
   /bin/echo "Your choice:"
   read ans
done
exit 0
Run Code Online (Sandbox Code Playgroud)

在您的.authorized_keys文件中使用它,例如:

command="/path/to/your/script.sh" <ssh-key>
Run Code Online (Sandbox Code Playgroud)

...在做的时候给你这个ssh

Welcome!
Your choices are:
1       See today's date
2       See who's logged in
3       See current processes
q       Quit
Your choice:
Run Code Online (Sandbox Code Playgroud)

  • @Jakuje 这通常对`command` 选项无效吗? (6认同)

Jak*_*uje 9

不。它不是“允许”命令,而是“强制”命令(作为ForceCommand选项)。

唯一的可能性是对不同的命令使用不同的键或从stdin.