Linux学习整理-网络防火墙firewalld
1 概念
1-1. firewall簡(jiǎn)介
firewalld是Linux系統(tǒng)的一款防火墻管理工具。通過(guò)充當(dāng)netfilter的前端來(lái)提供防火墻功能,默認(rèn)后端是iptables/ntftables。開(kāi)發(fā)語(yǔ)言是python。firewalld=firewall+daemon。實(shí)際上就是一個(gè)守護(hù)進(jìn)程。操作firewalld的是firewalld-cmd命令。
CentOS Ubuntu★ openSUSE+--------------+ +------------+ +---------------------+ -*-| firewall-cmd | | ufw | | SUSEfirewall2 | |+--------------+ +------------+ +---------------------+ |用戶空間+-----------------------------------------------------------+ || iptables command | |+-----------------------------------------------------------+ -*-+-----------------------------------------------------------+ -*-| | || OS(Netfilter) | 內(nèi)核空間| | |+-----------------------------------------------------------+ -*-firewalld主要是在centos7以后才作為默認(rèn)的防火墻管理工具的。像Ubuntu,Debian也可以安裝firewalld。
- firewall-cmd 是一個(gè)命令行的客戶端,通過(guò)D-Bus來(lái)操作后端。
- firewall-config是一個(gè)圖形界面的客戶端,也是通過(guò)D-Bus來(lái)操作后端。
- firewall-applet也是一個(gè)圖形界面客戶端,也是通過(guò)D-Bus來(lái)操作后端。
firewall-applet是firewalld中可選的狀態(tài)欄小圖標(biāo)程序,它能夠顯示防火墻日志通知,同時(shí)也是一種打開(kāi) firewall-config 的快捷方式
- firewall-offline-cmd是一個(gè)命令行客戶端,不需要通過(guò)D-Bus來(lái)操作后端。
firewalld?的配置儲(chǔ)存在?/usr/lib/firewalld/?和?/etc/firewalld/?里的各種 XML 文件里,這樣保持了這些文件被編輯、寫(xiě)入、備份的極大的靈活性,使之可作為其他安裝的備份等等。
1-2. 運(yùn)行時(shí)和永久
- 運(yùn)行時(shí)(runtime)
firewalld配置的防火墻策略默認(rèn)為運(yùn)行時(shí)(Runtime)模式,就是設(shè)置完立刻生效,但是系統(tǒng)的重啟會(huì)導(dǎo)致失效。要想設(shè)置完立刻生效,重啟也不失效需要--runtime-to-permanent屬性。
- 永久(permanent)
如果想讓配置策略一直存在,方法就是在firewall-cmd命令后面添加–permanent屬性,這樣配置的防火墻策略就可以永久生效了。但是,永久生效模式有一個(gè)“不近人情”的特點(diǎn),就是使用它設(shè)置的策略只有在系統(tǒng)重啟后才會(huì)生效。如果想讓配置的永久策略立即生效,需要手動(dòng)執(zhí)行firewall-cmd --reload命令。
1-3. 對(duì)網(wǎng)絡(luò)區(qū)域的理解
firewalld的網(wǎng)絡(luò)區(qū)域可以理解成windows聯(lián)網(wǎng)時(shí),彈出來(lái)的對(duì)話框,讓選擇網(wǎng)絡(luò)。
?
- drop(丟棄)
任何接收的網(wǎng)絡(luò)數(shù)據(jù)包都被丟棄,沒(méi)有任何回復(fù)。僅能有發(fā)送出去的網(wǎng)絡(luò)連接。
可以用firewall-cmd --list-all-zones來(lái)查看各個(gè)網(wǎng)絡(luò)區(qū)域的設(shè)置。
droptarget: DROPicmp-block-inversion: nointerfaces: sources: services: ports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules:- block(限制)
任何接收的網(wǎng)絡(luò)連接都被?IPv4?的?icmp-host-prohibited?信息和?IPv6?的?icmp6-adm-prohibited?信息所拒絕。
blocktarget: %%REJECT%%icmp-block-inversion: nointerfaces: sources: services: ports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules:- public(公共)
在公共區(qū)域內(nèi)使用,不能相信網(wǎng)絡(luò)內(nèi)的其他計(jì)算機(jī)不會(huì)對(duì)您的計(jì)算機(jī)造成危害,只能接收經(jīng)過(guò)選取的連接。
public (active)target: defaulticmp-block-inversion: nointerfaces: ens33sources: services: dhcpv6-client http sshports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules:- external(外部)
特別是為路由器啟用了偽裝功能的外部網(wǎng)。您不能信任來(lái)自網(wǎng)絡(luò)的其他計(jì)算,不能相信它們不會(huì)對(duì)您的計(jì)算機(jī)造成危害,只能接收經(jīng)過(guò)選擇的連接。
externaltarget: defaulticmp-block-inversion: nointerfaces: sources: services: sshports: protocols: masquerade: yesforward-ports: source-ports: icmp-blocks: rich rules:- dmz(非軍事區(qū))
用于您的非軍事區(qū)內(nèi)的電腦,此區(qū)域內(nèi)可公開(kāi)訪問(wèn),可以有限地進(jìn)入您的內(nèi)部網(wǎng)絡(luò),僅僅接收經(jīng)過(guò)選擇的連接。
dmztarget: defaulticmp-block-inversion: nointerfaces: sources: services: sshports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules:- work(工作)
用于工作區(qū)。您可以基本相信網(wǎng)絡(luò)內(nèi)的其他電腦不會(huì)危害您的電腦。僅僅接收經(jīng)過(guò)選擇的連接。
worktarget: defaulticmp-block-inversion: nointerfaces: sources: services: dhcpv6-client sshports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules:- home(家庭)
用于家庭網(wǎng)絡(luò)。您可以基本信任網(wǎng)絡(luò)內(nèi)的其他計(jì)算機(jī)不會(huì)危害您的計(jì)算機(jī)。僅僅接收經(jīng)過(guò)選擇的連接。
hometarget: defaulticmp-block-inversion: nointerfaces: sources: services: dhcpv6-client mdns samba-client sshports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules:- internal(內(nèi)部)
用于內(nèi)部網(wǎng)絡(luò)。您可以基本上信任網(wǎng)絡(luò)內(nèi)的其他計(jì)算機(jī)不會(huì)威脅您的計(jì)算機(jī)。僅僅接受經(jīng)過(guò)選擇的連接。
internaltarget: defaulticmp-block-inversion: nointerfaces: sources: services: dhcpv6-client mdns samba-client sshports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules:- trusted(信任)
可接受所有的網(wǎng)絡(luò)連接。
trustedtarget: ACCEPTicmp-block-inversion: nointerfaces: sources: services: ports: protocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules:1-4. 總結(jié)
- target
target: { default , ACCEPT , DROP , REJECT }
| DROP | 拒絕請(qǐng)求,不會(huì)回復(fù)信息,訪問(wèn)者處于等待狀態(tài),直到timeout |
| REJECT | 拒絕請(qǐng)求,會(huì)明確告訴訪問(wèn)者,請(qǐng)求被拒絕 |
| ACCEPT | 接受請(qǐng)求。 |
| default(默認(rèn)跟REJECT一樣) |
- icmp-block-inversion
icmp-block-inversion: { yes , no }
| yes | icmp-blocks里面記述的接受,沒(méi)記述的拒絕,也就是反轉(zhuǎn) |
| no | icmp-blocks里面記述的拒絕。 |
- ?interfaces
interfaces: { 網(wǎng)卡名稱?}
用ifconfig或者ip addr查詢到的網(wǎng)卡名稱
例:interfaces: ens33
- sources?
sources: { IP or CIDR}
例:sources:192.168.0.211
? ? ? ?sources:192.168.0.0/24
- services
?services: { servicetype}
firewall-cmd --get-services命令 可以查詢到所有的service。但是只能看到名字。除了大家熟悉的http,ssh等,其余的可以去/usr/lib/firewalld/services/ 下查詢。
例:zabbix-server 可以用?cat /usr/lib/firewalld/services/zabbix-server.xml 去看端口號(hào)
- ports?
ports: { port/protocol }
像http,https,ssh一類的可以用上面的services來(lái)定義。
像一些非常用端口,比如20001一類的,可以用ports定義。
例:ports:20001/tcp
- protocols
?protocols: { protocol }
protocol協(xié)議就是普通的熟知的協(xié)議,比如說(shuō)tcp udp icmp。
- masquerade
?這個(gè)東西是端口地址轉(zhuǎn)發(fā)用的,比如說(shuō)從別的機(jī)器訪問(wèn)本機(jī)的9090端口相當(dāng)于訪問(wèn)另一臺(tái)機(jī)器的80端口。這種情況下,需要開(kāi)啟masquerade,當(dāng)數(shù)據(jù)流量返回來(lái)的時(shí)候可以自動(dòng)返回。
例:也就是說(shuō)目標(biāo)地址轉(zhuǎn)換以后,從A(訪問(wèn)機(jī))->B(本機(jī))->C(地址轉(zhuǎn)換后的機(jī)器),這個(gè)route是通的,但是數(shù)據(jù)從C(地址轉(zhuǎn)換后的機(jī)器)->B(本機(jī))->A(訪問(wèn)機(jī)),如果不開(kāi)啟masquerade,就需要源地址轉(zhuǎn)換。
firewall-cmd --add-masquerade? ? ? ?開(kāi)啟
firewall-cmd --remove-masquerade 關(guān)閉
- forward-ports
?forward-ports: { port=port:proto=protocol:toport=轉(zhuǎn)換后的端口:toaddr=轉(zhuǎn)換后的IP }
例:forward-ports: port=8080:proto=tcp:toport=80:toaddr=192.168.1.1
就是到達(dá)本機(jī)8080端口的數(shù)據(jù)都會(huì)轉(zhuǎn)發(fā)到192.168.1.1:80。這個(gè)時(shí)候如果上面的masquerade不開(kāi)啟,從192.168.1.1:80返回的數(shù)據(jù)就不能到達(dá)本機(jī)的8080端口。
- source-ports
?source-ports:{ port/protocol }
跟前面的ports一樣的格式,這個(gè)指的是源port,ports指的是本機(jī)的port。
- icmp-blocks?
icmp-blocks: { ICMP Type }
ICMP type 可以用?firewall-cmd --get-icmptypes 來(lái)查看。具體的定義情報(bào)的話 參照
/usr/lib/firewalld/icmptype 下的xml文件
- rich-rules?
rich-rules: {rich rule}
前面的那些屬性設(shè)置不了規(guī)則了,可以用這個(gè)屬性來(lái)設(shè)置詳細(xì)規(guī)則。
例:firewall-cmd --add-rich-rule='rule family=ipv4 source address=192.168.200.0/24 port port=135 protocol=tcp accept'
來(lái)自192.168.200.0/24網(wǎng)段的訪問(wèn)本機(jī)端口135的tcp協(xié)議,都接受。
充分理解上面的東西以后剩下的就是命令的用法了。
2 firewalld的啟動(dòng)和關(guān)閉
# firewalld狀態(tài)查詢(兩個(gè)都可以) firewall-cmd --state systemctl status firewalld#firewalld啟動(dòng) systemctl start firewalld#firewalld開(kāi)機(jī)啟動(dòng) systemctl enable firewalld#firewalld關(guān)閉 systemctl stop firewalld3 firewalld的命令使用
3-1. 更改運(yùn)行時(shí)設(shè)置,然后將它們永久化
firewall-cmd <other options> firewall-cmd --runtime-to-permanent3-2.?設(shè)置永久設(shè)置并將設(shè)置重新加載到運(yùn)行時(shí)模式
firewall-cmd --permanent <other options> firewall-cmd --reload3-3. 小技巧,規(guī)則過(guò)期,例如下面的例子,增加ssh的規(guī)則,但是15分之后失效。
firewall-cmd --add-service=ssh --timeout 15m3-4. 列出當(dāng)前active zone的全屬性
firewall-cmd --list-all# 擴(kuò)展 # 列出指定zone的全屬性 firewall-cmd --list-all --zone=block#列出所有的zone的全屬性 firewall-cmd --list-all-zones3-5. 列出當(dāng)前active zone的各種屬性值
firewall-cmd --list-[option]
option可以取下面的值
? interfaces
? sources?
? services
? ports
? protocols
? forward-ports
? source-ports
? icmp-blocks
? rich-rules
3-6. 其它的command可以用下面的命令來(lái)看幫助文檔。
man firewall-cmdfirewall-cmd --help總結(jié)
以上是生活随笔為你收集整理的Linux学习整理-网络防火墙firewalld的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 拯救者win10重置系统出现“初始化出现
- 下一篇: Android EditText将软键盘