IP Route command: Create static routes or Change the default Gateway in Linux — LinuxTechLab

IP route command is an extension of IP command, we have already discussed IP commands in brief in our previous tutorial. IP route command is used to add, delete or modify the Linux system’s routing table.

We basically use the IP route command to create static routes in Linux to specific hosts or to change the default gateway for the Linux system. In this tutorial, we will discuss how we can use IP route command to create a static route & to change the default gateway for the Linux system.

Recommended Read: Bash Scripting: Learn to use REGEX (Part 2- Intermediate) Also Read: IP Aliasing : Assigning multiple IP addresses to single NIC

Let’s discuss some examples for the same.

IP ROUTE Command

1- Check the current routing table

To check the current routing table of the system, we can use the following commands,

Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3 0.0.0.0 10.10.1.10 0.0.0.0 UG 0 0 0 enp0s3

We can also use the following command to check the routes,

The assigned system IP for the system is 10.10.1.100 & we can see that the destination is within that range only in the first command output. The second command also shows the gateway for the IP range i.e. 10.10.1.10.

The routing files are located in the folder “/etc/sysconfig/network-scripts/”,

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

10.10.1.0/24 via 10.10.1.10 dev enp0s3

2- Create a new route i.e. create a new static route

To create a new static route to a new network range, the command will be

# ip route add 10.10.3.0/24 dev en0sp3

And we want this network to route from a particular gateway, the command is

# ip route add 10.10.3.0/24 via 10.10.1.10 dev en0sp3

3- Delete a route

To delete a route command is,

# ip route delete 10.10.3.0//24 dev en0sp3

4- Delete the existing default gateway

To delete the existing default gateway for the system, the commands are

# ip route delete default

Same can be accomplished with route command,

5- Add a new default gateway

To setup a new system as the default gateway, we need to use the following command,

# route add default gw 10.10.1.20

or we can also use the following command,

# ip route add default via 10.10.1.20

6- To reject network packets to a particular host or to a network

We can also use the IP route command to reject network traffic to a particular host or even to complete the network range.

To reject network packets to a single host, the command is,

# route add -host 10.10.2.20 reject

To reject the network packets to an entire network, we can use the following command,

# route add -net 10.10.2.0 netmask 255.0.0.0 reject

We have only discussed some of the important examples of use for managing the routes on our system. Please feel free to send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Linux TechLab is thankful for your continued support.

Originally published at https://linuxtechlab.com on April 13, 2020.

http://linuxtechlab.com is a beginner friendly website where you can learn Linux Tips & tricks,Scripting, also has lots of tutorials aimed at making Linux easy.