Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • 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 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)

1 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

How to Increase Size of Disk Partition in...

October 5, 2023

How to Use Ansible to Manage Windows Machines

September 25, 2023

Fixing ‘The Network Path Was Not Found’ 0x80070035...

August 30, 2023

How to Install and Configure Ansible on Linux

August 27, 2023

Computer Doesn’t Turn Off After Shutting Down Windows...

August 26, 2023

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

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

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Configure MariaDB Master-Master/Slave Replication?
  • How to Mount Google Drive or OneDrive in Linux?
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • Adding VLAN Interface in CentOS/Fedora/RHEL
  • Install and Configure SNMP on RHEL/CentOS/Fedor
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • How to Install and Configure Squid Proxy Server on Linux
Footer Logo

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


Back To Top