Network Configuration in Rocky Linux
Introduction
To get a better grasp of network administration, namely concepts such as IP address types, netmasks, subnets, etc., I recommend studying the article
«Fundamentals of computer networks»
Network Configuration
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 down, its DEVICE column will show --
Usually an inactive interface is highlighted in gray 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 bring the interface up with the command
nmcli conn up enp0s3
Enable the interface at boot
To avoid bringing the network interface up manually every time, you can specify in the settings
ONBOOT=yes
Alternatively, you can go to
/etc/sysconfig/network-scripts/ifcfg-enp0s3
And edit the files ifcfg-INTERFACE_NAME - for example ifcfg-enp0s3 using vi
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 «sed fundamentals»
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 enp0s3
connection.id: enp0s3 connection.uuid: 47685cbb-ee41-4294-9b6d-efaf7494588e connection.stable-id: -- connection.type: 802-3-ethernet connection.interface-name: enp0s3 …
The full output can be studied here
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 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 needs to be started at system boot;
IPADDR - the IP address that will be used for this computer;
GATEWAY - the gateway for internet access;
NETMASK - the network mask;
DNS1 - the server for DNS domain name resolution.
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
Using Iproute2
Example of assigning the IP address 10.0.2.16/24 to interface enp3s0
sudo ip addr add 10.0.2.16/24 dev enp3s0
Using 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 interface you need
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 netmask
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 an 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 adapter you need
Find out the Gateway
To find out the current gateway IP address, 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 included, for example, in net-tools
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 list 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 execute
sudo firewall-cmd --add-port=XXXX/tcp --permanent
sudo firewall-cmd --reload
Details are in the article rocky 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: rocky Linux 7 (Core) CPE OS Name: cpe:/o:rocky:rocky:7 Kernel: Linux 3.10.0-1160.el7.x86_64 Architecture: x86-64
The information you need is in the first line
Static hostname: localhost.localdomain
Step 2. Changing the hostname
Since rocky 7 supports only Fully Qualified Domain Names (FQDNs),
I recommend carefully checking the hostname you plan to use.
Allowed values:
- Lowercase letters from a to z
- Digits from 0 to 9
- Dots and hyphens
- Hostnames can be from 2 to 63 characters long
- Hostnames must begin and end with a digit or a letter
Run
hostnamectl set-hostname my.new-hostname.server
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... Apr 20 11:25:15 localhost.localdomain NetworkManager[841]: <info> [1618907115.2914] dhcp4 (ens192): gateway 10.1.103.254 Apr 20 11:25:15 localhost.localdomain NetworkManager[841]: <info> [1618907115.2914] dhcp4 (ens192): lease time 7200 Apr 20 11:25:15 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): nameserver '10.10.10.1' Apr 20 11:25:15 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): nameserver '10.1.0.2' Apr 20 11:25:15 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): domain name 'eth1.ru' Apr 20 11:25:15 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'eth1.ru.' Apr 20 11:25:15 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'hel.fi.eth1.ru.' Apr 20 11:25:15 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'ad.eth1.ru.' Apr 20 11:25:15 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): state changed bound -> bound Apr 20 11:25:15 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) Apr 20 12:57:03 localhost.localdomain systemd[1]: Starting LSB: Bring up/down networking... Apr 20 12:57:04 localhost.localdomain network[13652]: Bringing up loopback interface: [ OK ] Apr 20 12:57:04 localhost.localdomain network[13652]: Bringing up interface ens192: Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5) Apr 20 12:57:04 localhost.localdomain network[13652]: [ OK ] Apr 20 12:57:04 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.
Create a new network adapter
List of adapters
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) Jan 19 19:09:42 server1.example.com network[8268]: RTNETLINK answers: File exists Jan 19 19:09:42 server1.example.com network[8268]: RTNETLINK answers: File exists Jan 19 19:09:42 server1.example.com network[8268]: RTNETLINK answers: File exists Jan 19 19:09:42 server1.example.com network[8268]: RTNETLINK answers: File exists Jan 19 19:09:42 server1.example.com network[8268]: RTNETLINK answers: File exists Jan 19 19:09:42 server1.example.com network[8268]: RTNETLINK answers: File exists Jan 19 19:09:42 server1.example.com systemd[1]: network.service: control process exited, code=exited status=1 Jan 19 19:09:42 server1.example.com systemd[1]: Failed to start LSB: Bring up/down networking. Jan 19 19:09:42 server1.example.com systemd[1]: Unit network.service entered failed state. Jan 19 19:09:42 server1.example.com systemd[1]: network.service failed.
Full output of the command (back to the article)
nmcli connection show enp0s3
connection.id: enp0s3 connection.uuid: 47685cbb-ee41-4294-9b6d-efaf7494588e connection.stable-id: -- connection.type: 802-3-ethernet connection.interface-name: enp0s3 connection.autoconnect: no connection.autoconnect-priority: 0 connection.autoconnect-retries: -1 (default) connection.multi-connect: 0 (default) connection.auth-retries: -1 connection.timestamp: 1624022292 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) connection.wait-device-timeout: -1 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: -- ipv4.routes: -- ipv4.route-metric: -1 ipv4.route-table: 0 (unspec) ipv4.routing-rules: -- ipv4.ignore-auto-routes: no ipv4.ignore-auto-dns: no ipv4.dhcp-client-id: -- ipv4.dhcp-iaid: -- ipv4.dhcp-timeout: 0 (default) ipv4.dhcp-send-hostname: yes ipv4.dhcp-hostname: -- ipv4.dhcp-fqdn: -- ipv4.dhcp-hostname-flags: 0x0 (none) ipv4.never-default: no ipv4.may-fail: yes ipv4.dad-timeout: -1 (default) ipv4.dhcp-vendor-class-identifier: -- ipv4.dhcp-reject-servers: -- ipv6.method: auto ipv6.dns: -- ipv6.dns-search: -- ipv6.dns-options: -- ipv6.dns-priority: 0 ipv6.addresses: -- ipv6.gateway: -- ipv6.routes: -- ipv6.route-metric: -1 ipv6.route-table: 0 (unspec) ipv6.routing-rules: -- ipv6.ignore-auto-routes: no ipv6.ignore-auto-dns: no ipv6.never-default: no ipv6.may-fail: yes ipv6.ip6-privacy: -1 (unknown) ipv6.addr-gen-mode: eui64 ipv6.ra-timeout: 0 (default) ipv6.dhcp-duid: -- ipv6.dhcp-iaid: -- ipv6.dhcp-timeout: 0 (default) ipv6.dhcp-send-hostname: yes ipv6.dhcp-hostname: -- ipv6.dhcp-hostname-flags: 0x0 (none) ipv6.token: -- proxy.method: none proxy.browser-only: no proxy.pac-url: -- proxy.pac-script: -- GENERAL.NAME: enp0s3 GENERAL.UUID: 47685cbb-ee41-4294-9b6d-efaf7494588e GENERAL.DEVICES: enp0s3 GENERAL.IP-IFACE: enp0s3 GENERAL.STATE: activated GENERAL.DEFAULT: no GENERAL.DEFAULT6: no GENERAL.SPEC-OBJECT: -- GENERAL.VPN: no GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/1 GENERAL.ZONE: -- GENERAL.MASTER-PATH: -- IP4.ADDRESS[1]: 192.168.56.121/24 IP4.GATEWAY: -- IP4.ROUTE[1]: dst = 192.168.56.0/24, nh = 0.0.0.0, mt = 100 DHCP4.OPTION[1]: dhcp_lease_time = 600 DHCP4.OPTION[2]: dhcp_server_identifier = 192.168.56.100 DHCP4.OPTION[3]: expiry = 1624022892 DHCP4.OPTION[4]: ip_address = 192.168.56.121 DHCP4.OPTION[5]: requested_broadcast_address = 1 DHCP4.OPTION[6]: requested_domain_name = 1 DHCP4.OPTION[7]: requested_domain_name_servers = 1 DHCP4.OPTION[8]: requested_domain_search = 1 DHCP4.OPTION[9]: requested_host_name = 1 DHCP4.OPTION[10]: requested_interface_mtu = 1 DHCP4.OPTION[11]: requested_ms_classless_static_routes = 1 DHCP4.OPTION[12]: requested_nis_domain = 1 DHCP4.OPTION[13]: requested_nis_servers = 1 DHCP4.OPTION[14]: requested_ntp_servers = 1 DHCP4.OPTION[15]: requested_rfc3442_classless_static_routes = 1 DHCP4.OPTION[16]: requested_root_path = 1 DHCP4.OPTION[17]: requested_routers = 1 DHCP4.OPTION[18]: requested_static_routes = 1 DHCP4.OPTION[19]: requested_subnet_mask = 1 DHCP4.OPTION[20]: requested_time_offset = 1 DHCP4.OPTION[21]: requested_wpad = 1 DHCP4.OPTION[22]: subnet_mask = 255.255.255.0 IP6.ADDRESS[1]: fe80::a00:27ff:fe6c:cb2b/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 100 IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255
Article author: Andrei Olegovich