有时候在实际运维工作,需要修改主机网关又不想重启网卡服务(在低版本的linux偶尔遇到service network restart失败的情况),可以通过route 命令方式修改主机的默认网关(当然主机的配置文件也要随之修改,否则重启后网关就失效了)
查看当前的网关配置
1
2
3
4
5
6
7
|
# route -v 或 netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.0 * 255.255.255.224 U 0 0 0 bond0
192.168.20.0 * 255.255.255.0 U 0 0 0 bond0
link-
local
* 255.255.0.0 U 1004 0 0 bond0
default 192.168.20.1 0.0.0.0 UG 0 0 0 bond0
|
上面显示的默认网关为192.168.20.1
删除默认网关
用如下命令删除默认网关:
1
|
#route del default gw xxx.xxx.xxx.xxx
|
例如:
1
|
#route del default gw 192.168.20.1
|
添加默认网关
添加一条路由,输入:
1
|
#route add default gw 192.168.20.12
|
查看网关配置
1
2
3
4
5
6
7
|
# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.10.0 0.0.0.0 255.255.255.224 U 0 0 0 bond0
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0
0.0.0.0 192.168.20.12 0.0.0.0 UG 0 0 0 bond
|
网关已经变更