route的用法简介
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
打印当前的路由表:
route -n
常用参数:
add:添加一条新路由。
del:删除一条路由。
-net:目标地址是一个网络。
-host:目标地址是一个主机。
netmask:当添加一个网络路由时,需要使用网络掩码。
gw:路由数据包通过网关。注意,你指定的网关必须能够达到。
metric:设置路由跳数。
# route 命令添加的路由,机器重启或者网卡重启后就没掉了,在linux下设置永久路由的方法:
1.在
/etc/rc
.
local
里添加
2.在
/etc/sysconfig/network
里添加到末尾
3.
/etc/sysconfig/static-router
:
any net x.x.x.x
/24
gw y.y.y.y
route add default gw 192.168.1.254
route del default gw 192.168.1.254
route add -net 192.168.100.0 netmask 255.255.255.0 dev eth0
route add -host ip dev lo:1
route del -host ip
若是已经正确配置过IP,但想调整默认网关,如何操作呢?
【调整默认网关的实例】
查看路由:
[root@test44 ~]
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.200.0 0.0.0.0 255.255.255.0 U 0 0 0 em2
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 em2
0.0.0.0 10.0.200.128 0.0.0.0 UG 0 0 0 em2
增加默认网关
[root@test44 ~]
# route add default gw 10.0.200.2 && route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.200.0 0.0.0.0 255.255.255.0 U 0 0 0 em2
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 em2
0.0.0.0 10.0.200.2 0.0.0.0 UG 0 0 0 em2
0.0.0.0 10.0.200.128 0.0.0.0 UG 0 0 0 em2
默认路由只有一条,因此要删除旧的已失效的那条默认路由
[root@test44 ~]
# route del default gw 10.0.200.128 && route -n && ping qq.com
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.200.0 0.0.0.0 255.255.255.0 U 0 0 0 em2
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 em2
0.0.0.0 10.0.200.2 0.0.0.0 UG 0 0 0 em2
最后,记得更新一下配置文件:
[root@test44 ~]
# sed -i 's/200.128/200.2/' /etc/sysconfig/network-scripts/ifcfg-em1 && cat /etc/sysconfig/network-scripts/ifcfg-em1
删除单条路由的方式:
[root@test222 ~]
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
10.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 eth0
0.0.0.0 10.0.0.1 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.1.254 0.0.0.0 UG 0 0 0 eth0
[root@test222 ~]
# route del -net 10.0.0.0 netmask 255.0.0.0 gw 0.0.0.0
[root@test222 ~]
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 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.1 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.1.254 0.0.0.0 UG 0 0 0 eth0
[root@test222 ~]
# route del default gw 10.0.0.1
[root@test222 ~]
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 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 192.168.1.254 0.0.0.0 UG 0 0 0 eth0
如果有2个网卡eth0和eth1,都是static配置了gateway,则最终生效的gateway是第二个gateway中配置的。
此时,可以增加一条路由规则,
例如,调整配置文件“
/etc/rc
.
local
”:
echo
'route add -net 10.0.96.0
/20
eth0 >>
/etc/rc
.
local
或者,调整配置文件“
/etc/sysconfig/static-routes
”:
echo
'any net 10.0.96.0
/20
eth0 >>
/etc/sysconfig/static-routes
理由是:
# cat /etc/init.d/network |grep static-routes --color -A 3
# Add non interface-specific static-routes.
if
[ -f
/etc/sysconfig/static-routes
];
then
grep
"^any"
/etc/sysconfig/static-routes
|
while
read
ignore args ;
do
/sbin/route
add -$args
done
fi
如果eth1是dhcp获取的,则可以指定gateway添加到文件:
/etc/sysconfig/network
格式是:
GATEWAY=10.0.100.1
然后,可以重启network试试效果:
service network restart
|
本文转自 pcnk 51CTO博客,原文链接:http://blog.51cto.com/nosmoking/1659775,如需转载请自行联系原作者