linux如何通過防火墻iptables做隔離端口的腳本
通過防火墻iptables做隔離端口的腳本
vi iptables_fix.sh #!/bin/bash #備份舊的規(guī)則 iptables-save > “/opt/firewall-“date "+%Y-%m-%d-%H:%M:%S"”.txt”
獲取集群內(nèi)hosts的ip,空格分隔
clusters=cat /etc/hosts | grep -v ::1 | grep -v "^$" | awk '{print $1}'配置集群外的ip,空格分隔,格式如下
business=“127.0.0.1 172.17.0.1/16”
配置需要隔離的端口,空格分隔,以22為例
block_ports=“22” echo “FireWall fix…”
新建chain
iptables -t filter -N BIGDATA_BLOCK_PORTS
添加集群內(nèi)ip白名單
for block_port in $block_ports; do for chost in $clusters; do #echo $ahost iptables -I BIGDATA_BLOCK_PORTS -s $chost -p tcp --dport $block_port -j ACCEPT iptables -I BIGDATA_BLOCK_PORTS -s $chost -p udp --dport $block_port -j ACCEPT done done
添加集群外ip白名單
for block_port in $block_ports; do for bhost in $business; do #echo $ahost iptables -I BIGDATA_BLOCK_PORTS -s $bhost -p tcp --dport $block_port -j ACCEPT iptables -I BIGDATA_BLOCK_PORTS -s $bhost -p udp --dport $block_port -j ACCEPT done done
最后隔離端口
for block_port in $block_ports; do iptables -A BIGDATA_BLOCK_PORTS -p tcp --dport $block_port -j DROP iptables -A BIGDATA_BLOCK_PORTS -p udp --dport $block_port -j DROP done 將BIGDATA_BLOCK_PORTS加入INPUT和FORWARD iptables -I INPUT -j BIGDATA_BLOCK_PORTS iptables -I FORWARD -j BIGDATA_BLOCK_PORTS echo "fix finished
同時開啟firewall和iptables
使用向導
With the iptables service, every single change means flushing all the old rules and reading all the new rules from /etc/sysconfig/iptables, while with firewalld there is no recreating of all the rules. Only the differences are applied. Consequently, firewalld can change the settings during runtime without existing connections being lost.
翻譯
firewalld與iptables(和ip6tables)服務的本質區(qū)別是:
iptables服務將配置存儲在/etc/sysconfig/iptables和/etc/sysconfig/ip6tables中,而firewalld將配置存儲在/usr/lib/firewalld/和/etc/firewalld/中的各種XML文件中。
注意,/etc/sysconfig/iptables文件不存在,因為在Red Hat Enterprise Linux上默認安裝了firewalld。
在iptables服務中,每次更改都意味著刷新所有舊規(guī)則并從/etc/sysconfig/iptables讀取所有新規(guī)則,而在firewalld中不需要重新創(chuàng)建所有規(guī)則。
只應用差異。因此,firewalld可以在運行時更改設置,而不會丟失現(xiàn)有的連接。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
linux?docker?neo4j導出方式(windows導入)
在Docker中無法直接停止Neo4j服務備份,需創(chuàng)建臨時容器并掛載宿主機/backup目錄,確保版本一致及權限設置,導出文件后退出容器即可在宿主機備份目錄獲取數(shù)據(jù)2025-08-08
如何重置 RHEL7/CentOS7 系統(tǒng)的root密碼
這篇文章主要介紹了如何重置 RHEL7/CentOS7 系統(tǒng)的root密碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
Linux文件系統(tǒng)的創(chuàng)建與掛載方法完整流程
在現(xiàn)代操作系統(tǒng)中,文件系統(tǒng)是數(shù)據(jù)組織、存儲和檢索的核心機制,Linux?作為一款功能強大且高度靈活的操作系統(tǒng),支持多種文件系統(tǒng)類型,本文將深入探討?Linux?中文件系統(tǒng)的創(chuàng)建與掛載方法,需要的朋友可以參考下2026-03-03

