Linux ❀ route + ip route 系统路由配置内容梳理

Posted 无糖可乐没有灵魂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux ❀ route + ip route 系统路由配置内容梳理相关的知识,希望对你有一定的参考价值。

1、网络环境准备


查看网卡地址与路由信息

#查看网卡信息
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:55:09:64 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:55:57:ac brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:55:57:ac brd ff:ff:ff:ff:ff:ff

#查看路由信息
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

2、方法一:route


注意:使用route命令添加的路由,机器重启或网卡重启后路由会失效

[root@localhost ~]# route help
Usage: route [-nNvee] [-FC] [<AF>]           List kernel routing tables
       route [-v] [-FC] add|del|flush ...  Modify routing table for AF.

       route -h|--help [<AF>]              Detailed usage syntax for specified AF.
       route -V|--version                  Display version/author and exit.

        -v, --verbose            be verbose
        -n, --numeric            don't resolve names
        -e, --extend             display other/more information
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB

  <AF>=Use -4, -6, '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) 
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) 
    x25 (CCITT X.25) 

操作案例

#添加10.1.1.0/24网段静态路由,网关为192.168.122.1
[root@localhost ~]# route 
add        default    del        dev        dyn        gw         -host      irtt       metric     mod        mss        -net       netmask    reinstate  reject     window
[root@localhost ~]# route add 
default    del        dev        dyn        gw         -host      irtt       metric     mod        mss        -net       netmask    reinstate  reject     window     
[root@localhost ~]# route add -net 10.1.1.0/24 gw 192.168.122.1
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.1.1.0        192.168.122.1   255.255.255.0   UG    0      0        0 virbr0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

#添加10.1.1.1主机路由(32位)出口设备为virbr0
[root@localhost ~]# route add -host 10.1.1.1 dev 
ens160      lo          virbr0      virbr0-nic  
[root@localhost ~]# route add -host 10.1.1.1 dev virbr0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.1.1.0        192.168.122.1   255.255.255.0   UG    0      0        0 virbr0
10.1.1.1        0.0.0.0         255.255.255.255 UH    0      0        0 virbr0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

#添加默认路由,两种方法均可实现
[root@localhost ~]# route add -net 0.0.0.0/0 gw 192.168.122.1 
[root@localhost ~]# route add default gw 192.168.122.1 

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.122.1   0.0.0.0         UG    0      0        0 virbr0
10.1.1.0        192.168.122.1   255.255.255.0   UG    0      0        0 virbr0
10.1.1.1        0.0.0.0         255.255.255.255 UH    0      0        0 virbr0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

#删除某条路由
[root@localhost ~]# route del -net 10.1.1.0/24 gw 192.168.122.1
[root@localhost ~]# route del default gw 192.168.122.1 
[root@localhost ~]# route del -host 10.1.1.1 gw 192.168.122.1

3、方法二:ip route


注意:使用ip route命令添加的路由,机器重启或网卡重启后路由会失效

[root@localhost ~]# ip route help
Usage: ip route  list | flush  SELECTOR
       ip route save SELECTOR
       ip route restore
       ip route showdump
       ip route get [ ROUTE_GET_FLAGS ] ADDRESS
                            [ from ADDRESS iif STRING ]
                            [ oif STRING ] [ tos TOS ]
                            [ mark NUMBER ] [ vrf NAME ]
                            [ uid NUMBER ] [ ipproto PROTOCOL ]
                            [ sport NUMBER ] [ dport NUMBER ]
       ip route  add | del | change | append | replace  ROUTE
SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]
            [ table TABLE_ID ] [ vrf NAME ] [ proto RTPROTO ]
            [ type TYPE ] [ scope SCOPE ]
ROUTE := NODE_SPEC [ INFO_SPEC ]
NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]
             [ table TABLE_ID ] [ proto RTPROTO ]
             [ scope SCOPE ] [ metric METRIC ]
             [ ttl-propagate  enabled | disabled  ]
INFO_SPEC :=  NH | nhid ID  OPTIONS FLAGS [ nexthop NH ]...
NH := [ encap ENCAPTYPE ENCAPHDR ] [ via [ FAMILY ] ADDRESS ]
	    [ dev STRING ] [ weight NUMBER ] NHFLAGS
FAMILY := [ inet | inet6 | mpls | bridge | link ]
OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ] [ as [ to ] ADDRESS ]
           [ rtt TIME ] [ rttvar TIME ] [ reordering NUMBER ]
           [ window NUMBER ] [ cwnd NUMBER ] [ initcwnd NUMBER ]
           [ ssthresh NUMBER ] [ realms REALM ] [ src ADDRESS ]
           [ rto_min TIME ] [ hoplimit NUMBER ] [ initrwnd NUMBER ]
           [ features FEATURES ] [ quickack BOOL ] [ congctl NAME ]
           [ pref PREF ] [ expires TIME ] [ fastopen_no_cookie BOOL ]
TYPE :=  unicast | local | broadcast | multicast | throw |
          unreachable | prohibit | blackhole | nat 
TABLE_ID := [ local | main | default | all | NUMBER ]
SCOPE := [ host | link | global | NUMBER ]
NHFLAGS := [ onlink | pervasive ]
RTPROTO := [ kernel | boot | static | NUMBER ]
PREF := [ low | medium | high ]
TIME := NUMBER[s|ms]
BOOL := [1|0]
FEATURES := ecn
ENCAPTYPE := [ mpls | ip | ip6 | seg6 | seg6local ]
ENCAPHDR := [ MPLSLABEL | SEG6HDR ]
SEG6HDR := [ mode SEGMODE ] segs ADDR1,ADDRi,ADDRn [hmac HMACKEYID] [cleanup]
SEGMODE := [ encap | inline ]
ROUTE_GET_FLAGS := [ fibmatch ]

操作案例

#添加静态路由
[root@localhost ~]# ip route 
add      append   change   del      flush    get      help     list     monitor  replace  
[root@localhost ~]# ip route add 10.1.1.0/24 via 192.168.122.1
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.1.1.0        192.168.122.1   255.255.255.0   UG    0      0        0 virbr0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@localhost ~]# ip route add 10.1.1.1/32 dev virbr0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.1.1.0        192.168.122.1   255.255.255.0   UG    0      0        0 virbr0
10.1.1.1        0.0.0.0         255.255.255.255 UH    0      0        0 virbr0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

#添加默认路由
[root@localhost ~]# ip route add default via 192.168.122.1
[root@localhost ~]# ip route del default via 192.168.122.1
[root@localhost ~]# ip route add default dev virbr0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         0.0.0.0         0.0.0.0         U     0      0        0 virbr0
10.1.1.0        192.168.122.1   255.255.255.0   UG    0      0        0 virbr0
10.1.1.1        0.0.0.0         255.255.255.255 UH    0      0        0 virbr0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

4、添加永久生效路由


[root@localhost ~]# cat /etc/rc.d/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
#文件末尾添加路由配置,同第23节内容
route add -net 10.1.1.0/24 gw 192.168.122.1
route add -host 10.1.1.1 dev virbr0
ip route add 20.1.1.0/24 via 192.168.122.1 dev virbr0

#重启验证路由是否生效
[root@localhost ~]# reboot
[root@localhost ~]# shutdown -r now

以上是关于Linux ❀ route + ip route 系统路由配置内容梳理的主要内容,如果未能解决你的问题,请参考以下文章

Linux ❀ route + ip route 系统路由配置内容梳理

Linux ❀ route + ip route 系统路由配置内容梳理

linux shell route

linux常用命令:route 命令

linux route详细解读

linux学习笔记31--命令route