![SSH authorized_keys 命令选项:多个命令?](https://linux22.com/image/674939/SSH%20authorized_keys%20%E5%91%BD%E4%BB%A4%E9%80%89%E9%A1%B9%EF%BC%9A%E5%A4%9A%E4%B8%AA%E5%91%BD%E4%BB%A4%EF%BC%9F.png)
authorized_keys 有一个 command="..." 选项,将密钥限制为单个命令。有没有办法将密钥限制为多种的命令?例如,通过使用正则表达式,或者通过编辑其他配置文件?
答案1
不是。这不是“允许”的命令,而是“强制”的命令(因为部队指挥选项)。
唯一的可能性是使用不同的键来执行不同的命令或从中读取参数stdin
。
答案2
每个键只能有一个命令,因为该命令是“强制的”。
但是您可以使用包装脚本。被调用的命令将原始命令行作为环境变量$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",…
答案3
在伟大的SSH,安全外壳:权威指南O'Reilly 的书第八章中给出了一个很好的例子,使用如下脚本:
#!/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: