ubuntu - linux服务器双网卡连接到路由器共享网络,应该如何配置NAT?

 淑娇姿荣石文 发布于 2022-10-25 07:40

服务器是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

路由器设置如下:

2 个回答
    • 确认物理接线正确,是否能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
    • 如果还是不行,检查是否按依云的回答和评论做了
    2022-10-26 14:29 回答
  • echo 1 > /proc/sys/net/ipv4/ip_forward
    iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE
    

    另,你服务器的路由表是什么(route -n 输出)。

    2022-10-26 14:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有