3 ways to configure a network interface in Linux
Few things are more important to your machine than a good network connection. Here are three ways to configure the interfaces needed to make this happen.
Editor's note: This article was written while James Brigman was a member of the Red Hat Accelerator program.
Almost any useful work that one would want to do with a Linux system requires a network interface. Want to browse the web, watch YouTube, stream video, audio or files? It’s all done over the network interface. RPM-based Linux distributions using Gnome have several fundamental ways to configure the network interface. I'm describing three ways in this article. All of the configuration methods require the entry of sets of numbers that allow the network interface to operate.
You will need three fundamental pieces of numerical information in order to minimally configure a network interface to work over IPv4 and more if you want to define things like IPv6, hostnames, or DNS servers. This article covers the bare minimum for IPv4. Those three fundamental numerical pieces are:
IP Address: The unique number defining the access point to your network interface. It has the form: xxx.xxx.xxx.xxx, where “xxx” are three, or fewer, numbers between 0 and 255. It’s possible for this number to be purely made up, but normally it takes a form that works with the other three numbers. If you are using a home router with DHCP, which is the typical default configuration, the router will “assign” the IP address to your network interface. You won’t have to enter the number at all.
Gateway: The unique number assigned to the network interface at the "other end of the wire" that your computer must communicate through. Again, it has the general xxx.xxx.xxx.xxx format and takes a form that also works with the other two numbers. If you are using a home router, your home router generates this number because it is the gateway through which you communicate with the wider world.
[ Get the guide to installing applications on Linux. ]
Netmask: The non-unique number that defines the network itself. This number can be automatically generated but is sometimes requested by the method you use to configure the interface. It, too, has the format xxx.xxx.xxx.xxx.
Note that I’m not going into the how of these numbers; I’m just telling you they are needed to configure the network interface. I’ll skip that so you can get on into the point of the article: Three ways to configure network interfaces. In each case, the numbers I use will be real numbers applicable to the system I used to write this article. I used CentOS 8 to generate the images, but everything you see here is the same in Fedora and Red Hat Enterprise Linux.
At installation time
The Anaconda installer prompts for network configuration and you can’t complete the installation without providing these numbers to the installer. Here’s the initial screen, using “Network & Host Name” in the rightmost column, third selection down:
Choosing “Network & Host Name”, the next screen appears:
The installer has automatically identified the one ethernet interface available and selected it for configuration. The text next to the selected interface is the system name of the interface, connection state, hardware address (found from the system, we don’t enter that) and speed (also found from the system).
What to do in this case? Simply move the Ethernet connection button from OFF to ON and see what happens:
When you do this, the network information appears: IP address, Default Route, and DNS. These numbers were received from DHCP because when you move the Ethernet connection button to ON, the installer queries DHCP and registers the system with DHCP. DHCP is Dynamic Host Configuration Protocol. Your network is now configured and your system is connected to your local network and optionally, the internet.
From the desktop
If you are working with a Linux system using a GUI, you can configure the network interface via an icon in the far upper right of the screen. The function of this icon is very similar to the windows “TV Set” down in the lower right of the screen in the taskbar of a Windows desktop system.
[ Cheat sheet: Get a list of Linux utilities and commands for managing servers and networks. ]
You may well ask “How did you get this far if you didn’t have the network configured?” In this case, assume DHCP gave us our starting IP address, but for some reason you need to modify those parameters. Click on the Network icon in the upper right of the Linux GNOME desktop:
Clicking on the Network icon opens a dialog box:
Choosing “Wired Settings” opens a pop-up box:
There’s the ON/OFF button again, and in this case, it’s in the ON position. Choosing the gear button displays the network numbers that have been assigned (still DHCP) to the network interface. You will recognize our three numbers as the IPv4 Address, Default Route, and DNS.
If you click on IPv4, you can see that your old familiar numbers were given to your network interface by DHCP.
The parameters defined in these graphical tools have to live in a file you can see. That file is
/etc/sysconfig/network-scripts/ifcfg-enp0s3. It is controlled by the network management facility
nmcli. The relevant bit of information here is to use DHCP, which is used to collect those three important network numbers. Those numbers are not recorded in the
/etc/sysconfig/network-scripts/ifcfg-enp0s3 file. The important parameter seen below is:
That entry means every time this system boots, it will collect its network information from the DHCP server. But remember, the system doesn’t store those numbers in the file–it keeps them in RAM and sets the configuration of the network interface every time the system boots.
[ Learn how to manage your Linux environment for success. ]
How to correct the "localhost" problem?
Leaving your Linux system with no name means that it gets the default hostname of "localhost.localdomain". You don’t want to build more than one system with this default hostname. If you have multiple systems with the same name, you can only reference those systems on the network by the IP address and the system name becomes useless.
For DHCP, you can set the
DHCP_HOSTNAME in the
ifcfg-enp0s3 file. Edit the file and add the following line:
I'm using CentOS8/RHEL8 for this article. You can't restart the network using systemctl because
network.service is deprecated. In version 8, use
nmcli to restart the network.
$ sudo nmcli networking off $ sudo nmcli networking on
Exit your terminal window, because you started it before setting
DHCP_HOSTNAME, it still shows localhost as the hostname. You should see the following:
And after exiting and restarting the window:
There are several other ways to set the hostname (No need to Google it). You can find that information right here on Enable Sysadmin. Please refer to this detailed article on Enable Sysadmin by Seth Kenlon: "7 ways to set your hostname in Fedora, CentOS, or Red Hat Enterprise Linux."
For more information on configuring network interfaces in CentOS8/RHEL8, read Configuring and managing networking.