【服务器工具】Linux中转-nftables端口转发工具

11,750
BrightData

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

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

2、下载可执行文件并赋予执行权限;文章源自技术白-https://jishubai.com/99.html

  1. wget -O /usr/local/bin/nat https://jishubai.com/wp-content/sh/dnat
  2. chmod +x /usr/local/bin/nat

3、创建systemd服务;文章源自技术白-https://jishubai.com/99.html

  1. cat > /lib/systemd/system/nat.service <<EOF
  2. [Unit]
  3. Description=dnat-service
  4. After=network-online.target
  5. Wants=network-online.target
  6. [Service]
  7. ExecStart=/usr/local/bin/nat /etc/nat.conf
  8. LimitNOFILE=100000
  9. Restart=always
  10. RestartSec=60
  11. [Install]
  12. WantedBy=multi-user.target
  13. EOF

4、设置为开机启动,并启动该服务;文章源自技术白-https://jishubai.com/99.html

  1. systemctl daemon-reload
  2. systemctl enable nat
  3. systemctl start nat

5、生成配置文件,也可以使用 vi /etc/nat.conf 命令添加删除修改转发规则;文章源自技术白-https://jishubai.com/99.html

  1. cat > /etc/nat.conf <<EOF
  2. SINGLE,22222,6666,jishubai.com
  3. RANGE,10000,20000,jishubai.com
  4. 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

  1. ## 停止定时监听域名解析的任务
  2. service nat stop
  3. ## 清空nat规则
  4. nft add table ip nat
  5. nft delete table ip nat
  6. ## 禁止开机启动
  7. systemctl disable nat
文章源自技术白-https://jishubai.com/99.html
  1. 本站TG群组skill_blog   联系博主:nbskill (微信协助) 苹果ID号:曙光商店
IPRoyal
 
技术白
  • nftables端口转发
  • nftables中转脚本