ip route可以用于查看网络的路由信息,并设置路由表
-
route n 显示所有路由
[root@ceph-104 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.229.37.1 0.0.0.0 UG 101 0 0 enp2s1 10.229.37.0 0.0.0.0 255.255.255.0 U 101 0 0 enp2s1 111.192.168.0 0.0.0.0 255.255.255.0 U 100 0 0 enp2s3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 显示结果在前的路由优先使用
-
ip route show 查看路由信息
[root@ceph-104 ~]# ip route show default via 10.229.37.1 dev enp2s1 proto static metric 101 10.229.37.0/24 dev enp2s1 proto kernel scope link src 10.229.37.231 metric 101 111.192.168.0/24 dev enp2s3 proto kernel scope link src 111.192.168.104 metric 100 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown
-
ip route get ip 查看到达该ip地址的路由信息
[root@ceph-104 ~]# ip route get 111.192.168.101 111.192.168.101 dev enp2s3 src 111.192.168.104 uid 0 cache [root@ceph-104 ~]#
-
ip route add/delete
# 添加到主机的路由 [root@ceph-104 ~]# route add -host 目的IP dev 选择经过的网卡 [root@ceph-104 ~]# route add -host 111.192.168.101 dev enp2s3 [root@ceph-104 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.229.37.1 0.0.0.0 UG 101 0 0 enp2s1 10.229.37.0 0.0.0.0 255.255.255.0 U 101 0 0 enp2s1 111.192.168.0 0.0.0.0 255.255.255.0 U 100 0 0 enp2s3 111.192.168.101 0.0.0.0 255.255.255.255 UH 0 0 0 enp2s3 # 添加到主机的路由(经过指定网卡) 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 [root@ceph-104 ~]# route add -host 目的IP gw 经过的网关 [root@ceph-104 ~]# route add -host 111.192.168.101 gw 111.192.168.1 [root@ceph-104 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.229.37.1 0.0.0.0 UG 101 0 0 enp2s1 10.229.37.0 0.0.0.0 255.255.255.0 U 101 0 0 enp2s1 111.192.168.0 0.0.0.0 255.255.255.0 U 100 0 0 enp2s3 111.192.168.101 111.192.168.1 255.255.255.255 UGH 0 0 0 enp2s3 # 添加到主机的路由(经过网关) 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 #删除路由 [root@ceph-104 ~]# route del -host 111.192.168.101 删除一条匹配的路由 route del -host 111.192.168.101 gw 111.192.168.1 删除匹配的路由 [root@ceph-104 ~]# route del -host 111.192.168.101 dev enp2s3 删除匹配的路由 # 添加到主机的路由 #添加到某网段的路由 # 增加一条路由信息(发送到10.229.37.0网段的通信包全都要经过10.229.37.1这个网关) [root@ceph-104 ~]# route add -net 10.229.37.0 netmask 255.255.255.0 gw 10.229.37.1 [root@ceph-104 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.229.37.1 0.0.0.0 UG 101 0 0 enp2s1 10.229.37.0 10.229.37.1 255.255.255.0 UG 0 0 0 enp2s1 # 新增的路由信息 10.229.37.0 0.0.0.0 255.255.255.0 U 101 0 0 enp2s1 111.192.168.0 0.0.0.0 255.255.255.0 U 100 0 0 enp2s3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 # 删除一条路由信息 [root@ceph-104 ~]# route del -net 10.229.37.0 netmask 255.255.255.0 [root@ceph-104 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.229.37.1 0.0.0.0 UG 101 0 0 enp2s1 10.229.37.0 0.0.0.0 255.255.255.0 U 101 0 0 enp2s1 111.192.168.0 0.0.0.0 255.255.255.0 U 100 0 0 enp2s3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 #添加到某网段的路由
-
设置永久路由的方法
(1) 在/etc/rc.local里添加
# 配置的路由信息 route add -net 192.168.3.0/24 dev eth0 route add -net 192.168.2.0/24 gw 192.168.3.254
(2)在/etc/sysconfig/network里添加到末尾
GATEWAY=gw-ip` 或者 `GATEWAY=gw-dev
(3)/etc/sysconfig/static-router :
# 设置静态路由 any net x.x.x.x/24 gw y.y.y.y