Linux8.4 firewalld
firewalld的9个zone
禁iptables 打开firewalld
[root@chyuanliuNJ ~]# systemctl disable iptables Removed symlink /etc/systemd/system/basic.target.wants/iptables.service. [root@chyuanliuNJ ~]# systemctl stop iptables [root@chyuanliuNJ ~]# systemctl enable firewalld Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service. Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service. [root@chyuanliuNJ ~]# systemctl start firewalld
使用iptables -nvL 查看规则,发现有好多,都是firewalld的默认规则。也就是iptables -nvL也可以查看firewalld规则。
每个zone好比一个规则集,自带了好多规则。
#查看所有的zone [root@chyuanliuNJ ~]# firewall-cmd --get-zones block dmz drop external home internal public trusted work #查看默认的zone [root@chyuanliuNJ ~]# firewall-cmd --get-default-zone public
9个zone: drop:(丢弃),任何接受的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。 block:(限制)拒绝所有外部连接,允许内部发起的连接,任何接受的网络连接都被IPV4的icmp-host-prohibited信息和ipv6的icmp6-adm-prohibited信息所拒绝。 public:(公共)在公共区域内使用,不能相信网络内的其他计算机不会对你的计算造成危害,只能接受经过选取的连接。 external:(外部)特别是为路由器启用了伪装功能的外部网。你不能信任来自网络的其他计算,不嫩更相信他们不会对你的计算机造成危害,只能接受经过选择的连接。 dmz:(非军事区)用于你的非军事区内的电脑,此区域内可公开访问,可以有限地进入你的内部网络,仅仅接受经过选择的连接。 work:(工作)用于工作区。你可以基本相信网络内的其他电脑不会危害你的电脑。仅仅接受经过选择的连接。 home:(家庭)用于家庭网络。你可以基本相信网络内的其他计算机不会危害你的计算机。仅仅接受经过选择的连接。 internal:(内部)用于内部网络,你可以基本信任网络内的其他计算机不会威胁你的计算机,仅仅接受经过选择的连接。 trusted:(信任)可接受所有的网络连接 firewalld的终端管理工具是 firewall-cmd firewalld默认配置文件有两个:/usr/lib/firewalld/ (系统配置,尽量不要修改)和 /etc/firewalld/ (用户配置地址)
关于zone的操作
修改默认的zone
[root@chyuanliuNJ ~]# firewall-cmd --set-default-zone=work success [root@chyuanliuNJ ~]# firewall-cmd --get-default-zone work
查指定网卡
[root@chyuanliuNJ ~]# firewall-cmd --get-zone-of-interface=eth0 no zone [root@chyuanliuNJ ~]# firewall-cmd --get-zone-of-interface=lo no zone #全部都是no zone,需要对网卡配置文件进行设置 [root@chyuanliuNJ ~]# firewall-cmd --zone=work --add-interface=eth0 success [root@chyuanliuNJ ~]# firewall-cmd --get-zone-of-interface=eth0 work #修改网卡的zone [root@chyuanliuNJ ~]# firewall-cmd --zone=dmz --change-interface=eht0 success [root@chyuanliuNJ ~]# firewall-cmd --get-zone-of-interface=eth0 dmz #针对网卡删除zone [root@chyuanliuNJ ~]# firewall-cmd --zone=dmz --remove-interface=eth0 success [root@chyuanliuNJ ~]# firewall-cmd --get-zone-of-interface=eth0 no zone
查看系统所有网卡所在zone
[root@chyuanliuNJ ~]# firewall-cmd --get-active-zones dmz interfaces: eht0 work interfaces: eth0
关于service的操作
9种zone,每个zone里面都是用了不同的service,service可以理解为zone下面的子单元或者一个端口,而service就是针对一个服务(端口)做的iptables规则。这些service都是由一个个配置文件定义的,配置文件的模板在/usr/lib/firewalld/services/目录下,真正生效的配置在/etc/firewalld/services/目录下(默认为空)
查看系统所有service
[root@chyuanliuNJ ~]# firewall-cmd --get-services RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server
查看当前zone内有哪些service
[root@chyuanliuNJ ~]# firewall-cmd --list-service ssh dhcpv6-client
指定zone查看其service
[root@chyuanliuNJ ~]# firewall-cmd --zone=public --list-service dhcpv6-client ssh
给zone添加service
[root@chyuanliuNJ ~]# firewall-cmd --zone=public --add-service=http success [root@chyuanliuNJ ~]# firewall-cmd --zone=public --list-service dhcpv6-client ssh http #永久添加服务到配置文件中 [root@chyuanliuNJ ~]# firewall-cmd --zone=public --add-service=ftp --permanent success #查看配置文件,有ftp没有http [root@chyuanliuNJ ~]# 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="dhcpv6-client"/> <service name="ssh"/> <service name="ftp"/> </zone>
需求:ftp服务自定义端口1121,需要在work zone下面放行ftp
[root@chyuanliuNJ ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/ [root@chyuanliuNJ ~]# vi /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="1121"/> #修改这里的端口号 <module name="nf_conntrack_ftp"/> </service> ~ [root@chyuanliuNJ ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/ [root@chyuanliuNJ ~]# vi /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"/> #添加这一行ftp服务 </zone> [root@chyuanliuNJ ~]# firewall-cmd --reload success [root@chyuanliuNJ ~]# firewall-cmd --zone=work --list-services ssh dhcpv6-client ftp