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
使用向?qū)?/p>
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)服務(wù)的本質(zhì)區(qū)別是:
iptables服務(wù)將配置存儲在/etc/sysconfig/iptables和/etc/sysconfig/ip6tables中,而firewalld將配置存儲在/usr/lib/firewalld/和/etc/firewalld/中的各種XML文件中。
注意,/etc/sysconfig/iptables文件不存在,因?yàn)樵赗ed Hat Enterprise Linux上默認(rèn)安裝了firewalld。
在iptables服務(wù)中,每次更改都意味著刷新所有舊規(guī)則并從/etc/sysconfig/iptables讀取所有新規(guī)則,而在firewalld中不需要重新創(chuàng)建所有規(guī)則。
只應(yīng)用差異。因此,firewalld可以在運(yùn)行時更改設(shè)置,而不會丟失現(xiàn)有的連接。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
CentOS配置本地yum源/阿里云yum源/163yuan源并配置yum源的優(yōu)先級
這篇文章主要介紹了CentOS配置本地yum源/阿里云yum源/163yuan源并配置yum源的優(yōu)先級,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Linux使用 iftop 實(shí)時監(jiān)控網(wǎng)卡的流量
iftop可以用來監(jiān)控網(wǎng)卡的實(shí)時流量(可以指定網(wǎng)段)、反向解析IP、顯示端口信息等。這篇文章主要介紹了Linux 實(shí)時網(wǎng)卡流量監(jiān)控,需要的朋友可以參考下2019-11-11詳解Linux系統(tǒng)中網(wǎng)卡MAC地址克隆方法
本篇文章主要介紹了詳解Linux系統(tǒng)中網(wǎng)卡MAC地址克隆方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06Easypanel免費(fèi)的VPS主機(jī)面板 可跨平臺Linux和Windows
Easypanel是一款免費(fèi)的功能強(qiáng)大集開通虛擬主機(jī),ftp空間,數(shù)據(jù)庫等功能為一體的主機(jī)控制面板,具備跨平臺(windows,linux),安全穩(wěn)定、操作簡便等特點(diǎn),感興趣的小伙伴們可以關(guān)注一下2017-07-07