有時候在實際運維工作,需要修改主機網關又不想重啟網卡服務(在低版本的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
|
網關已經變更