wiki:TipAndDoc/network/defaultgw

Version 4 (modified by mitty, 15 years ago) (diff)

--

default gateway

  • default gateway (default route)の変更方法・扱い

CentOS

ifcfg-eth?

  • ifcfg-eth? に書いた場合
  • /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    BOOTPROTO=static
    IPADDR=192.168.40.200
    NETMASK=255.255.255.0
    GATEWAY=192.168.40.1
    ONBOOT=yes
    
  • /etc/sysconfig/network-scripts/ifcfg-eth1
    DEVICE=eth1
    BOOTPROTO=static
    IPADDR=192.168.50.200
    NETMASK=255.255.255.0
    GATEWAY=192.168.50.1
    ONBOOT=yes
    
  • service network restart
    Bringing up interface eth0:                                [  OK  ]
    Bringing up interface eth1:                                [  OK  ]
    
  • 結果 => route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.50.0    *               255.255.255.0   U     0      0        0 eth1
    192.168.40.0    *               255.255.255.0   U     0      0        0 eth0
    169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
    default         192.168.50.1    0.0.0.0         UG    0      0        0 eth1
    
    • この状態で、実際にWANへpingを送るとeth1を通る
    • tcpdump icmp -n -i eth1
      listening on eth1, link-type EN10MB (Ethernet), capture size 96 bytes
      01:32:42.314543 IP 192.168.50.200 > 219.94.129.108: ICMP echo request, id 16947, seq 1, length 64
      01:32:42.330849 IP 219.94.129.108 > 192.168.50.200: ICMP echo reply, id 16947, seq 1, length 64
      
  • eth0とeth1を逆にしてみる
    • eth0
      IPADDR=192.168.50.200
      NETMASK=255.255.255.0
      GATEWAY=192.168.50.1
      
    • eth1
      IPADDR=192.168.40.200
      NETMASK=255.255.255.0
      GATEWAY=192.168.40.1
      
    • service network restart
      Bringing up interface eth0:                                [  OK  ]
      Bringing up interface eth1:                                [  OK  ]
      
    • 結果 => route
      Kernel IP routing table
      Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
      192.168.50.0    *               255.255.255.0   U     0      0        0 eth0
      192.168.40.0    *               255.255.255.0   U     0      0        0 eth1
      169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
      default         192.168.40.1    0.0.0.0         UG    0      0        0 eth1
      
      • 今度もeth1を通る
      • tcpdump icmp -n -i eth1
        listening on eth1, link-type EN10MB (Ethernet), capture size 96 bytes
        01:39:05.380029 IP 192.168.40.200 > 219.94.129.108: ICMP echo request, id 26423, seq 1, length 64
        01:39:05.392541 IP 219.94.129.108 > 192.168.40.200: ICMP echo reply, id 26423, seq 1, length 64
        
  • LinkUPの順番を変える
    • ifup eth1; ifup eth0
      Kernel IP routing table
      Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
      192.168.50.0    *               255.255.255.0   U     0      0        0 eth0
      192.168.40.0    *               255.255.255.0   U     0      0        0 eth1
      169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
      default         192.168.50.1    0.0.0.0         UG    0      0        0 eth0
      
    • tcpdump icmp -n -i eth0
      listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
      01:42:55.952191 IP 192.168.50.200 > 219.94.129.108: ICMP echo request, id 52536, seq 1, length 64
      01:42:55.965580 IP 219.94.129.108 > 192.168.50.200: ICMP echo reply, id 52536, seq 1, length 64
      

sysconfig/network

  • /etc/sysconfig/network
    GATEWAY=192.168.50.2
    
  • ifcfg-eth? に GATEWAY= があるとき
    • そちらが優先される
  • 無いとき
    • /etc/sysconfig/network の GATEWAY= が設定される
      Kernel IP routing table
      Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
      192.168.50.0    *               255.255.255.0   U     0      0        0 eth0
      192.168.40.0    *               255.255.255.0   U     0      0        0 eth1
      169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
      default         192.168.50.2    0.0.0.0         UG    0      0        0 eth0
      

結論

  • /etc/sysconfig/network-scripts/ifcfg-eth? に GATEWAY= の指定がない場合は、/etc/sysconfig/network が参照される
  • ifcfg-eth? に GATEWAY= の指定がある際は、そちらが参照される
    • NICが複数ある場合は、最後にLinkUPしたデバイスのgatewayが有効になる

  • eth0
    GATEWAY=192.168.50.1
    
  • eth1
    • 指定なし
  • network
    GATEWAY=192.168.50.2
    
  • => route
    default         192.168.50.1    0.0.0.0         UG    0      0        0 eth0
    

route コマンド

  • 以下の状態でroute addする
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.50.0    *               255.255.255.0   U     0      0        0 eth0
    192.168.40.0    *               255.255.255.0   U     0      0        0 eth1
    169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
    default         192.168.50.1    0.0.0.0         UG    0      0        0 eth0
    
  • route add default gw 192.168.40.1
    default         192.168.40.1    0.0.0.0         UG    0      0        0 eth1
    default         192.168.50.1    0.0.0.0         UG    0      0        0 eth0
    
    • この場合、実際にはeth1を通る
      listening on eth1, link-type EN10MB (Ethernet), capture size 96 bytes
      02:03:27.195895 IP 192.168.40.200 > 219.94.129.108: ICMP echo request, id 51265, seq 1, length 64
      02:03:27.212349 IP 219.94.129.108 > 192.168.40.200: ICMP echo reply, id 51265, seq 1, length 64
      
  • route add default gw 192.168.0.1
    SIOCADDRT: Network is unreachable
    
    • routing table は変化せず

default が複数あるときの挙動

  • 挙動法則が不明
    • route コマンドで、先に表示された行のgwが必ず使われるとも限らない模様

  • ifconfig
    eth0 => 192.168.50.100
    eth1 => 192.168.60.100
    eth2 => 192.168.70.100
    eth3 => 192.168.40.100
    

例1

  • route
    default         192.168.70.200  0.0.0.0         UG    0      0        0 eth2
    default         192.168.60.200  0.0.0.0         UG    0      0        0 eth1
    default         192.168.50.200  0.0.0.0         UG    0      0        0 eth0
    default         192.168.40.200  0.0.0.0         UG    0      0        0 eth3
    
  • ping 219.94.129.108 -c1
    • client:eth2 (192.168.70.100)
      03:20:58.808400 IP 192.168.70.100 > 219.94.129.108: ICMP echo request, id 33555, seq 1, length 64
      
    • gw:eth2 (192.168.70.200)
      03:20:58.834723 IP 192.168.70.100 > 219.94.129.108: ICMP echo request, id 33555, seq 1, length 64
      

例2

  • route
    default         192.168.40.200  0.0.0.0         UG    0      0        0 eth3
    default         192.168.50.200  0.0.0.0         UG    0      0        0 eth0
    default         192.168.60.200  0.0.0.0         UG    0      0        0 eth1
    default         192.168.70.200  0.0.0.0         UG    0      0        0 eth2
    
  • ping 219.94.129.108 -c1
    • client:eth1 (192.168.60.100)
      03:25:08.077550 IP 192.168.60.100 > 219.94.129.108: ICMP echo request, id 37651, seq 1, length 64
      
    • gw:eth1 (192.168.60.200)
      03:25:08.106974 IP 192.168.60.100 > 219.94.129.108: ICMP echo request, id 37651, seq 1, length 64
      

例3

  • route
    default         192.168.50.200  0.0.0.0         UG    0      0        0 eth0
    default         192.168.60.200  0.0.0.0         UG    0      0        0 eth1
    default         192.168.70.200  0.0.0.0         UG    0      0        0 eth2
    default         192.168.40.200  0.0.0.0         UG    0      0        0 eth3
    
  • ping 219.94.129.108 -c1
    • client:eth3 (192.168.40.100)
      03:29:45.739584 IP 192.168.40.100 > 219.94.129.108: ICMP echo request, id 42515, seq 1, length 64
      
    • gw:eth3 (192.168.40.200)
      03:29:45.773401 IP 192.168.40.100 > 219.94.129.108: ICMP echo request, id 42515, seq 1, length 64