Linux操作系统

<p><span style="font-size:16px"><strong>添加临时路由</strong></span></p> <p>ECS_A 主机执行以下命令。</p> <table border="1" cellpadding="1" cellspacing="1" style="width:809px"> <tbody> <tr> <td style="background-color:#ededed; width:801px"> <p># 查看 eth0 和 eth1 网卡的网关,可以从DHCP信息里面获取</p> <p><strong>grep routers /var/lib/dhclient/dhclient-eth0.leases | tail -1 | awk &#39;{print $3}&#39; | sed &#39;s/;//g&#39;</strong></p> <p>10.16.117.1</p> <p><strong>grep routers /var/lib/dhclient/dhclient-eth1.leases | tail -1 | awk &#39;{print $3}&#39; | sed &#39;s/;//g&#39;</strong></p> <p>10.17.56.1</p> <p># 此例当中,eth0 网卡的网关是 10.16.117.1, eth1 网卡的网关 10.17.56.1</p> <p>&nbsp;</p> <p># 添加到ECS_B主机网段的路由规则</p> <p><strong>ip route add 10.17.60.0/25 via 10.17.56.1 dev eth1</strong></p> <p>&nbsp;</p> <p># 确认路由添加成功</p> <p><strong>ip route show | grep 10.17.60.0/25</strong></p> <p># 输出内容为10.17.60.0/25 via 10.17.56.1 dev eth1</p> <p>&nbsp;</p> <p># 因平安云agent等功能需要默认网卡连接服务器端,需要确认默认路由走eth0</p> <p><strong>ip route show | grep default</strong></p> <p># 输出内容为default via 10.16.117.1 dev eth0</p> <p>&nbsp;</p> <p># 如果缺少默认路由,则需要手动加上</p> <p><strong>ip route add default via 10.16.117.1 dev eth0</strong></p> </td> </tr> </tbody> </table> <p>ECS_B 主机添加临时路由方法与ECS_A一样,只是目的网络和网关不一样。</p> <table border="1" cellpadding="1" cellspacing="1" style="width:811px"> <tbody> <tr> <td style="background-color:#ededed; width:803px"> <p># 按照在ECS_A主机上面获取eth0,eth1默认网关的方法,首先同样获取ECS_B的默认网关</p> <p># 命令不再重复列出。</p> <p># 本例当中,ECS_B 的 eth0 网关为10.16.192.1,eth1 网关为10.17.60.1</p> <p>&nbsp;</p> <p># 添加到ECS_A主机网段的路由规则</p> <p><strong>ip route add 10.17.56.0/25 via 10.17.60.1 dev eth1</strong></p> <p>&nbsp;</p> <p># 确认路由添加成功</p> <p><strong>ip route show | grep 10.17.56.0/25</strong></p> <p># 输出内容为10.17.56.0/25 via 10.17.60.1 dev eth1</p> <p>&nbsp;</p> <p># 因平安云agent等功能需要默认网卡连接服务器端,需要确认默认路由走eth0</p> <p><strong>ip route show | grep default</strong></p> <p># 输出内容为default via 10.16.192.1 dev eth0</p> <p>&nbsp;</p> <p># 如果缺少默认路由,则需要手动加上</p> <p><strong>ip route add default via 10.16.192.1 dev eth0</strong></p> </td> </tr> </tbody> </table> <p>&nbsp;</p> <p><span style="font-size:16px"><strong>添加静态路由</strong></span></p> <p>添加临时路由后可以通过eth1访问到对方主机,但是如果ECS主机重启,则这条路由策略会丢失,需要将它写入配置文件。</p> <p><span style="font-size:16px"><strong>注意事项</strong></span></p> <p>静态路由并不是在添加文件后就立即生效,它的作用是在网卡被拉起的过程(可以是OS启动或者是执行ifup)中自动添加路由。如果需要立即能够通过eth1网卡相互访问,并且在OS或者网卡重启后也能保留这条路由,请先添加临时路由,再添加静态路由。</p> <p><span style="font-size:16px"><strong>CentOS/Red Hat Enterprise Linux 系统</strong></span></p> <p>在ECS_A主机,创建eth1网卡的路由配置文件 /etc/sysconfig/network-scripts/route-eth1。</p> <p>添加以下内容</p> <table border="1" cellpadding="1" cellspacing="1" style="width:810px"> <tbody> <tr> <td style="background-color:#ededed; width:802px">10.17.60.0/25 via 10.17.56.1</td> </tr> </tbody> </table> <p>在ECS_B主机,同样创建eth1网卡的路由配置文件 /etc/sysconfig/network-scripts/route-eth1。</p> <p>添加以下内容</p> <table border="1" cellpadding="1" cellspacing="1" style="width:810px"> <tbody> <tr> <td style="background-color:#ededed; width:802px">10.17.56.0/25 via 10.17.60.1</td> </tr> </tbody> </table> <p><span style="font-size:16px"><strong>Debian/Ubuntu 系统</strong></span></p> <p>对于Debian以及 16.04 或者以下版本的Ubuntu系统,在ECS_A主机编辑 /etc/network/interfaces 文件,添加如下红色的两行内容。</p> <table border="1" cellpadding="1" cellspacing="1" style="width:808px"> <tbody> <tr> <td style="background-color:#ededed; width:800px"> <p># This file describes the network interfaces available on your system</p> <p># and how to activate them. For more information, see interfaces(5).</p> <p>&nbsp;</p> <p>source /etc/network/interfaces.d/*</p> <p>&nbsp;</p> <p># The loopback network interface</p> <p>auto lo</p> <p>iface lo inet loopback</p> <p>&nbsp;</p> <p># The primary network interface</p> <p>auto eth0</p> <p>iface eth0 inet dhcp</p> <p>&nbsp;</p> <p>auto eth1</p> <p>iface eth1 inet dhcp</p> <p><span style="color:#f44336">up ip route add 10.17.60.0/25 via 10.17.56.1 dev eth1</span></p> <p><span style="color:#f44336">up ip route change default via 10.16.117.1 dev eth0</span></p> </td> </tr> </tbody> </table> <p>ECS_B主机同样的配置文件和位置,添加如下配置行。</p> <table border="1" cellpadding="1" cellspacing="1" style="width:807px"> <tbody> <tr> <td style="background-color:#ededed; width:799px"> <p>up ip route add 10.17.56.0/25 via 10.17.60.1 dev eth1</p> <p>up ip route change default via 10.16.192.1 dev eth0</p> </td> </tr> </tbody> </table> <p>对于 Ubuntu 18.04 , 它的默认网络管理工具已经换成了netplan,因此静态路由添加需要修改 /etc/netplan/01-netcfg.yaml 配置文件,在 eth1 配置段增加红色配置行。</p> <table border="1" cellpadding="1" cellspacing="1" style="width:807px"> <tbody> <tr> <td style="background-color:#ededed; width:799px"> <p>network:</p> <p>&nbsp; version: 2</p> <p>&nbsp; renderer: networkd</p> <p>&nbsp; ethernets:</p> <p>&nbsp;&nbsp;&nbsp; eth0:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dhcp4: true</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nameservers:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; search: [cloud.papub]</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addresses: [100.68.10.22, 100.68.10.23]</p> <p>&nbsp;&nbsp;&nbsp; eth1:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dhcp4: true</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; optional: true</p> <p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;routes:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#f44336">&nbsp;&nbsp;&nbsp; - to: 10.17.60.0/25</span></p> <p><span style="color:#f44336">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; via: 10.17.56.1</span></p> </td> </tr> </tbody> </table> <p>ECS_B 主机同样的配置文件和位置,添加如下配置行。</p> <table border="1" cellpadding="1" cellspacing="1" style="width:807px"> <tbody> <tr> <td style="background-color:#ededed; width:799px"> <p>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;routes:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - to: 10.17.56.0/25</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; via: 10.17.60.1</p> </td> </tr> </tbody> </table> <p>&nbsp;</p>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询 - 7x24 小时

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

7x24 小时,急速解答

工单支持

解决云产品相关技术问题