编辑 /etc/sysctl.conf
添加net.ipv4.ip_forward = 1
保存、应用sudo sysctl -p
添加转发规则
sudo iptables -P INPUT ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -t nat -A PREROUTING -d 本地IP -p tcp --dport 本地端口 -j DNAT --to-destination 远程IP:远程端口
sudo iptables -t nat -A PREROUTING -p udp -d 本地IP --dport 本地端口 -j DNAT --to-destination 远程IP:远程端口
sudo iptables -t nat -A POSTROUTING -d 远程IP -p tcp --dport 远程端口 -j SNAT --to-source 本地IP
sudo iptables -t nat -A POSTROUTING -p udp -d 远程IP --dport 远程端口 -j SNAT --to-source 本地IP
保存iptables规则(ubuntu, 安装包 iptables-persistent)
netfilter-persistent save