新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Linux中如何屏蔽海外流量
作為一名維護(hù)生產(chǎn)環(huán)境Linux服務(wù)器的系統(tǒng)管理員,在有些情況下,你需要根據(jù)地理位置,有選擇性地阻止或允許網(wǎng)絡(luò)流量。那么教你兩種屏蔽海外流量的方法。

網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站設(shè)計(jì),高端網(wǎng)頁(yè)制作,對(duì)成都火鍋店設(shè)計(jì)等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)seo優(yōu)化優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。
方法一:使用大神的開源腳本,屏蔽指定國(guó)家地區(qū)的IP訪問(wèn)
wget https://raw.githubusercontent.com/iiiiiii1/Block-IPs-from-countries/master/block-ips.sh
sh block-ips.sh
方法二:使用IPIP的數(shù)據(jù)庫(kù)進(jìn)行流量屏蔽(推薦,目前已支持centos6和7還有ubuntu系統(tǒng))
#!/bin/bash
#判斷是否具有root權(quán)限
root_need() {
if [[ $EUID -ne 0 ]]; then
echo "Error:This script must be run as root!" 1>&2
exit 1
fi
}
#檢查系統(tǒng)分支及版本(主要是:分支->>版本>>決定命令格式)
check_release() {
if uname -a | grep el7 ; then
release="centos7"
elif uname -a | grep el6 ; then
release="centos6"
yum install ipset -y
elif cat /etc/issue |grep -i ubuntu ; then
release="ubuntu"
apt install ipset -y
fi
}
#安裝必要的軟件(wget),并下載中國(guó)IP網(wǎng)段文件(最后將局域網(wǎng)地址也放進(jìn)去)
get_china_ip() {
#安裝必要的軟件(wget)
rpm --help >/dev/null 2>&1 && rpm -qa |grep wget >/dev/null 2>&1 ||yum install -y wget ipset >/dev/null 2>&1
dpkg --help >/dev/null 2>&1 && dpkg -l |grep wget >/dev/null 2>&1 ||apt-get install wget ipset -y >/dev/null 2>&1
#該文件由IPIP維護(hù)更新,大約一月一次更新(也可以用我放在國(guó)內(nèi)的存儲(chǔ)的版本,2018-9-8日版)
[ -f china_ip_list.txt ] && mv china_ip_list.txt china_ip_list.txt.old
wget https://github.com/17mon/china_ip_list/blob/master/china_ip_list.txt
cat china_ip_list.txt |grep 'js-file-line">' |awk -F'js-file-line">' '{print $2}' |awk -F' '{print $1}' >> china_ip.txt rm -rf china_ip_list.txt #wget https://qiniu.wsfnk.com/china_ip.txt #放行局域網(wǎng)地址 echo "192.168.0.0/18" >> china_ip.txt echo "10.0.0.0/8" >> china_ip.txt echo "172.16.0.0/12" >> china_ip.txt } #只允許國(guó)內(nèi)IP訪問(wèn) ipset_only_china() { echo "ipset create whitelist-china hash:net hashsize 10000 maxelem 1000000" > /etc/ip-black.sh for i in $( cat china_ip.txt ) do echo "ipset add whitelist-china $i" >> /etc/ip-black.sh done echo "iptables -I INPUT -m set --match-set whitelist-china src -j ACCEPT" >> /etc/ip-black.sh #拒絕非國(guó)內(nèi)和內(nèi)網(wǎng)地址發(fā)起的tcp連接請(qǐng)求(tcp syn 包)(注意,只是屏蔽了入向的tcp syn包,該主機(jī)主動(dòng)訪問(wèn)國(guó)外資源不用影響) echo "iptables -A INPUT -p tcp --syn -m connlimit --connlimit-above 0 -j DROP" >> /etc/ip-black.sh #拒絕非國(guó)內(nèi)和內(nèi)網(wǎng)發(fā)起的ping探測(cè)(不影響本機(jī)ping外部主機(jī)) echo "iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j DROP" >> /etc/ip-black.sh #echo "iptables -A INPUT -j DROP" >> /etc/ip-black.sh rm -rf china_ip.txt } run_setup() { chmod +x /etc/rc.local sh /etc/ip-black.sh rm -rf /etc/ip-black.sh #下面這句主要是兼容centos6不能使用"-f"參數(shù) ipset save whitelist-china -f /etc/ipset.conf || ipset save whitelist-china > /etc/ipset.conf [ $release = centos7 ] && echo "ipset restore -f /etc/ipset.conf" >> /etc/rc.local [ $release = centos6 ] && echo "ipset restore >> /etc/rc.local echo "iptables -I INPUT -m set --match-set whitelist-china src -j ACCEPT" >> /etc/rc.local echo "iptables -A INPUT -p tcp --syn -m connlimit --connlimit-above 0 -j DROP" >> /etc/rc.local echo "iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j DROP" >> /etc/rc.local #echo "iptables -A INPUT -j DROP" >> /etc/rc.local } main() { check_release get_china_ip ipset_only_china case "$release" in centos6) run_setup ;; centos7) chmod +x /etc/rc.d/rc.local run_setup ;; ubuntu) sed -i '/exit 0/d' /etc/rc.local run_setup echo "exit 0" >> /etc/rc.local ;; esac } main 網(wǎng)站欄目:Linux中如何屏蔽海外流量
分享地址:http://www.dlmjj.cn/article/dhejhcg.html


咨詢
建站咨詢
