使用FirewallD限制網(wǎng)絡(luò)訪問(wèn)方式
使用FirewallD限制網(wǎng)絡(luò)訪問(wèn)
作為一個(gè)Linux用戶(hù),你可以使用firewalld防火墻選擇允許或限制對(duì)某些服務(wù)或者IP地址的網(wǎng)絡(luò)訪問(wèn),這是CentOS/RHEL 8以及諸如Fedora的大部分基于RHEL發(fā)行版原生的。
firewalld防火墻使用firewall-cmd命令行工具配置防火墻規(guī)則。
在我們執(zhí)行任何配置前,首先使用systemctl工具啟用firewalld服務(wù),
如下:
?[root@localhost blctrl]# systemctl enable firewalld
一旦啟用了,我們現(xiàn)在可以通過(guò)執(zhí)行以下命令啟動(dòng)firewalld:
[root@localhost blctrl]# systemctl start firewalld
你可以通過(guò)運(yùn)行以下命令驗(yàn)證firewalld的狀態(tài)并且以下輸出確認(rèn)了firewalld啟動(dòng)了并且在運(yùn)行。
[root@localhost blctrl]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-07-21 19:40:25 CST; 4h 8min left
Docs: man:firewalld(1)
Main PID: 689 (firewalld)
CGroup: /system.slice/firewalld.service
└─689 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid使用firewalld配置規(guī)則
現(xiàn)在我們使firewalld運(yùn)行了,我們直接制定一些規(guī)則。
firewalld允許你添加和阻塞端口,黑名單,以及白名單IP,提供對(duì)服務(wù)器訪問(wèn)的地址。
一旦完成這些配置,總是確保你為了使新規(guī)則生效重載這個(gè)防火墻。
添加一個(gè)TCP/UDP端口
添加一個(gè)端口,用于HTTPS的443,使用以下語(yǔ)法。
注意:你必須在這個(gè)端口號(hào)之后指定端口是TCP或UDP。
[root@localhost blctrl]# firewall-cmd --add-port=443/tcp --permanent success
類(lèi)似地,要添加一個(gè)UDP端口,按如下指定這個(gè)UDP選項(xiàng):
[root@localhost blctrl]# firewall-cmd --add-port=53/udp --permanent success
--permanent標(biāo)記確保即使在重啟后規(guī)則有效。
阻塞一個(gè)TCP/UDP端口
TCP 80端口先前已經(jīng)被添加到了規(guī)則中了,用瀏覽器測(cè)試:

要阻塞一個(gè)TCP端口,如TCP 80,運(yùn)行以下命令:
[root@localhost blctrl]# firewall-cmd --remove-port=80/tcp --permanent success [root@localhost blctrl]# firewall-cmd --reload success
再次用瀏覽器測(cè)試:

類(lèi)似地,封鎖一個(gè)UDP端口,將按照相同語(yǔ)法:
[root@localhost blctrl]# firewall-cmd --remove-port=53/udp --permanent success [root@localhost blctrl]# firewall-cmd --reload success
允許一個(gè)服務(wù)
在/etc/services文件中定義了網(wǎng)絡(luò)服務(wù)。要允許像https地服務(wù),執(zhí)行命令:
[root@localhost blctrl]# firewall-cmd --add-service=https success
阻塞一個(gè)服務(wù)
要阻塞一個(gè)服務(wù),例如,https,執(zhí)行:
[root@localhost blctrl]# firewall-cmd --remove-service=https success
白名單一個(gè)IP地址
要允許單個(gè)IP地址穿過(guò)防火墻,執(zhí)行命令:
[root@localhost blctrl]# firewall-cmd --permanent --add-source=192.168.3.244 success [root@localhost blctrl]# firewall-cmd --reload success
你也可以使用CIDR標(biāo)注允許一個(gè)IPs范圍或者整個(gè)子網(wǎng)。
例如,以255.255.255.0允許一整個(gè)子網(wǎng)。
[root@localhost blctrl]# firewall-cmd --permanent --add-source=192.168.3.0/24 success
移除一個(gè)白名單IP地址
如果你在防火墻上移除一個(gè)白名單IP,使用--remove-source標(biāo)記:
[root@localhost blctrl]# firewall-cmd --permanent --remove-source=192.168.3.244 success
對(duì)于整個(gè)子網(wǎng):
[root@localhost blctrl]# firewall-cmd --permanent --remove-source=192.168.3.0/24 success
阻塞一個(gè)IP地址:
到目前為止,我們已經(jīng)看到了你如何添加和移除端口和服務(wù)以及添加和移除白名單IPs。
阻塞一個(gè)IP地址,'rich rules'用于這個(gè)目的。
例如,要阻塞這個(gè)IP 192.168.3.244,運(yùn)行命令:
[root@localhost blctrl]# firewall-cmd --add-rich-rule="rule family='ipv4' source address='192.168.3.244' reject" success
在192.168.3.244上進(jìn)行測(cè)試:
運(yùn)行阻塞命令前:可以ping通
[blctrl@localhost ~]$ ping -c2 192.168.3.246 PING 192.168.3.246 (192.168.3.246) 56(84) bytes of data. 64 bytes from 192.168.3.246: icmp_seq=1 ttl=64 time=0.466 ms 64 bytes from 192.168.3.246: icmp_seq=2 ttl=64 time=0.559 ms --- 192.168.3.246 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.466/0.512/0.559/0.051 ms
運(yùn)行阻塞命令后,ping不通了
[blctrl@localhost ~]$ ping -c2 192.168.3.246 PING 192.168.3.246 (192.168.3.246) 56(84) bytes of data. From 192.168.3.246 icmp_seq=1 Destination Port Unreachable From 192.168.3.246 icmp_seq=2 Destination Port Unreachable --- 192.168.3.246 ping statistics --- 2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1000ms
要阻塞整個(gè)子網(wǎng),運(yùn)行:
[root@localhost blctrl]# firewall-cmd --add-rich-rule="rule family='ipv4' source address='192.168.3.0/24' reject"
保存防火墻規(guī)則
如果你對(duì)防火墻規(guī)則做了任何修改,為了立即應(yīng)用更改,你需要運(yùn)行以下命令:
[root@localhost blctrl]# firewall-cmd --reload success
查看防火墻規(guī)則
要看一下在防火墻中所有規(guī)則,執(zhí)行這個(gè)命令:
[root@localhost blctrl]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client ssh
ports: 443/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
rule family="ipv4" source address="192.168.3.244" reject總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
7個(gè)Linux 命令輕松搞定線上Java日志管理難題
在線上Java應(yīng)用的運(yùn)維工作中,日志管理是至關(guān)重要的一環(huán),本文將針對(duì)這些常見(jiàn)難題,介紹7個(gè)實(shí)用的Linux命令,幫助你輕松搞定線上Java日志管理,希望對(duì)大家有所幫助2025-07-07
linux下搭建scala環(huán)境并寫(xiě)個(gè)簡(jiǎn)單的scala程序
今天小編就為大家分享一篇關(guān)于linux下搭建scala環(huán)境并寫(xiě)個(gè)簡(jiǎn)單的scala程序,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
Linux刪文件力度過(guò)大導(dǎo)致IO占用太高的解決方案
這篇文章主要介紹了Linux刪文件力度過(guò)大導(dǎo)致IO占用太高的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
Centos 6和Centos 7下服務(wù)啟動(dòng)方法及添加到開(kāi)機(jī)啟動(dòng)項(xiàng)的方法
這篇文章主要介紹了Centos 6和Centos 7下服務(wù)啟動(dòng)方法及添加到開(kāi)機(jī)啟動(dòng)項(xiàng)的方法的相關(guān)資料,需要的朋友可以參考下2016-10-10
CentOS 安裝軟件出現(xiàn)錯(cuò)誤:/lib/ld-linux.so.2: bad ELF interpreter 解決
這篇文章主要介紹了CentOS 安裝軟件出現(xiàn)錯(cuò)誤:/lib/ld-linux.so.2: bad ELF interpreter 解決的相關(guān)資料,需要的朋友可以參考下2017-03-03
Linux 中有效用戶(hù)組和初始用戶(hù)組的實(shí)現(xiàn)
這篇文章主要介紹了Linux 中有效用戶(hù)組和初始用戶(hù)組的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Win7下XAMPP集成環(huán)境中Apache無(wú)法啟動(dòng)的問(wèn)題解決
這篇文章主要介紹了Win7下XAMPP集成環(huán)境中Apache無(wú)法啟動(dòng)的問(wèn)題解決,通常是端口沖突,文中也講到了PotgreSQL的相關(guān)問(wèn)題,需要的朋友可以參考下2015-07-07

