Configuring the network in RHEL
Introduction
To navigate network administration better, namely concepts such as types of IP addresses, masks, subnets, etc., I advise studying the article
«Computer networking basics»
Configuring the network
To examine the connections, run one of the commands:
- sudo ls /sys/class/net/
- ip a s
- ifconfig
- nmcli conn show
For example:
nmcli conn show
NAME UUID TYPE DEVICE enp0s3 64486a76-a4b0-4693-9110-205da942c9c5 ethernet enp0s3 enp0s8 fc26857f-4cb0-3756-a7fa-95ec246b3981 ethernet enp0s8
If some interface, for example enp0s3, is disabled, its DEVICE column will show --
Usually an inactive interface is highlighted in grey and an active one in green.
NAME UUID TYPE DEVICE enp0s3 64486a76-a4b0-4693-9110-205da942c9c5 ethernet -- enp0s8 fc26857f-4cb0-3756-a7fa-95ec246b3981 ethernet enp0s8
You can enable the interface with the command
nmcli conn up enp0s3
Enabling the interface at boot
To avoid enabling the network interface by hand every time, you can specify in the settings ONBOOT=yes
As an option, you can go to
/etc/sysconfig/network-scripts/
And edit with vi the file ifcfg-INTERFACE_NAME - for example ifcfg-enp0s3
sudo vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
Or as root use sed
sudo sed -i s/ONBOOT=no/ONBOOT=yes/ /etc/sysconfig/network-scripts/ifcfg-enp0s3
If the command above is not entirely clear to you - study the article «The basics of sed»
You can check whether ONBOOT was changed to yes with the command
grep ONBOOT !$
Detailed information about each network interface can be obtained by running nmcli connection show interface_name
For example:
nmcli connection show ens192
connection.id: ens192 connection.uuid: 5c2584c5-7d87-4826-ba2a-79713eb62a9a connection.stable-id: -- connection.type: 802-3-ethernet connection.interface-name: ens192 connection.autoconnect: yes connection.autoconnect-priority: 0 connection.autoconnect-retries: -1 (default) connection.multi-connect: 0 (default) connection.auth-retries: -1 connection.timestamp: 1618914913 connection.read-only: no connection.permissions: -- connection.zone: -- connection.master: -- connection.slave-type: -- connection.autoconnect-slaves: -1 (default) connection.secondaries: -- connection.gateway-ping-timeout: 0 connection.metered: unknown connection.lldp: default connection.mdns: -1 (default) connection.llmnr: -1 (default) 802-3-ethernet.port: -- 802-3-ethernet.speed: 0 802-3-ethernet.duplex: -- 802-3-ethernet.auto-negotiate: no 802-3-ethernet.mac-address: -- 802-3-ethernet.cloned-mac-address: -- 802-3-ethernet.generate-mac-address-mask:-- 802-3-ethernet.mac-address-blacklist: -- 802-3-ethernet.mtu: auto 802-3-ethernet.s390-subchannels: -- 802-3-ethernet.s390-nettype: -- 802-3-ethernet.s390-options: -- 802-3-ethernet.wake-on-lan: default 802-3-ethernet.wake-on-lan-password: -- ipv4.method: auto ipv4.dns: -- ipv4.dns-search: -- ipv4.dns-options: "" ipv4.dns-priority: 0 ipv4.addresses: -- ipv4.gateway: --
Setting a static IP
nmcli conn show
NAME UUID TYPE DEVICE enp0s3 64486a76-a4b0-4693-9110-205da942c9c5 ethernet enp0s3 enp0s8 fc26857f-4cb0-3756-a7fa-95ec246b3981 ethernet enp0s8
Let us configure a static IP address for enp0s8
Network settings are stored in the directory
/etc/sysconfig/network-scripts/
You need to edit the file ifcfg-enp0s8
Main parameters:
TYPE - connection type, wired (Ethernet), wireless (Wired), etc.;
BOOTPROTO - the way the IP address is obtained: static, dhcp or none;
NAME - connection name;
DEVICE - network interface name;
ONBOOT - whether it must be started at system boot;
IPADDR - the IP address that will be used for this computer;
GATEWAY - the gateway for internet access;
NETMASK - network mask;
DNS1 - the server for resolving DNS domain names.
sudo vi /etc/sysconfig/network-scripts/ifcfg-enp0s8
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=enp0s8 UUID=939a0551-f5b5-46d8-87fe-24e5e7b1a0c1 DEVICE=enp0s8 ONBOOT=yes
Change the IP
You can change the IP quickly with ifconfig
First you need to install the package containing ifconfig -
following the instructions
You change the IP with the command
sudo ifconfig enp0s8 192.168.56.111
Instead of enp0s8 enter the required interface
Instead of 192.168.56.111 enter the IP you want to set
If you also need to change the subnet mask - run
sudo ifconfig enp0s8 192.168.56.111 netmask 255.255.255.0
Change the mask
If you need to change only the subnet mask - run
sudo ifconfig enp0s8 netmask 255.255.255.0
Add an IP
You can add an IP to the interface with the command
ip addr add 172.17.67.3/16 dev enp0s8
The IP address is added dynamically - that is, it will disappear after a reboot
You can remove this IP address with the command
ip addr del 172.17.67.3/16 dev enp0s8
Release the IP
If the IP was obtained via DHCP, you can release it by running
sudo dhcp -r enp0s8
Instead of enp0s8 enter the name of the required adapter
Find the Gateway
To find the current IP address of the gateway, run
ip r
default via 10.0.2.1 dev enp0s3 proto dhcp metric 100
The gateway IP address is 10.0.2.1
Installing ifconfig
yum provides ifconfig
or
yum whatprovides ifconfig
Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: ftp.funet.fi * epel: www.nic.funet.fi * extras: ftp.funet.fi * updates: ftp.funet.fi base/7/x86_64/filelists_db | 7.2 MB 00:00:01 epel/x86_64/filelists_db | 12 MB 00:00:03 extras/7/x86_64/filelists_db | 224 kB 00:00:00 ssh-products/x86_64/filelists_db | 89 kB 00:00:00 updates/7/x86_64/filelists_db | 3.4 MB 00:00:01 net-tools-2.0-0.25.20131004git.el7.x86_64 : Basic networking tools Repo : base Matched from: Filename : /sbin/ifconfig
ifconfig is part of net-tools, for example
Let us install net-tools
sudo yum install net-tools
Open ports
List of open ports
ss -tulpn
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
It will output all ports, so if you need to check a specific one it is convenient to use grep
ss -tulpn | grep 1234
tcp LISTEN 0 128 [::]:1234 [::]:* users:(("andrei",pid=5226,fd=14))
Open a port
To open port XXXX in firewall run
sudo firewall-cmd --add-port=XXXX/tcp --permanent
sudo firewall-cmd --reload
Details are in the article Centos firewall
hostname
Step 1. Checking the current hostname
To get information about the host, run
hostnamectl
Static hostname: localhost.localdomain Icon name: computer-vm Chassis: vm Machine ID: a714bee8007d114a84177dbb15072f2e Boot ID: b02d9498c1764a338c781672aac1be29 Virtualization: kvm Operating System: RHEL Linux 7 (Core) CPE OS Name: cpe:/o:rhel:centos:7 Kernel: Linux 3.10.0-1160.el7.x86_64 Architecture: x86-64
The required information is in the first line
Static hostname: localhost.localdomain
Step 2. Changing the hostname
Since RHEL 7 supports only Fully Qualified Domain Names (FQDNs),
I advise carefully checking the hostname you plan to use.
Allowed values:
- Lowercase letters from a to z
- Digits from 0 to 9
- Dots and hyphens
- A hostname can be from 2 to 63 characters long
- A hostname must begin and end with a digit or a letter
Run
hostnamectl set-hostname my.new-hostname.server
Details are in the article hostname
NetworkManager
You can check the status with the command
systemctl status NetworkManager
● NetworkManager.service - Network Manager Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-02-18 17:30:13 EET; 1 months 30 days ago Docs: man:NetworkManager(8) Main PID: 841 (NetworkManager) Tasks: 5 CGroup: /system.slice/NetworkManager.service ├─ 841 /usr/sbin/NetworkManager --no-daemon └─9599 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-ens192.pid -lf /var/lib/NetworkManager/dhclient-5c2584c5-7d87-4826-ba2a-79713eb62a9a-ens192.lease -cf /var/lib/Net... Dec 01 07:51:14 localhost.localdomain NetworkManager[841]: <info> [1618907115.2914] dhcp4 (ens192): gateway 10.1.103.254 Dec 01 07:51:14 localhost.localdomain NetworkManager[841]: <info> [1618907115.2914] dhcp4 (ens192): lease time 7200 Dec 01 07:51:14 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): nameserver '10.10.10.1' Dec 01 07:51:14 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): nameserver '10.1.0.2' Dec 01 07:51:14 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): domain name 'urn.su' Dec 01 07:51:14 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'urn.su.' Dec 01 07:51:14 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'hel.fi.urn.su.' Dec 01 07:51:14 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'ad.urn.su.' Dec 01 07:51:14 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): state changed bound -> bound Dec 01 07:51:14 localhost.localdomain dhclient[9599]: bound to 10.1.102.211 -- renewal in 2903 seconds.
network
You can check the status with the command
systemctl status network
● network.service - LSB: Bring up/down networking Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled) Active: active (exited) since Tue 2021-04-20 12:57:04 EEST; 7min ago Docs: man:systemd-sysv-generator(8) Process: 13493 ExecStop=/etc/rc.d/init.d/network stop (code=exited, status=0/SUCCESS) Process: 13652 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS) Dec 01 07:51:14 localhost.localdomain systemd[1]: Starting LSB: Bring up/down networking... Dec 01 07:51:14 localhost.localdomain network[13652]: Bringing up loopback interface: [ OK ] Dec 01 07:51:14 localhost.localdomain network[13652]: Bringing up interface ens192: Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5) Dec 01 07:51:14 localhost.localdomain network[13652]: [ OK ] Dec 01 07:51:14 localhost.localdomain systemd[1]: Started LSB: Bring up/down networking.
Virtual interface
A virtual interface is an alias for an existing interface. Suppose you have ens192
Run
cp /etc/sysconfig/network-scripts/ens192 /etc/sysconfig/network-scripts/ens192:0
vi /etc/sysconfig/network-scripts/ens192:0
Change something there, for example set another IP from the same subnet
IPADDR=10.1.10.2
Restart network
systemctl restart network
Since a virtual interface can only be within the same subnet, it will not let you listen on another network.
Creating a new network adapter
You can study a detailed breakdown of this action in the article «Create a new interface»
List of adapters
Get the list of adapters, including disabled ones.
nmcli d
DEVICE TYPE STATE CONNECTION wlp0s20f3 wifi connected SSH-office docker0 bridge connected docker0 p2p-dev-wlp0s20f3 wifi-p2p disconnected -- enp0s31f6 ethernet unavailable -- vboxnet0 ethernet unmanaged -- lo loopback unmanaged --
Errors and warnings
sudo systemctl restart networking
Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
systemctl status network.service
● network.service - LSB: Bring up/down networking Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled) Active: failed (Result: exit-code) since Tue 2021-01-19 19:09:42 EET; 7s ago Docs: man:systemd-sysv-generator(8) Process: 2448 ExecStop=/etc/rc.d/init.d/network stop (code=exited, status=0/SUCCESS) Process: 8268 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=1/FAILURE) Dec 01 07:51:14 server1.example.com network[8268]: RTNETLINK answers: File exists Dec 01 07:51:14 server1.example.com network[8268]: RTNETLINK answers: File exists Dec 01 07:51:14 server1.example.com network[8268]: RTNETLINK answers: File exists Dec 01 07:51:14 server1.example.com network[8268]: RTNETLINK answers: File exists Dec 01 07:51:14 server1.example.com network[8268]: RTNETLINK answers: File exists Dec 01 07:51:14 server1.example.com network[8268]: RTNETLINK answers: File exists Dec 01 07:51:14 server1.example.com systemd[1]: network.service: control process exited, code=exited status=1 Dec 01 07:51:14 server1.example.com systemd[1]: Failed to start LSB: Bring up/down networking. Dec 01 07:51:14 server1.example.com systemd[1]: Unit network.service entered failed state. Dec 01 07:51:14 server1.example.com systemd[1]: network.service failed.
Article author: Andrei Olegovich