我有一个程序运行在远程服务器端口9999上。因为它不支持任何forms的encryption和身份validation,我使用SSH隧道来访问它。
这是我正在使用的命令:
ssh -L 9999:localhost:9999 user@remotehost
为了保持这条隧道的活力。 我写了一个ssh脚本来监视并重新启动它,如果出了什么问题。 所以,我必须在脚本中存储密码。
但是,考虑到这个客户端服务器被黑客攻击的可能性。 我认为如果我可以把这个隧道限制在一个最小的权力机构里,那会更好。
那么,是否有可能限制远程主机用户只能使用ssh隧道转发到端口9999?
为了保持这条隧道的活力。 我写了一个ssh脚本来监视并重新启动它,如果出了什么问题。
你应该考虑使用autossh代替。
所以,我必须在脚本中存储密码。
您应该使用公钥authentication而不是密码。
那么,是否有可能限制远程主机用户只能使用ssh隧道转发到端口9999?
首先,确保用户无法打开服务器机器上的交互式shell。 只要创build一个特定的帐户,只用于打开这个隧道,如果你没有使用一个。 将此帐户的默认shell程序设置为/sbin/nologin (如果前者不存在,则为/bin/false )
useradd tunnel usermod -s /sbin/nologin tunnel
您应该在客户端计算机上生成一个ssh密钥对并将公钥复制到服务器。
ssh-keygen ssh-copy-id tunnel@server
最后,在服务器上 ,使用authorized_keys文件来限制对除localhost:9999之外的任何东西进行隧道传输的能力。 附加在ssh-copy-id上传的授权密钥上加上以下内容。
no-pty,permitopen="localhost:9999"
no-pty是另一个不允许打开交互式shell的安全设置。 结果行看起来像这样:
no-pty,permitopen="localhost:9999" ssh-rsa AAAAB3NzaC1y...Rdo/R user@clientbox
您可以在authorized_keys file设置其他有用的选项。 有关更多信息,请阅读man sshd 。
另外,您还可以通过该帐户的Match块locking/etc/ssh/sshd_config中的帐户:
Match User tunnel ForceCommand /sbin/nologin # An additional configuration to disable any shell command, superfluous when the user's shell is already set to `/sbin/nologin` AllowAgentForwarding no AllowTcpForwarding local # Disallow port forwarding of remote ports, superfluous when using the `permitopen` restriction in authorized_keys AllowStreamLocalForwarding no # Probably not necessary, as the user needs to have access rights to access the AF_UNIX port X11Forwarding no # Because some default configuration files, eg from Debian, set it to yes # Beware: Match does not end with indentation!
这些只是提高您当前的SSH隧道设置的提示。 正如Dennis所说,还有其他更优雅的隧道解决scheme应该考虑。
不,那是不可能的。 您可以禁用端口转发,但禁用转发任何端口,不存在每端口设置。
是的,这可以通过在服务器的~/.ssh/authorized_keys预先permitopen="localhost:9999"来实现,请参阅sshd手册页(感谢Kenny!)。 你还需要强制执行一个除了睡眠之外什么都不做的命令,以确保任何攻击者都不能得到一个shell(从而消除这个限制,并做其他坏事)。
这就是说,ssh是这样做的错误方式。 为什么不使用stunnel通过ssl使服务可用? 或者用openvpn在两台机器之间build立一个VPN连接?
我会看看autossh工具。
手册页可在这里find:
我使用它来保持打开在防火墙后面的IMAP和SMTP服务器的端口。 例如:
% autossh -M 0 -f -N -L 2025:localhost:25 -L 2143:localhost:143 \ myuser@remoteserver
看起来也在大多数更大的Linux发行版存储库中。