10.19 iptables規則備份和恢復
?
[root@hyc-01-01 ~]# service iptables save 保存iptables規則
該命令會將規則保存在/etc/sysconfig/iptables
?
將iptables規則備份到一個文件中
[root@hyc-01-01 ~]# iptables-save>/tmp/ipt.txt
將iptables規則備份到ipt.txt文件中
?
從備份規則的文件恢復iptables規則
[root@hyc-01-01 ~]# iptables-restore</tmp/ipt.txt
?
寫入/etc/sysconfig/iptables的規則會在開機時自動加載,其他備份文件的規則需要手動加載
?
10.20 firewalld的9個zone
?
[root@hyc-01-01 ~]# systemctl disable iptables 使netfilter殘廢
[root@hyc-01-01 ~]# systemctl stop iptables 停用netfilter
?
[root@hyc-01-01 ~]# systemctl enable firewalld 使能firewalld
[root@hyc-01-01 ~]# systemctl start firewalld 啟用firewalld
?
firewalld默認有9個ZONE,默認使用的ZONE是public;
每個ZONE都是一個規則集,包含多個規則;
查看所有的ZONE:
[root@hyc-01-01 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
查看默認的ZONE:
[root@hyc-01-01 ~]# firewall-cmd --get-default-zone
Public
?
ZONE的區別:
drop(丟棄):任何接收的網絡數據包都將被丟棄,無任何回復,僅能有發送出去的網絡連接
數據包無法進入,但可以出去,安全性最佳
block(限制)任何接收的網絡連接都被ipv4的icmp-host-prohibited信息和ipv6的icmp6-adm-prohibited信息拒絕
主要針對icmp數據包做限制
public(公共):在公共區域內使用,不相信網絡內其他計算機不會對本機造成危害,只接收經過選取的連接
部分數據包放行,部分數據包限制
external:(外部):適合為路由器啟用了偽裝功能的外部網,不相信來自網絡的其他計算機,只接收經過篩選的連接
dmz(非軍事區):用于非軍事區內的電腦,此區域可公開訪問,可以有限的進入內部網絡,僅接收經過篩選的連接
work(工作):用于工作區,可以基本相信網絡內的電腦不會對本機造成危害,接收經過篩選的連接
通常為公司內網
home(家庭):與work類似
internal(內部):用于內部網絡,可以基本相信網絡內的計算機不會對本機造成危害,接收經過篩選的連接
通常不連外網
trusted(信任):可接收所有網絡連接
無任何限制
?
10.21 firewalld關于zone的操作
?
指定默認的ZONE:
[root@hyc-01-01 ~]# firewall-cmd --set-default-zone=work
Error: COMMAND_FAILED
[root@hyc-01-01 ~]# firewall-cmd --set-default-zone=work
Warning: ZONE_ALREADY_SET: work
success
[root@hyc-01-01 ~]# firewall-cmd --get-default-zone
Work
?
ZONE可以針對網卡操作
查看指定網卡使用的ZONE
[root@hyc-01-01 ~]# firewall-cmd --get-zone-of-interface=ens33
public
[root@hyc-01-01 ~]# firewall-cmd --get-zone-of-interface=ens37
no zone
[root@hyc-01-01 ~]# firewall-cmd --get-zone-of-interface=lo
no zone
?
后來添加的ens37為no zone:
1 復制ens33為ens37并改寫配置文件,重啟網絡服務
[root@hyc-01-01 ~]# cd /etc/sysconfig/network-scripts
[root@hyc-01-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens37
[root@hyc-01-01 network-scripts]# vim ifcfg-ens37
[root@hyc-01-01 network-scripts]# systemctl restart network.service
2 重啟firewalld服務
[root@hyc-01-01 network-scripts]# systemctl restart firewalld
ens37使用work的ZONE
[root@hyc-01-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
work
這樣使某個沒有ZONE的網卡有了ZONE
?
修改某個網卡的ZONE:
[root@hyc-01-01 network-scripts]# firewall-cmd --zone=dmz --add-interface=ens37
The interface is under control of NetworkManager, setting zone to 'dmz'.
success
[root@hyc-01-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
Dmz
修改網卡ens37的ZONE為dmz
[root@hyc-01-01 network-scripts]# firewall-cmd --zone=public --add-interface=lo
success
[root@hyc-01-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
Public
將本來no zone的網卡增加ZONE為public
?
修改網卡的ZONE:
[root@hyc-01-01 network-scripts]# firewall-cmd --zone=block --change-interface=lo
success
[root@hyc-01-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
Block
?
刪除某個網卡的ZONE:
[root@hyc-01-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
block
[root@hyc-01-01 network-scripts]# firewall-cmd --zone=block --remove-interface=lo
success
[root@hyc-01-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
no zone
lo網卡的ZONE被刪除,變成no zone
[root@hyc-01-01 network-scripts]# firewall-cmd --zone=dmz --remove-interface=ens37
The interface is under control of NetworkManager, setting zone to default.
success
[root@hyc-01-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
work
[root@hyc-01-01 network-scripts]# firewall-cmd --get-default-zone
Work
ens37的ZONE被刪除,變成默認的ZONE
?
查看系統所有網卡所在的ZONE:
[root@hyc-01-01 network-scripts]# firewall-cmd --get-active-zones
work
? interfaces: ens33 ens37
block
? interfaces: l9
?
10.22 firewalld關于service的操作
Service是ZONE下面的一個子單元,可以理解為一個指定的端口(http操作80,https操作8080,ssh操作22…)
?
列出系統所有的service
[root@hyc-01-01 ~]# firewall-cmd --get-service
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 open*** 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
?
查看當前default ZONE中包含的service
[root@hyc-01-01 ~]# firewall-cmd --list-services
ssh dhcpv6-client
[root@hyc-01-01 ~]# firewall-cmd --get-default-zone
Work
?
查看指定ZONE包含的service
[root@hyc-01-01 ~]# firewall-cmd --zone=public --list-service
ssh dhcpv6-client
?
添加某個服務到某個ZONE下
[root@hyc-01-01 ~]# firewall-cmd --zone=public --add-service=http
success
[root@hyc-01-01 ~]# firewall-cmd --zone=public --list-service
ssh dhcpv6-client http
?
將ZONE添加的服務寫入配置文件
[root@hyc-01-01 ~]# firewall-cmd --zone=public --add-service=ftp --permanent
success
[root@hyc-01-01 ~]# 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"/>
</zone>
?
public.xml每修改一次都會將修改前的文件備份,備份文件名為public.xml.old
?
ZONE和service都有自己的模板
[root@hyc-01-01 ~]# ls /usr/lib/firewalld/zones
block.xml? drop.xml????? home.xml????? public.xml?? work.xml
dmz.xml??? external.xml? internal.xml? trusted.xml
[root@hyc-01-01 ~]# ls /usr/lib/firewalld/services
…
?
需求:將ftp服務端口改為1121,并在work zone下放行ftp
1 將ftp的service模板拷貝到/etc/firewalld/services/
[root@hyc-01-01 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
2 將配置文件中21端口改為1121
[root@hyc-01-01 ~]# vim /etc/firewalld/services/ftp.xml
3 將模板文件work.xml復制到/etc/firewalld/zones下
[root@hyc-01-01 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones
4 在/etc/firewalld/zones/work.xml中增加關于service ftp的一行
[root@hyc-01-01 ~]# vim /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 重加載firewall-cmd服務
[root@hyc-01-01 ~]# firewall-cmd --reload
Success
6 檢查
[root@hyc-01-01 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp
?
ZONE是防火墻的規則集合,每個zone下會有多個iptables規則;
每個ZONE下有service,這些service會被作為白名單被ZONE放行;
?
?
轉載于:https://blog.51cto.com/12216458/2145078