ip route 命令介紹


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
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM