ubuntu - linux伺服器雙網卡連接到路由器共享網絡,應該如何設定NAT?
高洛峰
高洛峰 2017-04-21 10:55:29
0
2
646

服務器是ubuntu server 12.04 i386,裝有雙網卡。其中eth0連接到外網,eth1連接到路由器的WAN端口。想創建一個共享wifi,使其他設備都能通過服務器上網

cat /etc/network/interfaces

auto lo  
iface lo inet loopback  
#连接到外网
auto eth0  
iface eth0 inet static  
address 212.114.20.78  
netmask 255.255.255.0  
gateway 212.114.20.126  
dns-nameservers 212.114.0.242 8.8.8.8  
#连接到路由器
auto eth1  
iface eth1 inet static  
address 192.168.1.254  
netmask 255.255.255.0

route -n

Destination     Gateway         Genmask         Flags Metric Ref      Use Iface  
0.0.0.0         212.114.20.126  0.0.0.0         UG    100    0        0   eth0  
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0   eth1  
212.114.20.0    0.0.0.0         255.255.255.0 U     0      0        0   eth0

路由器設置如下:

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回覆(2)
PHPzhong
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE

另,你伺服器的路由表是什麼(route -n 輸出)。

洪涛
  • 確認實體接線正確,是否能pinggatewaydnseth1是否接到WAN端口(非LAN端口)
  • 修改 /etc/network/interfaces (對應改動上面eth0部分)
  • 使修改生效:sudo /etc/init.d/networking restart
  • 路由器設定為上圖所示,並開啟路由器DHCP服務
  • sudo執行以下指令:
/sbin/iptables -F 
/sbin/iptables -X 
/sbin/iptables -Z 
/sbin/iptables -F -t nat 
/sbin/iptables -X -t nat 
/sbin/iptables -Z -t nat 
/sbin/iptables -P INPUT ACCEPT
/sbin/iptables -P OUTPUT ACCEPT 
/sbin/iptables -P FORWARD ACCEPT 
/sbin/iptables -t nat -P PREROUTING ACCEPT 
/sbin/iptables -t nat -P POSTROUTING ACCEPT 
/sbin/iptables -t nat -P OUTPUT ACCEPT 
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE
  • iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE/etc/rc.local
  • 如果還是不行,檢查是否按依雲的回答和評論做了
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板