Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

June 11, 2021 CentOSLinuxQuestions and AnswersRHEL

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 is 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 routing 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.

Here is a detailed article on the configuration of network interfaces on RHEL/CentOS Linux.

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 in order to automatically add static route when the server boot. Open the file:

# nano /etc/rc.local

And 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)

0 comment
0
Facebook Twitter Google + Pinterest
previous post
View Saved Wi-Fi Passwords on Windows 10
next post
How to Disable “Open File – Security Warnings” on Windows 10?

Related Reading

Enable Internet Explorer (IE) Compatibility Mode in Microsoft...

January 27, 2023

How to Disable or Uninstall Internet Explorer (IE)...

January 26, 2023

How to Stop Automatic Upgrade to Windows 11?

January 18, 2023

Fix: Windows Needs Your Current Credentials Pop-up Message

January 18, 2023

Adding Trusted Root Certificates on Linux

January 9, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023
  • Adding Trusted Root Certificates on Linux

    January 9, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • How to Configure MariaDB Master-Master/Slave Replication?
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • How to Mount Google Drive or OneDrive in Linux?
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • Install and Configure SNMP on RHEL/CentOS/Fedor
  • Hyper-V Boot Error: The Image’s Hash and Certificate Are not Allowed
  • Adding VLAN Interface in CentOS/Fedora/RHEL
Footer Logo

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


Back To Top