How Do You Find the Network Interface Name of a CentOS 7.X Server That Will Connect to the Default Gateway of Your Network?

Problem scenario:  You want to find the name of the network interface that can connect to the default gateway on your Linux CentOS 7.x server.  Typical new deployments of CentOS 7.x using the minimal .ISO do not have the "route" utility nor the "ifconfig" utility installed.  How do you find the interface's name (e.g., eth0) without using the "route" command? 

Solutions

Solution #1 (preferred)  

The easiest solution is to use this command:  ip route | awk '{print $3}'

Solution #2

This solution avoids the "ip route".  This solution takes more time.  

a.  Run this command:  ​ip addr show
b.  Evaluate the results.  The results of the above command will have integer values followed by discrete interfaces.  For example you'll see "1: lo" and "2: ..." 

The first result, "1: lo", is a loopback interface.  The second result, "2: enp0s3", will be the interface you want (assuming there are only two interfaces).   The lo interface by default will never have the default gateway.  For a simple CentOS server the other interface will.  

For CentOS 7.3 servers installed from the current .ISO (as of March 17, 2017)  with the default settings, the name of the interface that can connect to the default gateway is "enp0s3" as shown above.  There may be multiple interfaces with multiple default gateways for servers in complex environments with multiple NICs.

Also note that the corresponding file for this interface name will have a name that is prefaced with "ifcfg-".  Therefore in /etc/sysconfig/network-scripts/ the file that governs the interface will be "ifcfg-enp0s3".

Leave a comment

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