route 配置默认网关

时间:2023-12-13 11:11:20

影响Linux系统网络中网关配置信息的3种方式

1.生效文件cat /etc/sysconfig/network-scripts/ifcfg-eth0

GATEWAY=10.0.0.254 <- 局域网上网网关地址

命令行优先,且临时生效

[root@host01 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0

10.0.1.0        10.0.0.11       255.255.255.0   UG    0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0

[root@host01 ~]# netstat -nr

Kernel IP routing table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

10.0.0.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0

10.0.1.0        10.0.0.11       255.255.255.0   UG        0 0          0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0

0.0.0.0         10.0.0.254      0.0.0.0         UG        0 0          0 eth0

route命令添加

[root@host01 ~]# route add default gw 10.0.0.253

[root@host01 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0

10.0.1.0        10.0.0.11       255.255.255.0   UG    0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

0.0.0.0         10.0.0.253      0.0.0.0         UG    0      0        0 eth0

route命令删除

[root@host01 ~]# route del default gw 10.0.0.253

[root@host01 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0

10.0.1.0        10.0.0.11       255.255.255.0   UG    0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

查看路由表信息 ----局域网----不用到网关(局域网)

避免为了没有DHCP服务器,如果没有dhcp后会吧自己模拟成169.254.0.0

[root@host01 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0

10.0.1.0        10.0.0.11       255.255.255.0   UG    0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0

代表到达任意地址的时候,通过网关

默认的网关路由(没有这一条出不了外网)

0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0

临时删除网关路由条目方法

[root@host01 ~]# route del default 10.0.0.254

SIOCDELRT: No such device

[root@host01 ~]# route del default gw 10.0.0.254

[root@host01 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0

10.0.1.0        10.0.0.11       255.255.255.0   UG    0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

小结:

Ping域名的时候需要解析,wan

PingIP不需要解析:

Ping内网   在254内

Ping网关同的外网不行

[root@host01 ~]# ping 10.0.0.254

PING 10.0.0.254 (10.0.0.254) 56(84) bytes of data.

64 bytes from 10.0.0.254: icmp_seq=1 ttl=128 time=0.480 ms

64 bytes from 10.0.0.254: icmp_seq=2 ttl=128 time=0.252 ms

2.生效文件 /etc/sysconfig/network

GATEWAY=10.0.0.254 有地时候没有(工作可能有)