firewalld的防火墙
生活随笔
收集整理的這篇文章主要介紹了
firewalld的防火墙
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
firewalld的介紹與簡單應(yīng)用
CentOS7的默認(rèn)防火墻是firewalld,在之前使用iptables時,關(guān)閉了firewalld服務(wù),現(xiàn)在反過來關(guān)閉iptables服務(wù),打開firewalld服務(wù): systemctl disable iptables systemctl stop iptables systemctl enable firewalld systemctl start firewalld打開firewalld服務(wù)之后,iptables相關(guān)的命令其實(shí)也可以繼續(xù)使用的,但在CentOS7中不用那么操作,因?yàn)閒irewalld有自己的命令。 firewalld有兩個基礎(chǔ)概念,zone和service,每個zone里面有不同的iptables規(guī)則,默認(rèn)一共有9個zone,CentOS7默認(rèn)zone為public,查看系統(tǒng)所有zone,使用命令 firewall-cmd --get-zones //查看所有zone firewall-cmd --get-default-zone //查看默認(rèn)zone 9個zone的簡單介紹:drop(丟棄):任何接受的網(wǎng)絡(luò)數(shù)據(jù)包都被丟棄,沒有任何回復(fù)。僅能有發(fā)送出去的網(wǎng)絡(luò)連接。 block(限制):任何接受的網(wǎng)絡(luò)連接被IPv4的 icmp-host-prohibited 信息和 IPv6 的icmp6-adm-prohibited 信息所拒絕。 public (公共) :在公共區(qū)域內(nèi)使用,不能相信網(wǎng)絡(luò)內(nèi)的其他計(jì)算機(jī)不會對你的計(jì)算機(jī)造成危害,只能接收經(jīng)過選取的連接。 external (外部):特別是為路由器啟用了偽裝功能的外部網(wǎng)。你不能信任來自網(wǎng)絡(luò)的其他計(jì)算,不能相信它們不會對你的計(jì)算機(jī)造成危害,只能接收經(jīng)過選擇的連接。 dmz(非軍事區(qū)):用于的非軍事區(qū)內(nèi)的電腦,此區(qū)域內(nèi)可公開訪問,可以有限地進(jìn)入你的內(nèi)部網(wǎng)絡(luò),僅僅接收經(jīng)過選擇的連接。 work(工作):用于工作區(qū)。你可以基本相信網(wǎng)絡(luò)內(nèi)的其他電腦不會危害你的電腦。僅僅接收經(jīng)過選擇的連接。 home(家庭):用于家庭網(wǎng)絡(luò)。你可以基本信任網(wǎng)絡(luò)內(nèi)的其他計(jì)算機(jī)不會危害你的計(jì)算機(jī)。僅僅接收經(jīng)過選擇的連接。 internal(內(nèi)部):用于內(nèi)部網(wǎng)絡(luò)。你可以基本上信任網(wǎng)絡(luò)內(nèi)的其他計(jì)算機(jī)不會威脅你的計(jì)算機(jī)。僅僅接收經(jīng)過選擇的連接。 trusted(信任):可接受所有的網(wǎng)絡(luò)連接幾個關(guān)于zone的命令 # firewall-cmd --set-default-zone=work //設(shè)定默認(rèn)zone # firewall-cmd --get-zone-of-interface=ens33 //查看指定網(wǎng)卡 # firewall-cmd --zone=public --add-interface=lo //給指定網(wǎng)卡設(shè)置zone # firewall-cmd --zone=dmz --change-interface=lo //針對網(wǎng)卡更改zone # firewall-cmd --zone=dmz --remove-interface=lo //針對網(wǎng)卡刪除zone # firewall-cmd --get-zctive-zones //查看系統(tǒng)所有網(wǎng)卡所在的zone fired中的serviceservice 就是zone下面的一個子單元,每個zone里都使用了不同的service,而service就是針對服務(wù)(端口)做的iptables規(guī)則。 firewall-cmd --get-services //查看當(dāng)前系統(tǒng)所有service 這些service都是由一個個配置文件定義的,配置文件的模版子啊/usr/lib/firewalld/services/目錄下,真正生效的配置文件則在/etc/firewalld/services/目錄下(默認(rèn)為空)關(guān)于service的一些操作命令: # firewall-cmd --get-services //查看所有的servies # firewall-cmd --list-services //查看當(dāng)前zone下有哪些service # firewall-cmd --zone=public --add-service=http //把http服務(wù)增加到public zone下面 # firewall-cmd --zone=public --remove-service=http //把http服務(wù)移除 # ls /usr/lib/firewalld/zones/ //zone的配置文件模板 # firewall-cmd --zone=public --add-service=http --permanent //更改配置文件,否則只是保存在內(nèi)存中,之后會在/etc/firewalld/zones目錄下面生成配置文件 實(shí)例: 需求:ftp服務(wù)自定義端口1121,需要在work zone下面放行ftp。 cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/vi /etc/firewalld/services/ftp.xml 修改ftp端口號<?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"/> ##修改端口為1121<module name="nf_conntrack_ftp"/> </service> [root@zlinux ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/ [root@zlinux ~]# vi /etc/firewalld/zones/work.xml //增加ftp相關(guān)配置<?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@zlinux ~]# firewall-cmd --reload //重新加載 success [root@zlinux ~]# firewall-cmd --zone=work --list-services //查看是否有ftp ssh dhcpv6-client ftp firewalld一些常用的語句 查看默認(rèn)的zone,結(jié)果為trusted firewall-cmd --get-default-zone public 設(shè)置默認(rèn)zone為trusted firewall-cmd --set-default-zone=trusted success 顯示當(dāng)前正在使用的zone消息 firewall-cmd --get-active-zones trustedinterfaces: eno16777736 顯示系統(tǒng)預(yù)定義的zone,默認(rèn)是9個 firewall-cmd --get-zones block dmz drop external home internal public trusted work 顯示系統(tǒng)預(yù)定義的服務(wù)名稱: firewall-cmd --get-services amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https 查詢eno16接口和那個zone匹配,網(wǎng)卡與trusted匹配,則該網(wǎng)卡的流量執(zhí)行eno16中定義的規(guī)則,默認(rèn)允許訪問所以的服務(wù): firewall-cmd --get-zone-of-interface=eno16777736 trusted 顯示所以zone及其相對應(yīng)的規(guī)則信息 firewall-cmd --list-all-zone blockinterfaces: sources: services: ports: masquerade: noforward-ports: icmp-blocks: rich rules: dmzinterfaces: sources: services: sshports: masquerade: noforward-ports: icmp-blocks: rich rules: ....... 在public這個zone中添加允許訪問ftp服務(wù)的規(guī)則 firewall-cmd --add-service=ftp --zone=public success 從public這個zone中刪除允許訪問ftp服務(wù)的規(guī)則 firewall-cmd --remove-service=ftp --zone=public success 在public這個zone中添加允許訪問3306端口的規(guī)則 firewall-cmd --add-port=3306/tcp --zone=public 從public這個zone中刪除允許訪問3306端口的規(guī)則 firewall-cmd --remove-port=3306/tcp --zone=public success 將eno16網(wǎng)卡和public綁定,以后從該接口進(jìn)入的流量,匹配到public中的規(guī)則 firewall-cmd --add-interface=eno16 --zone=public success 將eno16網(wǎng)卡與public解除綁定 firewall-cmd --remove-interface=eno16 --zone trusted 將源ip地址1.1.1.1與public綁定,以后該主機(jī)訪問本機(jī)時匹配public中的規(guī)則 firewall-cmd --add-source=1.1.1.1 --zone=public success 在public這個zone中添加一條永久規(guī)則(允許訪問3306端口),規(guī)則重啟防火墻后依然生效 firewall-cmd --permanent --add-port=3306/tcp --zone=public 重新加載讀取防火墻規(guī)則 firewall-cmd --reload firewall指定端口和ip訪問
1、啟動防火墻 systemctl start firewalld.service 2、指定IP與端口 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="5432" accept" 3、重新載入,使配置生效 systemctl restart firewalld.service 4、查看配置結(jié)果 firewall-cmd --list-all 5、刪除規(guī)則 firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="11300" accept" 6.對指定的IP開放指定的端口段 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="30000-31000" accept"
CentOS7的默認(rèn)防火墻是firewalld,在之前使用iptables時,關(guān)閉了firewalld服務(wù),現(xiàn)在反過來關(guān)閉iptables服務(wù),打開firewalld服務(wù): systemctl disable iptables systemctl stop iptables systemctl enable firewalld systemctl start firewalld打開firewalld服務(wù)之后,iptables相關(guān)的命令其實(shí)也可以繼續(xù)使用的,但在CentOS7中不用那么操作,因?yàn)閒irewalld有自己的命令。 firewalld有兩個基礎(chǔ)概念,zone和service,每個zone里面有不同的iptables規(guī)則,默認(rèn)一共有9個zone,CentOS7默認(rèn)zone為public,查看系統(tǒng)所有zone,使用命令 firewall-cmd --get-zones //查看所有zone firewall-cmd --get-default-zone //查看默認(rèn)zone 9個zone的簡單介紹:drop(丟棄):任何接受的網(wǎng)絡(luò)數(shù)據(jù)包都被丟棄,沒有任何回復(fù)。僅能有發(fā)送出去的網(wǎng)絡(luò)連接。 block(限制):任何接受的網(wǎng)絡(luò)連接被IPv4的 icmp-host-prohibited 信息和 IPv6 的icmp6-adm-prohibited 信息所拒絕。 public (公共) :在公共區(qū)域內(nèi)使用,不能相信網(wǎng)絡(luò)內(nèi)的其他計(jì)算機(jī)不會對你的計(jì)算機(jī)造成危害,只能接收經(jīng)過選取的連接。 external (外部):特別是為路由器啟用了偽裝功能的外部網(wǎng)。你不能信任來自網(wǎng)絡(luò)的其他計(jì)算,不能相信它們不會對你的計(jì)算機(jī)造成危害,只能接收經(jīng)過選擇的連接。 dmz(非軍事區(qū)):用于的非軍事區(qū)內(nèi)的電腦,此區(qū)域內(nèi)可公開訪問,可以有限地進(jìn)入你的內(nèi)部網(wǎng)絡(luò),僅僅接收經(jīng)過選擇的連接。 work(工作):用于工作區(qū)。你可以基本相信網(wǎng)絡(luò)內(nèi)的其他電腦不會危害你的電腦。僅僅接收經(jīng)過選擇的連接。 home(家庭):用于家庭網(wǎng)絡(luò)。你可以基本信任網(wǎng)絡(luò)內(nèi)的其他計(jì)算機(jī)不會危害你的計(jì)算機(jī)。僅僅接收經(jīng)過選擇的連接。 internal(內(nèi)部):用于內(nèi)部網(wǎng)絡(luò)。你可以基本上信任網(wǎng)絡(luò)內(nèi)的其他計(jì)算機(jī)不會威脅你的計(jì)算機(jī)。僅僅接收經(jīng)過選擇的連接。 trusted(信任):可接受所有的網(wǎng)絡(luò)連接幾個關(guān)于zone的命令 # firewall-cmd --set-default-zone=work //設(shè)定默認(rèn)zone # firewall-cmd --get-zone-of-interface=ens33 //查看指定網(wǎng)卡 # firewall-cmd --zone=public --add-interface=lo //給指定網(wǎng)卡設(shè)置zone # firewall-cmd --zone=dmz --change-interface=lo //針對網(wǎng)卡更改zone # firewall-cmd --zone=dmz --remove-interface=lo //針對網(wǎng)卡刪除zone # firewall-cmd --get-zctive-zones //查看系統(tǒng)所有網(wǎng)卡所在的zone fired中的serviceservice 就是zone下面的一個子單元,每個zone里都使用了不同的service,而service就是針對服務(wù)(端口)做的iptables規(guī)則。 firewall-cmd --get-services //查看當(dāng)前系統(tǒng)所有service 這些service都是由一個個配置文件定義的,配置文件的模版子啊/usr/lib/firewalld/services/目錄下,真正生效的配置文件則在/etc/firewalld/services/目錄下(默認(rèn)為空)關(guān)于service的一些操作命令: # firewall-cmd --get-services //查看所有的servies # firewall-cmd --list-services //查看當(dāng)前zone下有哪些service # firewall-cmd --zone=public --add-service=http //把http服務(wù)增加到public zone下面 # firewall-cmd --zone=public --remove-service=http //把http服務(wù)移除 # ls /usr/lib/firewalld/zones/ //zone的配置文件模板 # firewall-cmd --zone=public --add-service=http --permanent //更改配置文件,否則只是保存在內(nèi)存中,之后會在/etc/firewalld/zones目錄下面生成配置文件 實(shí)例: 需求:ftp服務(wù)自定義端口1121,需要在work zone下面放行ftp。 cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/vi /etc/firewalld/services/ftp.xml 修改ftp端口號<?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"/> ##修改端口為1121<module name="nf_conntrack_ftp"/> </service> [root@zlinux ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/ [root@zlinux ~]# vi /etc/firewalld/zones/work.xml //增加ftp相關(guān)配置<?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@zlinux ~]# firewall-cmd --reload //重新加載 success [root@zlinux ~]# firewall-cmd --zone=work --list-services //查看是否有ftp ssh dhcpv6-client ftp firewalld一些常用的語句 查看默認(rèn)的zone,結(jié)果為trusted firewall-cmd --get-default-zone public 設(shè)置默認(rèn)zone為trusted firewall-cmd --set-default-zone=trusted success 顯示當(dāng)前正在使用的zone消息 firewall-cmd --get-active-zones trustedinterfaces: eno16777736 顯示系統(tǒng)預(yù)定義的zone,默認(rèn)是9個 firewall-cmd --get-zones block dmz drop external home internal public trusted work 顯示系統(tǒng)預(yù)定義的服務(wù)名稱: firewall-cmd --get-services amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https 查詢eno16接口和那個zone匹配,網(wǎng)卡與trusted匹配,則該網(wǎng)卡的流量執(zhí)行eno16中定義的規(guī)則,默認(rèn)允許訪問所以的服務(wù): firewall-cmd --get-zone-of-interface=eno16777736 trusted 顯示所以zone及其相對應(yīng)的規(guī)則信息 firewall-cmd --list-all-zone blockinterfaces: sources: services: ports: masquerade: noforward-ports: icmp-blocks: rich rules: dmzinterfaces: sources: services: sshports: masquerade: noforward-ports: icmp-blocks: rich rules: ....... 在public這個zone中添加允許訪問ftp服務(wù)的規(guī)則 firewall-cmd --add-service=ftp --zone=public success 從public這個zone中刪除允許訪問ftp服務(wù)的規(guī)則 firewall-cmd --remove-service=ftp --zone=public success 在public這個zone中添加允許訪問3306端口的規(guī)則 firewall-cmd --add-port=3306/tcp --zone=public 從public這個zone中刪除允許訪問3306端口的規(guī)則 firewall-cmd --remove-port=3306/tcp --zone=public success 將eno16網(wǎng)卡和public綁定,以后從該接口進(jìn)入的流量,匹配到public中的規(guī)則 firewall-cmd --add-interface=eno16 --zone=public success 將eno16網(wǎng)卡與public解除綁定 firewall-cmd --remove-interface=eno16 --zone trusted 將源ip地址1.1.1.1與public綁定,以后該主機(jī)訪問本機(jī)時匹配public中的規(guī)則 firewall-cmd --add-source=1.1.1.1 --zone=public success 在public這個zone中添加一條永久規(guī)則(允許訪問3306端口),規(guī)則重啟防火墻后依然生效 firewall-cmd --permanent --add-port=3306/tcp --zone=public 重新加載讀取防火墻規(guī)則 firewall-cmd --reload firewall指定端口和ip訪問
1、啟動防火墻 systemctl start firewalld.service 2、指定IP與端口 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="5432" accept" 3、重新載入,使配置生效 systemctl restart firewalld.service 4、查看配置結(jié)果 firewall-cmd --list-all 5、刪除規(guī)則 firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="11300" accept" 6.對指定的IP開放指定的端口段 firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="30000-31000" accept"
?
轉(zhuǎn)載于:https://www.cnblogs.com/fengzhongzhuzu/p/10070098.html
總結(jié)
以上是生活随笔為你收集整理的firewalld的防火墙的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS3展开带弹性动画的手风琴菜单
- 下一篇: 分组,命名分组,url的命名和反向解析