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)
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)