How to set a default route permanently in Linux

Last updated on October 23, 2020 by Dan Nanni

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 every network interface that is 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 Ubuntu/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

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean