使用firewalld 封锁除少数 IP 之外的所有 IP

使用firewalld 封锁除少数 IP 之外的所有 IP

在 Linux 联网机器上,我想限制“公共”区域(firewalld 概念)上允许访问的地址集。因此,最终结果是,除了明确允许的端口或协议外,其他任何机器都无法访问任何端口或协议,这有点像

  --add-rich-rule='rule family="ipv4" source not  address="192.168.56.120" drop'

  --add-rich-rule='rule family="ipv4" source not  address="192.168.56.105" drop'

上面的问题是,这不是一个真正的列表,它会阻止所有内容,因为如果一个地址因与另一个地址不同而被阻止,则会产生意外的“全部删除”效果,我该如何“解除阻止”特定的非连续集合?源是否接受地址列表?到目前为止,我在查看文档或谷歌搜索结果时没有看到任何内容。


编辑:我刚刚创建了这个:

# firewall-cmd  --zone=encrypt --list-all
encrypt (active)
  interfaces: eth1
  sources: 192.168.56.120
  services: ssh
  ports: 6000/tcp
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

但我仍然可以访问端口 6000,因为 .123 我的意图是,如果未列出源,它就不应该能够访问任何服务或端口

答案1

富人规则根本就没有必要。

如果您想将区域限制为特定的 IP 集,只需将这些 IP 定义为区域本身的源(并删除可能存在的任何接口定义,因为它们会覆盖源 IP)。

不过,您可能不想对“公共”区域执行此操作,因为这在语义上意味着面向公众的服务向世界开放。

相反,请尝试使用其他区域(例如“内部”),以便大多数受信任的 IP 地址可以访问潜在的敏感服务(例如 sshd)。(您也可以创建自己的区域。)

警告:不要将特殊的“受信任”区域与普通的“内部”区域混淆。任何添加到“受信任”区域的源都将被允许通过所有端口;允许将服务添加到“受信任”区域,但这样做没有任何意义。

firewall-cmd --zone=internal --add-service=ssh
firewall-cmd --zone=internal --add-source=192.168.56.105/32
firewall-cmd --zone=internal --add-source=192.168.56.120/32
firewall-cmd --zone=public --remove-service=ssh

这样做的结果将是一个允许访问 ssh 的“内部”区域,但只能从两个给定的 IP 地址访问。要使其持久,请重新运行每个命令并--permanent附加 ,或者更好的方法是使用firewall-cmd --runtime-to-permanent

答案2

首先安装并启动firewalld服务

sudo yum install -y firewalld
sudo systemctl start firewalld 

然后开源IP地址192.168.56.120192.168.56.121

如果需要,打开 ssh 22 作为远程 shell,并使用 [--permanent] 标志在系统重启后保留更改。

sudo firewall-cmd --zone=public --permanent --add-service=ssh
sudo firewall-cmd --zone=public --permanent --add-source=192.168.56.120
sudo firewall-cmd --zone=public --permanent --add-source=192.168.56.121

然后重新加载firewalld服务以激活新配置

sudo systemctl reload firewalld

您还可以使用以下方法混合传入地址和目标端口丰富的规则的特征防火墙命令

答案3

按照firewalld.richlanguage

来源 source [非] address="地址[/mask]"

   With the source address the origin of a connection attempt can be limited to the source address. An address is either a single IP address, or a network IP address. The address has to match the rule family (IPv4/IPv6). Subnet mask is expressed in either
   dot-decimal (/x.x.x.x) or prefix (/x) notations for IPv4, and in prefix notation (/x) for IPv6 network addresses. It is possible to invert the sense of an address by adding not before address. All but the specified address will match then.

指定地址的网络掩码以允许连续的块。

除此之外,您可以尝试创建一个ipset不连续的允许 IP 列表。

例如,在/etc/firewalld/direct.xml

<?xml version="1.0" encoding="utf-8"?>
<direct>
   <rule ipv="ipv4" table="filter" chain="INPUT" priority="0">-m set --match-set whitelist src -j ACCEPT</rule>
</direct>

实际情况ipset必须单独创建。

答案4

拒绝所有连接

firewall-cmd --permanent --zone=drop --change-interface=eth0

接受特殊连接

    firewall-cmd --permanent --zone=home --add-source=192.168.1.1/32
    or
    firewall-cmd --permanent --zone=drop --add-rich-rule='rule family="ipv4" source address="192.168.1.1/32" accept'

相关内容