How to Add or remove the Default Gateway in Linux

Today, We will learn how to add or remove the default gateway in Linux. Read: ifconfig

Sometimes you may need to change the default route of your system. This is especially true if you have multiple network adapters or routers on the network.

How to check the default route or gateway in Linux?

We can check the default route or gateway in Linux from “route“. Here is the example to show the default route in Linux.

#route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
102.64.219.14    100.64.88.107   255.255.255.252 UG    0      0        0 eth4
192.168.12.0       *               255.255.252.0   U     1      0        0 eth0
link-local      *               255.255.0.0     U     1004   0        0 eth2
link-local      *               255.255.0.0     U     1005   0        0 eth3
default         192.168.12.1       0.0.0.0         UG    0      0        0 eth0

In the above example shown, the “route” command is showing the default route or gateway Which is connected to the eth0 interface.

How to delete the default route or gateway in Linux?

We can delete the default route or gateway from the command like the below example:

#route delete default gw 10.0.2.2 eth0

or

#ip route flush 0/0

or 

#ip route del default

Here is the above example, We can use any of these commands to delete the default route or gateway in Linux.

How to add a default route or gateway in Linux?

We can also add a default route or gateway in Linux from the below command.

Here is the syntax of the command.

route add default gw {IP-ADDRESS} {INTERFACE-NAME}

Example of adding a default route or gateway in Linux:


# route add default gw 192.168.12.1 eth0

Read also: Linux command to check space

Leave a Reply