欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ip route 命令介绍

程序员文章站 2022-06-04 08:45:36
...

ip route可以用于查看网络的路由信息,并设置路由表

  • route n 显示所有路由

    [[email protected] ~]# 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 查看路由信息

    [[email protected] ~]# 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地址的路由信息

    [[email protected] ~]# ip route get 111.192.168.101
    111.192.168.101 dev enp2s3 src 111.192.168.104 uid 0
        cache
    [[email protected] ~]#
    
  • ip route add/delete

    # 添加到主机的路由
    [[email protected] ~]# route add -host 目的IP dev 选择经过的网卡
    [[email protected] ~]# route add -host 111.192.168.101 dev enp2s3
    [[email protected] ~]# 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
    
    [[email protected] ~]# route add -host 目的IP gw 经过的网关
    [[email protected] ~]# route add -host 111.192.168.101 gw 111.192.168.1
    [[email protected] ~]# 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
    #删除路由
    [[email protected] ~]# route del -host 111.192.168.101   删除一条匹配的路由
    route del -host 111.192.168.101 gw 111.192.168.1     删除匹配的路由
    [[email protected] ~]# route del -host 111.192.168.101 dev enp2s3 删除匹配的路由
    # 添加到主机的路由
    
    
    #添加到某网段的路由
    # 增加一条路由信息(发送到10.229.37.0网段的通信包全都要经过10.229.37.1这个网关)
    [[email protected] ~]# route add -net 10.229.37.0 netmask 255.255.255.0 gw 10.229.37.1
    [[email protected] ~]# 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
    
    # 删除一条路由信息
    [[email protected] ~]# route del -net 10.229.37.0 netmask 255.255.255.0
    [[email protected] ~]# 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
    
相关标签: linux 网络