firewalld的防火墙
生活随笔
收集整理的這篇文章主要介紹了
firewalld的防火墙
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
firewalld的介紹與簡單應用
CentOS7的默認防火墻是firewalld,在之前使用iptables時,關閉了firewalld服務,現在反過來關閉iptables服務,打開firewalld服務: systemctl disable iptables systemctl stop iptables systemctl enable firewalld systemctl start firewalld打開firewalld服務之后,iptables相關的命令其實也可以繼續使用的,但在CentOS7中不用那么操作,因為firewalld有自己的命令。 firewalld有兩個基礎概念,zone和service,每個zone里面有不同的iptables規則,默認一共有9個zone,CentOS7默認zone為public,查看系統所有zone,使用命令 firewall-cmd --get-zones //查看所有zone firewall-cmd --get-default-zone //查看默認zone 9個zone的簡單介紹:drop(丟棄):任何接受的網絡數據包都被丟棄,沒有任何回復。僅能有發送出去的網絡連接。 block(限制):任何接受的網絡連接被IPv4的 icmp-host-prohibited 信息和 IPv6 的icmp6-adm-prohibited 信息所拒絕。 public (公共) :在公共區域內使用,不能相信網絡內的其他計算機不會對你的計算機造成危害,只能接收經過選取的連接。 external (外部):特別是為路由器啟用了偽裝功能的外部網。你不能信任來自網絡的其他計算,不能相信它們不會對你的計算機造成危害,只能接收經過選擇的連接。 dmz(非軍事區):用于的非軍事區內的電腦,此區域內可公開訪問,可以有限地進入你的內部網絡,僅僅接收經過選擇的連接。 work(工作):用于工作區。你可以基本相信網絡內的其他電腦不會危害你的電腦。僅僅接收經過選擇的連接。 home(家庭):用于家庭網絡。你可以基本信任網絡內的其他計算機不會危害你的計算機。僅僅接收經過選擇的連接。 internal(內部):用于內部網絡。你可以基本上信任網絡內的其他計算機不會威脅你的計算機。僅僅接收經過選擇的連接。 trusted(信任):可接受所有的網絡連接幾個關于zone的命令 # firewall-cmd --set-default-zone=work //設定默認zone # firewall-cmd --get-zone-of-interface=ens33 //查看指定網卡 # firewall-cmd --zone=public --add-interface=lo //給指定網卡設置zone # firewall-cmd --zone=dmz --change-interface=lo //針對網卡更改zone # firewall-cmd --zone=dmz --remove-interface=lo //針對網卡刪除zone # firewall-cmd --get-zctive-zones //查看系統所有網卡所在的zone fired中的serviceservice 就是zone下面的一個子單元,每個zone里都使用了不同的service,而service就是針對服務(端口)做的iptables規則。 firewall-cmd --get-services //查看當前系統所有service 這些service都是由一個個配置文件定義的,配置文件的模版子啊/usr/lib/firewalld/services/目錄下,真正生效的配置文件則在/etc/firewalld/services/目錄下(默認為空)關于service的一些操作命令: # firewall-cmd --get-services //查看所有的servies # firewall-cmd --list-services //查看當前zone下有哪些service # firewall-cmd --zone=public --add-service=http //把http服務增加到public zone下面 # firewall-cmd --zone=public --remove-service=http //把http服務移除 # ls /usr/lib/firewalld/zones/ //zone的配置文件模板 # firewall-cmd --zone=public --add-service=http --permanent //更改配置文件,否則只是保存在內存中,之后會在/etc/firewalld/zones目錄下面生成配置文件 實例: 需求:ftp服務自定義端口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相關配置<?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一些常用的語句 查看默認的zone,結果為trusted firewall-cmd --get-default-zone public 設置默認zone為trusted firewall-cmd --set-default-zone=trusted success 顯示當前正在使用的zone消息 firewall-cmd --get-active-zones trustedinterfaces: eno16777736 顯示系統預定義的zone,默認是9個 firewall-cmd --get-zones block dmz drop external home internal public trusted work 顯示系統預定義的服務名稱: 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匹配,網卡與trusted匹配,則該網卡的流量執行eno16中定義的規則,默認允許訪問所以的服務: firewall-cmd --get-zone-of-interface=eno16777736 trusted 顯示所以zone及其相對應的規則信息 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服務的規則 firewall-cmd --add-service=ftp --zone=public success 從public這個zone中刪除允許訪問ftp服務的規則 firewall-cmd --remove-service=ftp --zone=public success 在public這個zone中添加允許訪問3306端口的規則 firewall-cmd --add-port=3306/tcp --zone=public 從public這個zone中刪除允許訪問3306端口的規則 firewall-cmd --remove-port=3306/tcp --zone=public success 將eno16網卡和public綁定,以后從該接口進入的流量,匹配到public中的規則 firewall-cmd --add-interface=eno16 --zone=public success 將eno16網卡與public解除綁定 firewall-cmd --remove-interface=eno16 --zone trusted 將源ip地址1.1.1.1與public綁定,以后該主機訪問本機時匹配public中的規則 firewall-cmd --add-source=1.1.1.1 --zone=public success 在public這個zone中添加一條永久規則(允許訪問3306端口),規則重啟防火墻后依然生效 firewall-cmd --permanent --add-port=3306/tcp --zone=public 重新加載讀取防火墻規則 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、查看配置結果 firewall-cmd --list-all 5、刪除規則 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的默認防火墻是firewalld,在之前使用iptables時,關閉了firewalld服務,現在反過來關閉iptables服務,打開firewalld服務: systemctl disable iptables systemctl stop iptables systemctl enable firewalld systemctl start firewalld打開firewalld服務之后,iptables相關的命令其實也可以繼續使用的,但在CentOS7中不用那么操作,因為firewalld有自己的命令。 firewalld有兩個基礎概念,zone和service,每個zone里面有不同的iptables規則,默認一共有9個zone,CentOS7默認zone為public,查看系統所有zone,使用命令 firewall-cmd --get-zones //查看所有zone firewall-cmd --get-default-zone //查看默認zone 9個zone的簡單介紹:drop(丟棄):任何接受的網絡數據包都被丟棄,沒有任何回復。僅能有發送出去的網絡連接。 block(限制):任何接受的網絡連接被IPv4的 icmp-host-prohibited 信息和 IPv6 的icmp6-adm-prohibited 信息所拒絕。 public (公共) :在公共區域內使用,不能相信網絡內的其他計算機不會對你的計算機造成危害,只能接收經過選取的連接。 external (外部):特別是為路由器啟用了偽裝功能的外部網。你不能信任來自網絡的其他計算,不能相信它們不會對你的計算機造成危害,只能接收經過選擇的連接。 dmz(非軍事區):用于的非軍事區內的電腦,此區域內可公開訪問,可以有限地進入你的內部網絡,僅僅接收經過選擇的連接。 work(工作):用于工作區。你可以基本相信網絡內的其他電腦不會危害你的電腦。僅僅接收經過選擇的連接。 home(家庭):用于家庭網絡。你可以基本信任網絡內的其他計算機不會危害你的計算機。僅僅接收經過選擇的連接。 internal(內部):用于內部網絡。你可以基本上信任網絡內的其他計算機不會威脅你的計算機。僅僅接收經過選擇的連接。 trusted(信任):可接受所有的網絡連接幾個關于zone的命令 # firewall-cmd --set-default-zone=work //設定默認zone # firewall-cmd --get-zone-of-interface=ens33 //查看指定網卡 # firewall-cmd --zone=public --add-interface=lo //給指定網卡設置zone # firewall-cmd --zone=dmz --change-interface=lo //針對網卡更改zone # firewall-cmd --zone=dmz --remove-interface=lo //針對網卡刪除zone # firewall-cmd --get-zctive-zones //查看系統所有網卡所在的zone fired中的serviceservice 就是zone下面的一個子單元,每個zone里都使用了不同的service,而service就是針對服務(端口)做的iptables規則。 firewall-cmd --get-services //查看當前系統所有service 這些service都是由一個個配置文件定義的,配置文件的模版子啊/usr/lib/firewalld/services/目錄下,真正生效的配置文件則在/etc/firewalld/services/目錄下(默認為空)關于service的一些操作命令: # firewall-cmd --get-services //查看所有的servies # firewall-cmd --list-services //查看當前zone下有哪些service # firewall-cmd --zone=public --add-service=http //把http服務增加到public zone下面 # firewall-cmd --zone=public --remove-service=http //把http服務移除 # ls /usr/lib/firewalld/zones/ //zone的配置文件模板 # firewall-cmd --zone=public --add-service=http --permanent //更改配置文件,否則只是保存在內存中,之后會在/etc/firewalld/zones目錄下面生成配置文件 實例: 需求:ftp服務自定義端口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相關配置<?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一些常用的語句 查看默認的zone,結果為trusted firewall-cmd --get-default-zone public 設置默認zone為trusted firewall-cmd --set-default-zone=trusted success 顯示當前正在使用的zone消息 firewall-cmd --get-active-zones trustedinterfaces: eno16777736 顯示系統預定義的zone,默認是9個 firewall-cmd --get-zones block dmz drop external home internal public trusted work 顯示系統預定義的服務名稱: 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匹配,網卡與trusted匹配,則該網卡的流量執行eno16中定義的規則,默認允許訪問所以的服務: firewall-cmd --get-zone-of-interface=eno16777736 trusted 顯示所以zone及其相對應的規則信息 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服務的規則 firewall-cmd --add-service=ftp --zone=public success 從public這個zone中刪除允許訪問ftp服務的規則 firewall-cmd --remove-service=ftp --zone=public success 在public這個zone中添加允許訪問3306端口的規則 firewall-cmd --add-port=3306/tcp --zone=public 從public這個zone中刪除允許訪問3306端口的規則 firewall-cmd --remove-port=3306/tcp --zone=public success 將eno16網卡和public綁定,以后從該接口進入的流量,匹配到public中的規則 firewall-cmd --add-interface=eno16 --zone=public success 將eno16網卡與public解除綁定 firewall-cmd --remove-interface=eno16 --zone trusted 將源ip地址1.1.1.1與public綁定,以后該主機訪問本機時匹配public中的規則 firewall-cmd --add-source=1.1.1.1 --zone=public success 在public這個zone中添加一條永久規則(允許訪問3306端口),規則重啟防火墻后依然生效 firewall-cmd --permanent --add-port=3306/tcp --zone=public 重新加載讀取防火墻規則 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、查看配置結果 firewall-cmd --list-all 5、刪除規則 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"
?
轉載于:https://www.cnblogs.com/fengzhongzhuzu/p/10070098.html
總結
以上是生活随笔為你收集整理的firewalld的防火墙的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS3展开带弹性动画的手风琴菜单
- 下一篇: 分组,命名分组,url的命名和反向解析