SSH authorized_keys命令选项:多个命令?

authorized_keys有一个命令=“…”选项,将一个键限制为一个命令。 有没有办法将一个键限制到多个命令? 例如通过在那里有一个正则expression式,或者通过编辑其他configuration文件?

号是不是“允许”命令,而是“强制”命令(如ForceCommand选项)。

唯一的可能是对不同的命令使用不同的键或从stdin读取参数。

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

但是你可以使用包装脚本。 被调用的命令获取原始命令行作为环境variables$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 

然后在~/.ssh/authorized_keys引用它

 command="/home/user/.ssh/allowed-commands.sh",… 

在很好的SSH中, O'Reilly的“Secure Shell:权威指南 ”第八章给出了一个很好的例子:

 #!/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 

在你的.authorized_keys文件中使用这个:

 command="/path/to/your/script.sh" <ssh-key> 

在做ssh时候给你这个:

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

其他方法对于给定的用户使用例如受限制的shell,或者使用封装器来限制对在特定目录中find的al文件/脚本的命令,从而允许增加命令列表而不改变封装器。

另一篇文章描述了一个通用的脚本,它也允许允许的命令的命令行参数,但允许使用正则expression式expression的规则将其locking。

这个例子将expression如下:

 command="only systemctl shutdown" 

而一个.onlyrules文件将用这个内容来制作:

 \:^systemctl restart cups$:{p;q} \:^shutdown -r now$:{p;q} 

这种“唯一”方法的优点是不需要为每个用户和情况编写单独的脚本。