The first command we will look at in this Network configuration chapter is hostname. Let’s look at some examples to get an idea of what it can be used for:
marc:/tmp> hostname -> show current hostname
server1
.
marc:/tmp> hostname -f -> show main FQDN (fully qualified domain name)
server1.bogomips.net
.
marc:/tmp> hostname --
fqdn -> same as above
server1.bogomips.net
.
marc:/tmp> hostname -A -> show all currently configured FQDNs
server1.bogomips.net server1-backup.bogomips.net
.
marc:/tmp> hostname --
all-fqdns -> same as above
server1.bogomips.net server1-backup.bogomips.net
.
marc:/tmp> hostname -I -> show all IP addresses configured
192.168.100.15 192.168.55.12
.
marc:/tmp> hostname --
all-ip-addresses -> same as above
192.168.100.15 192.168.55.12
We can set or change the hostname in 2 ways as superuser…
# hostname server2
# echo server2 > /etc/hostname
The first way will change the hostname instantly but the change will not survive a reboot. The second way will only change it persistently after a reboot. So we would need both commands executed to change the hostname instantly and persistently.
To determine the current domain and to set it we can use:
# domainname -> gets the NIS domain by calling the function gethostname()
bogomips.net
.
# ypdomainname -> gets the NIS domain by calling the function yp_get_default_domain()
bogomips.net
.
# nisdomainname -> alias of the ypdomainname command
bogomips.net
The 3 commands above can also be used to set the domain name if called by root with the domain as the first and only argument. However, as with the hostname command, the change does not survive a reboot. We can also determine the domain with dnsdomain but won’t be able to change it.
marc:/tmp> dnsdomain
bogomips.net
Even though we can state the fully qualified hostname in /etc/hostname it is not a good practice. Instead, we should set just the hostname in /etc/hostname and then use /etc/hosts to specify the domain:
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.190 rhce7.bogomips.net rhce7
The /etc/resolv.conf file is used to set DNS resolution settings. Let’s look at a typical such file:
# cat /etc/resolv.conf
domain bogomips.net
nameserver 192.168.0.1
nameserver 8.8.8.8
search bogomips.net
There might be some other options, but this file typically contains 3 parameters:
domain → local DNS domain
nameserver → DNS server to use for resolution (one or more)
search → domain name to add to hostnames for resolution (by default the same as the local domain)
Another important file from the networking perspective is /etc/sysconfig/network:
# cat /etc/sysconfig/network
NETWORKING=YES
HOSTNAME=rhce7.bogomips.net
GATEWAY=192.168.0.1
#GATEWAYDEV=eth0
#NISDOMAIN=bogomips.net
As long as /etc/hostname, /etc/hosts, /etc/resolv.conf and the interface files are properly set up,
we can get by just with the first line ( “NETWORKING=YES” ). But at we might want to populate it to set the default gateway (“GATEWAY“) for all interfaces and the NIS domain (“NISDOMAIN“)
The trickiest part of setting up the network would be configuring the interface files in /etc/sysconf/networkscripts/ifcfg<linkname>. Let’s start by having a look at a typical interface file and then we’ll cover the different parameters we must and can use:
# cat /etc/sysconf/network-scripts/ifcfg-eno16780032
DEVICE=eno16780032
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=no
IPV4_FAILURE_FATAL=yes
IPV6_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPADDR0=192.168.0.190
PREFIX0=24
GATEWAY0=192.168.0.1
BROADCAST0=192.168.0.255
DNS1=192.168.0.1
DNS2=8.8.8.8
DNS3=8.8.4.4
DOMAIN=bogomips.net
UUID=33a08e5001f1430e9fd8ba04895a79af
HWADDR=00:50:56:87:BD:92
USERCTL=no
NM_CONTROLLED=no
The example above would be the interface file for a link called eno16780032 . Most of the time only a subset of the following parameters will be present in an interface configuration file.
-
The parameter DEVICE should always be set to the link name.
-
The parameter TYPE defines what scripts [ /etc/sysconfig/networkscripts/if[down|up]* ] are run to bring the interface up or down.
-
The parameter BOOTPROTO should be set to NONE or STATIC for static IP addresses, or to DHCP for dynamic ones.
-
The ONBOOT parameter should be set to YES if we want the interface brought up on startup.
-
The DEFROUTE states that this interface is the one with the default route to be used by all others.
-
The PEERDNS parameter states whether /etc/resolv.conf can be modified (i.e. to add DNS nameservers) when configuring this interface. The IPV6_PEERDNS does the same for IPv6.
-
The PEERROUTES & IPV6_PEERROUTES parameters determine whether or not the default gateway should be set by the DHCP server.
-
The IPV4_FAILURE_FATAL states whether or not the interface should be down if IPv4 configuration fails. The IPV6_FAILURE_FATAL states the same for IPv6 and should be set to “no” if IPv6 is not meant to be used. Otherwise failure to configure IPv6 will down the interface even if it is meant to use only version 4.
-
The IPV6INIT parameter states whether IPv6 should be available for the interface.
-
The IPV6_AUTOCONF parameter states whether or not IPv6 autoconfiguraton with the Neighbour Discovery.
-
The IPV6_DEFROUTE parameter determines whether or not this is meant to be the interface used for the default route for all the other interfaces.
-
The IPADDR0, PREFIX0, GATEWAY0 and BROADCAST0 state the settings for the one and only IP address configured for this interface. We can configure more IPs with IPADDR1-PREFIX1-GATEWAY1, IPADDR2-PREFIX2-GATEWAY2, etc.
-
The DNS{1,2,3,…} parameters define the nameservers to be used by this interface. These nameservers might or might not be added to /etc/resolv.conf depending on the value of PEERDNS & IPV6_PEERDNS.
-
The UUID parameter is a system-generated unique identifier for this interface and should not be manually modified.
-
The HWADDR parameter is the MAC address.
-
The USERCTL parameter states whether or not users other than root can modify this interface.
-
The NM_CONTROLLED parameter states whether or not NetworkManager can modify this interface.
The easiest way to configure the ifcfg-eth* files is by using the NetworkManager (CLI nm-connection-editor or settings->network) to configure the first one (if none has been configured at installation time). Once we have all the parameters in one file, it should be straight-forward to copy and modify them to configure extra interfaces. Once we are done with all the configuration files, it might be a good idea to disable the NetworkManager setting NM_CONTROLLED=no for all interfaces.
# systemctl stop NetworkManager
# systemctl disable NetworkManager
Rebooting the OS to test the interface settings is dangerous and unnecessary. Let’s say that we have a link called eth1 and the corresponding file /etc/sysconfig/ifcfg-eth1 with all the parameters customised to our needs. If we want to test that the interface will be brought up and down without a problem, we can do so with the commands:
# ifup eth1
# ip addr show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
. link/ether 52:54:00:c8:d4:87 brd ff:ff:ff:ff:ff:ff
. inet 192.168.122.161/24 brd 192.168.122.255 scope global eth1
. valid_lft forever preferred_lft forever
. inet6 fe80::5054:ff:fec8:d487/64 scope link
. valid_lft forever preferred_lft forever
# ifdown eth1
We can also use the ip command to add & delete IP addresses and to modify the interface’s properties in various ways…
# ip addr add 192.168.122.162 dev eth1
# ip addr add 192.168.122.163 dev eth1
# ip addr show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
. link/ether 52:54:00:c8:d4:87 brd ff:ff:ff:ff:ff:ff
. inet 192.168.122.161/24 brd 192.168.122.255 scope global eth1
. valid_lft forever preferred_lft forever
. inet 192.168.122.162/24 brd 192.168.122.255 scope global secondary eth1
. valid_lft forever preferred_lft forever
. inet 192.168.122.163/24 brd 192.168.122.255 scope global secondary eth1
. valid_lft forever preferred_lft forever
…but these changes will not be persisted and won’t survive a reboot.
There should always be one default gateway/route per OS instance. We can specify it in /etc/sysconfig/network as seen before or we can specify a default route per interface. In most cases that will suffice as the routing of packets should be performed on the networking gear (routers, switches, etc). However, in some circumstances (i.e. VPNs, tunnels, security or cost constraints, etc) we might need to add a non-default route for traffic meant for certain networks or IPs.
# ip route add 192.168.5.0/24 via 192.168.0.1 dev eth0
# ip route add 192.168.6.6 via 192.168.1.1 dev eth0
The first ip route add command instructs all traffic meant for the network 192.168.5.0/24 to be sent through the IP 192.168.0.1.
The second ip route add command instructs all traffic meant for the IP 192.168.1.1 to be sent through the IP 192.168.1.1.
If we need to define a non-default route for an interface persistently, we can do so by creating an extra file called route-<if_name>. For instance, if we need to define a non-default route for interface eth2…
# echo “default via 192.168.1.1 dev eth2” > /etc/sysconfig/route-eth2
Or if we need to specify a non-default route to reach a certain network…
# echo “10.0.5.0/24 via 192.168.1.1 dev eth2” > /etc/sysconfig/route-eth2
There are other ways to configure networking such as NetworkManager, nmcli & nmtui. But as the end result should be the same and they offer no obvious advantages, we can stick to the traditional way of doing things: editing configuration files and running ip commands.
We have seen how to use DHCP for interfaces with configuration files but if we need to use it on ad-hoc basis we can do it as easily as:
# dhclient wlo1 → get an IP from the DHCP server with all defaults
# dhclient -4 -H envy18 -s 192.168.0.1/24 -v → same but use IPv4, set hostname and specify the IP
. of the DHCP server to use