Basic network settings in CentOS

This article is also available in Russian: Russian version.

Introduction

To find your way around network administration, that is, concepts such as IP address types, masks, subnets and so on, I recommend reading the article

«Computer Network Basics»

Network settings

To inspect the connections, run one of these commands:

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 an interface, for example enp0s3, is down, its DEVICE column shows --

An inactive interface is usually shown 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 bring the interface up with the command

nmcli conn up enp0s3

Bring the interface up at boot

To avoid bringing the network interface up by hand every time, you can set ONBOOT=yes in its settings

Alternatively, 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, read the article «sed Basics»

You can check whether ONBOOT was changed to yes with the command

grep ONBOOT !$

Detailed information about each network interface is available 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 set a static IP address for enp0s8

The network settings are stored in the directory

/etc/sysconfig/network-scripts/

The file to edit is ifcfg-enp0s8

Main parameters:
TYPE - connection type, wired (Ethernet), wireless (Wired), etc.;
BOOTPROTO - how the IP address is obtained: static, dhcp or none;
NAME - connection name;
DEVICE - network interface name;
ONBOOT - whether to bring it up at system start;
IPADDR - the IP address to be used for this computer;
GATEWAY - the gateway for internet access;
NETMASK - network mask;
DNS1 - the server for resolving domain names (DNS).

sudo vi /etc/sysconfig/network-scripts/ifcfg-enp0s8

For DHCP, which is most likely the default, it will be something like

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 BOOTPROTO to none and add a few lines

TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none # Server IP # IPADDR=192.168.56.130 # Subnet # PREFIX=24 # Defautl Gateway # GATEWAY=192.168.56.1 # DNS Servers # DNS1=8.8.8.8 DNS2=1.1.1.1 DNS3=192.168.56.1 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=enp0s3 UUID=40a957b1-90fd-428c-9e99-df7849e1d104 DEVICE=enp0s3 ONBOOT=yes

sudo systemctl restart network

Change the IP

You can quickly change the IP with ifconfig

First you need to install the package that contains ifconfig - see the instructions

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 your IP was obtained from a DHCP server, tell it about the change by running

sudo dhclient -r

An IP address changed this way will be reset by the DHCP server to the one it prefers after the first reboot.

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 only need to change 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 disappears after a reboot

You can remove this IP address with the command

ip addr del 172.17.67.3/16 dev enp0s8

Get a DHCP address

If you need to request an address from DHCP explicitly, run

sudo dhclient enp0s8

Instead of enp0s8 enter the name of the adapter you need

Release the IP

If the IP was obtained via DHCP, you can release it by running

sudo dhclient -r enp0s8

Instead of enp0s8 enter the name of the adapter you need. This command is usually needed by those who have reset the address.

Find the gateway

To find out 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, for example, 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 prints all ports, so to check a particular 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 the firewall, run

sudo firewall-cmd --add-port=XXXX/tcp --permanent
sudo firewall-cmd --reload

Details in the article Centos firewall

hostname

Step 1. Check 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: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.el7.x86_64 Architecture: x86-64

The information you need is on the first line

Static hostname: localhost.localdomain

Step 2. Change the hostname

Since CentOS 7 supports only Fully Qualified Domain Names (FQDNs), I advise you to check carefully the hostname you plan to use.

Allowed values:

Run

hostnamectl set-hostname my.new-hostname.server

Details 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... Nov 27 14:07:58 localhost.localdomain NetworkManager[841]: <info> [1618907115.2914] dhcp4 (ens192): gateway 10.1.103.254 Nov 27 14:07:58 localhost.localdomain NetworkManager[841]: <info> [1618907115.2914] dhcp4 (ens192): lease time 7200 Nov 27 14:07:58 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): nameserver '10.10.10.1' Nov 27 14:07:58 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): nameserver '10.1.0.2' Nov 27 14:07:58 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): domain name 'eth1.ru' Nov 27 14:07:58 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'eth1.ru.' Nov 27 14:07:58 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'hel.fi.eth1.ru.' Nov 27 14:07:58 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'ad.eth1.ru.' Nov 27 14:07:58 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): state changed bound -> bound Nov 27 14:07:58 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) Nov 27 14:07:58 localhost.localdomain systemd[1]: Starting LSB: Bring up/down networking... Nov 27 14:07:58 localhost.localdomain network[13652]: Bringing up loopback interface: [ OK ] Nov 27 14:07:58 localhost.localdomain network[13652]: Bringing up interface ens192: Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5) Nov 27 14:07:58 localhost.localdomain network[13652]: [ OK ] Nov 27 14:07:58 localhost.localdomain systemd[1]: Started LSB: Bring up/down networking.

Virtual interface

A virtual interface is an alias to an existing interface. Say 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 exist only inside the same subnet, it will not let you listen to another network.

Create a new network adapter

You can find a detailed walkthrough of this in the article «Add a new network interface in CentOS»

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) Nov 27 14:07:58 server1.example.com network[8268]: RTNETLINK answers: File exists Nov 27 14:07:58 server1.example.com network[8268]: RTNETLINK answers: File exists Nov 27 14:07:58 server1.example.com network[8268]: RTNETLINK answers: File exists Nov 27 14:07:58 server1.example.com network[8268]: RTNETLINK answers: File exists Nov 27 14:07:58 server1.example.com network[8268]: RTNETLINK answers: File exists Nov 27 14:07:58 server1.example.com network[8268]: RTNETLINK answers: File exists Nov 27 14:07:58 server1.example.com systemd[1]: network.service: control process exited, code=exited status=1 Nov 27 14:07:58 server1.example.com systemd[1]: Failed to start LSB: Bring up/down networking. Nov 27 14:07:58 server1.example.com systemd[1]: Unit network.service entered failed state. Nov 27 14:07:58 server1.example.com systemd[1]: network.service failed.

Article author: Arthur Isaev