ubuntu添加路由

ubuntu添加路由

I have a box with Ubuntu Linux and I want to add a new route to my box. Because I want to access an external network from a different interface and network. How can I add a new route to my Ubuntu, Debian, Fedora, Cent-OS Linux box? Because ip-tools is the same for all of these Linux distributions.

我有一个装有Ubuntu Linux的盒子,我想向盒子中添加一条新路由。 因为我想从其他接口和网络访问外部网络。 如何在我的Ubuntu,Debian,Fedora,Cent-OS Linux盒中添加新路由? 因为ip-tools对于所有这些Linux发行版都是相同的。

使用ip命令显示/列出现有路由表和信息 (Display/List Existing Routing Table and Information By Using ip Command)

First, we should display existing routing table. Our target network should not intersect existing routing information. The routing table contains different routes to the remote networks with the same or different gateways. In general, the same default gateway is used.

首先,我们应该显示现有的路由表。 我们的目标网络不应与现有的路由信息​​相交。 路由表包含到具有相同或不同网关的远程网络的不同路由。 通常,使用相同的默认网关。

$ ip route show
Display Existing Routing Table
Display Existing Routing Table
显示现有路由表

We can see that there is a default route that is listed in the first line. It also named as default . The default gateway IP address is 192.168.142.2

我们可以看到*行列出了默认路由。 它也被命名为default 。 默认网关IP地址为192.168.142.2

使用route命令显示/列出现有路由表和信息 (Display/List Existing Routing Table and Information By Using route Command)

Linux provides alternative ways and commands to list the existing routing tables and information. We can use the command route in order to list the existing routing table with information like destination, gateway, netmask, flags, metric, interface. route command is provided by the net-tools in Ubuntu which is not installed by default. We can install the net-tools package with the following command to use route command.

Linux提供了其他方法和命令来列出现有的路由表和信息。 我们可以使用命令route来列出现有的路由表,其中包含诸如目的地,网关,网络掩码,标志,度量,接口之类的信息。 route命令由Ubuntu中的net-tools提供,默认情况下未安装。 我们可以使用以下命令安装net-tools软件包以使用route命令。

$ sudo apt install net-tools
%title插图%num
Install net-tools Package For route Command
为路由命令安装net-tools软件包

We will only type the command route to the Linux shell.

我们将只键入到Linux Shell的命令route 。

$ route
%title插图%num
List Route Information with route Command
使用route命令列出路线信息

使用netstat命令显示/列出现有路由表和信息(Display/List Existing Routing Table and Information By Using netstat Command)

Alternatively, the command netstat can be also used to list the routing table and information. In order to list routing information the options -n and -r should be provided to the command netstat.

或者,也可以使用命令netstat列出路由表和信息。 为了列出路由信息,应该在命令netstat中提供选项-n-r 。

$ netstat -nr

使用ip命令添加新路由 (Add New Route with ip Command)

In order to add a new route, we will use the command ip route add by providing related information. We will add a new route to the 172.16.0.0/16 network.

为了添加新路由,我们将通过提供相关信息使用命令ip route add 。 我们将向172.16.0.0/16网络添加一条新路由。

$ sudo ip route add 172.16.0.0/24 via 192.168.122.1 dev ens3
  • ip route add  is our command to add a new route.

    ip route add是我们的命令,用于添加新路由。

  • 172.16.0.0/24 is a target network that is the destination range.

    172.16.0.0/24是作为目标范围的目标网络。

  • via 192.168.122.1 specifies the next hoop which should be directly connected to our system network.

    通过192.168.122.1指定应该直接连接到我们的系统网络的下一个箍。

  • dev ens3 specifies our interface which is the same network with 192.168.122.1

    dev ens3指定我们的接口,该接口与192.168.122.1是同一网络

LEARN MORE  How To Start, Stop, Restart Networking On Linux?
了解更多如何在Linux上启动,停止,重新启动网络?

使用route命令添加新路线 (Add New Route By Using route Command)

Alternatively, we can add a new route by using the command route. We will use the option add of the route command where also provide other parameters like destination network, gateway, and interface name. In the following example, we will add a route to the network 10.0.0.0 by using the -net option by setting 192.168.1.1 as gateway for the interface eth0.

另外,我们可以使用命令route添加新路由。 我们将使用route命令的选项add ,其中还提供其他参数,例如目标网络,网关和接口名称。 在以下示例中,我们将通过使用-net选项(将192.168.1.1设置为接口eth0网关)使用-net选项添加到网络10.0.0.0的路由。

$ sudo route add -net 10.0.0.0/8 gw 192.168.1.1 eth0

检查新路线 (Check New Route)

A newly added route will be activated instantly after adding the routing table. We can also list and check the newly added route information by using the command ip route show like below. We will see that the new route is added successfully.

添加路由表后,将立即激活新添加的路由。 我们还可以使用以下命令ip route show列出并检查新添加的路由信息​​。 我们将看到新路由已成功添加。

  1. $ ip route show
  2. default via 192.168.122.1 dev ens3
  3. 10.0.3.0/24 dev lxcbr0  proto kernel  scope link  src 10.0.3.1
  4. 172.16.0.0/24 via 192.168.122.1 dev ens3
  5. 192.168.122.0/24 dev ens3  proto kernel  scope link  src 192.168.122.211

使用route命令删除/删除现有路由 (Delete/Remove Existing Route By Using route Command)

We can delete an existing route by using the command route. It is very similar to the adding route where we will replace the add options with the del option.

我们可以使用命令route删除现有路由。 这与添加路由非常相似,在添加路由中,我们将用del选项替换add选项。

$ sudo route del -net 10.0.0.0/8 gw 192.168.1.1 eth0

You can use the following infographic in order to get brief information about adding new routes for Linux and Ubuntu.

您可以使用以下信息图来获取有关为Linux和Ubuntu添加新路由的简要信息。

How To Add New Route In Ubuntu, Linux? Infografic
How To Add New Route In Ubuntu, Linux? Infographic
如何在Ubuntu,Linux中添加新路由? 信息图