zoukankan      html  css  js  c++  java
  • Centos7如何配置路由

    一,临时方式

    1,用命令修改路由(重启后失效)

    ip route add 192.168.30.0/24 via 192.168.30.254 dev enp1s0f0
    ip route add 10.100.5.0/24 via 192.168.30.254 dev enp1s0f0
    ip route add 10.100.6.0/24 via 192.168.30.254 dev enp1s0f0
    ip route add 10.100.7.0/24 via 192.168.30.254 dev enp1s0f0

     

    2,查看

    [root@localhost network-scripts]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref     Use Iface
    0.0.0.0         xx.xx.xx.254    0.0.0.0         UG    350    0        0  bond1
    xx.xx.xx.0      0.0.0.0         255.255.255.0   U     350    0        0  bond1
    10.100.5.0      192.168.30.254  255.255.255.0   UG    100    0        0  enp1s0f0
    10.100.6.0      192.168.30.254  255.255.255.0   UG    100    0        0  enp1s0f0
    10.100.7.0      192.168.30.254  255.255.255.0   UG    100    0        0  enp1s0f0
    192.168.30.0    0.0.0.0         255.255.255.0   U     100    0        0  enp1s0f0
    

      

    二, 永久性方式

    其实,配置默认路由的方式有很多种,这里我们仅来谈谈Redhat7官方给的建议,需要配置到:/etc/sysconfig/network-scripts/route-eth0,下面来看一下官方的说法 。

    如果需要根据接口设置的配置文件,例如 /etc/sysconfig/network-scripts/route-eth0 中第一行定义默认网关的路由。只有不是使用 DHCP 设置的网关需要此操作,且不会在/etc/sysconfig/network 文件中进行全局设置:

    default via 192.168.1.1 dev interface
    

      其中 192.168.1.1 是默认网关的 IP 地址。interface 是连接到,或可连接网关的接口。可省略 dev 选项,它是自选项。注:这个设置可覆盖 /etc/sysconfig/network 文件中的设置。

    如果需要路由到远程网络,可按以下方式指定静态路由。每行都解析为一个独立路由:

     

    10.10.10.0/24 via 192.168.1.1 [dev interface]
    

      

     其中 10.10.10.0/24 是网络地址及远程或目标网络的前缀长度。地址 192.168.1.1 是远程网络的第一个 IP 地址。首选 下一个跃点地址,但也可以使用退出接口。“下一个跃点”的含义是链接的远端点,例如网关或路由器。可使用 dev 选项指定退出接口 interface,但不一定要这么做。根据需要添加所有静态路由。

    以下是使用 ip 命令参数格式的 route-interface 文件示例。默认网关是 192.168.0.1,接口为eth0,以及 192.168.0.10 中的租用专线或 WAN连接。两个静态路由是用来连接 10.10.10.0/24 网络和 172.16.1.10/32 主机:

    default via 192.168.0.1 dev eth0
    10.10.10.0/24 via 192.168.0.10 dev eth0
    172.16.1.10/32 via 192.168.0.10 dev eth0

    在以上示例中,会将进入本地 192.168.0.0/24 网络的数据包指向附加到那个网络的接口。会将进入10.10.10.0/24 网络和 172.16.1.10/32 主机的数据包指向 192.168.0.10。进入未知、远程、网络的数据包将使用默认网关,因此只应在默认路由不适用时为远程网络或主机配置静态路由。在这里远程是指没有直接连接到该系统的网络或主机。

    指定退出接口为自选项,要强制让流量离开某个具体接口时有用。例如:在使用 VPN 时,可强制让流量通过远程网络使用 tun0接口,即便该接口处于不同于目标网络的子网中。

    注意:如果已由 DHCP 分配默认网关,且在配置文件中指定了使用同一跃点的同一网关,则会在启动时出错,或者在激活某个接口时出错,并显示以下出错信息:"RTNETLINK answers: File exists"。可忽略该信息。

    1,修改配置文件

    [root@localhost network-scripts]# pwd
    /etc/sysconfig/network-scripts
    [root@localhost network-scripts]# vim route-enp1s0f0   # 注意这个配置文件是不存在,需要自己创建route-interface(后面跟网卡名)
    
    192.168.30.0/24 via 192.168.30.254 dev enp1s0f0
    10.100.5.0/24 via 192.168.30.254 dev enp1s0f0
    10.100.6.0/24 via 192.168.30.254 dev enp1s0f0
    10.100.7.0/24 via 192.168.30.254 dev enp1s0f0

      

      2,重启网卡

    # systemctl restart network
    nmcli device conncet enp1s0f0
    

      

      3,检查

    [root@localhost network-scripts]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref     Use Iface
    0.0.0.0         xx.xx.xx.254    0.0.0.0         UG    350    0        0  bond1
    xx.xx.xx.0      0.0.0.0         255.255.255.0   U     350    0        0  bond1
    10.100.5.0      192.168.30.254  255.255.255.0   UG    100    0        0  enp1s0f0
    10.100.6.0      192.168.30.254  255.255.255.0   UG    100    0        0  enp1s0f0
    10.100.7.0      192.168.30.254  255.255.255.0   UG    100    0        0  enp1s0f0
    192.168.30.0    0.0.0.0         255.255.255.0   U     100    0        0  enp1s0f0

      

  • 相关阅读:
    什么是webview
    juqery.fn.extend和jquery.extend
    LeetCode
    5. Longest Palindromic Substring
    42. Trapping Rain Water
    11. Container With Most Water
    621. Task Scheduler
    49. Group Anagrams
    739. Daily Temperatures
    3. Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/yang-ning/p/6639254.html
Copyright © 2011-2022 走看看