1. 方法一使用firewalld
1.1 开启伪装IP
firewall-cmd --permanent --add-masquerade
1.2 配置端口转发,将到达本机的12345端口的访问转发到另一台服务器的22端口
firewall-cmd --permanent --add-forward-port=port=12345:proto=tcp:toaddr=192.168.172.131:toport=22
1.3 重新载入,使其失效
firewall-cmd --reload
2. 方法二配置iptables
其实firewalld也是基于iptables的
2.1 开启数据转发功能
vi /etc/sysctl.conf
2.2 增加一行
net.ipv4.ip_forward=1
2.3 使数据转发功能生效
sysctl -p
2.4 将本地的端口转发到本机端口
iptables -t nat -A PREROUTING -p tcp --dport 2222 -j REDIRECT --to-port 22
2.5 将本机的端口转发到其他机器
iptables -t nat -A PREROUTING -d 192.168.172.130 -p tcp --dport 8000 -j DNAT --to-destination 192.168.172.131:80
或者
iptables -t nat -A POSTROUTING -d 192.168.172.131 -p tcp --dport 80 -j SNAT --to 192.168.172.130
2.6 清空nat表的所有链
iptables -t nat -F PREROUTING