Detail the Linux host network access configuration

  • 2020-06-15 11:01:45
  • OfStack

Detail Linux host network access configuration

Preface:

Network configuration is the first step we need to take after installing the operating system. In the modern era, a host without access to the Internet is already equivalent to a pile of scrap metal. During network configuration, we usually need to configure the native IP address, default gateway, DNS, host name, and so on. This article describes how to connect a host to the network in the Linux environment, mainly in the traditional command line way. New commands for network configuration such as ip, nmcli, and so on are described in a future article.

1. Overview of network configuration

Host access to the Internet prerequisite: TCP/IP protocol stack

Configure the host to access TCP/IP network

  IP/Netmask
The & # 8195; The & # 8195; Routing:
The & # 8195; The & # 8195; The & # 8195; The & # 8195; Host routing: 1 host
The & # 8195; The & # 8195; The & # 8195; The & # 8195; Network routing: is 1 network
The & # 8195; The & # 8195; The & # 8195; The & # 8195; Default route (default gateway) :
The & # 8195; The & # 8195; DNS server:
The & # 8195; The & # 8195; The & # 8195; The & # 8195; Main DNS server
The & # 8195; The & # 8195; The & # 8195; The & # 8195; Standby DNS server
The & # 8195; The & # 8195; Host name: computer_ES37en.domain.com

Network Device Representation (CentOS7 version)

The & # 8195; The & # 8195; Kernel identifies the device: the driver

The & # 8195; The & # 8195; Network card naming mechanism

The & # 8195; The & # 8195; The & # 8195; The & # 8195; In CentOS 7, systemd named network devices
The & # 8195; The & # 8195; The & # 8195; The & # 8195; (a) If the index information provided by Firmware or BIOS for integrated devices on the motherboard is available and predictable, name it according to the index, such as eno1
The & # 8195; The & # 8195; The & # 8195; The & # 8195; If the index information provided by Firmware or BIOS for the ES62en-ES63en extension slot is available and predictable, name it according to the index, such as ens1
The & # 8195; The & # 8195; The & # 8195; The & # 8195; (c) If physical location information for the hardware interface is available, name it based on that information, such as enp2s0
The & # 8195; The & # 8195; The & # 8195; The & # 8195; (d) if the user explicitly start, also can be named according to MAC address, enx2387a1dc56;
The & # 8195; The & # 8195; The & # 8195; The & # 8195; (e) When none of the above is available, the traditional naming mechanism is used, which is supported in biosdevname based on BIOS
The & # 8195; The & # 8195; The & # 8195; The & # 8195; (f) Local loop lo
The & # 8195; The & # 8195; The & # 8195; The & # 8195; Built-in network card: em1,em2
The & # 8195; The & # 8195; The & # 8195; The & # 8195; pci card: pYpX Y: slot,X:port

The & # 8195; The & # 8195; Name composition format

The & # 8195; The & # 8195; The & # 8195; The & # 8195; en: Ethernet wired LAN
The & # 8195; The & # 8195; The & # 8195; The & # 8195; wl: wlan wireless LAN
The & # 8195; The & # 8195; The & # 8195; The & # 8195; ww: wwan wireless Wan

The & # 8195; The & # 8195; Name Type:

The & # 8195; The & # 8195; The & # 8195; The & # 8195; o: Device index number of the integrated device
The & # 8195; The & # 8195; The & # 8195; The & # 8195; s: Index number of the expansion slot
The & # 8195; The & # 8195; The & # 8195; The & # 8195; x: Naming based on the MAC address
    ps: enp2s1

2. IP address configuration

Dynamic configuration (IP/Netmask) :

The & # 8195; The & # 8195; DHCP Server (Dynamic Host Configuration Protocol)

Static specification:

The & # 8195; The & # 8195; User space tool (ifconfig, ip command), effective immediately, but not for long (address configured in kernel, displayed in user space)
The & # 8195; The & # 8195; Network equipment service configuration file to complete long-term effective configuration:
    /etc/sysconfig/network-script/ifcfg-eth0
The & # 8195; The & # 8195; GUI/TUI configuration tool

Network configuration command

ifconfig(Traditional configuration command)
The & # 8195; The & # 8195; ifconfig: Displays information about all active status
The & # 8195; The & # 8195; ifconfig Interface: Displays only information about the specified interface
  ifconfig Interface Address
    ip/mask
The & # 8195; The & # 8195; The & # 8195; The & # 8195; The & # 8195; The & # 8195; Long format:
      ifconfig Interface IP netmask MASK
The & # 8195; The & # 8195; The & # 8195; The & # 8195; CIDR: Predictive routing without category
    ifconfig Interface IP/MASK

ifconfig sample


# more /etc/redhat-release ### The current environment 
CentOS Linux release 7.2.1511 (Core) 
# ifconfig |grep eno -A1 ### The current server has 2 Block network CARDS, respectively eno16777728,eno33554960
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.24.8.131 netmask 255.255.255.0 broadcast 172.24.8.255
--
eno33554960: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.81.144 netmask 255.255.255.0 broadcast 192.168.81.255

# ifconfig eno33554960 192.168.81.145/24
# ifconfig eno33554960|head -2
eno33554960: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.81.145 netmask 255.255.255.0 broadcast 192.168.81.255  

# ifconfig eno33554960 down ### Disable the current network card 
# ifconfig eno33554960 up  ### Activate the current network card 
# ifconfig eno33554960|head -2 ### Check the network card again because it is used DHCP . IP The address is assigned to the original address 
eno33554960: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.81.144 netmask 255.255.255.0 broadcast 192.168.81.255

 Example of network card manual configuration 
# vi /etc/sysconfig/network-scripts/ifcfg-eno16777728 
TYPE="Ethernet"   // Specifies the network type as Ethernet mode 
BOOTPROTO="dhcp" // Specifies how to get the boot address protocol (dhcp or bootp For automatic acquisition ,none
DEFROUTE="yes"  // Whether to put this eno16777728 Set as the default route  
PEERDNS="yes"   // Whether to allow DHCP Updates directly when the service allocates the address /etc/resolv.conf In the DNS Server address 
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no" // if IPv4 Configuration failed , Whether the device is disabled 
IPV6INIT="yes"   // Allows startup on this network card IPV6 The function of the  
IPV6_AUTOCONF="yes" // Whether to use IPV6 Automatic configuration of addresses 
IPV6_DEFROUTE="yes"  ### Author : Leshami 
IPV6_PEERDNS="yes"   ### Blog : http://blog.csdn.net/leshami 
IPV6_PEERROUTES="yes" ### QQ/Weixin : 645746311
IPV6_FAILURE_FATAL="no"
NAME="eno16777728"  // Network connection identification name  
UUID="52ff246a-d965-4056-b34f-16e8f4df2c0a" // Network card worldwide only 1 Identification code 
DEVICE="eno16777728"      // Network connection identification name 
ONBOOT="yes"        // The network card will start automatically after power on 
USERCTL=no         // Whether ordinary users are allowed to operate the network card 
PEERDNS={yes|no}  // Whether to allow DHCP Updates directly when the service allocates the address /etc/resolv.conf In the DNS Server address 
# systemctl restart network.service  // Restart network service , Enable configuration 

3. Routing configuration


route( Traditional configuration command )
route : 
 route : Displays routing information ( Gateway means down 1 Jump to the address below 1 The hop address must be in the same location as the local host 1 network )
  -n : Displays in numeric format, does not reverse address to hostname 
 route add // Add the routing 
  -host HOST_IP gw Next [dev Device]
  -net Net_ADDR
  -net 0.0.0.0  The destination address is any address, that is, the default route 

  route del // Delete the routing 
   -host HOST_IP gw  Under the 1 jump 
   -net Net_ADDR gw  Under the 1 jump 

 Example: 
# route 
# route
Kernel IP routing table
Destination Gateway  Genmask  Flags Metric Ref Use Iface
default  192.168.81.2 0.0.0.0  UG 100 0  0 eno33554960
172.24.8.0  0.0.0.0  255.255.255.0 U 100 0  0 eno16777728
192.168.81.0 0.0.0.0  255.255.255.0 U 100 0  0 eno33554960

 In the example, for the table is 192.168.81.0 As well as 172.24.8.0 Access to the network segment, Gateway for 4 a 0 Means that there is no network, that is, a direct connection network, no routing is required 
 For the target is default the ( When using route -n When, default for 4 a 0) Network segments through the default gateway 192.168.81.2 The connection target points to a single 
 The host is the host route, the target network address is the network route, the target points to all addresses (0.0.0.0 or default) Is the default route.   

route Specific description of output 
route The routing table fields output by the command have the following meanings: 
 Destination  The target 
   The destination network or destination host.  Target network or target host. 
 Gateway  The gateway 
   The gateway address or '*' if none set.  Gateway address. If not, asterisk is displayed. 
 Genmask  Network mask 
   The netmask for the destination net; '255.255.255.255' for a
   host destination and '0.0.0.0' for the default route.
 Flags : There are a number of flags, which have the following meanings:       
  U (route is up) : The road is active;       
  H (target is a host) : the target is 1 The host  (IP)  Rather than domain;       
  G (use gateway) : Need to pass through external host  (gateway)  To transmit packets;       
  R (reinstate route for dynamic routing) : Recovering the flag of routing information when using dynamic routing;       
  D (dynamically installed by daemon or redirect) : has been served or transferred  port  The function is set to dynamic routing       
  M (modified from routing daemon or redirect) : The route has been modified;       
  ! (reject route) : This route will not be accepted ( Used to block insecure domains! )
  A (installed by addrconf)
  C (cache entry)
 Metric  Distance, hops. It useless. 
   The 'distance' to the target (usually counted in hops). It is
   not used by recent kernels, but may be needed by routing dae-
   mons.
 Ref  You don't have to worry about it 0 . 
   Number of references to this route. (Not used in the Linux ker-nel.)
 Use  The number of times this route is used can be a rough estimate of network traffic to a given network address. 
   Count of lookups for the route. Depending on the use of -F and
 Iface  From which 1 Five network interfaces are routed, as in this case eno33554960
   Interface to which packets for this route will be sent.

 Add network routing 
  The current native IP Address is 172.24.8.131
  Suppose you want to reach the destination address :192.168.10.0/24 Under the, 1 Jump for the  172.24.8.2  Add the routing command as follows 
 # route add -net 192.168.10.0/24 gw 172.24.8.2 ### At this point is the network gateway ( routing )

 # route -n
 Kernel IP routing table
 Destination Gateway  Genmask  Flags Metric Ref Use Iface
 0.0.0.0  192.168.81.2 0.0.0.0  UG 100 0  0 eno33554960
 172.24.8.0  0.0.0.0  255.255.255.0 U 100 0  0 eno16777728
 192.168.10.0 172.24.8.2  255.255.255.0 UG 0  0  0 eno16777728 
 192.168.81.0 0.0.0.0  255.255.255.0 U 100 0  0 eno33554960

  So you can see that there's a lot of that 1 The destination network is 192.168.10.0
  The newly added routing interface device is used automatically eno16777728 Network card, because 172.24.8.2 with eno16777728 IP For the same 1 Within the network segment 

 Add host routing 
  Assume that the destination address to be reached is: 192.168.20.1 Under the, 1 Jump for the : 172.16.100.177 
 # route add -host 192.168.20.1 gw 172.16.100.177
 SIOCADDRT: Network is unreachable # Indicates that the network is not reachable because 172.16.100.177 Not in the current two network CARDS 1 Within the network segment 

  So let's modify that 1 Jump address is 172.24.8.254 Try adding again 
  # route add -host 192.168.20.1 gw 172.24.8.254 ### There is no need to specify a hidden code here 
  # route -n ### In the list below Flags There are 1 a H , indicates that this is the host route 
  Kernel IP routing table
  Destination Gateway  Genmask  Flags Metric Ref Use Iface
  0.0.0.0  192.168.81.2 0.0.0.0  UG 100 0  0 eno33554960
  172.24.8.0  0.0.0.0  255.255.255.0 U 100 0  0 eno16777728
  192.168.10.0 172.24.8.2  255.255.255.0 UG 0  0  0 eno16777728
  192.168.20.1 172.24.8.254 255.255.255.255 UGH 0  0  0 eno16777728
  192.168.81.0 0.0.0.0  255.255.255.0 U 100 0  0 eno33554960

  Routing table description 
   For requests to 192.168.10.0 And the routing address is 172.24.8.2 , via network card eno16777728
   For requests to 192.168.20.1 Host, routing address is 172.24.8.254 , via network card eno16777728
   For the request 172.24.8.0 As well as 192.168.81.0 Is a directly connected network, no routing is required 
   For requests to 0.0.0.0 Of the Internet via 192.168.81.2 From the network card eno33554960 Carry on route, if visit Baidu then this route 

 # ping www.baidu.com -c 2
 PING www.baidu.com (14.215.177.38) 56(84) bytes of data.
 64 bytes from 14.215.177.38: icmp_seq=1 ttl=128 time=5.76 ms
 64 bytes from 14.215.177.38: icmp_seq=2 ttl=128 time=6.13 ms

 Delete the routing 
  Suppose you want to remove the default route 
  # route del -net 0.0.0.0 netmask 0.0.0.0 ### Is equivalent to  route del default
  # route |grep -i default

  # ping www.baidu.com ### After the default route is removed, ping Baidu unreachable 
  connect: Network is unreachable    

  Remove the previously added network and host routes     
  # route del -net 192.168.10.0/24
  # route del -host 192.168.20.1
  # route -n
  Kernel IP routing table
  Destination Gateway  Genmask  Flags Metric Ref Use Iface
  172.24.8.0  0.0.0.0  255.255.255.0 U 100 0  0 eno16777728
  192.168.81.0 0.0.0.0  255.255.255.0 U 100 0  0 eno33554960

 Add a default gateway 
 # route add default gw 192.168.81.2
 # route add -net 0.0.0.0 gw 192.168.81.2 ### And on the 1 Bar command equivalence 

 Routing static configuration file 
 /etc/sysconfig/network
 /etc/sysconfig/network-scripts/route-ethX | route-pppX

  The format of the configuration file 1 Each row: 1 A path entry to be configured 
   The target  via  Under the 1 jump  
   Example: 
  192.168.0.0/24 via 172.16.100.177

  The format of the configuration file 2 : each 1 group 1 10 routing entries 
  ADDRESS#= The target 
  NETMASK#= mask 
  GATEWAY#= Under the 1 jump 

  ADDRESS0=192.168.0.0
  NETMASK0=255.255.255.0
  GATEWAY0=172.16.100.1

4. DNS configuration


DNS Server points to: 
  Configuration file: /etc/resolv.conf
   most 3 A: 
    nameserver DNS_SERVER_IP
    nameserver 8.8.8.8

  test DNS parsing 
  dig -t A  The domain name  // Positive solution test 
  dig -x IP  // The test solution 

 View current host DNS Parsing configuration 
 # more /etc/resolv.conf 
 # Generated by NetworkManager
 search localdomain example.com
 nameserver 192.168.81.2
 nameserver 172.24.8.1

 # dig -t A www.oracle.com

 ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7 <<>> -t A www.oracle.com
 ;; global options: +cmd
 ;; Got answer:
 ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11680
 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

 ;; QUESTION SECTION:
 ;www.oracle.com.      IN  A

 ;; ANSWER SECTION:
 www.oracle.com.  5  IN  CNAME ds-www.oracle.com.edgekey.net.
 ds-www.oracle.com.edgekey.net. 5 IN CNAME e11582.dscx.akamaiedge.net.
 e11582.dscx.akamaiedge.net. 5 IN  A  184.28.37.127

 ;; Query time: 12 msec
 ;; SERVER: 192.168.81.2#53(192.168.81.2) // Through local IP 192.168.81.2 Submit the external DNS Server resolution 
 ;; WHEN: Mon Sep 04 21:14:31 CST 2017
 ;; MSG SIZE rcvd: 128

 # dig -x 184.28.37.127

 ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7 <<>> -x 184.28.37.127
 ;; global options: +cmd
 ;; Got answer:
 ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20357
 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 8, ADDITIONAL: 9

 ;; OPT PSEUDOSECTION:
 ; EDNS: version: 0, flags:; MBZ: 0005 , udp: 512
 ;; QUESTION SECTION:
 ;127.37.28.184.in-addr.arpa. IN  PTR

 ;; ANSWER SECTION:
 127.37.28.184.in-addr.arpa. 5 IN  PTR a184-28-37-127.deploy.static.akamaitechnologies.com.

 ;; AUTHORITY SECTION:
 28.184.in-addr.arpa. 5  IN  NS  ns5.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  IN  NS  ns1.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  IN  NS  ns7.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  IN  NS  ns3.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  IN  NS  ns4.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  IN  NS  ns8.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  IN  NS  ns2.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  IN  NS  ns6.reverse.deploy.akamaitechnologies.com.

 ;; ADDITIONAL SECTION:
 ns1.reverse.deploy.akamaitechnologies.com. 5 IN A 96.7.50.32
 ns2.reverse.deploy.akamaitechnologies.com. 5 IN A 2.16.40.32
 ns3.reverse.deploy.akamaitechnologies.com. 5 IN A 23.74.25.32
 ns4.reverse.deploy.akamaitechnologies.com. 5 IN A 95.100.168.32
 ns5.reverse.deploy.akamaitechnologies.com. 5 IN A 95.100.173.32
 ns6.reverse.deploy.akamaitechnologies.com. 5 IN A 23.61.199.32
 ns7.reverse.deploy.akamaitechnologies.com. 5 IN A 184.26.161.32
 ns8.reverse.deploy.akamaitechnologies.com. 5 IN A 2.22.230.32

 ;; Query time: 205 msec
 ;; SERVER: 192.168.81.2#53(192.168.81.2)
 ;; WHEN: Mon Sep 04 21:17:05 CST 2017
 ;; MSG SIZE rcvd: 407

Network service startup script


 CentOS 5/6 : 
   Service script: /etc/init.d/network

 CentOS 6
   Service script: /etc/init.d/NetworkManager

   Service script: SysV style LSB
    The control runs in the background daemon and accepts parameters {start|stop|restart|status}
    Script location: /etc/rc.d/init.d or /etc/init.d

   Configure a service to start automatically: chkconfig Src_Script_Name on
   Do not start the machine automatically: chkconfig Src_Script_Name off
   To view  chkconfig --list

   Invocation mechanism: 
   1./etc/init.d/Script_Name {start|stop|restart|status}
   1.service Script_Name {start|stop|restart|status}

  CentOS 7
    Service script: /etc/init.d/network
   # systemctl start network.service

 How to configure multiple addresses on a single network card ( Usually used for VIP)
  Implemented through network interface aliases 
 eth0X
 eth0 : X eth0:0 eth0:1....

  configuration 1 : 
 ifconfig ethX:Y IP/mask
  configuration 2 : 
 ifcfg-ethX:Y
  DEVICE=ethX:Y
  BOOTPROTO={none|static}
  IPADDR=
  NETMASK=
  GATEWAY=
  The primary address ( Alias address ) Does not support DHCP Agreement for 

5. Host name configuration


 Short hostname, such as mycomputer1
 Long host name (FQDN) , such as mycomputer.example.com

CentOS 5/6 Configure host name 
  View host name:  
  # hostname 
  # more /etc/sysconfig/network
  # more /proc/sys/kernel/hostname

  Temporary modification of host name: 
  # hostname new_host_name 
  # hostname new_host_name.domain.com

  Permanently change the host name ( Need to restart ) : 
  # vi /etc/sysconfig/network 

  Amendments and effective immediately 
  # echo new_host_name >/proc/sys/kernel/hostname 
  # sysctl kernel.hostname=new_host_name     

   The sample 
   # more /etc/redhat-release 
   CentOS release 6.7 (Final)
   # hostname
   node133
   # hostname host133
   # hostname
   host133

   [root@host133 ~]# echo host133.example.com >/proc/sys/kernel/hostname
   [root@host133 ~]# hostname
   host133.example.com
   [root@host133 ~]# sysctl kernel.hostname=host133.example.com
   kernel.hostname = host133.example.com
   [root@host133 ~]# hostname
   host133.example.com

CentOS 7  Configure host name   
  in RHEL7 In the , Introducing static (static) And the transient (transient) And flexible (pretty)3 Type host name. 
  "Static" hostname -- also known as the kernel hostname , Is the system at boot from /etc/hostname Automatically initializes the host name. 

  Transient hostname -- a hostname that is temporarily assigned while the system is running , For example, , through DHCP or DNS Server allocation. 
   Both static and transient host names follow the same character-limiting rules as Internet domain names. 

  "Flexible" hostname - is allowed to use free form ( May include special / White space characters ) The host name , To show to the end user ( Such as Tom ' s Computer)

  The command to view the hostname 1 The general format is as follows :

  hostnamectl [status] [--static|--transient|--pretty] 

   The options are as follows: 
  status - Static, transient and flexible can be viewed simultaneously 3 The host name and its associated Settings information. 
  --static View static only ( permanent ) The host name. 
  --transient -- View transients only ( temporary ) The host name. 
  --pretty  View only flexible host names. 

  [root@centos7 ~]# hostnamectl status
   Static hostname: centos7.example.com
     Icon name: computer-vm
     Chassis: vm
    Machine ID: 8ef737feff1942798ec060bf5a162df6
     Boot ID: e2c8eded07f04e879d2b160d9a6bc6cf
   Virtualization: vmware
   Operating System: CentOS Linux 7 (Core)
    CPE OS Name: cpe:/o:centos:centos:7
     Kernel: Linux 3.10.0-327.el7.x86_64
    Architecture: x86-64

   A command to modify the hostname 1 The general format is as follows 
  # hostnamectl [--static|--transient|--pretty] set-hostname <new_hostname>

  [root@centos7 ~]# hostnamectl --transient
  centos7.example.com

  # hostnamectl --transient set-hostname centos7-a // Modify the transient host name 
  # hostnamectl       // View the modified transient host name 
   Static hostname: centos7.example.com
  Transient hostname: centos7-a

  # hostnamectl --static  // View the static host name before modification 
  centos7.example.com   
  # hostnamectl --static set-hostname centos7-a.example.com // Modify the static host name 
  # hostnamectl --static        // View the modified transient host name 
  centos7-a.example.com

  Hostnamectl Once the hostname is set, the kernel hostname is refreshed and updated immediately /etc/hostname File, 
     But the current Bash The prompt needs to login again shell

  [root@centos7 ~]# bash     // reopened shell
  #

  /etc/hosts The hostname in the file will not be updated automatically, the hostname and should be updated manually IP The mapping of addresses 

   vim /etc/hosts
  127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
  ::1  localhost localhost.localdomain localhost6 localhost6.localdomain6
  192.168.81.142 centos7-a.example.com centos7-a // Add the trip , Among them 192.168.81.142 Is this machine IP address 
  :wq    // Save the exit  

If you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, hope to help you, thank you for your support to this site!


Related articles: