Ubuntu网关SNAT源地址转换
时间:2014-12-10 23:55 来源:linux.it.net.cn 作者:IT
Ubuntu网关SNAT源地址转换
1
2
内网主机访问外网, 使用指定外网IP地址;
每台内网主机, 对应一个外网IP地址;
环境与需求:
1台ubuntu双网卡服务器;
3台win03服务器;
需要这3台内网的服务器,以指定的公网IP地址,进行访问外网
1
2
3
内网 192.168.6.10 转换成 202.96.128.10 公网IP出去访问
内网 192.168.6.11 转换成 202.96.128.11 公网IP出去访问
内网 192.168.6.12 转换成 202.96.128.12 公网IP出去访问
1
2
3
4
5
6
7
公网IP地址:
eth0 202.96.128.10
eth0:1 202.96.128.11
eth0:2 202.96.128.12
内网IP地址(内网网关):
eth1:192.168.6.1
1
2
3
4
内网主机:
192.168.6.10
192.168.6.11
192.168.6.12
1
2
3
4
1.开启转发
sudo vim /etc/sysctl.conf
net.ipv4.ip_forward=1
sudo sysctl -p /etc/sysctl.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
2.配置防火墙规则
#允许所有转发
sudo iptables -A FORWARD -j ACCEPT
#第1条 SNAT规则
sudo iptables -t nat -A POSTROUTING -s 192.168.6.10 -o eth0 -j SNAT --to-source 202.96.128.10
#第2条 SNAT规则
sudo iptables -t nat -A POSTROUTING -s 192.168.6.11 -j SNAT --to-source 202.96.128.11
#第3条 SNAT规则
sudo iptables -t nat -A POSTROUTING -s 192.168.6.12 -j SNAT --to-source 202.96.128.12
1
2
3
4
3.查看防火墙规则
sudo iptables -t nat -L -n
sudo iptables -t nat -vnL POSTROUTING --line-numbe
sudo iptables -t nat -D POSTROUTING 8 #数字8表示删除第8条规则
1
2
4.查看状态访问信息
sudo cat /proc/net/ip_conntrack |grep tcp |grep TIME_WAIT | awk '{print $5,$10}' |sort -u
1
5.内网主机正常访问公网并以指定的公网IP出去访问
1
2
3
4
5
6.保存防火墙规则
sudo sh -c "iptables-save > /etc/iptables.rules" #保存
sudo vim /etc/network/interfaces #网卡配置文件,添加下面这行
pre-up iptables-restore < /etc/iptables.rules #启用网卡时加载
(责任编辑:IT)
Ubuntu网关SNAT源地址转换
环境与需求:
(责任编辑:IT) |