Route add linux

The route add command in Linux is used to manually add static routes to the system’s routing table. These routes define how network traffic should be directed to reach specific destinations. While route is still available, the modern and preferred method for managing routes is the ip route command (from the iproute2 package). However, it’s useful to understand route as you might encounter it in older scripts or systems.

Using Route add (Legacy Method):

The basic syntax for adding a route using route is:

Sudo route add -net network_address netmask netmask_value gw gateway_address

Sudo route add -host host_address gw gateway_address

Arguments:

    sudo: Required to execute the command with root privileges, as modifying the routing table requires administrative access. add: Specifies that you want to add a new route. — net network_address: Specifies that you’re adding a route to a network. network_address is the IP address of the network you want to reach. netmask netmask_value: Specifies the netmask for the network. netmask_value can be in dotted decimal notation (e. g., 255.255.255.0) or as a CIDR prefix length (e. g., 24). — host host_address: Specifies that you’re adding a route to a specific host. host_address is the IP address of the host. gw gateway_address: Specifies the IP address of the gateway (router) that should be used to reach the network or host. This is the next hop in the path to the destination. If you are on the same network, you often do not need to specify a gateway. dev interface_name: Specifies the network interface to use for this route. If not specified, the system will try to determine the correct interface.

Examples:

Adding a route to a network:

Suppose you want to add a route to the network 192.168.10.0/24 through the gateway 192.168.1.1:

Sudo route add — net 192.168.10.0 netmask 255.255.255.0 gw 192.168.1.1

Or, using CIDR notation:

Sudo route add — net 192.168.10.0/24 gw 192.168.1.1

Adding a route to a specific host:

Suppose you want to add a route to the host 10.0.0.5 through the gateway 192.168.1.1:

Sudo route add — host 10.0.0.5 gw 192.168.1.1

Adding a default route (gateway of last resort):

A default route is used for traffic that doesn’t match any other route in the routing table. It directs traffic to the internet or a larger network.

Sudo route add default gw 192.168.1.1

Specifying an interface:

5. sudo route add — net 10.0.0.0 netmask 255.255.255.0 dev eth0

This directs traffic for the 10.0.0.0/24 network to the eth0 interface. No gateway is specified because the destination network is assumed to be directly connected to that interface.

Important Considerations with Route add:

    Non-Persistent: Routes added with route add are Not persistent across reboots. When the system restarts, the routes will be lost. Configuration Files for Persistent Routes: To make routes persistent, you need to configure them in network configuration files. The location and format of these files vary depending on the Linux distribution:

      Debian/Ubuntu: Edit the /etc/network/interfaces file or use ip route and configure via /etc/network/if-up. d/ scripts. CentOS/RHEL/Fedora: Create route files in the /etc/sysconfig/network-scripts/ directory (e. g., route-eth0). Other Distributions: Consult your distribution’s documentation for the correct way to configure persistent routes.

    Default Gateway: Typically, the default gateway is configured automatically by DHCP or through the network configuration files. Manually adding a default gateway with route add should be done only if necessary and with caution. Conflicting Routes: Be careful not to create conflicting routes, as this can lead to routing problems. Modern Replacement: Ip route: The ip route command (part of the iproute2 suite) is the modern and preferred way to manage routes in Linux. It’s more powerful and flexible than route.

The Ip route Command (Modern Alternative):

The ip route command provides a more versatile and feature-rich way to manage routes.

    Adding a route (equivalent to Route add):

· sudo ip route add network_address/netmask via gateway_address

Example:

Sudo ip route add 192.168.10.0/24 via 192.168.1.1

    Adding a route to a host:

· sudo ip route add host host_address via gateway_address

Example:

Sudo ip route add host 10.0.0.5 via 192.168.1.1

    Adding a default route:

· sudo ip route add default via gateway_address

Example:

Sudo ip route add default via 192.168.1.1

    Listing Routes:

· ip route show

Or simply:

Ip route

    Deleting a route:

· sudo ip route del network_address/netmask via gateway_address

Example:

Sudo ip route del 192.168.10.0/24 via 192.168.1.1

Example: Setting up a static route with Ip route (and making it persistent on Debian/Ubuntu):

Add the route using Ip route:

2. sudo ip route add 192.168.5.0/24 via 192.168.1.1

Create a script to add the route on boot:

4. sudo nano /etc/network/if-up. d/static-route

Add the following lines to the script:

#!/bin/sh

Ip route add 192.168.5.0/24 via 192.168.1.1

Exit 0

Make the script executable:

6. sudo chmod +x /etc/network/if-up. d/static-route

Now, the route will be added automatically each time the network interface comes up. A similar approach would be used for other distributions, but the location and syntax of the startup script may differ. The key is to find the appropriate place to add the ip route command so it’s executed during the boot process.

Which to use: Route or Ip route?

In modern Linux systems, ip route is the preferred tool due to its greater flexibility and features. While route commands may still work, they are considered legacy. New scripts and configurations should use ip route.

Оставьте комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Прокрутить вверх