How to set a default route permanently in Linux

If you have more than one network interface, you have to designate one network interface as the default route. In order to set a default route persistently in Linux, you can do the following. I assume that there are two interfaces: eth0 & eth1, and that you wish to use eth0 as the default route. I also assume that you are not using Network Manager on your Linux.

Set a Default Route Permanently on CentOS, Fedora or RHEL

On a RedHat-based system, you can explicitly declare the default route using "DEFROUTE: yes". In addition, you should add "DEFROUTE: no" to all network interfaces, NOT used as the default route.

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEFROUTE=yes
$ sudo vi /etc/sysconfig/network-scripts/ifcfg-eth1
DEFROUTE=no

Set a Default Route Permanently on Ubuntu, Debian or Linux Mint

On a Debian-based system, you can set a permanent default route by leveraging post-up command in /etc/network/interfaces as follows.

$ sudo vi /etc/network/interfaces
auto eth0
iface eth1 inet dhcp
post-up route add default via [gateway-ip-address] dev eth0

 
auto eth1
iface eth1 inet dhcp
post-up route del default dev eth1

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

2 thoughts on “How to set a default route permanently in Linux

  1. Perfect. This allowed me to use an ENI in AWS for a control network, but keep the traffic on eth0. Can't thank you enough! --ty

Leave a comment

Your email address will not be published. Required fields are marked *