允许一组十个IP地址的ssh连接,并阻止使用hosts.allow和hosts.deny文件的所有其他IP。该怎么做呢?
发布于 2015-08-26 17:32:30
SSHD将首先检查/etc/hosts.allow中的条目。如果/etc/hosts.allow中没有适合的规则,SSHD将继续检查/etc/hosts.deny中的规则。
因此,您需要添加:
/etc/hosts.deny
sshd: ALL EXCEPT LOCAL以阻止除本地主机之外的所有SSH连接,本地主机不在/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。最后一条规则是网段的示例。
但是,使用/etc/hosts.allow和/etc/hosts.deny并不是仅允许对少数IP使用SSH的推荐方法。你应该考虑使用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。
发布于 2018-04-19 06:39:04
我找到了如何使用允许和拒绝主机文件阻止或允许ips的示例
路径文件/etc/hosts.allow
sshd : 192.168.0.1: allow
sshd : 192.168.0.2: allow
#add the ips that you want
sshd : 192.168.0.3: allow
#example of localhost
sshd : localhost : allow
#example 192.168.0.x subnet
192.168.0.
# for 192.168.x.x subnet
192.168.
#add this at the end
sshd : ALL : deny源和其他示例
警告
如果主机访问文件的最后一行不是换行符(通过按Enter键创建),则文件中的最后一个规则将失败,并将错误记录到/var/log/messages或/var/log/secure。对于跨多行而不使用反斜杠的规则也是如此。以下示例说明了由于以下两种情况之一导致的规则失败的日志消息的相关部分:
warning: /etc/hosts.allow, line 20: missing newline or line too long
https://stackoverflow.com/questions/32221168
复制相似问题