Network Configuration in CentOS
Introduction
To better understand this article, especially if you have concerns about IP address, netmask, subnet, etc.
I recommend to go through the
«Networks»
article.
Network Configuration
To study connections execute one of the following commands:
- sudo ls /sys/class/net/
- ip a s
- ifconfig
- nmcli conn show
E.g.:
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, it will have a -- in the DEVICE column
Usually the inactive interface is highlighted in gray and the 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
Enable the interface when loading
To not turn on the network interface manually each time, you can specify ONBOOT=yes in the settings
Alternatively, you can go to
/etc/sysconfig/network-scripts/
And edit ifcfg-INTERFACE_NAME file - e.g. ifcfg-enp0s3 e.g. with vi
sudo vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
Or use sed as root
sudo sed -i s/ONBOOT=no/ONBOOT=yes/ /etc/sysconfig/network-scripts/ifcfg-enp0s3
If the command above is not fully clear to you, study the article «sed basics»
To check whether it was possible to change ONBOOT to yes, you can use 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: --
Set 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
Setting up a static IP address for enp0s8
The network settings are stored in the directory
/etc/sysconfig/network-scripts/
You need to edit the ifcfg-enp0s8 flle
Basic parameters:
TYPE - connection type, wired (Ethernet), Wireless(Wired), etc;
BOOTPROTO - a way to get an IP address, static, dhcp or none;
NAME - the name of the connection;
DEVICE - name of the network interface;
ONBOOT - is it necessary to start at system startup;
IPADDR - The IP address that will be used for this computer;
GATEWAY - gateway for Internet access;
NETMASK - network mask;
DNS1 is a DNS domain name resolution server.
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 IP
You can quickly change the IP using ifconfig
First you need to install the package containing ifconfig -
according to the instructions
You need to change the IP with the command
sudo ifconfig enp0s8 192.168.56.111
Instead of enp0s8, enter the desired interface
Instead of 192.168.56.111, enter the IP you want to set
Если нужно поменять ещё и маску подсети - execute
sudo ifconfig enp0s8 192.168.56.111 netmask 255.255.255.0
Change Mask
If you need to change only the subnet mask execute
sudo ifconfig enp0s8 netmask 255.255.255.0
Add IP
To add an IP to the interface execute
ip addr add 172.17.67.3/16 dev enp0s8
IP address is added in a dynamic way - it will disappear after reboot.
To delete this IP address execute
ip addr del 172.17.67.3/16 dev enp0s8
Release IP
If IP was obtained via DHCP you can release it by running
sudo dhclient -r enp0s8
Type your own adapter name instead of enp0s8
Get Gateway
To get the gateway IP address execute
ip r
default via 10.0.2.1 dev enp0s3 proto dhcp metric 100
IP address of the gea 10.0.2.1
Install 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 a part of net-tools
Install net-tools
sudo yum install net-tools
Open Ports
To get list of open ports execute
ss -tulpn
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
Use grep to check a specific port
ss -tulpn | grep 1234
tcp LISTEN 0 128 [::]:1234 [::]:* users:(("andrei",pid=5226,fd=14))
Open port
To open port XXXX in the firewall execute
sudo firewall-cmd --add-port=XXXX/tcp --permanent
sudo firewall-cmd --reload
You can find more details here: Centos firewall
hostname
Step 1. Check current hostname
To get host info execute
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
Required info is in the first line.
Static hostname: localhost.localdomain
Step 2. Change hostname
Since CentOS 7 only supports Fully Qualified Domain Name (Fqdn),
I advise you to carefully check the hostname that you plan to use.
Acceptable values:
- Lowercase letters from a to z
- Numbers from 0 to 9
- Dots and hyphens
- The hostname can be from 2 to 63 characters
- Hostnames must start and end with a number or letter
Execute
hostnamectl set-hostname my.new-hostname.server
You can find more details here: 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... Aug 30 17:17:26 localhost.localdomain NetworkManager[841]: <info> [1618907115.2914] dhcp4 (ens192): gateway 10.1.103.254 Aug 30 17:17:26 localhost.localdomain NetworkManager[841]: <info> [1618907115.2914] dhcp4 (ens192): lease time 7200 Aug 30 17:17:26 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): nameserver '10.10.10.1' Aug 30 17:17:26 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): nameserver '10.1.0.2' Aug 30 17:17:26 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): domain name 'eth1.ru' Aug 30 17:17:26 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'eth1.ru.' Aug 30 17:17:26 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'hel.fi.eth1.ru.' Aug 30 17:17:26 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp (ens192): domain search 'ad.eth1.ru.' Aug 30 17:17:26 localhost.localdomain NetworkManager[841]: <info> [1618907115.2915] dhcp4 (ens192): state changed bound -> bound Aug 30 17:17:26 localhost.localdomain dhclient[9599]: bound to 10.1.102.211 -- renewal in 2903 seconds.
network
You can check status with the following 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) Aug 30 17:17:26 localhost.localdomain systemd[1]: Starting LSB: Bring up/down networking... Aug 30 17:17:26 localhost.localdomain network[13652]: Bringing up loopback interface: [ OK ] Aug 30 17:17:26 localhost.localdomain network[13652]: Bringing up interface ens192: Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5) Aug 30 17:17:26 localhost.localdomain network[13652]: [ OK ] Aug 30 17:17:26 localhost.localdomain systemd[1]: Started LSB: Bring up/down networking.
Virtual Interface
A virtual interface is an alias to an existing interface. Let's say you have ens192
Do the following
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 a different IP from the same subnet
IPADDR=10.1.10.2
Restart network
systemctl restart network
Since the virtual interface can only be inside the same subnet, it will not give you the opportunity to listen to another session.
Create a new network adapter
Adapter list
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) Aug 30 17:17:26 server1.example.com network[8268]: RTNETLINK answers: File exists Aug 30 17:17:26 server1.example.com network[8268]: RTNETLINK answers: File exists Aug 30 17:17:26 server1.example.com network[8268]: RTNETLINK answers: File exists Aug 30 17:17:26 server1.example.com network[8268]: RTNETLINK answers: File exists Aug 30 17:17:26 server1.example.com network[8268]: RTNETLINK answers: File exists Aug 30 17:17:26 server1.example.com network[8268]: RTNETLINK answers: File exists Aug 30 17:17:26 server1.example.com systemd[1]: network.service: control process exited, code=exited status=1 Aug 30 17:17:26 server1.example.com systemd[1]: Failed to start LSB: Bring up/down networking. Aug 30 17:17:26 server1.example.com systemd[1]: Unit network.service entered failed state. Aug 30 17:17:26 server1.example.com systemd[1]: network.service failed.