Theory + experiment: Linux network settings

1. View network configuration

1.1, view the network interface information ifconfig

■ View all active network interface information

  • Execute ifconfig command

■ View specified network interface information

ifconfig  网络接口
ifconfig Just look at the activated network interface
ifconfig -a View unactivated and activated (view all)
ifconfig -s View in digital mode

Insert picture description here

Insert picture description here

1.2, view the host name from hostname

■ hostname command

  • View or set the current hostname
hostname[主机名]

■ Example

[root@master ~]# hostname
master

1.3, view the routing table entry route

■ route command

  • View or set the routing table information in the host
route [-n]

■ Example

-n : 将路由记录中的地址信息显示为数字形式
[root@master ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         20.0.0.2        0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33

1.4, check the network connection netstat

■ View the system's network connection status, routing table, interface statistics and other information

netstat[选项]

■ Common options

  • -a、-n、-p、-t、-u、-r

  • -a: Display all active network connection information in the host (including monitoring and non-monitoring service ports).

  • -n: Display related host address, port and other information in digital form.

  • -r: Display routing table information.

  • -l: Display the network connection and port information in the listening state.

  • -t: View TCP (Transmission Control Protocol, Transmission Control Protocol) related information.

  • -u: Display UDP (User Datagram Protocol, user datagram protocol) protocol related information.

  • -p: Display the process number and process name information associated with the network connection (this option requires root privileges).

        Usually use the "-anpt" combination option to display all the TCP connection information in the current system in digital form, and display the corresponding process information at the same time. Using the "grep" command in combination with the pipeline command, you can also filter out the specific records you need in the results.

1.5, get socket statistics information ss

■ ss command

  • View the network connection of the system and obtain socket statistics
ss[选项]

■ Common options
-t, -u, -n, -l, -p, -a, -r

        In addition to netstat, the ss command can also view the network connection. It is the abbreviation of Socket Statistics, which is mainly used to obtain socket statistics. It can display output similar to the netsta command. But the advantage of ss is that it can display more and more detailed information about TCP and connection status, and it is faster and more efficient than netstat. To use the ss command, first make sure that the iproute package has been installed, you can install it through yum.

  • -V: --version displays the version number of the software.
  • -t: --tcp displays sockets of TCP protocol.
  • -u: --udp displays the sockets of the UDP protocol.
  • -n: --numeric does not resolve the name of the service, such as "22" port will not be displayed as "ssh".
  • -:–Listening only displays ports that are in the listening state.
  • -p: --processes displays the processes listening on the port.
  • -a: --all For TCP protocol, it includes both the listening port and the established connection.
  • -r: –resolve interprets IP as domain name and port number as protocol name.

2. Test the network connection

2.1, test network connection ping

  • ping command to
    test network connectivity
ping [选项] 目标主机

■ Example

ping 192.168
按ctrl+c终止测试

2.2, trace data packet traceroute

■ traceroute command

  • Test the network nodes that pass from the current host to the destination host
traceroute 目标主机地址

■ Example

tarceroute 192.168.7.7

2.3, domain name resolution nslookup

■ nslookup command

  • Test DNS resolution
nslookup 目标主机地址 [DNS服务器地址]

Three, use network configuration commands

3.1. Ways to set network parameters

■ Temporary configuration-use commands to adjust network parameters

  • Simple and fast, can directly modify the network parameters in operation
  • Generally only suitable for use in the process of debugging the network
  • After the system restarts, the modifications made will be invalid.
    ■ Fixed settings-modify network parameters through configuration files
  • Modify configuration files of various network parameters
  • Suitable for use when setting fixed parameters for the server
  • Need to reload the network service or restart to take effect

3.2, set the network interface parameter ifconfig

■ Set the IP address and subnet mask of the network interface

ifconfig网络接口ip地址[netmask子网掩码]
ifconfig网络接口ip地址/子网掩码长度]

■ Disable or reactivate the network card

ifconfig 网络接口up    
ifconfig 网络接口down

■ Set up virtual network interface

ifconfig 网络接口:序号  IP地址
  • Set the network interface ip address and subnet mask-classed and non-classed:
    Insert picture description here
  • Disable or reactivate the network card:
    Insert picture description here

3.3, set routing record route (temporary)

■ Route records added to the specified network segment

route add -net 网段地址 gw IP地址

■ Delete routing records to the specified network segment

route del -net网段地址

■ Add the default gateway record to the routing table

route add default gw IP地址

■ Delete the default gateway record in the routing table

route del default gw IP地址

1. Edit and modify network card information
Insert picture description here

2. Change to static mode, comment out or delete the gateway
Insert picture description here

3. Restart the network card
Insert picture description here

4. Check if there is any route, and it will disappear after commenting it out
Insert picture description here

5. Add routing records for specified network segments
Insert picture description here

6. Delete the routing record of the specified network segment
Insert picture description here

3.4, modify the hostname hostname (temporary)

■ hostname command

  • Set hostname
hostname 主机名称

Fourth, modify the network configuration file

4.1, /etc/sysconfig/network-scripts/ directory

■ ifcfg-ens33: configuration file of the Ethernet card

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
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=ens33
UUID=a5016f4f-20c9-46ad-910b-4e6a5d7f88ca
DEVICE=ens33
ONBOOT=yes
IPADDR=20.0.0.23
NETMASK=255.255.255.0
GATEWAY=20.0.0.2
DNS1=8.8.8.8
DNS2=114.114.114.114

Supplementary knowledge points: nmcli connection to view network card UUID information

Insert picture description here

4.2, enable and disable network interface configuration

■ Restart network service

systemctl restart network

■ Disable and enable network interface

ifdown ens33
ifup ens33

4.3, modify the hostname hostname

  • hostname command
    set hostname
hostname 主机名称

4.3.1, host name configuration file, permanent change hostname host name

■ Modify the host name through the /etc/hostname file

vi /etc/hostname
zk
 wq 保存退出
init 6 ## 重启后生效

4.3.2, temporarily change hostname hostname

hostname set-hostname 主机名
su  ## 刷新一下

4.5. Domain name resolution configuration file

■ /etc/resolv.conf file

  • Save the IP address of the DNS server that the machine needs to use
vi /etc/resolv.conf
# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 114.114.114.114

4.6, local host mapping file

■ /etc/hosts file

  • Save the mapping record of host name and IP address
[root@tx ~]# cat  /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

■ Comparison of hosts file and DNS server

  • By default, the system first searches for the analysis record from the hosts file
  • The hosts file is only valid for the current host
  • The hosts file can reduce the DNS query process, thereby speeding up access

4.6.1, local host mapping file

1. Who check your ip is
Insert picture description here

2. Enter the etc/hosts file

Insert picture description here

3. Add a local host mapping relationship
Insert picture description here

4. You can ping the host name directly

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44733021/article/details/109190943