使用nftables屏蔽国外流量:修订间差异
来自md5.pw
更多语言
更多操作
修复语法高亮标签的语言参数 |
|||
| (未显示同一用户的1个中间版本) | |||
| 第2行: | 第2行: | ||
=== 1. 准备 === | === 1. 准备 === | ||
确保 nftables 与 python3 已经安装<syntaxhighlight> | 确保 nftables 与 python3 已经安装<syntaxhighlight lang="text"> | ||
nft --version | nft --version | ||
python3 --version | python3 --version | ||
</syntaxhighlight>若没有安装, 以Debian13为例<syntaxhighlight>apt update | </syntaxhighlight>若没有安装, 以Debian13为例<syntaxhighlight lang="text">apt update | ||
apt install nftables python3 unzip | apt install nftables python3 unzip | ||
systemctl enable nftables</syntaxhighlight>拷贝原有的nftables配置, 到一个好找的目录里, 以 <code>/main/nftables</code> 为例, 再对原路径进行链接<syntaxhighlight>cp /etc/nftables.conf /main/nftables/ | systemctl enable nftables</syntaxhighlight>拷贝原有的nftables配置, 到一个好找的目录里, 以 <code>/main/nftables</code> 为例, 再对原路径进行链接<syntaxhighlight lang="text">cp /etc/nftables.conf /main/nftables/ | ||
ln -f /main/nftables/nftables.conf /etc/nftables.conf</syntaxhighlight>并准备好编辑 <code>/main/nftables/nftables.conf</code> | ln -f /main/nftables/nftables.conf /etc/nftables.conf</syntaxhighlight>并准备好编辑 <code>/main/nftables/nftables.conf</code> | ||
=== 2. 获取国内IP段 === | === 2. 获取国内IP段 === | ||
先获取并解压项目文件<syntaxhighlight> | 先获取并解压项目文件<syntaxhighlight lang="text"> | ||
mkdir /main/nftables/geoip | mkdir /main/nftables/geoip | ||
cd /main/nftables/geoip | cd /main/nftables/geoip | ||
| 第17行: | 第17行: | ||
unzip master.zip | unzip master.zip | ||
rm master.zip | rm master.zip | ||
</syntaxhighlight>执行脚本<syntaxhighlight>mv nftables-geoip-master/* ./ | </syntaxhighlight>执行脚本<syntaxhighlight lang="text">mv nftables-geoip-master/* ./ | ||
chmod u+x nft_geoip.py | chmod u+x nft_geoip.py | ||
./nft_geoip.py --file-location location.csv --download | ./nft_geoip.py --file-location location.csv --download | ||
| 第23行: | 第23行: | ||
=== 3. 配置nftables === | === 3. 配置nftables === | ||
在合适位置添加或与其它的table进行合并, 这里比如只有访问tcp 46端口的国内IP, 才能放行<syntaxhighlight line="1"> | 在合适位置添加或与其它的table进行合并, 这里比如只有访问tcp 46端口的国内IP, 才能放行<syntaxhighlight lang="text" line="1"> | ||
table inet geoip_filter { | table inet geoip_filter { | ||
| 第53行: | 第53行: | ||
=== 4. 完成配置 === | === 4. 完成配置 === | ||
保存后并重启<syntaxhighlight> | 保存后并重启<syntaxhighlight lang="text"> | ||
systemctl restart nftables.service | systemctl restart nftables.service | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:500 常见应用指南 — Application Guides]] | |||
2026年9月18日 (五) 15:40的最新版本
1. 准备
确保 nftables 与 python3 已经安装
nft --version
python3 --version
若没有安装, 以Debian13为例
apt update
apt install nftables python3 unzip
systemctl enable nftables
拷贝原有的nftables配置, 到一个好找的目录里, 以 /main/nftables 为例, 再对原路径进行链接
cp /etc/nftables.conf /main/nftables/
ln -f /main/nftables/nftables.conf /etc/nftables.conf
并准备好编辑 /main/nftables/nftables.conf
2. 获取国内IP段
先获取并解压项目文件
mkdir /main/nftables/geoip
cd /main/nftables/geoip
wget https://github.com/pvxe/nftables-geoip/archive/refs/heads/master.zip
unzip master.zip
rm master.zip
执行脚本
mv nftables-geoip-master/* ./
chmod u+x nft_geoip.py
./nft_geoip.py --file-location location.csv --download
./nft_geoip.py --download -c cn
3. 配置nftables
在合适位置添加或与其它的table进行合并, 这里比如只有访问tcp 46端口的国内IP, 才能放行
table inet geoip_filter {
include "/main/nftables/geoip/geoip-def-all.nft";
include "/main/nftables/geoip/geoip-ipv4-interesting.nft";
include "/main/nftables/geoip/geoip-ipv6-interesting.nft";
chain geoip_mark_input {
type filter hook prerouting priority -10;
policy accept;
meta mark set ip saddr map @geoip4;
meta mark set ip6 saddr map @geoip6;
}
chain input {
type filter hook input priority 0;
policy drop;
# 放行本地/已建立/相关的连接
iif lo accept;
ct state established,related accept;
tcp dport { 46 } meta mark $CN ct state new accept;
}
}
4. 完成配置
保存后并重启
systemctl restart nftables.service