CentOS 9: Configure Network (Step-by-Step)

Requirements

Before we dive into configure your network on CentOS 9, let’s ensure you have everything you need for a smooth experience. These prerequisites cover essential aspects to guarantee you’re ready to tackle network configuration effectively.

User Privileges

First and foremost, you’ll need root or sudo privileges to modify network settings. CentOS 9, like other Linux distributions, restricts network configuration changes to administrative accounts for security reasons. Ensure you’re logged in as root or have sudo access before proceeding.

Understanding Network Interfaces

It’s beneficial to identify your network interfaces. Common interfaces include eth0, enp0s3, or wlan0. Knowing these names is crucial when you use commands like ip addr or nmcli. If you don’t know your interface names, use the command below.

ip link show

This command will list all available interfaces along with their current status.

Basic Networking Knowledge

Familiarity with basic networking concepts such as IP addresses, subnets, gateways, and DNS servers is helpful. Knowing how these elements interact will enable you to configure your network more effectively. For instance, understanding the difference between a static IP and dynamic IP address can prevent configuration headaches.

Access to a Terminal

You’ll need access to a terminal or command-line interface. CentOS 9 primarily relies on command-line tools for network configuration, so make sure you can access the terminal either directly on the server or through SSH.

Text Editor

A text editor is essential for modifying configuration files. Common choices include vi, nano, or gedit. If you’re comfortable with vi or vim, that works great, but for beginners, nano is often easier to use.

Verifying NetworkManager Installation

First, let’s confirm that NetworkManager is installed. Open your terminal and run:

rpm -qa | grep NetworkManager

This command lists all installed packages and filters the output to show only those containing “NetworkManager”. If NetworkManager is installed, you’ll see it listed. If not, proceed to the next step.

Installing NetworkManager

If NetworkManager is missing, you can install it using the dnf package manager. Execute the following command:

dnf install NetworkManager -y

The dnf command is the primary tool for installing, updating, and removing packages on CentOS 9. The install option tells dnf to install the specified package (NetworkManager), and the -y option automatically answers “yes” to any prompts, ensuring a non-interactive installation.

Installing Network Tools

To aid in network configuration and troubleshooting, installing the net-tools package is highly recommended. It provides essential utilities like ifconfig, netstat, and route. Although some of these tools are being replaced by newer alternatives, they remain useful for quick checks.

dnf install net-tools -y

Similar to the NetworkManager installation, this command uses dnf to install the net-tools package along with all its dependencies.

Installing iproute2

The iproute2 package provides modern networking tools such as the ip command, which is a powerful alternative to ifconfig. It’s an essential tool for configuring network interfaces, routing, and network neighbor discovery. To install iproute2, use the following command:

dnf install iproute2 -y

This command ensures that you have the latest iproute2 utilities, which are crucial for advanced network settings and troubleshooting.

Enabling and Starting NetworkManager

After installation, ensure NetworkManager is enabled and running. Use these commands:

systemctl enable NetworkManager
systemctl start NetworkManager

The enable command configures the NetworkManager service to start automatically at boot time. The start command initiates the service immediately. To verify that NetworkManager is running, use:

systemctl status NetworkManager

This will display the current status of the NetworkManager service, including whether it’s active and any recent log messages.

With these tools installed, you are now set to configure network interfaces on your CentOS 9 system effectively. Let’s proceed to the next chapter to learn about configuring network interfaces.

Now that we have the necessary tools installed, let’s dive into the configuration of network interfaces on CentOS 9. This involves understanding and editing the interface configuration files and using NetworkManager with nmcli and nmtui.

Network Interface Configuration Files

In CentOS, network interfaces are configured using files located in the /etc/sysconfig/network-scripts/ directory. Each interface has its own configuration file, typically named ifcfg-eth0, ifcfg-enp0s3, or similar, depending on the interface name.

Understanding ifcfg files

ifcfg files contain settings that define how a network interface operates. Key parameters include:

  • TYPE: Specifies the interface type (e.g., Ethernet).
  • NAME: The interface name.
  • DEVICE: The device name that the configuration applies to.
  • BOOTPROTO: Specifies how the IP address is obtained (dhcp for dynamic, static for static).
  • IPADDR: The static IP address for the interface.
  • NETMASK or PREFIX: The network mask or prefix length.
  • GATEWAY: The gateway IP address.
  • DNS1 and DNS2: Primary and secondary DNS server IP addresses.
  • ONBOOT: Set to yes to activate the interface at boot.

Here’s an example of a static IP configuration:

TYPE=Ethernet
NAME=enp0s3
DEVICE=enp0s3
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
ONBOOT=yes

And here’s an example of a dynamic IP configuration using DHCP:

TYPE=Ethernet
NAME=enp0s3
DEVICE=enp0s3
BOOTPROTO=dhcp
ONBOOT=yes

Editing ifcfg files

To modify an ifcfg file, use a text editor like nano or vi. For example:

nano /etc/sysconfig/network-scripts/ifcfg-enp0s3

Make the necessary changes, save the file, and exit the editor. After editing, restart the network service to apply the changes:

systemctl restart network

Configuring NetworkManager

NetworkManager is a dynamic network control and configuration system that simplifies managing network connections. It can be controlled via the command line using nmcli or through a text-based user interface using nmtui.

nmcli command examples

The nmcli command allows you to manage network connections, devices, and the NetworkManager itself. Here are some common examples:

  • Display network status:
nmcli general status
  • Show all network connections:
nmcli connection show
  • Show details of a specific connection:
nmcli connection show enp0s3
  • Modify a connection to use a static IP:
nmcli connection modify enp0s3 ipv4.method manual ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns 8.8.8.8 connection.autoconnect yes
  • Set a connection to use DHCP:
nmcli connection modify enp0s3 ipv4.method auto connection.autoconnect yes
  • Activate a connection:
nmcli connection up enp0s3
  • Deactivate a connection:
nmcli connection down enp0s3

Each option in the nmcli command serves a specific purpose. For instance, ipv4.method manual sets the IPv4 configuration method to manual, while ipv4.addresses specifies the IP address and subnet in CIDR notation (e.g., 192.168.1.100/24). connection.autoconnect yes ensures that the connection automatically activates on boot. For more details, check the official NetworkManager documentation.

nmtui command examples

nmtui provides a text-based user interface for managing NetworkManager. To start nmtui, simply type:

nmtui

This will open a simple interface in your terminal, allowing you to:

  • Edit a connection: Modify existing network connections.
  • Activate a connection: Bring a connection up or down.
  • Set system hostname: Configure the system’s hostname.

Use the arrow keys to navigate, the Enter key to select options, and the Tab key to move between elements. nmtui is particularly useful for those who prefer a menu-driven interface over command-line syntax. For example, to configure network settings, select “Edit a connection”, choose the connection you want to modify, and enter the desired IP address, gateway, and DNS settings. Once finished, select “OK” to save the changes.

By using either nmcli or nmtui, you can effectively manage and configure network connections on your CentOS 9 system. Both tools offer powerful features for managing everything from basic IP settings to more advanced configurations. Make sure that CentOS firewall is configured appropriatelly.

Now that we have the necessary tools installed, let’s dive into the real-world scenarios for configuring network interfaces on CentOS 9. Whether you need a stable, unchanging IP address or dynamic configurations, this section will guide you through the common use cases.

Setting a Static IP Address

A static IP address is essential for servers or devices that need a consistent address for reliable access. Here’s how to set it up:

Step-by-step guide

  1. Identify the Interface: Determine the name of your network interface (e.g., enp0s3). You can use ip link show to list available interfaces.
  2. Edit the Configuration File: Open the interface configuration file using a text editor:
nano /etc/sysconfig/network-scripts/ifcfg-enp0s3
  1. Modify the File: Change the BOOTPROTO to static, and add the IPADDR, NETMASK (or PREFIX), GATEWAY, and DNS settings:
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
ONBOOT=yes
  1. Restart the Network Service: Apply the changes by restarting the network service:
systemctl restart network
  1. Verify the Configuration: Check the new IP address using:
ip addr show enp0s3

Configuration file example

Here’s a complete example of a static IP configuration file:

TYPE=Ethernet
NAME=enp0s3
DEVICE=enp0s3
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
ONBOOT=yes

Configuring DNS Settings

DNS (Domain Name System) settings are crucial for resolving domain names to IP addresses, allowing your system to access websites and other network resources using human-readable names.

Understanding DNS resolution

DNS resolution is the process of translating domain names (like google.com) into IP addresses (like 142.250.185.142). Your system needs to know which DNS servers to use to perform this translation.

Setting DNS servers

You can configure DNS servers in two primary ways:

  1. Using ifcfg Files: Add or modify the DNS1 and DNS2 parameters in the interface configuration file:
DNS1=8.8.8.8
DNS2=8.8.4.4
  1. Using nmcli: Modify the connection using the nmcli command:
nmcli connection modify enp0s3 ipv4.dns "8.8.8.8 8.8.4.4"
  1. Restart the Connection: After making changes, restart the connection:
nmcli connection up enp0s3

Configuring a Gateway

The gateway is the network node that your computer uses to access networks outside of its local network. It’s essential for communicating with the internet or other remote networks.

Understanding default gateways

The default gateway is the IP address of the router that forwards traffic from your local network to other networks. Without a correctly configured gateway, your system won’t be able to access external resources.

Configuring the gateway

You can configure the gateway in the ifcfg file or using nmcli:

  1. Using ifcfg Files: Add or modify the GATEWAY parameter in the interface configuration file:
GATEWAY=192.168.1.1
  1. Using nmcli: Modify the connection using the nmcli command:
nmcli connection modify enp0s3 ipv4.gateway 192.168.1.1
  1. Restart the Connection: Apply the changes:
nmcli connection up enp0s3

By understanding and implementing these real use cases, you can effectively configure network settings such as static IP addresses, DNS settings, and gateways on your CentOS 9 system, ensuring reliable and efficient network connectivity. NetworkManager, a daemon that simplifies the use of computer networks, is used to manage the network interfaces. Its configuration is stored in /etc/NetworkManager/NetworkManager.conf.

Let’s move on to troubleshooting common network issues in CentOS 9. Troubleshooting involves identifying, diagnosing, and resolving problems to restore normal operation. As we’ve learned, it’s a systematic search for the source of a problem.

Common issues

When things go wrong with your network configuration, here are a couple of common problems you might encounter:

Connectivity problems

Connectivity issues can manifest in several ways, such as the inability to reach external websites or communicate with other devices on the network. Here’s how to diagnose and address them:

  • Check the Interface Status: Ensure that the network interface is active and connected. Use the ip link show command to verify the interface status. Look for the UP state.
ip link show enp0s3
  • Verify IP Configuration: Confirm that the interface has a valid IP address, subnet mask, and gateway. Use the ip addr show command:
ip addr show enp0s3
  • Test Basic Connectivity: Use the ping command to check connectivity to the gateway and external hosts:
ping 192.168.1.1
ping google.com
  • Check the Routing Table: Ensure that the routing table is correctly configured to route traffic to the appropriate network. Use the ip route show command:
ip route show

DNS resolution failures

DNS resolution failures occur when the system cannot translate domain names into IP addresses. This can prevent you from accessing websites and other network resources. To troubleshoot DNS issues:

  • Verify DNS Settings: Check the DNS server settings in the /etc/resolv.conf file or using nmcli:
cat /etc/resolv.conf
  • Test DNS Resolution: Use the nslookup or dig command to query DNS servers:
nslookup google.com
dig google.com
  • Check DNS Server Reachability: Ping the DNS servers to ensure they are reachable:
ping 8.8.8.8

Troubleshooting tools

Several command-line tools are invaluable for diagnosing network issues. Here are some of the most useful:

ping

The ping command tests basic network connectivity by sending ICMP echo requests to a target host. It helps determine if a host is reachable and measures the round-trip time for packets to reach the host and return. By sending ICMP (Internet Control Message Protocol) packets to a specified network host you can verify that the host exists and is accepting requests.

ping google.com

ip

The ip command from the iproute2 suite is a versatile tool for managing network interfaces, routing, and network neighbors. It replaces older tools like ifconfig and route. It is a powerful command line tool, used to show or manipulate routing, devices, policy routing and tunnels. For example, the command ip addr show can be used to view all available interfaces with its current configuration.

ip addr show

traceroute

The traceroute command traces the route that packets take to reach a destination host. It identifies each hop along the path, providing valuable information for diagnosing routing issues. It displays the path (sequence of routers) to a network host and measures transit delays of packets across an Internet Protocol (IP) network. The history of transit delays of packets through the network is deduced from the recorded round-trip times for packets, at each successive hop (intermediate destination) in the path to a given destination.

traceroute google.com

By using these troubleshooting tools and techniques, you can effectively diagnose and resolve network issues in CentOS 9, ensuring smooth and reliable network operation.

Now that we have the necessary tools installed, let’s discuss advanced networking techniques in CentOS 9, including bonding, VLANs, and network teaming. These configurations enable increased bandwidth, redundancy, and network segmentation.

Bonding

Bonding, also known as link aggregation, combines multiple network interfaces into a single logical interface. This provides increased bandwidth and redundancy. If one interface fails, the other(s) continue to operate, maintaining network connectivity. This is particularly useful for servers needing high availability.

Configuration and use cases

To configure bonding, you’ll need to create a bond interface and then configure the individual interfaces to be part of that bond. Here’s a basic example:

  1. Create the Bond Interface: Create a configuration file for the bond interface (e.g., ifcfg-bond0) in /etc/sysconfig/network-scripts/:
TYPE=Bond
NAME=bond0
DEVICE=bond0
BOOTPROTO=none
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
ONBOOT=yes
BONDING_OPTS="mode=1 miimon=100"

The BONDING_OPTS line specifies the bonding mode and monitoring interval. mode=1 sets the bonding mode to active-backup, where one interface is active and the others are on standby. The miimon=100 sets the monitoring interval to 100ms.

  1. Configure Slave Interfaces: Modify the configuration files for the physical interfaces (e.g., ifcfg-eth0 and ifcfg-eth1) to be part of the bond:
TYPE=Ethernet
NAME=eth0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes

Repeat for eth1. The MASTER=bond0 and SLAVE=yes lines assign the interface to the bond0 interface.

  1. Restart the Network Service: Apply the changes by restarting the network service:
systemctl restart network

One use case for bonding is to create a link aggregation between end-devices (servers) connected to different access-switches, offering these servers a redundant, load-balancing connection to the core-network in a loop-free environment, eliminating the requirement for the use of a spanning-tree protocol.

VLANs

A VLANs (Virtual LANs) segment a physical network into multiple logical networks. VLANs improve security, network management, and reduce broadcast traffic. Each VLAN operates as an independent broadcast domain.

Configuration and use cases

To configure VLANs, you’ll need to create VLAN interfaces on top of a physical interface. Here’s how:

  1. Create the VLAN Interface: Create a configuration file for the VLAN interface (e.g., ifcfg-eth0.10 for VLAN ID 10 on eth0):
TYPE=Vlan
NAME=eth0.10
DEVICE=eth0.10
VLAN=yes
VLAN_ID=10
BOOTPROTO=none
IPADDR=192.168.10.100
NETMASK=255.255.255.0
ONBOOT=yes

The VLAN=yes and VLAN_ID=10 lines specify that this is a VLAN interface with VLAN ID 10.

  1. Bring Up the Interface: Activate the VLAN interface:
ip link set dev eth0.10 up
  1. Assign IP Address: Assign the IP address to the interface:
ip addr add 192.168.10.100/24 dev eth0.10

One use case for VLANs is to isolate network traffic for different departments or services within an organization. This provides enhanced security and simplifies network management. Prior to supporting standardized IEEE 802.1Q tagging, some systems used proprietary Virtual LAN Trunking (VLT).

Network Teaming

Network Teaming is another method for link aggregation, similar to bonding, but implemented differently. It is often seen as more flexible and modern approach.

Configuration and use cases

  1. Install Teamd: Ensure that the teamd daemon is installed:
dnf install teamd -y
  1. Create a Team Interface: Create a team interface configuration file (e.g., ifcfg-team0):
TYPE=Team
NAME=team0
DEVICE=team0
BOOTPROTO=none
IPADDR=192.168.2.100
NETMASK=255.255.255.0
GATEWAY=192.168.2.1
DNS1=8.8.8.8
ONBOOT=yes
TEAM_CONFIG='{"device": {"name": "team0", "runner": {"name": "activebackup"}, "link_watch": {"name": "ethtool"}}}'
  1. Configure Team Ports: Configure the physical interfaces to be used as team ports (e.g., ifcfg-eth0):
TYPE=Ethernet
NAME=eth0
DEVICE=eth0
ONBOOT=yes
TEAM_MASTER=team0
TEAM_PORT=yes
BOOTPROTO=none

Repeat the configuration for the other ethernet port

  1. Restart the network
systemctl restart network

Network Teaming can provide similar benefits to bonding, such as increased bandwidth and link redundancy, but with a potentially simpler configuration.

By implementing bonding, VLANs, or network teaming, you can significantly enhance the performance, reliability, and security of your CentOS 9 network. These advanced network settings are essential for modern server environments.

Now that we have the necessary tools installed, let’s discuss advanced networking techniques in CentOS 9, including bonding, VLANs, and network teaming. These configurations enable increased bandwidth, redundancy, and network segmentation.

Bonding

Bonding, also known as link aggregation, combines multiple network interfaces into a single logical interface. This provides increased bandwidth and redundancy. If one interface fails, the other(s) continue to operate, maintaining network connectivity. This is particularly useful for servers needing high availability.

Configuration and use cases

To configure bonding, you’ll need to create a bond interface and then configure the individual interfaces to be part of that bond. Here’s a basic example:

  1. Create the Bond Interface: Create a configuration file for the bond interface (e.g., ifcfg-bond0) in /etc/sysconfig/network-scripts/:
TYPE=Bond
NAME=bond0
DEVICE=bond0
BOOTPROTO=none
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
ONBOOT=yes
BONDING_OPTS="mode=1 miimon=100"

The BONDING_OPTS line specifies the bonding mode and monitoring interval. mode=1 sets the bonding mode to active-backup, where one interface is active and the others are on standby. The miimon=100 sets the monitoring interval to 100ms.

  1. Configure Slave Interfaces: Modify the configuration files for the physical interfaces (e.g., ifcfg-eth0 and ifcfg-eth1) to be part of the bond:
TYPE=Ethernet
NAME=eth0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes

Repeat for eth1. The MASTER=bond0 and SLAVE=yes lines assign the interface to the bond0 interface.

  1. Restart the Network Service: Apply the changes by restarting the network service:
systemctl restart network

One use case for bonding is to create a link aggregation between end-devices (servers) connected to different access-switches, offering these servers a redundant, load-balancing connection to the core-network in a loop-free environment, eliminating the requirement for the use of a spanning-tree protocol.

Conclusion

In conclusion, mastering the CentOS 9 configure network process is crucial for anyone managing CentOS 9 servers, wether for personal projects or enterprise environments. From understanding basic network settings to implementing advanced configurations like bonding and VLANs, and taking into account the CentOS firewall, you now have a solid foundation to manage and troubleshoot your network. Remember to always back up your configurations and test changes in a controlled environment. With tools like nmcli and the ip command, you can efficiently handle both simple and complex networking tasks. Keep exploring and experimenting to further enhance your skills!

Reference

CentOS Official Site –https://www.centos.org/