CentOS 7 Default Route for Static IP on Two Interface
I have a server with two interfaces(em1 and em2). When the network comes up it sets the default route to em2:
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 0 0 0 em2
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 em1
link-local 0.0.0.0 255.255.0.0 U 1002 0 0 em1
link-local 0.0.0.0 255.255.0.0 U 1003 0 0 em2
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 em2
# cat ifcfg-em1
TYPE=Ethernet
NAME=em1
DEVICE=em1
ONBOOT=yes
BOOTPROTO=none
UUID=1*********************
IPV6INIT=no
MTU=15000
IPADDR=192.168.2.2
NETMASK=255.255.255.0
GATEWAY=192.168.2.1
NM_CONTROLLED="no"
# cat ifcfg-em2
TYPE=Ethernet
NAME=em2
DEVICE=em2
ONBOOT=yes
BOOTPROTO=none
UUID=2*********************
IPV6INIT=no
MTU=15000
IPADDR=192.168.1.2
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
NM_CONTROLLED="no"How can I get the default route to be set on em1 instead of em2?
3 Answers
Simply set the DEFROUTE directive to yes on the interface that you want to be the default gateway. So:
DEFROUTE=yes 3 Okay so interfaces are superceded by network routes. To change the default normally in CentOS 7 to route out a selected interface regardless of its order.
Verify/edit the following file: /etc/sysconfig/network GATEWAY=192.168.2.1 GATEWAYDEV=em1 NETWORKING=YES
Of course there are other ways to do this as well as additional entries to this file but this will infact get you your desired outcome.
On my CentOS 7 minimal I had to set the following in /etc/sysconfig/network-scripts/ifcfg-eth0 for my primary interface eth0 to have the default route:
DEFROUTE=yesAnd for the other (secondary) interface eth1 in /etc/sysconfig/network-scripts/ifcfg-eth1 explicitly the other way around:
DEFROUTE=noThat way, it's possible to set ONBOOT=yes for both interfaces being enabled right after boot and being configured correctly :)