firewalld:linux
禁用iptables:ssh
[root@linux ~]# systemctl stop iptables.service [root@linux ~]# systemctl disable iptables.service Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
啓用firewalld並設置開機啓動:tcp
[root@linux ~]# systemctl start firewalld.service [root@linux ~]# systemctl enable firewalld.service Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service. Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
firewalld有9個zone,每一個zone能夠看做一個規則集,每一個規則集的內容都不一樣,默認的zone是publicthis
查看全部zone:code
[root@linux ~]# firewall-cmd --get-zones block dmz drop external home internal public trusted work
查看默認zone:server
[root@linux ~]# firewall-cmd --get-default-zone public
設置默認的zone:xml
[root@linux ~]# firewall-cmd --set-default-zone=work success [root@linux ~]# firewall-cmd --get-default-zone work
查看指定網卡的zone:ip
[root@linux ~]# firewall-cmd --get-zone-of-interface=ens33 public
對於沒有zone的網卡,能夠手動添加:內存
[root@linux ~]# firewall-cmd --get-zone-of-interface=lo no zone [root@linux ~]# firewall-cmd --zone=public --add-interface=lo success [root@linux ~]# firewall-cmd --get-zone-of-interface=lo public
更改指定網卡的zone:utf-8
[root@linux ~]# firewall-cmd --zone=work --change-interface=lo success [root@linux ~]# firewall-cmd --get-zone-of-interface=lo work
刪除指定網卡的zone:
[root@linux ~]# firewall-cmd --zone=work --remove-interface=lo success [root@linux ~]# firewall-cmd --get-zone-of-interface=lo no zone
查看全部網卡所在的zone:
[root@linux ~]# firewall-cmd --get-active-zones public interfaces: ens33
查看全部zone包含的service:
[root@linux ~]# firewall-cmd --get-services
查看當前zone中的service:
[root@linux ~]# firewall-cmd --list-services ssh dhcpv6-client
查看指定zone中的service:
[root@linux ~]# firewall-cmd --zone=work --list-services
添加service到當前zone:
[root@linux ~]# firewall-cmd --add-service=http --permanent [root@linux ~]# firewall-cmd --list-services ssh dhcpv6-client http
添加service到指定zone:
[root@linux ~]# firewall-cmd --zone=work --add-service=http --permanent success [root@linux ~]# firewall-cmd --zone=work --list-services ssh dhcpv6-client
查看zone的配置文件:
[root@linux ~]# cat /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"/> <service name="http"/> #剛添加的http已顯示 </zone>
#在給zone添加service時,不加 -permanent 參數添加的service存在內存中,重啓服務失效,如需永久有些,加上 -permanent 參數新增的service纔會寫入配置文件
zone配置模板路徑:
[root@linux zones]# ls /usr/lib/firewalld/zones block.xml dmz.xml drop.xml external.xml home.xml internal.xml public.xml trusted.xml work.xml
service配置模板路徑:
cd /usr/lib/firewalld/services/
#該目錄下的每一個文件都包含了對應的每一個服務的名稱、協議、端口號
案例:
需求:自定義ftp服務端口爲2121,work zone下面放行ftp
1.拷貝service配置模板到 /etc/firewalld/services/目錄下:
[root@linux ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
2.更改配置模板中端口號port爲2121:
[root@linux ~]# cat /etc/firewalld/services/ftp.xml <?xml version="1.0" encoding="utf-8"?> <service> <short>FTP</short> <description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description> <port protocol="tcp" port="2121"/> <module name="nf_conntrack_ftp"/> </service>
3.拷貝work模板到 /etc/firewalld/zones/目錄下:
[root@linux ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
4.添加ftp服務:
[root@linux ~]# vi /etc/firewalld/zones/work.xml [root@linux ~]# cat !$ cat /etc/firewalld/zones/work.xml <?xml version="1.0" encoding="utf-8"?> <zone> <short>Work</short> <description>For use in work areas. You mostly 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"/> <service name="ftp"/> </zone>
5.從新加載:
[root@linux ~]# firewall-cmd --reload success
6.查看work zone 的service:
[root@linux ~]# firewall-cmd --zone=work --list-services ssh dhcpv6-client ftp