How to Add a Permanent Route in Linux CentOS 7?
Introduction
In Linux, configuring a permanent route is essential for accessing a network or a service that is not directly connected to your system. A route is a mapping of an IP address to a gateway, which enables your system to forward traffic to that IP address. In this article, we will explore the steps to add a permanent route in Linux CentOS 7.
Understanding Routes in Linux
A route is a fundamental concept in Linux networking. It is a mapping of an IP address to a gateway, which enables your system to forward traffic to that IP address. Routes are used to connect multiple networks together, allowing data to be transmitted between them. There are two types of routes: static and dynamic.
- Static Routes: These are routes that are manually configured on the system and are always active.
- Dynamic Routes: These are routes that are created automatically by the routing daemon (e.g., RIP, OSPF, and EIGRP) based on the state of the network.
Why Add a Permanent Route?
Adding a permanent route in Linux CentOS 7 is necessary when you want to:
- Access a network or service that is not directly connected to your system
- Forward traffic to a specific IP address or network
- Configure a VPN or proxy server
Adding a Permanent Route in Linux CentOS 7
To add a permanent route in Linux CentOS 7, you can use the ip command or the /etc/sysconfig/network-scripts/route file. We will discuss both methods below.
Method 1: Using the ip Command
The ip command is a powerful tool for managing network interfaces and routes in Linux. To add a permanent route using the ip command, follow these steps:
- Check the current routes: Before adding a new route, it is essential to check the current routes on your system. Use the following command to do so:
ip route show - Add the route: Use the following command to add a new route:
ip route add <destination_ip>/<prefix> via <gateway_ip>Replace<destination_ip>with the IP address of the network you want to access,<prefix>with the prefix length (e.g., 24 for a Class C network), and<gateway_ip>with the IP address of the gateway. - Make the route persistent: To make the route persistent across reboots, add the
-toption:ip route add -t <route_flags> <destination_ip>/<prefix> via <gateway_ip>
Example:
ip route add 192.168.1.0/24 via 192.168.1.1
This command adds a route to the 192.168.1.0/24 network via the 192.168.1.1 gateway.
Method 2: Using the /etc/sysconfig/network-scripts/route File
The /etc/sysconfig/network-scripts/route file is used to store static routes in Linux. To add a permanent route using this file, follow these steps:
- Edit the route file: Open the
/etc/sysconfig/network-scripts/routefile in a text editor (e.g.,nano,vim) and add the following line:daily route -A <interface> <destination_ip> <gateway_ip>Replace<interface>with the name of the network interface (e.g.,eth0),<destination_ip>with the IP address of the network you want to access, and<gateway_ip>with the IP address of the gateway. - Restart the network service: To apply the changes, restart the network service:
service network restart
Example:
daily route -A eth0 192.168.1.0 192.168.1.1
This command adds a route to the 192.168.1.0/24 network via the 192.168.1.1 gateway on the eth0 interface.
Tips and Considerations
- Route Flags: The
ipcommand supports various route flags, including:persistent: Marks the route as persistent across rebootslocal: Marks the route as local to the system only- `proto**: Marks the route as belonged to a specific protocol (e.g., RIP, OSPF)
- Route Priority: You can set a priority for a route using the
-poption:ip route add -t priority <destination_ip>/<prefix> via <gateway_ip> - Route Metric: You can set a metric for a route using the
metricoption:ip route add metric <metric> <destination_ip>/<prefix> via <gateway_ip> - Route expiration: Routes can be configured to expire after a certain period using the
timeoption:ip route add time <seconds> <destination_ip>/<prefix> via <gateway_ip>
Conclusion
In this article, we have discussed how to add a permanent route in Linux CentOS 7 using the ip command and the /etc/sysconfig/network-scripts/route file. We have also covered various tips and considerations for configuring routes in Linux, including route flags, priorities, and metrics. By following these guidelines, you can successfully add a permanent route in Linux CentOS 7 and access networks or services that are not directly connected to your system.
