有时候在实际运维工作,需要修改主机网关又不想重启网卡服务(在低版本的linux偶尔遇到service network restart失败的情况),可以通过route 命令方式修改主机的默认网关(当然主机的配置文件也要随之修改,否则重启后网关就失效了)
查看当前的网关配置
|
1
2
3
4
5
6
7
|
# route -v 或 netstat -rnKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.10.0 * 255.255.255.224 U 0 0 0 bond0192.168.20.0 * 255.255.255.0 U 0 0 0 bond0link-local * 255.255.0.0 U 1004 0 0 bond0default 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 -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.10.0 0.0.0.0 255.255.255.224 U 0 0 0 bond0192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond00.0.0.0 192.168.20.12 0.0.0.0 UG 0 0 0 bond |
网关已经变更