CentOS 7 network configuration details

  • 2020-05-13 04:23:56
  • OfStack

Today, I installed CentOS 7 on one PC. At that time, I selected the minimum installation mode, and immediately after the installation, I used ifconfig to check the ip address of the machine (the LAN already has DHCP). I found an error, indicating that the ifconfig command was not found.


[root@centos1 ~]# ifconfig

-bash: ifconfig: command not found

First, the customary echo $PATH(see the current PATH environment variable, which is similar to DOS's path command 1, and note that the commands in Linux are case sensitive) displays the following:


[root@centos1 ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

As shown above, the path to place the hypervisor /usr/sbin already exists, which is the path to put the external command. Directly use ls to view /usr/sbin/ directory, but you don't see ifconfig either. Why?


[root@centos1 ~]# ls /usr/sbin/

I still don't give up and can't find ifconfig with find command.


[root@centos1 ~]# find / -name "ifconfig"

At this point in my mind, I think I have replaced ifconfig with some command. Baidu 1 search, sure enough, has used ip command instead of ifconfig command. Common parameters for the ip command are listed below.

ip [option] operation object {link|addr|route... }

# ip link show # displays network interface information
ip link set eth0 upi # open the network card
ip link set eth0 down # close the network card
# ip link set eth0 promisc on # opens the network card mix mode
Shut down the network card in the mixed mode # ip link set eth0 promisc offi #
# ip link set eth0 txqueuelen 1200 # set the network card queue length
# ip link set eth0 mtu 1400 # set the maximum network card transfer unit
# ip addr show # displays the network card IP information
# ip addr add 192.168.0.1/24 dev eth0 # set eth0 network card IP address 192.168.0.1
dev 192.168.0.1/24 dev dev eth # delete the eth0 network card IP address
# ip route list # view routing information
via 192.168.0.254 dev eth0 # set the gateway of 192.168.4.0 network segment to 192.168.0.254, and the data goes to eth0 interface
# ip route add default via 192.168.0.254 dev eth0 # set the default gateway to 192.168.0.254
# ip route del 192.168.4.0/24 # remove the gateway for the 192.168.4.0 segment
# ip route del default # remove the default route

After entering the ip addr command, I found that the enp2s0 network card (this enp2s0 is my network card here) does not have the ip address.


[root@centos1 ~]# ip addr

Since there is no ip address, go directly to the /etc/sysconfig/ network-scripts directory and see the profile name of the ip information for the network card under 1.


[root@centos1 ~]# ls /etc/sysconfig/network-scripts/
ifcfg-enp2s0 ifdown-eth  ifdown-post  ifdown-Team   ifup-aliases ifup-ipv6  ifup-post  ifup-Team   init.ipv6-global
ifcfg-lo   ifdown-ippp ifdown-ppp   ifdown-TeamPort ifup-bnep   ifup-isdn  ifup-ppp   ifup-TeamPort network-functions
ifdown    ifdown-ipv6 ifdown-routes ifdown-tunnel  ifup-eth   ifup-plip  ifup-routes ifup-tunnel  network-functions-ipv6
ifdown-bnep  ifdown-isdn ifdown-sit   ifup       ifup-ippp   ifup-plusb ifup-sit   ifup-wireless

As a result, the configuration file name of ip information was changed from ifcfg-eth0 to ifcfg-enp2s0. Well, since you gave him that name, I'll use it first. Under the first cat1 ifcfg - enp2s0


[root@centos1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp2s0
HWADDR=00:E0:69:01:6A:96
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=enp2s0
UUID=5b0a7d76-1602-4e19-aee6-29f57618ca01
ONBOOT=no

As you can see from the above configuration, BOOTPROTO=dhcp, ONBOOT=no, ONBOOT=no = ONBOOT=yes, vi = ONBOOT=yes, and then restart CentOS.


[root@centos1 ~]# shutdown -r

After the restart, enter the account number and password to enter the command prompt operator and continue to view the network card information using ip addr. The results are as follows:


[root@centos1 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
    valid_lft forever preferred_lft forever
  inet6 ::1/128 scope host
    valid_lft forever preferred_lft forever
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:e0:69:01:6a:96 brd ff:ff:ff:ff:ff:ff
  inet 172.8.1.200/24 brd 172.8.1.255 scope global enp2s0
    valid_lft forever preferred_lft forever
  inet6 fe80::2e0:69ff:fe01:6a96/64 scope link
    valid_lft forever preferred_lft forever

As can be seen from the above results, the address of ip assigned by DHCP is 172.8.1.200. Although it is a test machine, we should configure this machine with a fixed ip for the convenience of remote connection in the future.

Open ifcfg-enp2s0 with vi, enter the following parameters, and comment BOOTPROTO=dhcp with #.

IPADDR0=172.8.1.211
PREFIX0=24
GATEWAY0=172.8.1.1
DNS1=172.8.1.1

The full parameters are as follows, ok, the network is connected. Continue with other functional tests tomorrow.


[root@centos1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp2s0
HWADDR=00:E0:69:01:6A:96
TYPE=Ethernet
#BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=enp2s0
UUID=5b0a7d76-1602-4e19-aee6-29f57618ca01
ONBOOT=yes
IPADDR0=172.8.1.211
PREFIX0=24
GATEWAY0=172.8.1.1
DNS1=172.8.1.1

When connected to the Internet, we can use yum install net-tools to install the net-tools component and retrieve the ifconfig command. With the Internet 1 cut are easy to do.


[root@centos1 ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
0

=======================================
linux - start and close the nic command

linux commands 1: ifup, ifdown:

To manually modify some network interface parameters in real time, ifconfig can be used. If you want to start directly with the configuration file, ifcfg-ethx in /etc/sysconfig/ network-scripts, you have to use ifdown or ifup.


[root@centos1 ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
1

ifup and ifdown are really easy. These two programs are actually script, it will go directly to /etc/ sysconfig/ network-scripts directory to search for the corresponding configuration file, such as ifup eth0, it will find the content of ifcfg-eth0, and then set it. For the setup of ifcfg-eth0, please refer to the instructions attached to Internet in the previous chapter.

However, since these two programs mainly search for Settings files (ifcfg-ethx) to start and close, make sure that ifcfg-ethx really exists in the correct directory before using it, otherwise the startup will fail. In addition, if you set or modify the network interface with ifconfig eth0, you will no longer be able to close it with ifdown eth0. Because ifdown will analyze and compare whether the current network parameters are consistent with ifcfg-eth0, if not, the operation will be abandoned. Therefore, after you have modified ifconfig, you should be able to close the interface with ifconfig eth0 down.

How to make LINUX network card start automatically

The command to activate the network card is: ifconfig eth0 up. Now that you know the command, you can make a script and put it in the startup project.

Modify the network card configuration file by editing it in the /etc/sysconfig/ network-scripts directory.

The file for the first nic is ifcfg-eth0

The file for the second nic is ifcfg-eth1

And so on, if you have 1 block, then you just type 1


[root@centos1 ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2

After opening, ONBOOT=no, ONBOOT=yes,(if not, manually add ONBOOT=yes),:wq save exit, restart the service.


echo "ONBOOT=yes" >> /etc/sysconfig/network-script/ifcfg-eth0

Add ONBOOT=yes to /etc/sysconfig/ network-scripts and set the network boot to start automatically

linux command 2: / etc init d/network restart and service network restart


[root@centos1 ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
4

Closing interface eth1: [ok]
Close the loopback interface: [ok]
Popup loopback interface: [ok]
eth1: Determining if ip address 10.0.0.168 is already in use for device eth1...
[sure]


[root@centos1 ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
5

Closing interface eth1: [ok]
Close the loopback interface: [ok]
Popup loopback interface: [ok]
eth1: Determining if ip address 10.0.0.168 is already use use for eth1...
[sure]


Related articles: