Adding A Default Gateway Route In Linux

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
  1. Open a terminal or connect to the server via SSH.
  2. 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.

  1. 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.

  1. Restart the network service to apply the changes:

sudo systemctl restart network

Method 2: Modifying Network Configuration Files
  1. 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

Impact Financial

Good draw knew bred ham busy his hour. Ask agreed answer rather joy nature admire wisdom.

Latest Posts

  • All Posts
  • Amazon
  • Amazon EFS
  • apache
  • AWS
  • aws backup
  • aws cli
  • aws cli version 2
  • aws sync
  • Banking
  • bash scripting
  • Budgeting
  • cloud
  • cyber attack
  • cyber scam
  • database
  • efs
  • Email
  • Email Client
  • Email Server
  • Insurance
  • Investing
  • Learning
  • lets encrypt
  • Linux
  • php
  • pip
  • postgresql
  • python
  • S3
  • s3 buckets
  • scam prevention
  • scams
  • security
  • ssl
  • Tax Strategies
  • Troubleshooting
  • website
  • wordpress
  • wordpress setup
    •   Back
    • Linux tips
    •   Back
    • nextcloud
    •   Back
    • Rainloop

Categories

Tags