Skip to main content
Welcome to this comprehensive guide on configuring networking and hostname resolution in Linux—both statically and dynamically. Every device connected to a network requires an IP address (e.g., 192.168.0.5 or 10.0.0.9). In addition, internet connectivity demands proper gateway and DNS resolver settings. For instance, when accessing google.com, the typical process is:
  1. The device queries a DNS resolver to obtain google.com’s IP address (e.g., 203.0.113.9).
  2. With the resolved IP, the device sends data to its gateway.
  3. The gateway forwards the data hop-by-hop until it reaches the destination.
These settings—IP addresses, gateways, DNS resolvers, network routes, and so forth—can be configured dynamically (typically using DHCP) or statically (via manual configuration).
On Red Hat-based systems (such as Red Hat Enterprise Linux or CentOS), the configuration files and tools may vary slightly compared to other Linux distributions.

Identifying Your Network Interface

Before modifying any settings, it is important to identify the correct network interface. Start by listing all network interfaces with the following command:
A sample output might look like this:
In this example, the first non-loopback interface is enp0s3. To see the IP addresses assigned to these interfaces, you can run:
Alternatively, use the abbreviated command:
The loopback adapter typically has the IP address 127.0.0.1 (localhost), while the physical adapter (enp0s3) might have an IP like 192.168.1.79/24. Note that the CIDR notation (“/24”) designates the network prefix length, meaning 24 bits are fixed and only the remaining 8 bits are available for device addressing. For example, with a /16 configuration, the first 16 bits are fixed (e.g., 192.168), and the last two octets can vary. A detailed sample output from ip address show is:
Note that physical network interfaces typically start with “e” (e.g., enp0s3) and wireless interfaces generally start with “w”.

Understanding IPv6 Addresses

The output above also displays IPv6 addresses. For example, the IPv6 address on enp0s3 appears as:
IPv6 addresses are 128 bits long. The “/64” indicates that the first 64 bits are reserved as the network prefix, leaving the remaining bits for the device’s interface identifier.

Viewing the Routing Table

The routing table dictates how network traffic is directed. To review the system’s routing table, use:
Or the shorter version:
A typical routing table output might be:
This output confirms that the default gateway is 192.168.1.1. To check the DNS resolver settings, display the contents of the /etc/resolv.conf file:
A sample output might be:
Each line beginning with nameserver signifies a valid DNS resolver. It is common to configure multiple resolvers for redundancy. The automatic appearance of these settings suggests they were assigned via DHCP or a similar mechanism.

Network Configuration Files in Red Hat-Based Systems

On Red Hat-based distributions, networking configuration files are stored in the /etc/sysconfig/network-scripts/ directory. Files often follow the naming scheme ifcfg-<interface>. For example, the configuration file for the enp0s3 interface might be:
Viewing the contents of the interface configuration file is simple:
A typical file using DHCP might look like:
Notice that BOOTPROTO=dhcp signifies a dynamic configuration. For a static setup, change this value to none and manually assign the IP address, gateway, and DNS details.

Using NetworkManager Tools: nmtui and nmcli

NetworkManager is the default network management tool on most Red Hat-based systems. Two key utilities provided are:
  • nmtui: A text-based user interface.
  • nmcli: A command-line interface.

Editing a Connection with nmtui

Launch nmtui by executing:
You will see an interface similar to the following:
The image shows a terminal window with the NetworkManager TUI interface, offering options to edit a connection, activate a connection, set the system hostname, or quit.
Select “Edit a connection” and choose the network connection corresponding to enp0s3:
The image shows a terminal window on a CentOS system displaying a network configuration interface, listing an Ethernet connection "enp0s3" and a bridge "virbr0" with options to add, edit, or delete.
Within the editor, use the Tab and arrow keys to navigate between fields. The profile name can be arbitrary, but the “Device” field must exactly match the network interface (e.g., enp0s3). By default, the IPv4 and IPv6 settings use DHCP. To configure IPv4 with a static IP, change the method from “automatic” to “manual” and specify your desired static address (e.g., 192.168.1.79/24) along with the default gateway and DNS information.
The image shows a network configuration window on a CentOS system, displaying settings for an Ethernet connection, including IPv4 configuration options.
After entering the static settings, navigate to the “OK” button to save your changes:
The image shows a network configuration window on a CentOS system, displaying settings for an Ethernet connection, including IPv4 address, gateway, and DNS server details.
If you are connected remotely (e.g., via SSH), saving these changes may temporarily disrupt your network connection. It is advisable to test the new configuration locally before applying it to remote systems.
It is also possible to combine both DHCP and manual assignments. In such a hybrid configuration, the system obtains a primary IP via DHCP, while additional manual IP addresses are applied upon connection restart or system boot. To apply the changes immediately, use nmcli. For example, if your network interface is correctly identified as enp0s3, run:
If you mistype the device name, you will receive an error such as:
which produces:
After verifying the correct device name with ip link show, reapply using:
A successful operation returns:

Hostname Resolution

Hostname resolution is essential for converting human-friendly domain names into IP addresses. When you run a command like:
the system resolves google.com to an IP address (e.g., 142.251.33.46) and sends ICMP packets to it. A sample ping output could be:
This dynamic resolution relies on the DNS server configured earlier (in this case, 192.168.1.1).

Static Hostname Resolution via /etc/hosts

Static hostname resolution bypasses DNS by using the local /etc/hosts file. Open the file using a text editor with elevated privileges:
A sample file might contain:
To add a new static mapping—for instance, for a database server—append a new line:
After saving the file, verify the static mapping by pinging the new hostname:
Expected output (even if the host is unreachable) will confirm that the hostname is resolving to 192.168.1.82:
This ensures that commands like:
will use the static IP mapping, making it easier to connect without remembering complex IP addresses. If you need reminders on available commands, simply type:
Press the Tab key twice to view available commands, and similarly for nmcli. A sample output for the ip command might be:

Conclusion

This guide has covered essential topics for configuring networking and hostname resolution on Linux both statically and dynamically. The areas discussed include: By carefully following these steps, you can seamlessly manage network settings and hostname resolution on your Linux system. Continue exploring and practicing these configurations to ensure robust and reliable network management. Happy networking!

Watch Video