Linux下使用nftables进行端口转发教程,支持DDNS动态IP/端口段转发设置

网络中转评论
IPRoyal

nftables是用于替换iptables的数据包过滤框架,目前已在Centos、Debian等最新Linux系统发行版作为生产工具提供。相比较iptables,nftables优势更明显、支持对动态IP进行转发、端口段转发、自动检测本机IP等,以配置文件保存转发规则、对其设置更轻松。

前言:适用于centos8、redhat8、fedora31和支持nftables的debian系linux发行版如debian10,项目地址:https://github.com/arloor/nftables-nat-rust文章源自Get主机优惠信息-https://getzhuji.com/5811.html

1、一般情况下,Linux最新发行版会默认安装nftables,使用以下命令关闭firewalld、关闭selinux、开启内核端口转发、安装nftables;文章源自Get主机优惠信息-https://getzhuji.com/5811.html

service firewalld stop
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config  
sed -n '/^net.ipv4.ip_forward=1/'p /etc/sysctl.conf | grep -q "net.ipv4.ip_forward=1"
echo 1 > /proc/sys/net/ipv4/ip_forward
if [ $? -ne 0 ]; then
    echo -e "net.ipv4.ip_forward=1" >> /etc/sysctl.conf && sysctl -p
fi
yum install -y  nftables

2、下载可执行文件并赋予执行权限;文章源自Get主机优惠信息-https://getzhuji.com/5811.html

wget -O /usr/local/bin/nat https://getzhuji.com/wp-content/uploads/sh/dnat
chmod +x /usr/local/bin/nat

3、创建systemd服务;文章源自Get主机优惠信息-https://getzhuji.com/5811.html

cat > /lib/systemd/system/nat.service <<EOF
[Unit] 
Description=dnat-service 
After=network-online.target 
Wants=network-online.target 

[Service] 
ExecStart=/usr/local/bin/nat /etc/nat.conf 
LimitNOFILE=100000 
Restart=always 
RestartSec=60 

[Install] 
WantedBy=multi-user.target 
EOF

4、设置为开机启动,并启动该服务;文章源自Get主机优惠信息-https://getzhuji.com/5811.html

systemctl daemon-reload
systemctl enable nat
systemctl start nat

5、生成配置文件,也可以使用 vi /etc/nat.conf 命令添加删除修改转发规则;文章源自Get主机优惠信息-https://getzhuji.com/5811.html

cat > /etc/nat.conf <<EOF 
SINGLE,22222,6666,getzhuji.com 
RANGE,10000,20000,getzhuji.com 
EOF

注释:文章源自Get主机优惠信息-https://getzhuji.com/5811.html

  • 每行代表1个规则,行内以英文逗号分隔为4段内容
  • SINGLE:代表单端口转发:本机22222端口转发到远程getzhuji.com域名或IP的6666端口
  • RANGE:代表端口段转发:本机10000-20000转发到远程getzhuji.com域名或IP的10000-20000端口

6、停止以及卸载命令文章源自Get主机优惠信息-https://getzhuji.com/5811.html

## 停止定时监听域名解析地任务
service nat stop

## 清空nat规则
nft add table ip nat
nft delete table ip nat

## 禁止开机启动
systemctl disable nat
文章源自Get主机优惠信息-https://getzhuji.com/5811.html文章源自Get主机优惠信息-https://getzhuji.com/5811.html
联系博主:@nbsxk(TG电报) 杂货铺曙光商店 机场推荐NaiTu云 (高速专线节点)
  • 【声明】:本站资料均来源于网络,仅供大家参考和学习,请勿违法使用!如无意侵犯到您的权利,请及时与我们联系!
评论  0  访客  0
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定