Centos7 防火墙端口例外设置
2022-06-01 网络
1、查看防火墙状态命令:
systemctl status firewalld
或
firewall-cmd --state
命令执行如下:
● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 一 2022-05-30 16:19:23 CST; 3min 46s ago Docs: man:firewalld(1) Main PID: 3685 (firewalld) CGroup: /system.slice/firewalld.service └─3685 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 5月 30 16:19:23 bogon systemd[1]: Starting firewalld - dynamic firewall daemon... 5月 30 16:19:23 bogon systemd[1]: Started firewalld - dynamic firewall daemon.
2、查看已开放端口:
firewall-cmd --list-all #查看所有口
命令执行如下:
public (active) target: default icmp-block-inversion: no interfaces: enp0s3 sources: services: ssh dhcpv6-client ports: 80/tcp 443/tcp 22/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
firewall-cmd --zone=public --list-ports #查看添加到zone=publi区域的端
执行如下:
ports: 80/tcp 443/tcp 22/tcp
以上结果表示防火墙开放的端口为80,443,与22端口。
3、添加防火墙例外端口命令:
firewall-cmd --zone=public --add-port=80/tcp --permanent #添加80端口到防火墙例外 firewall-cmd --zone=public --add-port=443/tcp --permanent #添加443端口到防火墙例外 firewall-cmd --zone=public --add-port==3000-4000/udp --permanent #添加upd的3000~4000端口到防火墙例外,并设置为public级别,并永久生效
PS:
zone #作用域,具体值有block、dmz、drop、external、home、internal、public、trusted、work;可以将具体的端口制定到具体的zone配置文件中。
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
防火墙规则设置为 zone=public 的规则保存在 /etc/firewalld/zones/public.xml中。如下:
<?xml version="1.0" encoding="utf-8"?> <zone> <short>Public</short> <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description> <service name="ssh"/> <service name="dhcpv6-client"/> <port protocol="tcp" port="80"/> <port protocol="tcp" port="443"/> <port protocol="tcp" port="22"/> </zone>
4、从防火墙删除例外端口命令:
firewall-cmd --zone=public --remove-port=80/tcp --permanent #从防火墙删除80端口 firewall-cmd --zone=public --remove-port=443/tcp --permanent #从防火墙删除443端口 firewall-cmd --list-rich-rules #查看防火墙屏蔽结果
5、重启加载、打开、关闭、重启防火墙
firewall-cmd --reload #重新加载防火 systemctl stop firewalld #停止 systemctl start firewalld #启动 systemctl restart firewalld #重启
6、设置防火墙随系统开机或禁止启动
systemctl disable firewalld.service #禁止firewall开机启动 systemctl enable firewalld.service #禁止firewall开机启动 systemctl is-enabled firewalld.service #查看是否开机启动