我想限制我的 ssh 登录仅限一个 IP 地址,所以我修改了以下文件
$ nano /etc/ssh/sshd_config
Run Code Online (Sandbox Code Playgroud)
并添加了这一行(即我的 ip )希望它能起作用
ListenAddress 172.168.0.21
Run Code Online (Sandbox Code Playgroud)
但没有运气连接被拒绝,我不想使用任何 iptables 的东西。为什么它不能以这种方式工作,我该如何修复它以及任何解释?
use*_*517 12
您可以使用AllowUsers/etc/ssh/sshd_config 中的指令,例如
AllowUsers you@ip.add.re.ss
Run Code Online (Sandbox Code Playgroud)
如果您对 sshd_config 文件进行了任何更改,请不要忘记重新启动 sshd。
来自 sshd_config 联机帮助页
This keyword can be followed by a list of user name patterns,
separated by spaces. If specified, login is allowed only for
user names that match one of the patterns. ‘*’ and ‘?’ can be
used as wildcards in the patterns. Only user names are valid; a
numerical user ID is not recognized. By default, login is
allowed for all users. If the pattern takes the form USER@HOST
then USER and HOST are separately checked, restricting logins to
particular users from particular hosts.
Run Code Online (Sandbox Code Playgroud)
小智 7
我必须同意 dunxd,IPTables 不应该被视为一种可行的方法。但是,您很幸运,因为您可以利用 tcpwrappers 实现相同的功能。尽管比表面上更复杂,但 tcpwrappers 基本上可以归结为两个文件:/etc/hosts.allow 和 /etc/hosts.deny 如果这些文件尚不存在,您可以安全地将它们创建为空文件:sudo touch /etc/hosts.{allow,deny}.
现在是时候让事情变得更复杂了。保护网络访问的“最佳”方法是将您的默认且唯一的 hosts.deny 条目设置为ALL:ALL,但是,这可能会导致意外的访问限制。出于这个原因,以及这个问题的目的,输入sshd:ALL/etc/hosts.deny就足够了,这将禁止对主机的所有 ssh 访问。
就 sshd 而言,/etc/hosts.allow 中的所有条目现在将取代默认拒绝规则:sshd: 172.168.0.21仅允许访问主机 172.168.0.21 并拒绝所有其他人。
tcpwrappers 文件接受逗号分隔的条目列表,因此您可以将地址附加到上面的第一个条目。tcpwrappers 也接受部分 IP 地址作为子网,因此您可以允许整个 172.168.0.0/24 作为sshd: 172.168.0.
请参考手册页以获取更多详细信息。tcpwrappers 实际上功能非常丰富,我建议阅读更多内容,而不是上面的简短检查。