CentOS实现双网卡网络共享

一、双网卡网络共享

具体描述:服务器eth0连接外网,eth1连接笔记本,笔记本能够上网。

操作步骤

  1. 修改网卡信息
    • 外网
    vi /etc/sysconfig/network-scripts/ifcfg-eth0
    
    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    NM_CONTROLLED=yes
    BOOTPROTO=static
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=no
    IPV6_AUTOCONF=no
    IPV6_DEFROUTE=yes
    IPV6_FAILURE_FATAL=no
    IPV6_ADDR_GEN_MODE=stable-privacy
    NAME=eth0
    UUID=38938e8a-79e4-4c01-8c2a-effdadcca483
    DEVICE=eth0
    ONBOOT=yes
    DNS1=8.8.8.8
    IPADDR=192.168.0.145
    NETMASK=255.255.255.0
    GATEWAY=192.168.0.1
    IPV6_PEERDNS=yes
    IPV6_PEERROUTES=yes
    IPV6_PRIVACY=no
    ZONE=
    MTU=1492
    
    • 内网
    vi /etc/sysconfig/network-scripts/ifcfg-eth1
    
    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=static
    IPADDR=192.168.100.100
    NETMASK=255.255.255.0
    NM_CONTROLLED=yes
    DEFROUTE=yes
    PEERDNS=yes
    PEERROUTES=yes
    IPV4_FAILURE_FATAL=yes
    IPV6INIT=no
    IPV6_AUTOCONF=yes
    IPV6_DEFROUTE=yes
    IPV6_PEERDNS=yes
    IPV6_PEERROUTES=yes
    IPV6_FAILURE_FATAL=no
    IPV6_ADDR_GEN_MODE=stable-privacy
    NAME=eth1
    UUID=0ce8bb17-3486-4ce0-a9ee-efa25eb8edc9
    DEVICE=eth1
    ONBOOT=yes
    DNS1=8.8.8.8
    
  2. Linux系统启用端口转发(NAT)功能
    # 非永久修改
    echo 1 > /proc/sys/net/ipv4/ip_forward  
    # 永久修改
    vi /etc/sysctl.conf
    net.ipv4.ip_forward=1
    sysctl -p
    
  3. 修改防火墙设置,将网卡eth1改成trusted区域:
    systemctl stop NetworkManager
    systemctl disable NetworkManager
    firewall-cmd --permanent --zone=public --remove-interface=eth1
    firewall-cmd --permanent --zone=trusted --add-interface=eth1
    firewall-cmd --reload
    systemctl restart firewalld
    
  4. 配置 NAT 规则:将所有发送到192.168.100.*的数据包转发到eth0(外网网卡)
    iptables -t nat -A POSTROUTING -s 192.168.100.0/255.255.255.0 -o eth0 -j MASQUERADE
    
  5. 允许端口转发,执行命令
    iptables -A FORWARD -i eth1 -j ACCEPT
    
  6. 配置连接内网的主机ip信息
    ip:192.168.100.5
    子网掩码:255.255.255.0
    网关:192.168.100.100(服务器eth1的ip)
    dns:8.8.8.8
    

shell脚本

  1. share-eth.sh
    #!/bin/sh
    # use to share the two network card and make the eth0 linked public network, eth1 linked LAN.
    # And make the post data of eth1 would be sent to eth0.
    
    echo "# 修改网卡eth1"
    sed -i 's/IPADDR=.*/IPADDR=192.168.100.100/g' /etc/sysconfig/network-scripts/ifcfg-eth1
    sed -i 's/ONBOOT=.*/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth1
    sed -i 's/BOOTPROTO=.*/BOOTPROTO=static/g' /etc/sysconfig/network-scripts/ifcfg-eth1
    service network restart
    
    echo "# 启用端口转发(NAT)功能"
    echo 1 > /proc/sys/net/ipv4/ip_forward
    
    echo "# 修改防火墙设置,将网卡eth1改成trusted区域:"
    systemctl stop NetworkManager
    systemctl disable NetworkManager
    firewall-cmd --permanent --zone=public --remove-interface=eth1
    firewall-cmd --permanent --zone=trusted --add-interface=eth1
    firewall-cmd --reload
    systemctl restart firewalld
    
    echo "# 配置 NAT 规则:将所有发送到 eth1(192.168.100.100) 的数据包转发到 eth0(外网网卡)"
    #ip=$(awk -F "=" '/^IPADDR/{print $2}' /etc/sysconfig/network-scripts/ifcfg-eth1)
    iptables -t nat -A POSTROUTING -s "192.168.100.100/255.255.255.0" -o eth0 -j MASQUERADE
    
    echo "# 允许端口转发,执行命令"
    iptables -A FORWARD -i eth1 -j ACCEPT
    
  2. trans.sh
    #!/bin/sh
    iptables -t nat -A POSTROUTING -s "192.168.100.100/255.255.255.0" -o eth0 -j MASQUERADE
    

注意:执行shell脚本方式配置的话,执行完share-eth.sh之后还需再执行一次trans.sh

当eth0和eth1处于同一个网段的时候会出现什么情况?

这时连接的主机的ip也和它们处于同一网段,相互之间并不连通。

怎么解决?

  1. 服务器网卡信息:
    eth0 : 192.168.0.145	Gateway: 192.168.0.1
    eth1 : 192.168.0.100
    
    连接的主机:
    192.168.0.111	Gateway: 192.168.0.100
    
  2. 解决方法:将连接的主机ip加入服务器的路由表
    route add -host 192.168.0.111 dev eth1
    

你可能感兴趣的:(linux,shell,网关,centos)