使用FirewallD限制網(wǎng)絡(luò)訪問方式
使用FirewallD限制網(wǎng)絡(luò)訪問
作為一個(gè)Linux用戶,你可以使用firewalld防火墻選擇允許或限制對(duì)某些服務(wù)或者IP地址的網(wǎng)絡(luò)訪問,這是CentOS/RHEL 8以及諸如Fedora的大部分基于RHEL發(fā)行版原生的。
firewalld防火墻使用firewall-cmd命令行工具配置防火墻規(guī)則。
在我們執(zhí)行任何配置前,首先使用systemctl工具啟用firewalld服務(wù),
如下:
?[root@localhost blctrl]# systemctl enable firewalld
一旦啟用了,我們現(xiàn)在可以通過執(zhí)行以下命令啟動(dòng)firewalld:
[root@localhost blctrl]# systemctl start firewalld
你可以通過運(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ù)器訪問的地址。
一旦完成這些配置,總是確保你為了使新規(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
類似地,要添加一個(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è)試:
類似地,封鎖一個(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地址穿過防火墻,執(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)文章
Linux入門之網(wǎng)絡(luò)系統(tǒng)詳解
大家好,本篇文章主要講的是Linux入門之網(wǎng)絡(luò)系統(tǒng)詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12linux環(huán)境下安裝 openOffice 并啟動(dòng)服務(wù) 的方法
這篇文章主要介紹了linux環(huán)境下安裝 openOffice 并啟動(dòng)服務(wù) 的方法,需要的朋友可以參考下2018-06-06在Ubuntu中如何查看網(wǎng)絡(luò)路由表詳解
這篇文章主要給大家介紹了關(guān)于在Ubuntu中如何查看網(wǎng)絡(luò)路由表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ubuntu具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12Linux掛載硬盤并設(shè)置開機(jī)自動(dòng)掛載的實(shí)現(xiàn)步驟
本文介紹了在Linux系統(tǒng)下掛載硬盤的概念和步驟,并講解了開機(jī)自動(dòng)掛載的方法,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定幫助,需要的朋友可以參考下2024-08-08VMware虛擬機(jī)安裝Linux系統(tǒng)圖文教程
這篇文章主要為大家詳細(xì)介紹了VMware虛擬機(jī)安裝Linux系統(tǒng)教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Linux 和Windows 安裝Git 步驟詳細(xì)介紹
這篇文章主要介紹了Linux 和Windows 安裝Git 步驟詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-01-01