linux使用FirewallD限制网络访问

发布时间 2023-07-14 11:22:34作者: 风行天下-2080

1、

https://blog.csdn.net/yuyuyuliang00/article/details/125913307

firewalld防火墙使用firewall-cmd命令行工具配置防火墙规则。

在我们执行任何配置前,首先使用systemctl工具启用firewalld服务,如下:

​[root@localhost blctrl]# systemctl enable firewalld

一旦启用了,我们现在可以通过执行以下命令启动firewalld:

[root@localhost blctrl]# systemctl start firewalld

你可以通过运行以下命令验证firewalld的状态并且以下输出确认了firewalld启动了并且在运行。

    [root@localhost blctrl]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
       Active: active (running) since Thu 2022-07-21 19:40:25 CST; 4h 8min left
         Docs: man:firewalld(1)
     Main PID: 689 (firewalld)
       CGroup: /system.slice/firewalld.service
               └─689 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

使用firewalld配置规则

现在我们使firewalld运行了,我们直接制定一些规则。firewalld允许你添加和阻塞端口,黑名单,以及白名单IP,提供对服务器访问的地址。一旦完成这些配置,总是确保你为了使新规则生效重载这个防火墙。
添加一个TCP/UDP端口

添加一个端口,用于HTTPS的443,使用以下语法。注意:你必须在这个端口号之后指定端口是TCP或UDP。

    [root@localhost blctrl]# firewall-cmd --add-port=443/tcp --permanent
    success

类似地,要添加一个UDP端口,按如下指定这个UDP选项:

    [root@localhost blctrl]# firewall-cmd --add-port=53/udp --permanent
    success

--permanent标记确保即使在重启后规则有效。

阻塞一个TCP/UDP端口

TCP 80端口先前已经被添加到了规则中了,用浏览器测试:

要阻塞一个TCP端口,如TCP 80,运行以下命令:

    [root@localhost blctrl]# firewall-cmd --remove-port=80/tcp --permanent
    success
    [root@localhost blctrl]# firewall-cmd --reload
    success

再次用浏览器测试:

 类似地,封锁一个UDP端口,将按照相同语法:

    [root@localhost blctrl]# firewall-cmd --remove-port=53/udp --permanent
    success
    [root@localhost blctrl]# firewall-cmd --reload
    success

允许一个服务

在/etc/services文件中定义了网络服务。要允许像https地服务,执行命令:

    [root@localhost blctrl]# firewall-cmd --add-service=https
    success

阻塞一个服务

要阻塞一个服务,例如,https,执行:

    [root@localhost blctrl]# firewall-cmd --remove-service=https
    success

白名单一个IP地址

要允许单个IP地址穿过防火墙,执行命令:

    [root@localhost blctrl]# firewall-cmd --permanent --add-source=192.168.3.244
    success
    [root@localhost blctrl]# firewall-cmd --reload
    success

你也可以使用CIDR标注允许一个IPs范围或者整个子网。例如,以255.255.255.0允许一整个子网。

    [root@localhost blctrl]# firewall-cmd --permanent --add-source=192.168.3.0/24
    success

移除一个白名单IP地址

如果你在防火墙上移除一个白名单IP,使用--remove-source标记:

    [root@localhost blctrl]# firewall-cmd --permanent --remove-source=192.168.3.244
    success

对于整个子网:

    [root@localhost blctrl]# firewall-cmd --permanent --remove-source=192.168.3.0/24
    success

阻塞一个IP地址:

到目前为止,我们已经看到了你如何添加和移除端口和服务以及添加和移除白名单IPs。要阻塞一个IP地址,'rich rules'用于这个目的。

例如,要阻塞这个IP 192.168.3.244,运行命令:

    [root@localhost blctrl]# firewall-cmd --add-rich-rule="rule family='ipv4' source address='192.168.3.244' reject"
    success

 在192.168.3.244上进行测试:

运行阻塞命令前:可以ping通

    [blctrl@localhost ~]$ ping -c2 192.168.3.246
    PING 192.168.3.246 (192.168.3.246) 56(84) bytes of data.
    64 bytes from 192.168.3.246: icmp_seq=1 ttl=64 time=0.466 ms
    64 bytes from 192.168.3.246: icmp_seq=2 ttl=64 time=0.559 ms
     
    --- 192.168.3.246 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 999ms
    rtt min/avg/max/mdev = 0.466/0.512/0.559/0.051 ms

运行阻塞命令后,ping不通了

    [blctrl@localhost ~]$ ping -c2 192.168.3.246
    PING 192.168.3.246 (192.168.3.246) 56(84) bytes of data.
    From 192.168.3.246 icmp_seq=1 Destination Port Unreachable
    From 192.168.3.246 icmp_seq=2 Destination Port Unreachable
     
    --- 192.168.3.246 ping statistics ---
    2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1000ms

要阻塞整个子网,运行:

[root@localhost blctrl]# firewall-cmd --add-rich-rule="rule family='ipv4' source address='192.168.3.0/24' reject"

保存防火墙规则

如果你对防火墙规则做了任何修改,为了立即应用更改,你需要运行以下命令:

    [root@localhost blctrl]# firewall-cmd --reload
    success

查看防火墙规则

要看一下在防火墙中所有规则,执行这个命令:

    [root@localhost blctrl]# firewall-cmd --list-all
    public (active)
      target: default
      icmp-block-inversion: no
      interfaces: eth0
      sources:
      services: dhcpv6-client ssh
      ports: 443/tcp
      protocols:
      masquerade: no
      forward-ports:
      source-ports:
      icmp-blocks:
      rich rules:
            rule family="ipv4" source address="192.168.3.244" reject
————————————————
版权声明:本文为CSDN博主「yuyuyuliang00」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yuyuyuliang00/article/details/125913307

 

 

 

 

2、