centos 使用iptables实现只允许中国IP访问服务器或者禁止访问

安装ipset

#Debian/Ubuntu系统
apt-get -y install ipset

#CentOS系统
yum -y install ipset

CentOS 7还需要关闭firewall防火墙:

systemctl stop firewalld.service
systemctl disable firewalld.service

清空之前的规则

防止设置不生效,建议清空下之前的防火墙规则
iptables -P INPUT ACCEPT
iptables -F

创建新规则

#创建一个名为cnip的规则
ipset -N cnip hash:net
#下载国家IP段,这里以中国为例
wget -P . http://www.ipdeny.com/ipblocks/data/countries/cn.zone
#将IP段添加到cnip规则中
for i in $(cat /root/cn.zone ); do ipset -A cnip $i; done

设置IP段白名单

#放行IP段
iptables -A INPUT -p tcp -m set --match-set cnip src -j ACCEPT
#关掉所有端口
iptables -P INPUT DROP

这时候就只有中国的IP能访问服务器了。

禁止规则内的ip访问

iptables -A INPUT -p tcp -m set --match-set cnip src -j DROP

每个国家的IP段汇总

https://www.ipdeny.com/ipblocks/

标签: iptables, centos

已有 2 条评论

  1. 新盘 上车集合 留下 我要发发 立马进裙

  2. 2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
    新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
    新车首发,新的一年,只带想赚米的人coinsrore.com
    新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
    做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
    新车上路,只带前10个人coinsrore.com
    新盘首开 新盘首开 征召客户!!!coinsrore.com
    新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
    新车即将上线 真正的项目,期待你的参与coinsrore.com
    新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
    新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com

添加新评论