用于 shell 的 SSH ForceCommand,同时保持常规登录和远程命令执行成为可能

用于 shell 的 SSH ForceCommand,同时保持常规登录和远程命令执行成为可能

我如何运行/调整此命令并使用 ForceCommand 为该用户提供 shell?

客户端命令

(cat ./sudoPassword ./someCommandInput) | ssh user@ip "sudo -Sp '' someCommand"

服务器 sshd_config

ForceCommand /bin/bash

幕后限制是,ForceCommand 需要成为为该用户提供 shell 的机制,除了上面的典型命令ssh user@ip也需要工作之外。

我尝试过各种配置,例如

ForceCommand /bin/bash -ic $SSH_ORIGINAL_COMMAND ls
ForceCommand /bin/bash -s < $SSH_ORIGINAL_COMMAND
ForceCommand /bin/bash -c $SSH_ORIGINAL_COMMAND

我也尝试过使用客户端命令,提供像 -tt 这样的 ssh 选项,但我似乎找不到正确的配置。

答案1

使用包装器脚本作为ForceCommand.像这样的脚本(例如,保存在/usr/local/bin/myshell):

#! /bin/bash

if [[ -n $SSH_ORIGINAL_COMMAND ]] # command given, so run it
then
    exec /bin/bash -c "$SSH_ORIGINAL_COMMAND"
else # no command, so interactive login shell
    exec bash -il
fi

行动中:

% grep ForceCommand -B1 /etc/ssh/sshd_config
Match user muru
    ForceCommand /usr/local/bin/forceshell
% ssh muru@localhost
$ ls
Desktop   Documents   Downloads   Music   Pictures   Public   Templates   Videos
$ logout
Connection to localhost closed.
% ssh muru@localhost echo foo
foo
% ssh muru@localhost echo '*'
Desktop   Documents   Downloads   Music   Pictures   Public   Templates   Videos

相关内容