<分区>
允许一组十个 IP 地址的 ssh 连接,并使用 hosts.allow 和 hosts.deny 文件阻止所有其他 IP。这个怎么做?
标签 linux
<分区>
允许一组十个 IP 地址的 ssh 连接,并使用 hosts.allow 和 hosts.deny 文件阻止所有其他 IP。这个怎么做?
最佳答案
SSHD
将首先检查 /etc/hosts.allow
中的条目。如果 /etc/hosts.allow
中没有适合 SSHD
的规则,将继续检查 /etc/hosts.deny
规则。
所以你需要添加:
/etc/hosts.deny
sshd: ALL EXCEPT LOCAL
阻止每个 SSH 连接,除了 localhost,它不在 /etc/hosts.allow
中。
/etc/hosts.allow
sshd: 192.168.178.10
sshd: 192.168.178.11
sshd: 192.168.178.10/255.255.255.0
允许特定 IP。最后一条规则是 IP 范围的示例。
然而,使用 /etc/hosts.allow
和 /etc/hosts.deny
不是推荐的方法来允许 SSH
仅用于几个IP。您应该考虑使用 iptables
来完成这项工作。
您可以使用如下规则为特定 IP 允许 SSH
:
iptables -A INPUT -m state --state NEW,ESTABLISHED,RELATED --source x.x.x.x -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m state --state NEW,ESTABLISHED,RELATED -p tcp --dport 22 -j DROP
如果你想使用 iptables,你应该看看 this stackoverflow question .
关于linux - 允许特定 IP 地址使用 ssh 并使用/etc/hosts.allow 和/etc/hosts.deny 文件阻止所有其他服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32221168/