📜  linux ip route add - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:02:42.339000             🧑  作者: Mango

Linux IP Route Add

Introduction

In Linux, the ip command is used for controlling networking. The ip route add command is specifically used for adding a new entry to the routing table.

Routing is the process of sending network traffic from one device to another. Routing tables are used to determine the best path for network traffic to take in order to get from the source device to the destination device. The ip route add command is used to add new routes to this routing table.

Syntax

The basic syntax for the ip route add command is:

ip route add <destination network> via <next hop>
  • The <destination network> is the IP address of the network that the traffic is destined for.
  • The <next hop> is the IP address of the next router that the traffic should be sent to.

There are several other options and arguments that can be used with the ip route add command. These include:

  • dev: Specifies the network interface that should be used to send the traffic.
  • src: Specifies the source IP address that should be used when sending the traffic.
  • metric: Specifies the metric (cost) for the route.
  • table: Specifies the routing table that the route should be added to.
Examples

To illustrate how the ip route add command works, let's look at a few examples.

Example 1

Let's say that you have a router with two network interfaces:

  • eth0: IP address 10.0.0.1/24
  • eth1: IP address 192.168.1.1/24

And you want to add a route for traffic destined for the 172.16.0.0/16 network that should be sent to the 10.0.0.254 router.

The ip route add command would look like this:

ip route add 172.16.0.0/16 via 10.0.0.254 dev eth0
  • The <destination network> is 172.16.0.0/16.
  • The <next hop> is 10.0.0.254.
  • The dev option specifies that the traffic should be sent out the eth0 interface.
Example 2

Let's say that you have two routers:

  • Router 1: IP address 10.0.0.1/24
  • Router 2: IP address 192.168.1.1/24

And you want to add a route to the 10.10.10.0/24 network on Router 1 that should be sent to Router 2.

The ip route add command would look like this:

ip route add 10.10.10.0/24 via 192.168.1.1
  • The <destination network> is 10.10.10.0/24.
  • The <next hop> is 192.168.1.1.
Conclusion

The ip route add command is an essential tool for managing networking on Linux systems. By understanding how to use this command, you can effectively control how network traffic flows through your devices.