Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Linux / Configuring Routing on Linux (RHEL/CentOS)

March 13, 2024

Configuring Routing on Linux (RHEL/CentOS)

In this article, we will show how to configure routing and manage network routes on Linux using the ip command on Linux CentOS (how to view the routing table, add/remove static routes, etc.). The article is applicable to any other Linux distro with ip tool (Red Hat, Fedora, etc.)

To manage routing in Linux, it is recommended to use the ip command instead of route. The route command doesn’t allow to configure advanced routing features (like routing policies) and cannot show special routing settings if they were set using the ip tool.

Contents:
  • How to View the Network Routing Table in Linux?
  • Adding and Removing Static Routes in Linux
  • Modifying an Existing Route Entry in Linux
  • How to Change the Default Route or Default Gateway on Linux?

How to View the Network Routing Table in Linux?

To display the current routing table in Linux, run this command:

# ip route

ip route comand in Linux - swow routing table

  • default via 192.168.1.1 dev enp0s3 is the default gateway using the enp0s3 interface in this example. If the target IP address does not have a static route in the routing table, the packet is sent through that gateway (the default route);
  • 192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.201 is a static route for the 192.168.1.0/24 network through the interface 192.168.1.201;
  • proto kernel is a route created by the kernel (proto static – the route added by an administrator);
  • metric is the route priority (the less the metric value, the higher the priority is). If there are two routes with the same metric (never do it!), the kernel will select routes randomly.

To find out which interface (gateway) is used to route traffic to the specific IP address, the following command is used:
# ip route get 192.168.2.45

192.168.2.45 via 192.168.1.1 dev enp0s3 src 192.168.1.201
You can use your Linux server with two or more interfaces as a router or an Internet gateway. To enable packet routing between multiple interfaces, enable the net.ipv4.ip_forward = 1 kernel parameter.

Adding and Removing Static Routes in Linux

To add a new route to a specific IP network in the Linux routing table, run this command:

# ip route add 192.168.0.0/24 via 192.168.1.1

Thus, we will add the route for the 192.168.0.0/24 IP network through the gateway 192.168.1.1.

The format of the ip route command is very much like Cisco IOS syntax. You can also use abbreviations here, for example, you can use ip pro ad instead of ip route add.

ip rout add static route on Linux

You can also add a separate static route for a single IP address (host):

# ip route add 192.168.1.0 via 192.168.1.1

You can create a null route similar to Cisco (ip route null0). The packets sent to this network are dropped due to No route to host:

# ip route add blackhole 10.1.20.0/24

The routes added using this method are temporary and will work until you restart the network service or the server.

To remove a static route created manually, run the following command:

# ip route del 192.168.0.0/24

remove a static route on linux

As you can see, the route has been removed from the Linux routing table.

To add a persistent route, you must create a file for the route, or add a rule to the rc.local file (run on host startup).

To add a permanent static route, you need a name of the network interface to be used for the routing. You can get the network interface name using this command:

# ip a

In my case, it is enp0s3.

get network interface name

Then open the following file:

# nano /etc/sysconfig/network-scripts/route-enp0s3

And add the line containing the route here:

192.168.0.0/24 via 192.168.1.1

After you have added the route to the file, restart the network service:

# service network restart

add persistent route on linux

After restarting the network, a static route appeared in the routing table.

You can also use a command to add a new route to the rc.local file to automatically add a static route when the server boots. Open the file:

# nano /etc/rc.local

Enter the command that adds the static route:

# ip route add 192.168.0.0/24 via 192.168.1.1

add permanent route to stratup via rc.local file

Then if your server is restarted, the route will be automatically added during the system boot.

Modifying an Existing Route Entry in Linux

To change an existing route, you can use the ip route replace command:

# ip route replace 192.168.0.0/24 via 192.168.1.1

ip route replace - change route

To reset all temporary routes in the routing table, just restart the network service:

# service network restart

Restarting network (via systemctl): [ OK ]

# ip route

default via 192.168.1.1 dev enp0s3 proto static metric 100
192.168.0.0/24 via 192.168.1.1 dev enp0s3 proto static metric 100
192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.201 metric 100

How to Change the Default Route or Default Gateway on Linux?

You can remove a default route using the ip route del command:

# ip route del default via 192.168.1.1 dev enp0s3

To set a new default route, the following command is used in CentOS/RHEL Linux:

# ip route add default via 192.168.1.2 (a route via gateway IP address)

# ip route add default via enp0s3 (a route using a device name)

To change the default route settings, this command is used:

# ip route replace default via 192.168.1.2

replace default route or gateway address in linux rhel (centos)

1 comment
3
Facebook Twitter Google + Pinterest
LinuxQuestions and Answers
previous post
Using Windows Defender Antivirus on Windows Server
next post
How to Disable the Open File Security Warnings on Windows

Related Reading

Installing an Open Source KMS Server (Vlmcsd) on...

March 13, 2024

Ubuntu/Mint/Kali Boots to Initramfs Prompt in BusyBox

March 11, 2024

How to Check Disk Performance (IOPS and Latency)...

March 11, 2024

Install and Configure SNMP on RHEL/CentOS/Fedor

March 13, 2024

Compress, Defrag and Optimize MariaDB/MySQL Database

March 11, 2024

How to Install and Use ClamAV Antivirus on...

March 13, 2024

How to Disable Microsoft Teams Auto Startup

March 12, 2024

Keepalived: Configuring High Availability with IP Failover on...

March 13, 2024

1 comment

ayoub May 16, 2023 - 2:01 pm

hi dear can i use my server to route packt and serv auter services in the samme timme if somme on can help 🙂

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Installing an Open Source KMS Server (Vlmcsd) on Linux
  • How to Access VMFS Datastore from Linux, Windows, or ESXi
  • How to Configure MariaDB Master-Master/Slave Replication
  • Using iPerf to Test Network Speed and Bandwidth
  • Moving WSL to Another Drive in Windows
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • Ubuntu/Mint/Kali Boots to Initramfs Prompt in BusyBox
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top