Linux network advanced: view and test network in linux system

1. View network configuration

1. View network interface information------ifconfig

(1) View active network interface devices

ifconfig

Insert picture description here

(2) View the specified network interface device

ifconfig 网卡名称  #可查看所有存在的网络接口,包括未激活的

Insert picture description here
Supplement:
MTU is the abbreviation of Maximum Transmission Unit, which translates to Maximum Transmission Unit. The maximum value that can be set is 1500 bytes.
When the MTU value of a packet or frame sent locally> the MTU value set on the network, unpacking is required, which will result in a decrease in efficiency.
When the MTU value of a packet or frame sent locally <the MTU value set on the network, the maximum transmission is not played ability
Insert picture description here

2. View the routing table------route

route [-n]       #-n是将地址显示为数字形式 

Insert picture description here

3. Check the network connection ------netstat

  • View the system's network connection status, routing table, interface statistics and other information
netstat [选项]
Common options effect
-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.
-t View TCP-related information.
-u Display information related to UDP protocol.
-p Display the process number and process name information associated with the network connection (this option requires root privileges)
-r Display routing table information.
-l Display the network connection and port information in the monitoring state.

Commonly used to view TCP and UDP protocol information, combination options

netstat -anpt    #以数字的形式显示当前系统中所有TCP信息
netstat -anpu    #以数字的形式显示当前系统中所有UDP信息

Insert picture description here
Insert picture description here
Netstat can also use the grep command in combination with the pipe character to filter out specific records.
For example: query the specified port number
Insert picture description here

4. Get socket statistics information------ss

  • Check the network connection of the system and obtain socket statistics.
  • The content displayed is similar to netstat, but ss can display more and more detailed information about TCP and connection status, and is faster and more efficient than netstat.
ss   [选项]  
Common options effect
-t Display sockets of TCP protocol
-u Display UDP protocol sockets
-n Do not resolve the name of the service, such as "22" port will not be displayed as "ssh"
-l Only show ports in listening state
-p Show the process of listening port
-a Show all ports and connections
-r Interpret IP as domain name and port number as protocol name

This command is not used much, just to understand.
Insert picture description here

2. Test the network connection

1. Test network connectivity------ping

ping [选项] 目标主机

The usage here is similar to the network part, that is, the default long ping in the linu system requires the Ctrl+C key combination to stop.
Insert picture description here

2. Trace the routing path of data packets ------traceroute

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

Insert picture description here

3. Test DNS domain name resolution ------nslookup

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

Insert picture description here

Three, set the network address parameters

In the Linux system, there are two basic methods:

  • Temporary configuration---------Use commands to adjust network parameters
    • Take effect immediately after modification
    • 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 changes 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

1. Use commands to modify (temporary configuration)

(1) Modify the address and status of the network card ------ifconfig

  • Set the IP address and subnet mask of the network interface
ifconfig 网络接口 ip地址 [ netmask 子网掩码 ]
ifconfig 网络接口 ip地址 [/子网掩码长度]      #常用

Insert picture description here

  • Disable or reactivate the network card
ifconfig 网络接口 up 
ifconfig 网络接口 down

Insert picture description here

  • Setting a virtual network interface is
    generally used to temporarily use a new IP address on a network card in use, but it cannot overwrite the original IP address and some services cannot be used. At this time, you can use this command to define a virtual network card tied to the original Some network cards.
ifconfig   网络接口:序号   IP地址

Insert picture description here

(2) Add and delete static route records------route

  • Add and delete routing records to the specified network segment
route add -net 网段地址 gw IP地址
route del -net 网段地址

Insert picture description here
Insert picture description here

  • Adding and deleting default gateway records
    The route of the same host is best to have only one default route. If there are more than one, it may affect the network.
route   add   default    gw  IP地址
route  del   default  gw   IP地址

Insert picture description here

2. Modify the configuration file (fixed setting)

(1) Network interface configuration file

The network interface configuration files are all under the /etc/sysconfig/network-scripts/ directory. To
Insert picture description here
permanently modify the network interface information, you need to modify the network interface configuration file

vim   /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet                #设置网卡类型,“Ethernet”表示以太网
DEVICE=ens33                        #设置网卡的名称
ONBOOT=yes                             #设置网卡是否在 Linux 操作系统启动时激活
BOOTPROTO=static                  #设置网卡的配置方式,“static”表示使用静态IP地址,“dhcp”时表示动态获取地址
IPADDR=192.168.163.25         #设置网卡的 IP 地址
NETMASK=255.255.255.0        #设置网卡的子网掩码
GATEWAY=192.168.163.2          #设置网卡的默认网关地址
DNS1=192.168.163.2              #设置DNS服务器的 IP地址

Insert picture description here

(2) Routing configuration file

Permanently add a route (restart the network service to take effect)
Method 1:

vim /etc/sysconfig/static-routes      #编辑/etc/sysconfig/static-routes 文件,没有自动建立
any net any gw 192.168.163.2               #任何网段的网关都是193.168.163.2
any net 192.168.3.0/24 gw 192.168.163.100    #3.0/24网段的网关是193.168.163.100
any net 10.0.0.0 netmask 255.0.0.0 gw 192.168.163.2   #10.0.0.0/8网段的网关是193.168.163.2
any host 192.168.100.100  gw 192.168.163.2  #主机192.168.100.100的网关是193.168.163.2

systemctl  restart  network     #重启network服务,让刚才的配置生效

Insert picture description here
Insert picture description here

Method Two:

vim  /etc/sysconfig/network-scripts/route-ens33
default via 192.168.163.2  dev ens33    #默认路由,另一种格式   0.0.0.0/0  192.168.14.254  dev ens33
10.0.6.0/24  via  192.168.163.2 dev ens33
192.168.100.200 via 192.168.14.254 dev ens33

systemctl restart network

Insert picture description here
Insert picture description here

(3) Domain name resolution configuration file

vim  /etc/resolv.conf 

Save the IP address of the DNS server that the machine needs to use
Insert picture description here

Note: For CentOS 7, for example, to prevent the content of the /etc/resolv.conf file from being modified or overwritten by the system, you need to set dns=none in the main section of the /etc/NetworkManager/NetworkManager.conf file and restart the NetworkManager service, or use CentOs 7 Newly added nmcli command for setting
Insert picture description here

(4) Local host mapping file

  • /etc/hosts file

    • Save the mapping record of host name and IP address
      Insert picture description here
  • 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

supplement:

I’ve said it before, but I need to use it here.

Enable and disable network interface configuration

systemctl restart network		#重启所有的网卡
ifdown ens33 ; ifup ens33
ifdown ens33					#关闭某个网卡
ifup ens33						#启动某个网卡

ifconfig ens33 down				#临时禁用某个网卡
ifconfig ens33 up				#重新激活某个网卡(不会更新IP地址)

Permanently set the hostname

hostnamectl   set-hostname    [主机名]
vim  /etc/hostname      #只有第一行有效
#设置完后需重启系统才生效

Guess you like

Origin blog.csdn.net/weixin_51326240/article/details/110751739