nftables是用于替换iptables的数据包过滤框架,对比iptables的端口转发功能,它的优势更明显、支持对动态IP进行转发、自动检测本机IP、单端口/端口段转发等。以配置文件保存转发规则、设置规则更轻松,非常适合需要对大量IP同时进行转发的用户,且已在Centos、Debian等最新Linux系统发行版作为生产工具提供。
nftables脚本使用方法文章源自技术白-https://jishubai.com/99.html
说明:本脚本适用于centos8、redhat8、fedora31和支持nftables的debian系linux发行版如debian10,项目地址:https://github.com/arloor/nftables-nat-rust文章源自技术白-https://jishubai.com/99.html
1、一般情况下,Linux最新发行版会默认安装nftables,centos系统使用以下命令关闭firewalld、关闭selinux、开启内核端口转发、安装nftables,debian系自行禁用iptables并使用apt命令下载安装;文章源自技术白-https://jishubai.com/99.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、下载可执行文件并赋予执行权限;文章源自技术白-https://jishubai.com/99.html
- wget -O /usr/local/bin/nat https://jishubai.com/wp-content/sh/dnat
chmod +x /usr/local/bin/nat
3、创建systemd服务;文章源自技术白-https://jishubai.com/99.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、设置为开机启动,并启动该服务;文章源自技术白-https://jishubai.com/99.html
- systemctl daemon-reload
- systemctl enable nat
- systemctl start nat
5、生成配置文件,也可以使用 vi /etc/nat.conf 命令添加删除修改转发规则;文章源自技术白-https://jishubai.com/99.html
- cat > /etc/nat.conf <<EOF
- SINGLE,22222,6666,jishubai.com
- RANGE,10000,20000,jishubai.com
- EOF
注释:文章源自技术白-https://jishubai.com/99.html
- 每行代表1个规则,行内以英文逗号分隔为4段内容
- SINGLE:代表单端口转发:本机22222端口转发到远程jishubai.com域名或IP的6666端口
- RANGE:代表端口段转发:本机10000-20000转发到远程jishubai.com域名或IP的10000-20000端口
6、停止以及卸载命令文章源自技术白-https://jishubai.com/99.html
- ## 停止定时监听域名解析的任务
- service nat stop
- ## 清空nat规则
- nft add table ip nat
- nft delete table ip nat
- ## 禁止开机启动
- systemctl disable nat
- 本站TG群组:skill_blog 联系博主:nbskill (微信协助) 苹果ID号:曙光商店