Categories
Cloud

Remove the default gateway for the Secondary interface eth1 on EC2 instance

When we add an EC2 instance, the default gateway gets added to the interface automatically. It should be fine for an Instance with single Network Interface. When we add an instance with 2 interfaces say eth0 and eth1, the default gateway gets added to both the interfaces. This becomes a problem when we add the interfaces as part of different subnets and one subnet doesn’t have the internet access.

If we remove the default route on the secondary interface using “route del” CLI command, it gets added again after a reboot.

To resolve this issue we need to edit the secondary interface using the nmtui tool and remove the default route option.

Normal route table with default route pointing to Secondary interface also

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags  MSS Window  irtt Iface
0.0.0.0         10.0.1.1        0.0.0.0         UG        0 0          0 eth0
0.0.0.0         10.0.2.1        0.0.0.0         UG        0 0          0 eth1
10.0.1.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 eth1

To Prevent the Default route from getting added again for eth1 interface, follow below steps.

# sudo -i
# nmtui  
=> Edit a connection
=> Select eth1 from the list # sometimes you will observe a different name in the list like ‘wired connection 1’  
=> Edit eth1 or ‘wired connection1’  
=> Select <Show> IPv4 Configuration and choose  
=> [X] Never use this network for default route  
=> Save the config by selecting <OK>

After exiting from nmtui utility, reboot the server.

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.0.1.1        0.0.0.0         UG        0 0          0 eth0
10.0.1.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 eth1

Now the default route for eth2 interface is removed from the server and does not get added again across reboot.

Leave a Reply