firewalld
firewalld作为redhat7系统中变更对于netfilter内核模块的管理工具;firewalld加入了区域(zone)的概念。
区域就是firewalld预先准备了几套防火墙策略集合(策略模板),用户可以根据生产场景的不同而选择合适的策略集合,从而实现防火墙策略之间的快速切换。例如,我们有一台笔记本电脑,每天都要在办公室、咖啡厅和家里使用。
按常理来讲,这三者的安全性按照由高到低的顺序来排列,应该是家庭、公司办公室、咖啡厅。当前,我们希望为这台笔记本电脑指定如下防火墙策略规则:在家中允许访问所有服务;在办公室内仅允许访问文件共享服务;在咖啡厅仅允许上网浏览。
- 9个Zone说明
zone操作
[root@tanyvlinux zones]# firewall-cmd --get-zones #查看所有zones;
block dmz drop external home internal public trusted work
[root@tanyvlinux zones]# firewall-cmd --get-default-zone #查看默认zone;
public
[root@tanyvlinux zones]# firewall-cmd --set-default-zone=work #设置默认zone;
success
[root@tanyvlinux zones]# firewall-cmd --get-default-zone
work
[root@tanyvlinux zones]# firewall-cmd --get-zone-of-interface=ens37 #查看网卡属于哪个zone;
public
[root@tanyvlinux zones]# firewall-cmd --get-zone-of-interface=ens33
public
[root@tanyvlinux zones]# firewall-cmd --get-zone-of-interface=lo
no zone
[root@tanyvlinux zones]# firewall-cmd --zone=public --add-interface=lo #添加网卡到一个zone;
success
[root@tanyvlinux zones]# firewall-cmd --get-zone-of-interface=lo
public
[root@tanyvlinux zones]# firewall-cmd --zone=dmz --change-interface=lo #改变网卡的zone;
success
[root@tanyvlinux zones]# firewall-cmd --get-zone-of-interface=lo
dmz
[root@tanyvlinux zones]# firewall-cmd --get-active-zones #查看活动的zone;
dmz
interfaces: lo
public
interfaces: ens33 ens37
[root@tanyvlinux zones]# firewall-cmd --zone=dmz --remove-interface=lo #从一个zone中删除网卡;
success
[root@tanyvlinux zones]# firewall-cmd --get-zone-of-interface=lo
no zone
- zone的不同就在于zone里面的service不一样,代表各自放行的服务不一样
service操作
[root@tanyvlinux zones]# firewall-cmd --get-services #查看所有services;
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry docker-swarm dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls managesieve mdns minidlna mongodb mosh mountd ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server
[root@tanyvlinux zones]# firewall-cmd --list-services #当前zone放行的services;
ssh dhcpv6-client ftp
[root@tanyvlinux zones]# firewall-cmd --zone=public --add-service=http
#临时增加services,重启后需要重新添加;
success
[root@tanyvlinux zones]# firewall-cmd --list-services
ssh dhcpv6-client ftp http
[root@tanyvlinux zones]# firewall-cmd --zone=public --remove-service=http #删除services;
success
[root@tanyvlinux zones]# firewall-cmd --list-services
ssh dhcpv6-client ftp
[root@tanyvlinux zones]# cat /etc/firewalld/zones/public.xml #当前zone的配置文件;
<?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="ftp"/>
<port protocol="tcp" port="30060-30090"/>
<port protocol="tcp" port="80"/>
</zone>
[root@tanyvlinux zones]# firewall-cmd --zone=public --add-service=http --permanent
#使用permanent参数后,修改更新在配置文件里,重启后也会加载;
success
[root@tanyvlinux zones]# 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="ftp"/>
<service name="http"/> #增加了这一行;
<port protocol="tcp" port="30060-30090"/>
<port protocol="tcp" port="80"/>
</zone>
- 修改一个zone的配置情况,到有需要时随时切换;默认各个zone开放的服务很少;
方法是添加服务配置文件到/etc/firewalld/services目录里;添加zone配置文件到/etc/firewalld/zones目录里;修改zone配置文件引用一个servcie配置;重新加载;
服务的配置模板文件保存在/usr/lib/firewalld/services/里;
zone的配置模板文件保存在/usr/lib/firewalld/zones/里;
- 需求:ftp服务自定义端口1121,需要在work zone下面放行ftp
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
vi /etc/firewalld/services/ftp.xml //把21改为1121
cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
vi /etc/firewalld/zones/work.xml //增加一行 <service name="ftp"/>
firewall-cmd --reload //重新加载
firewall-cmd --zone=work --list-services //查看work zone放行的服务;