Configuring Network Interfaces

Howto configure a Linux network interface card

Network Configuration


The following is a basic guide for the configuring of network settings on Red Hat Enterprise Linux 7.0, CentOS 7.0 and Oracle Linux 7.0 servers. In the following examples we will cover how to manually configure the network by editing configuration files and how to use new Network Manager configuration tool "nmtui". In most cases, Red Hat 7.0 based servers will automatically configure any active interfaces found during the installation process. However, there will be times when you will want to add additional interfaces, assign static IP addresses or switch to using DHCP. The following examples were all carried out on a standard Red Hat Enterprise Linux 7.0 server configured with a desktop GUI.


Location of Network Configuration Files


The following files are used for the configuration of network settings. These settings include Network Interfaces, Routing, DNS, Domain Search order, configuring of the hostname and host file resolution. These files can be manually modified with a text editor of your choice or by using the recommended network tool "nmtui".



Network Interface Files


The location of the network interface files used for holding the configuration settings of our interfaces and routing can be found in the following location:

/etc/sysconfig/network-scripts

Interfaces generally follow the naming convention of: ifcfg-eth0

The "ifcfg" indicates that this is an interface config file and the latter part of the name identifies the interface. Traditionally interfaces were called eth0, eth1, eth2 and so on, however, under Red Hat 7, interfaces are automatically named after the adapter. On this particular system the interface is called "ifcfg-enp0s3". Static routing files can also be found in this location. These are identified by the prefix of "route".

Interfaces: ifcfg-interface
Routing: route-interface


DHCP vs Static IP Addressing


For most desktop scenarios using DHCP (Dynamic Host Configuration Protocol) to obtain an IP address automatically is generally acceptable. However, most servers in a production environment will have a static IP address assigned. During a basic installation, you can specify whether you wish to configure the network manually or accept a DHCP address.


DHCP IP Addressing


DHCP (Dynamic Host Configuration Protocol) is the name given to a protocol for the assigning of IP addresses automatically to a network interface at system startup. These IP addresses are leased for a set period of time. This means that over a period of time the IP address may change.


Static IP Addressing


Static IP addresses are IP addresses that have been manually assigned to an interface. Unlike DHCP, these IP addresses will remain assigned to the interface until manually changed by an administrator. Most server environments will use static IP addressing.


Example DHCP configuration file - ifcfg-enp0s3


The important parameters here are: BOOTPROTO=dhcp for a dhcp connection type.



TYPE="Ethernet"
BOOTPROTO=dhcp
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT=no
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="enp0s3"
UUID="6df8f600-ea9f-43c1-9ce9-fca5d4c3a899"
ONBOOT="yes"
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
HWADDR=08:00:27:AE:5C:3F
PEERDNS=yes
PEERROUTES=yes

Example Static IP Addresses Configuration file - ifcfg-enp0s8


The important parameters here are BOOTPROTO=none for a static connection type.



TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME="enp0s8"
UUID=44404333-262f-4634-ae23-6ad978de1d08
ONBOOT=yes
HWADDR=08:00:27:4C:A5:9E
IPADDR0=192.168.0.75
PREFIX0=32
GATEWAY0=192.168.0.1
DNS1=8.8.8.8
DNS2=8.8.4.4
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

BOOTPROTO is set to "dhcp" for a automatic configuration of an IP address. If this is set to "none", then a static route is to be used. (Note the word "static" is no longer valid)

Type=Ethernet Specifies that a wired connection is to be used.

ONBOOT=yes Specifies that the interface should be started at system boot.

NAME=xxxxxxx Specifies the name of the interface.

IPADDR0=xxx.xxx.xxx.xxx Specifies the first IP address to be assigned to the interface. Additional IP addresses can be added using IPADDR1, IPADDR2 ....

DNS1=xxx.xxx.xxx.xxx Specifies a DNS server to use. Additional DNS servers are added by using DNS2 .....

HWADDR= These are generated automatically and will be different on your systems.



Configuring the hostname


Normally during installation one of the steps is to provide a unique hostname by which your system will be identified by on your network. This name can be changed by editing the following configuration file:

/etc/hostname

An alternative way to manage your hostname is to use the command: hostnamectl


hostnamectl


Hostnamectl is a command that allows you to define/change the hostname on your systems. The "hostnamectl" tool has three different types of hostname options that can be set:

The high-level "pretty" hostname can include all kinds of special characters (e.g. "Tux's's Laptop"), the "static" hostname which is used to initialize the kernel hostname at boot (e.g. "rhel07a"), and the "transient" hostname which might be assigned temporarily due to network configuration and may revert back to a static hostname if network connectivity is lost and is only temporarily written to the kernel hostname.

Note that the pretty hostname has little restrictions on the characters used, while the static and transient hostnames are limited to the usually accepted characters of Internet domain names.

Example of hostnamectl command:

If you issue the hostnamectl command with no parameters or options, then information relating to your system will be displayed:


  
[root@rhel07a network-scripts]# hostnamectl
   Static hostname: rhel07a
         Icon name: computer
           Chassis: n/a
        Machine ID: 5904595331544c5fb48da4501a98ae7c
           Boot ID: aa8349956c294c1f92ff096454a0d62e
    Virtualization: oracle
  Operating System: Red Hat Enterprise Linux Server 7.0 (Maipo)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:7.0:GA:server
            Kernel: Linux 3.10.0-121.el7.x86_64
      Architecture: x86_64

Setting a Static Hostname



# hostnamectl --static set-hostname rhel07a

Setting the Pretty Hostname Name



# hostnamectl --pretty set-hostname "Land of Linux's RHEL 7 test server"

The pretty hostname details can be found in a file "/etc/machine-info"):



[root@rhel07a etc]# cat machine-info 
PRETTY_HOSTNAME="Land of Linux\'s RHEL 7 test server"

Setting the Transient Hostname Name


# hostnamectl --transient set-hostname myserver

Verify all hostname settings


Now if we issue the command "hostnamectl" with no parameters we can now see the defined areas:



[root@rhel07a network-scripts]# hostnamectl
   Static hostname: rhel07a
   Pretty hostname: Land of Linux's RHEL 7 test server
Transient hostname: myserver
         Icon name: computer
           Chassis: n/a
        Machine ID: 5904595331544c5fb48da4501a98ae7c
           Boot ID: aa8349956c294c1f92ff096454a0d62e
    Virtualization: oracle
  Operating System: Red Hat Enterprise Linux Server 7.0 (Maipo)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:7.0:GA:server
            Kernel: Linux 3.10.0-121.el7.x86_64
      Architecture: x86_64



Location of DNS settings and domain search order


During installation, you may have configured your network manually providing the name of a DNS server and the domain search order. Again, these entries can be modified. Additional DNS servers may be added easily. The configuration of these settings can be found in the following location:

/etc/resolv.conf

By default most systems will use a DNS server to make a lookup, however, you can override this by modifying the "nsswitch.conf" file (see below).


DNS or Host file lookups - Name Service Switch configuration file "nsswitch.conf"


The above file can be found in the following location:

/etc/nsswitch.conf

The Name Service Switch (NSS) configuration file "/etc/nsswitch.conf", is used by the GNU C Library to determine the sources from which to obtain name service information in a range of categories, and in what order.

Example taken from RHEL 7.0 server



#
# /etc/nsswitch.conf
#

passwd:     files sss
shadow:     files sss
group:      files sss

hosts:      files dns

bootparams: nisplus [NOTFOUND=return] files

ethers:     files
netmasks:   files
networks:   files
protocols:  files
rpc:        files
services:   files sss

netgroup:   files sss

publickey:  nisplus

automount:  files
aliases:    files nisplus

The line that specifies the lookup order is as follows:


hosts:        files dns

This entry specifies that we will look in the host file first for an entry before trying a DNS server.


Hosts File - "/etc/hosts"


The "/etc/hosts" file is a simple text file that associates IP addresses with hostnames, one line per IP address. For each host a single line should be present with the following information:

IP_address canonical_hostname [aliases...]

Example:
192.168.0.75  www.example.com  myserver

Modifications to this file normally take effect immediately, except in cases where the file is cached by an application.


Adding a Persistent Static Route


There are several ways to add or remove static routing information. The traditional way was to use the "route" command which allowed a systems administrator to manipulate the routing table. When the "add" or "del" options are used, the route command would modify the routing table. If no options are passed to the route command, then the routing table is displayed:



[root@rhel07a etc]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.0.1     0.0.0.0         UG    1024   0        0 enp0s3
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 enp0s3

Note: The route command is now classed as obsolete and the "ip route" command should be used instead.


ip route command


The "ip route" command is used for routing table management and is a replacement for the obsolete "route" command. Static routes are for traffic that must not, or should not, go through the default gateway.

The "default gateway" is for any and all traffic which is not destined for the local network and for which no preferred route has been specified in the routing table.


Configuring Static Routes Using the Command Line


If static routes are required, they can be added to the routing table by means of the "ip route add" command and removed using the "ip route del" command. Commonly used options with the "ip route" command are as follows:



ip route [ add | del | change | append | replace ] destination-address

If you issue the "ip route" command without any options/parameters you will see output similar to the output below:



[root@rhel07a etc]# ip route
default via 192.168.0.1 dev enp0s3  proto static  metric 1024 
192.168.0.0/24 dev enp0s3  proto kernel  scope link  src 192.168.0.20 

Add a static route to a host address


To add a static route to a host address, issue the following command as root:



ip route add 192.0.2.1 via 10.0.0.1 [dev interface]

Where "192.0.2.1" is the IP address of the host and "10.0.0.1" is the next hop address and "interface" is the exit interface leading to the next hop. To add a static route to a network representing a range of IP addresses, issue the following command as root:



ip route add 192.0.2.0/24 via 10.0.0.1 [dev interface]

where "192.0.2.0" is the IP address of the destination network and "/24" is the network prefix. The network prefix is the number of enabled bits in the subnet mask.

Static route configurations can be stored per interface in a file with a naming convention of "/etc/sysconfig/network-scripts/route-interface. An interface with the name of "eth0" would be stored in a file by the name of /etc/sysconfig/network-scripts/route-eth0.

There are two formats available to the "route-interface" file. One is to use the "ip arguments" and the other is to use "network/netmask" directives.


Configuring a Default Gateway


The default gateway is determined by the network scripts which parse the "/etc/sysconfig/network" file first and then any network interface "ifcfg" files for interfaces that are in a state of “up”. These ifcfg interface files are parsed in numerical ascending order. The last GATEWAY directive to be read is used to compose a default route in the routing table.

A default route can be specified by means of a GATEWAY directive and can be either set globally or in an interface specific configuration file.

A Global Default Gateway's configuration is stored in the file "/etc/sysconfig/network". This configuration file specifies gateway and host information for all network interfaces.


Configuring Static Routes in ifcfg interface files


Static routes that are set using the ip command from the command line are not persistent, they will be lost if the system is shutdown or restarted. To configure static routes that are persistent after a system restart, they must be placed in an interface configuration file in the "/etc/sysconfig/network-scripts/" directory.

The file name should be of the format "route-interface".


Static Routes Using the IP Command Arguments


Create an interface file with the following naming convention (the name eth0 is used in the example), for example "/etc/sysconfig/network-scripts/route-eth0", then define a route to a default gateway on the first line. This will only be needed if the gateway has not been set by DHCP and is not set globally in the "/etc/sysconfig/network" file:



default via 192.168.1.1 dev interface
.

"192.168.1.1" is the IP address of the default gateway. The "interface" is the interface that is connected to, or can reach, the default gateway. The "dev" option can be omitted, as it is optional. This interface configuration file setting takes precedence over a setting defined in the "/etc/sysconfig/network" file.

If a route to a remote network is required, a static route can be specified as follows:



10.10.10.0/24 via 192.168.1.2 [dev interface]

Each line is parsed as an individual route. 10.10.10.0/24 is the network address and the prefix length of the remote or destination network (CIDR format). The address 192.168.1.2 is the IP address leading to the remote network. It's preferably the next hop address, however, the address of the exit interface will work.

The “next hop” refers to the remote end of a link (gateway/router). The "dev" option can be used to specify the exit interface.


Network/Netmask Directives Format


The following format uses the "ADDRESS", "NETMASK" and "GATEWAY" fields. Each of these fields has a number appended to it indicating which route it is applicable to.



ADDRESS<N>=xxx.xxx.xxx.xxx
NETMASK0<N>=xxx.xxx.xxx.xxx
GATEWAY<N>0=xxx.xxx.xxx.xxx

Example:



ADDRESS0=10.10.10.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.1.1

Where "10.10.10.0" is the network address of the remote network or remote host to be reached.
"255.255.255.0" is the netmask of the network address defined with "10.10.10.0".
"192.168.1.1" is the default gateway, or an IP address that can be used to reach the address "10.10.10.0"

The following is an example of a route-interface layout using the network/netmask directives format. In this example several routes have been added.



ADDRESS<0>=xxx.xxx.xxx.xxx
NETMASK<0>=xxx.xxx.xxx.xxx
GATEWAY<0>=xxx.xxx.xxx.xxx
ADDRESS<1>=xxx.xxx.xxx.xxx
NETMASK<1>=xxx.xxx.xxx.xxx
GATEWAY<1>=xxx.xxx.xxx.xxx


Static routes must be numbered sequentially. For example, ADDRESS0, ADDRESS1, ADDRESS2, and so on.


Network Configuration Using "nmtui" - Network Manager Text User Interface


The NetworkManager text user interface tool (nmtui), provides a text interface to configure networking by controlling the NetworkManager. The tool should now be included by default with the NetworkManager, however, if it is missing, it can be easily installed with the following commands:



yum install NetworkManager-tui

You can easily identify if your interfaces are under the control of the NetworkManager by issuing the following command from a console or terminal:

Command Issued: nmcli dev status



[root@rhel07a ~]# nmcli dev status
DEVICE  TYPE      STATE      CONNECTION 
enp0s3  ethernet  connected  enp0s3     
lo      loopback  unmanaged  --   

From the above output you can see that an interface by the name of "enp0s3" is under the control of the NetworkManager.


Starting nmtui


To start the NM Text User Interface issue the command nmtui as the root user. Although the command can be issued as a none root user, you will not be able to make any changes to your configuration.


NetworkManager nmtui

To navigate, use the arrow keys or press the Tab key to step forwards and Shift+Tab to step backwards through the presented options.
Press Enter to select an option. The Space Bar is used to toggle the status of a check box.

If you know the name of your connection interface, you can go straight to the appropriate screen:

Command Issued: nmtui edit connection-interface



# nmtui edit enp0s3

NetworkManager nmtui - edit connection

Any changes made to the interface "enp0s3" will be recorded in the interface file: /etc/sysconfig/network-scripts/ifcfg-enp0s3

Connection can also be activated by issuing the following nmtui command:



nmtui connect connection-interface

BY navigating through the various panels, you can configure your network settings easily. Static/DHCP settings, Routing and hostname information can be configured using this tool. The nmtui is the recommended way to configure your interfaces. The "nmtui" tool can be used in a graphical and none graphical environment.


Verify the Status of the NetworkManager


To verify the status of NetworkManager service you can issue the following "systemctl" command:

systemctl status NetworkManager.service



[root@rhel07a ~]# systemctl status NetworkManager.service
NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled)
   Active: active (running) since Fri 2014-10-03 20:01:32 BST; 43min ago
 Main PID: 797 (NetworkManager)
   CGroup: /system.slice/NetworkManager.service
           ├─ 797 /usr/sbin/NetworkManager --no-daemon
           └─1233 /sbin/dhclient -d -sf /usr/libexec/nm-dhcp-helper -pf /var/...

Oct 03 20:25:47 rhel07a dhclient[1233]: bound to 192.168.0.34 -- renewal in ....
Oct 03 20:25:47 rhel07a NetworkManager[797]: bound to 192.168.0.34 -- renewa....
Oct 03 20:25:47 rhel07a NetworkManager[797]:  (enp0s3): DHCPv4 state c...w
Oct 03 20:25:47 rhel07a NetworkManager[797]:    address 192.168.0.34
Oct 03 20:25:47 rhel07a NetworkManager[797]:    plen 24 (255.255.255.0)
Oct 03 20:25:47 rhel07a NetworkManager[797]:    gateway 192.168.0.1
Oct 03 20:25:47 rhel07a NetworkManager[797]:    server identifier 192....1
Oct 03 20:25:47 rhel07a NetworkManager[797]:    lease time 3600
Oct 03 20:25:47 rhel07a NetworkManager[797]:    nameserver '194.168.4.100'
Oct 03 20:25:47 rhel07a NetworkManager[797]:    nameserver '194.168.8.100'

From the above output you can see that the service is active.


Starting and Stopping Services and interfaces


Red Hat Enterprise Linux 7.0, CentOS 7.0, Oracle Linux 7.0 and Scientific Linux 7.0 all use systemd commands for manipulating services.

Below are some useful Networking related commands:

Display Current Network Status: systemctl status network.service

Restart Network Services: systemctl restart network.service

Stop Network Service: systemctl stop network.service

Start Network Service: systemctl start network.service

Stop an Interface: ifdown interface

Activate an interface: ifup interface


Miscellaneous Network Commands


Display Devices controlled by NetworkManager: nmcli dev status

NetworkManager Text User Interface (nmtui): nmtui

NetworkManager Text User Interface (nmtui): nmtui edit interface

Display Interface Connections: ip link

Display Interface Statistics: ip -s link

Display Assigned IP Address: ip address show

Display Routing Table: ip route show

Manage Routing table: ip route { add | del | change | append | replace } ROUTE

Display Routing Table: netstat -nr

Display Routing Table (obsolete): route

Display and Configure interfaces (obsolete): ifconfig

Display Hostname Information: hostnamectl

Set Hostname - Static: hostnamectl --static set-hostname rhel07a

Set Hostname - Pretty: hostnamectl --pretty set-hostname "Land of Linux's RHEL 7 test server"

Set Hostname - Transient: hostnamectl --transient set-hostname myserver


Documentation - Red Hat


A comprehensive Networking Guide in "pdf" format is freely available from Red Hat. To download, Click the link below:


Download RHEL 7.0 Networking Administration Guide