我的用户通过 LDAP 在多台计算机上共享。
为了一这些机器(我们称之为“fileserf”),我想限制一些用户可以做什么(实际上:阻止他们通过 ssh 登录交互式会话)。上其他机器上,这些用户应该能够正常使用 ssh。
所以我最初的想法是使用该internal-sftp
子系统,大致如下:
Match group sftponly
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
sftponly
这工作正常,因为它仅限制单个主机上的(本地)组的成员fileserf
,但不幸的是internal-sftp
子系统仅有的允许sftp
和不允许scp
(或rsync
)。
所以我做了一些更多的研究并发现rssh
,这似乎让我能够完全做我想做的事(在许可方面)。
现在的问题是我无法在 LDAP 中设置这些用户的登录 shell /usr/bin/rssh
,因为这意味着他们将受到限制全部机器,不仅仅是在fileserf
.
所以我的想法是通过 fileserf 中的一些配置来覆盖登录 shell sshd_config
:
Match group sftponly
X11Forwarding no
AllowTcpForwarding no
ForceCommand /usr/bin/rssh
不幸的是,这似乎不起作用,因为现在用户Connection closed
每当尝试sftp
进入机器时都会收到:
$ ssh user@fileserf
This account is restricted by rssh.
Allowed commands: scp sftp
If you believe this is in error, please contact your system administrator.
Connection to fileserf closed.
$ sftp user@fileserf
Connection closed
$
我怎样才能ForceCommand
与 合作rssh
?
或者,如何配置sshd
以覆盖一组用户的登录 shell?
答案1
这rssh
联机帮助页表明它应该是这些用户的登录 shell:
The system administrator should install the shell on the restricted
system. Then the password file entry of any user for whom it is
desireable to provide restricted access should be edited, such that
their shell is rssh. For example:
luser:x:666:666::/home/luser:/usr/bin/rssh
使用 时ForceCommand
,仅运行该命令。当您运行scp
或 时sftp
,命令由 SSH 运行(分别为scp
、/usr/lib/openssh/sftp-server
),当然,除非 执行的程序ForceCommand
使用SSH_ORIGINAL_COMMAND
它们,否则它们无法运行。因此,为了rssh
完成其工作,它必须是登录 shell,而不是ForceCommand
.
有关的:
相反,您可以使用包装器脚本来rssh
代替登录 shell 来执行命令。例如:
/usr/local/bin/wrapper-shell
:
#! /bin/sh
rssh -c "$SSH_ORIGINAL_COMMAND"
并在/etc/ssh/sshd_config
:
Match group sftponly
X11Forwarding no
AllowTcpForwarding no
ForceCommand /usr/local/bin/wrapper-shell
具有/usr/local/bin/wrapper-shell
可执行性应该可以工作。
答案2
我们遇到了同样的问题:服务器必须允许所有用户使用 scp sftp 和 rsync,但不允许与命令行连接。用户数据库位于 ldap 中,我们无法在本地修改 /etc/passwd。因此 rssh 不是一个选择。
我发现的一种解决方案是使用ForceCommand
shell 脚本。在 /etc/ssh/sshd_config 中添加以下行:
Match user *
X11Forwarding no
AllowTcpForwarding no
ForceCommand /usr/local/bin/wrapper-shell user1 user2 user3
哪里userX
允许特殊用户通过ssh登录。执行实际过滤的包装外壳脚本是:
#!/bin/sh
SSHCMD=`echo "$SSH_ORIGINAL_COMMAND" | awk '{ print $1 }'`
ME=`id -u -n`
DOIT=Maybe
# Root is always allowed in order to not being locked out
for n in root $*
do
if [ "$ME" = "$n" ]
then
DOIT=YES
break
fi
done
if [ "$DOIT" = YES -o "$SSHCMD" = "scp" -o "$SSHCMD" = "rsync" -o "$SSHCMD" = /usr/lib/openssh/sftp-server ]
then
sh -c "$SSH_ORIGINAL_COMMAND"
else
cat <<EOF 1>&2
This account is restricted and the command is not allowed.
User $ME is locked out.
If you believe this is in error, please contact your system administrator.
EOF
exit 1
fi