Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of Linux host network access configuration

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Detailed explanation of Linux host network access configuration

Foreword:

Network configuration is the first step that we need to solve after we have installed the operating system. Nowadays, hosts without access to the network are already equivalent to a pile of scrap iron. In the process of network configuration, we usually need to configure the native IP address, default gateway, DNS, hostname, and so on. This paper mainly describes how to connect the host to the network in the traditional command line under the Linux environment. New commands for network configuration, such as ip,nmcli, etc., will be described in future articles.

I. Overview of network configuration

Prerequisite for host access to the Internet: follow TCP/IP protocol stack

What needs to be configured to configure host access to the TCP/IP network:

   IP/Netmask

   routing:

     host routing: one host

     network routing: is a network

     default route (default gateway):

   DNS server:

     primary DNS server

     alternate DNS server

   hostname: computer_name.domain.com

Network device representation (CentOS7 version)

   kernel recognition device: driver

Naming mechanism of    network card

How systemd names network devices in      CentOS 7

     (a) if the index information provided by Firmware or BIOS for devices integrated on the motherboard is available and predictable, it is named after this index, such as eno1

     (b) if the index information provided by Firmware or BIOS for the PCI-E expansion slot is available and predictable, it is named after this index, such as ens1

     (c) if the physical location information of the hardware interface is available, it is named based on this information, such as enp2s0

     (d) if the user starts it explicitly, it can also be named after the MAC address, enx2387a1dc56

When      (e) is not available, the traditional naming mechanism is used to support biosdevname based on BIOS.

     (f) Local loopback lo

     built-in network card: em1,em2

     PCI card: pYpX Y:slot, X:port

   name composition format

     en: Ethernet wired local area network

     wl: wlan Wireless Local area Network

     ww: wwan Wireless wide area Network

   name type:

     o: the device index number of the integrated device

     s: the index number of the expansion slot

     x: naming based on MAC addresses

     ps: enp2s1

II. IP address configuration

Dynamic configuration (IP/Netmask):

   DHCP Server (Dynamic Host Configuration Protocol)

Static assignment:

   user space tool (ifconfig,ip command), effective immediately, but not for long (address configured in kernel, displayed in user space)

   network device service profile to complete long-term valid configuration:

     / etc/sysconfig/network-script/ifcfg-eth0

   GUI/TUI configuration tool

Network configuration command

Ifconfig (traditional configuration command)

   ifconfig: displays information about all activity statu

   ifconfig Interface: displays only information about the specified interface

   ifconfig Interface Address

     ip/mask

  long format:

  ifconfig Interface IP netmask MASK

     CIDR: classless foresight rout

     ifconfig Interface IP/MASK

Ifconfig example

# more / etc/redhat-release # current environment CentOS Linux release 7.2.1511 (Core) # ifconfig | grep eno-A1 # the server currently has 2 network cards They are eno16777728 Eno33554960eno16777728: flags=4163 mtu 1500inet 172.24.8.131 netmask 255.255.255.0 broadcast 172.24.8.255--eno33554960: flags=4163 mtu 1500inet 192.168.81.144 netmask 255.255.255.0 broadcast 192.168.81.25 "ifconfig eno33554960 192.168.81.145 head ifconfig eno33554960 | head-2eno33554960: flags=4163 mtu 1500inet 192.168.81.145 netmask 255.255.255.255.0 broadcast 192.168.81.25" 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 Due to the use of DHCP The IP address is assigned as the original address eno33554960: flags=4163 mtu 1500inet 192.168.81.144 netmask 255.255.255.0 broadcast 192.168.81.255 Network Card manual configuration example # vi / etc/sysconfig/network-scripts/ifcfg-eno16777728 TYPE= "Ethernet" / / specify the network type as Ethernet mode BOOTPROTO= "dhcp" / / specify the access method of the startup address protocol (dhcp or bootp is automatic acquisition NoneDEFROUTE= "yes" / / whether to set this eno16777728 as the default route PEERDNS= "yes" / / whether to allow the DHCP service to directly update the DNS server address in / etc/resolv.conf when assigning addresses / PEERROUTES= "yes" IPV4_FAILURE_FATAL= "no" / / if IPv4 configuration fails Whether the device is disabled IPV6INIT= "yes" / / allow the function of IPV6 to be launched on this network card IPV6_AUTOCONF= "yes" / / whether to use the automatic configuration of IPV6 address IPV6_DEFROUTE= "yes" # Author: Leshami IPV6_PEERDNS= "yes" # Blog: http://blog.csdn.net/leshami IPV6_PEERROUTES= "yes" # QQ/Weixin: 645746311 IPv6 FAILUREFATALL = "no" NAME= "eno16777728" / / Network connection label Identify the name UUID= "52ff246a-d965-4056-b34f-16e8f4df2c0a" / / the worldwide unique identification code of the network card DEVICE= "eno16777728" / / the network connection identification name ONBOOT= "yes" / / automatically start USERCTL=no / / whether to allow ordinary users to operate the network card PEERDNS= {yes | no} / / whether to directly update the DNS server address in / etc/resolv.conf when the network card is assigned an address. # Systemctl restart network.service / / restart the network service Make the configuration effective

III. Routing configuration

Route (traditional configuration command) route: route: displays routing information (the gateway refers to the address of the next hop, which must be on the same network as the local host)-n: display in numeric format, do not reverse the address to the host name route add / / add route-host HOST_IP gw Next [dev Device]-net Net_ADDR-net 0.0.0.0 destination address is any address That is, default route route del / / delete route-host HOST_IP gw next hop-net Net_ADDR gw next hop example: # route # routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Ifacedefault 192.168.81.2 0.0.0.0 UG 10000 eno33554960172.24.8.0 0.0.0.0 255.255.255.0 U 10000 eno16777728192.168.81.0 0.0.0.0 255.255.255.0 U 1000 Access to network segments with destination tables of 192.168.81.0 and 172.24.8.0 in the 0 eno33554960 example A Gateway of 4 zeros means that there is no network, that is, there is no need for routing. For a network segment with a destination of default (when route-n is used, default is 4 zeros), the destination pointing to a single host through the default gateway 192.168.81.2 is the host route, the destination network address is the network route, and the destination pointing to all addresses (0.0.0.0 or default) is the default route. Route output specific description route command output routing table field meaning is as follows: Destination destination The destination network or destination host. Target network or target host. Gateway gateway The gateway address or'*'if none set. Gateway address, if not, an 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 total of multiple flags, which represent the following meanings: U (route is up): the route is initiated; H (target is a host): the target is a host (IP) rather than a domain; G (use gateway): packets need to be forwarded through an external host (gateway); R (reinstate route for dynamic routing): flag to recover routing information when dynamic routing is used D (dynamically installed by daemon or redirect): the service or port function has been set to dynamic routing M (modified from routing daemon or redirect): the route has been modified;! (reject route): this route will not be accepted (to fend off insecure domains!) A (installed by addrconf) C (cache entry) Metric distance, hop count. It's useless for now. The 'distance' to the target (usually counted in hops). It is not used by recent kernels, but may be needed by routing dae- mons. Don't worry about Ref, it is always 0. Number of references to this route. (Not used in the Linux ker-nel.) Use the number of times this route is used, you can roughly estimate the network traffic to a specified network address. Count of lookups for the route. Which network interface does Depending on the use of-F and Iface route out, such as eno33554960 Interface to which packets for this route will be sent. Add a network route the current native IP address is 172.24.8.131 assume that the destination address is 192.168.10.0 Universe 24 If the next hop is 172.24.8.2, add the routing command as follows: # route add-net 192.168.10.0and24 gw 172.24.8.2 # in this case, it is the network gateway (route) # 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 UG 10000 eno33554960 172.24.8.0 0.0.0.0 255.255.255. 0 U 1000 eno16777728 192.168.10.0 172.24.8.2 255.255.255.0 UG 00 0 eno16777728 192.168.81.0 0.0.0.0 255.255.255.0 U 10000 eno33554960 you can see the above extra route record The destination network is 192.168.10.0 the newly added routing interface device automatically uses the eno16777728 network card, because 172.24.8.2 and eno16777728 IP add a host route to the same network segment assuming that the destination address is 192.168.20.1, and the next hop is: 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 unreachable Because 172.16.100.177 does not change the next hop address to 172.24.8.254 in either segment of the current two network cards, try to add # route add-host 192.168.20.1 gw 172.24.8.254 # there is no need to specify the hidden code # route-n # # in the list below Flags has an 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 UG 1000 eno33554960 172.24.8.0 0.0.0 255.255.255.0 U 10000 eno16777728 192.168.10.0 172.8.2 255.255.0 UG 00 0 eno16777728 192.168.20.1 172.24.8.254 255.255.255. 255 UGH 00 eno16777728 192.168.81.0 0.0.0.0 255.255.255.0 U 1000 eno33554960 routing table describes the network that requests to 192.168.10.0 The routing address is 172.24.8.2. For the host requesting to 192.168.20.1 via the network card eno16777728, the routing address is 172.24.8.254. For the network requesting 172.24.8.8.0 and 192.168.81.0 via the network card eno16777728, it is a directly connected network, and there is no need to route the network requesting to 0.0.0.0 from the network card eno33554960 via 192.168.81.2. If you visit Baidu, the route is # 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 route suppose you need to delete the default route # route del-net 0.0.0.0 netmask 0.0.0.0 # equivalent to route del default # route | grep-I default # ping www.baidu.com # after the default route is deleted Ping Baidu unreachable connect: Network is unreachable removes network and host routes added before # route del-net 192.168.10.0 route del 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 10000 eno16777728 192.168.81.0 0.0.0.0 255.255 .255.0 U 10000 eno33554960 add default gateway # route add default gw 192.168.81.2 # route add-net 0.0.0.0 gw 192.168.81.2 # equivalent routing static configuration file / etc/sysconfig/network / etc/sysconfig/network-scripts/route-ethX with the previous command | format of route-pppX configuration file 1: one path entry per line to configure destination via next hop Example: 192.168.0.0 NETMASK#= 24 via 172.16.100.177 configuration file format 2: one route entry per group ADDRESS#= destination NETMASK#= mask GATEWAY#= next hop ADDRESS0=192.168.0.0 NETMASK0=255.255.255.0 GATEWAY0=172.16.100.1

IV. DNS configuration

DNS server points to: configuration file: / etc/resolv.conf up to three: nameserver DNS_SERVER_IP nameserver 8.8.8.8 test DNS resolution dig-t A domain name / / forward solution test dig-x IP / / inverse solution test View current host DNS resolution 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: -> > HEADERHEADER/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.comCentOS 7 configuration hostnames in RHEL7, static (static), transient (transient) and flexible (pretty) hostnames are introduced. The "static" hostname, also known as the kernel hostname, is the hostname that the system initializes automatically from / etc/hostname at startup. "transient" hostname-is a hostname that is temporarily assigned while the system is running, for example, through a DHCP or DNS server. Both static and transient hostnames follow the same character restrictions as Internet domain names. "flexible" hostname-A command that allows the use of free-form hostnames (which can include special / white space characters) to show to end users (such as Tom's Computer) to view hostnames. The general format of the command is as follows: hostnamectl [status] [--static |-- transient |-- pretty] option: status-- can view static, transient and flexible hostnames and their related setting information at the same time. -- static-- only views static (permanent) hostnames. -- transient-- only looks at transient (temporary) hostnames. -- pretty-- View only flexible hostnames. [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 the command to modify the hostname is in the following format: # hostnamectl [--static |-- Transient |-- pretty] set-hostname [root@centos7 ~] # hostnamectl-- transient centos7.example.com # hostnamectl-- transient set-hostname centos7-a / / modify transient hostname # hostnamectl / / View the modified transient hostname Static hostname: centos7.example.com Transient hostname: centos7-a # hostnamectl-- static / / View the static hostname centos7.example.com # hostnamectl-- static set-hostname centos7-a. Example.com / / modify static hostname # hostnamectl-- static / / View the modified transient hostname centos7-a.example.com Hostnamectl after setting the hostname The kernel hostname will be refreshed immediately and the / etc/hostname file will be updated, but the current Bash prompt needs to log in to shell [root@centos7 ~] # bash / / reopen the shell # / etc/hosts file and the hostname will not be updated automatically You should manually update the mapping relationship between hostname and IP address 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 this line, where 192.168.81.142 is the native IP address: wq / / save exit

If you have any questions, please leave a message or go to the community to exchange and discuss, thank you for reading, hope to help you, thank you for your support!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report