如何配置SSHd以允许单个命令,而又不授予用户完整的登录权限?


11

我正在寻找通过SSH调用远程命令的最佳方法。我创建用户“ rpcall”,生成新证书并填写authorized_keys。通过以下方式进一步保护它

from="ip",no-agent-forwarding,no-X11-forwarding,no-port-forwarding,no-pty ssh-rsa ......

现在用户rpcall无法登录到终端

ssh -l rpc 192.168.12.1
PTY allocation request failed on channel 0

但是可以运行任何命令

ssh -l rpc 192.168.12.1 cat /etc/passwd

有什么解决方案可以将命令执行限制为仅一个处理脚本吗?例如/home/rpcall/bin/command.sh

我为此用户设置了bash shell,并使用.bashrc强制运行处理脚本,但是我不知道如何从ssh调用传递参数。

.bashrc用于用户rpcall

/home/rpcall/bin/command.sh $params1 $params2
exit

来自其他机器的ssh呼叫

ssh -l rpcall 192.168.12.1 "param1" "param2"

Answers:


19

您可以使用authorized_keys文件来限制命令。command="/home/rpcall/bin/command.sh"在密钥之前放置在authorized_keys文件中,并且用户只有在连接时才运行该命令。

检查手册页中的authorized_keys,这是来自该手册页的信息,

 command="command"
         Specifies that the command is executed whenever this key is used
         for authentication.  The command supplied by the user (if any) is
         ignored.  The command is run on a pty if the client requests a
         pty; otherwise it is run without a tty.  If an 8-bit clean chan-
         nel is required, one must not request a pty or should specify
         no-pty.  A quote may be included in the command by quoting it
         with a backslash.  This option might be useful to restrict cer-
         tain public keys to perform just a specific operation.  An exam-
         ple might be a key that permits remote backups but nothing else.
         Note that the client may specify TCP and/or X11 forwarding unless
         they are explicitly prohibited.  The command originally supplied
         by the client is available in the SSH_ORIGINAL_COMMAND environ-
         ment variable.  Note that this option applies to shell, command
         or subsystem execution.

如果需要多个命令,则需要基本设置几组键,并使用不同的键为您提供不同的命令。

编辑:我刚刚注意到,原始命令在SSH_ORIGINAL_COMMAND环境变量中可用,因此您确实可以使用自己的脚本处理该输入,做一些巧妙的事情。


4
指定的命令能够通过SSH_ORIGINAL_COMMAND环境变量访问参数。这样可以将参数传递给该命令,它只需要解析该环境变量并完成请求的工作即可。
奥利弗(Oliver)

是的,几分钟前我也注意到了最新答案。
AugustBitTony 2012年

使用命令选项和SSH_ORIGINAL_COMMAND的完美解决方案,正是我所需要的。谢谢AugustBitTony和Oliver!
安德鲁
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.