Network Configuration on CentOS
Introduction
Network Configuration
To examine the connections, execute 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 contain --
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
To avoid bringing the network interface up manually every time, you can set ONBOOT yes in the settings
Alternatively, you can open /etc/sysconfig/network-scripts/ifcfg-enp0s3
and edit it with
vi
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, read the article «Sed basics»
To check whether ONBOOT was changed to yes, use the command
grep ONBOOT !$
Installing 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's set up 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) and so on;
BOOTPROTO - the method of obtaining an IP address, static, dhcp or none;
NAME - connection name;
DEVICE - network interface name;
ONBOOT - whether it should be started at system boot;
IPADDR - the IP address that will be used for this computer;
GATEWAY - gateway for internet access;
NETMASK - network mask;
DNS1 - 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
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.
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
Installation 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's install net-tools
sudo yum install net-tools
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 interface you need
Instead of 192.168.56.111, enter the IP you want to set
| Installing a virtual CentOS on Ubuntu | |
| A CentOS Docker container with SSH access | |
| Configuring the date and time | |
| News CentOS | |
| X Window System CentOS | |
| Changing the machine-id |
Article author: Andrei Olegovich