To add a default gateway to the route table in Red Hat Enterprise Linux (RHEL), CentOS, or Rocky Linux, you can use the ip command or modify the network configuration files. Here are both methods:
Method 1: Using the ip Command
- Open a terminal or connect to the server via SSH.
- Use the following command to add a default gateway temporarily (until the next reboot):
sudo ip route add default via <gateway_ip>
Replace <gateway_ip> with the actual IP address of your default gateway.
- To make the change persistent across reboots, you can add the command to a startup script or create a new route configuration file. For example, create a new file in the /etc/sysconfig/network-scripts/ directory (e.g., route-enp0s3) and add the following line:
GATEWAY=<gateway_ip>
Save the file.
- Restart the network service to apply the changes:
sudo systemctl restart network
Method 2: Modifying Network Configuration Files
- Open the network configuration file for editing. This file is typically located in the /etc/sysconfig/network-scripts/ directory and named something like ifcfg-enp0s3 (replace enp0s3 with your actual network interface).
sudo nano /etc/sysconfig/network-scripts/ifcfg-enp0s3
2. Add or modify the GATEWAY parameter with the IP address of your default gateway:
GATEWAY=<gateway_ip>
3. Restart the network service to apply the changes:
sudo systemctl restart network
After following either of these methods, your default gateway should be set, and the changes will persist across reboots.
Note: It’s important to replace <gateway_ip> with the actual IP address of your default gateway. If you’re unsure about the gateway IP, you can check your router or contact your network administrator.
Here is the Demo:
Add Default Gateway to Route table in Redhat / CentOs / RockyLinux
To add default gateway to your routing table in any of the above OS use the following command.
[root@Linuxking ~]# route add default gw 10.1.1.100 eth0
Note: Gateway machine’s IP is 10.1.1.100 and your ethernet is eth0
[root@Linuxking ~]# route -n
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.1.1.100 0.0.0.0 UG 0 0 0 eth0
However if you restart the network the route will be removed as well. So to make it persistent do the following
Create a file
[root@Linuxking ~]#vim /etc/sysconfig/network-scripts/route-eth0
and add the following lines
0.0.0.0/0 via 10.1.1.100 dev eth0
:wq (write and quit the vim)
Now restart the network the route will be persistent.
[root@Linuxking ~]# route -n
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.1.1.100 0.0.0.0 UG 0 0 0 eth0