# 簡易阻擋國別 [TOC] ## 安装iptables、ipset ``` #Ubuntu/Debian安装方式 apt-get install iptables apt-get install ipset ``` ## 設定ipset群集以及iptables阻擋規則 ### 創建ipset 集合 ``` #在ipset建立一個台灣的ip清單 ipset -N twip hash:net #下載國家IP網段,以台灣為例 wget -P /root/ http://www.ipdeny.com/ipblocks/data/countries/tw.zone #将IP段添加到twip规则中(用迴圈方式添加) for a in $(cat /root/tw.zone ); do ipset -A twip $a; done ``` ### iptables規則設定 ``` #允許twip集合的IP通過 (要先有集合名稱才可設定,建議多加自己內部IP允許通過) iptables -A INPUT -p tcp -m set --match-set twip src -j ACCEPT #阻擋所有INPUT (阻擋所有進入主機,此條一下,會將所有擋掉,所以上面允許的一定要先下) iptables -P INPUT DROP ``` ## 其他相關指令 ### ipset 常用指令 ``` #清空twip這個集合 ipset flush twip #銷毀集合 (等於這個集合都不要了) ipset destroy twip #列出集合 (規則、Members 等) ipset list twip #備份 ipset save twip -f twip.backup #還原 ipset restore -f twip.backup ``` ## iptables其他下法 ``` #阻擋台灣IP iptables -A INPUT -m set --match-set twip src -j DROP ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up