我有一个在远程服务器端口 9999 上运行的程序。由于它不支持任何类型的加密和身份验证,我使用 ssh 隧道来访问它。
这是我正在使用的命令:
ssh -L 9999:localhost:9999 user@remotehost
Run Code Online (Sandbox Code Playgroud)
为了保持这条隧道的活力。我编写了一个 ssh 脚本来监视并在出现任何问题时重新启动它。所以,我必须将密码存储在脚本中。
但是,考虑到此客户端服务器被黑客入侵的可能性。我认为如果我可以将此隧道限制为最小权限会更好。
那么,是否可以限制 remotehost 用户只能使用 ssh 隧道转发到端口 9999?
为了保持这条隧道的活力。我编写了一个 ssh 脚本来监视并在出现任何问题时重新启动它。
您应该考虑使用autossh。
所以,我必须将密码存储在脚本中。
您应该使用公钥身份验证而不是密码。
那么,是否可以限制 remotehost 用户只能使用 ssh 隧道转发到端口 9999?
首先,确保用户无法在服务器计算机上打开交互式 shell 。如果您尚未使用,只需创建一个仅用于打开此隧道的特定帐户。将此帐户的默认外壳设置为/sbin/nologin(或/bin/false如果前者不存在)
useradd tunnel
usermod -s /sbin/nologin tunnel
Run Code Online (Sandbox Code Playgroud)
您应该在客户端机器上生成一个 ssh 密钥对并将公钥复制到服务器。
ssh-keygen
ssh-copy-id tunnel@server
Run Code Online (Sandbox Code Playgroud)
最后,在服务器上,使用authorized_keys 文件限制除localhost:9999 之外的任何隧道的能力。附加将以下内容添加到使用 上传的授权密钥中ssh-copy-id。
no-pty,permitopen="localhost:9999"
Run Code Online (Sandbox Code Playgroud)
no-pty 是另一个不允许打开交互式 shell 的安全设置。结果行看起来像这样:
no-pty,permitopen="localhost:9999" ssh-rsa AAAAB3NzaC1y...Rdo/R user@clientbox
Run Code Online (Sandbox Code Playgroud)
可能还有其他有用的选项可以在authorized_keys file. 有关更多信息,请阅读man sshd。
此外,您还可以/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, e.g. from Debian, set it to yes
# Beware: Match does not end with indentation!
Run Code Online (Sandbox Code Playgroud)
这些只是改进当前 ssh 隧道设置的技巧。正如丹尼斯建议的那样,您应该考虑其他更优雅的隧道解决方案。
| 归档时间: |
|
| 查看次数: |
5425 次 |
| 最近记录: |