[Linux] Common commands to view various information of the system (CPU, memory, process, network port, disk, hardware, etc.)

Linux is an open source Unix-like operating system, which has many different distributions, such as Ubuntu, CentOS, Debian, etc. The Linux system provides many command line tools that allow users to easily view and manage various information of the system, such as hardware configuration, memory usage, process status, network connection, etc. This article will introduce some commonly used commands, along with their usage and examples.

Use: man commandor command -hto view the full parameters of the relevant command.

Basic system information: uname

The uname command can display the basic information of the system, such as the kernel version, operating system name, host name, hardware architecture, etc. It has the following commonly used options:

  • -a: display all information
  • -s: display the kernel name
  • -r: display kernel version
  • -v: display kernel release date
  • -o: display operating system name
  • -n: display hostname
  • -m: display hardware architecture

For example, input uname -a, you can get output similar to the following:

root@CQUPTLEI:~# uname -a
Linux CQUPTLEI 5.4.0-149-generic #166-Ubuntu SMP Tue Apr 18 16:51:45 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

This means that the current system is Linux, the kernel version is 5.4.0-149-generic, the release date is April 18, 2023, the hardware architecture is x86_64, and the operating system name is GNU/Linux.

Linux distribution information: lsb_release

The lsb_release command can display the information of the Linux distribution, such as the distribution name, version number, code name, etc. It has the following commonly used options:

  • -a: display all information
  • -d: display the distribution description
  • -c: display the release code
  • -r: display the release version number

For example, input lsb_release -a, you can get output similar to the following:

root@CQUPTLEI:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:        20.04
Codename:       focal

This means that the current system is the Ubuntu distribution, the version number is 20.04.6 LTS, and the code name is focal.

LSBIt is the abbreviation of Linux Standard Base. It is a standard jointly developed by Linux infrastructure providers, application developers, and the Linux community to provide consistent software interfaces and binary compatibility to enhance interoperability between different Linux distributions.

The goal of the LSB is to define a set of core specifications and standards to ensure that applications developed and run on LSB-compliant Linux systems maintain consistent behavior across different Linux distributions. This enables application developers to more easily port their software to different Linux environments without requiring additional customization and tweaking for each distribution.

The LSB standard covers various aspects, including file system layout, shared libraries, command tools, system call interface, init scripts, package management and logging system, etc. It defines some basic commands and tools, such as lsb_release, for viewing information about a Linux distribution, and other tools for checking and verifying that a system complies with the LSB standard.

By following the LSB standard, Linux distributions can provide greater interoperability, allowing developers to more easily deliver and run applications on different Linux systems. This also provides greater flexibility for businesses and organizations to choose from different Linux distributions and ensure that their applications will run properly on those distributions.

CPU details: lscpu

lscpu is used to display detailed information about the CPU. It provides information about processor architecture, number of logical cores, endianness, CPU frequency, cache hierarchy, and supported features .

Common parameters:

  • -a, --all:
    Display all available CPU information, including default and extended information.
  1. -p, --parse:
    Parse the /proc/cpuinfo file and display processor information in a readable format.

  2. -s, --socket:
    Only display the information of the physical socket (socket), including the socket number, the number of cores and the number of threads, etc.

  3. -c, --cpu:
    Only display logical CPU information, including CPU number, core number, thread number, etc.

  4. -x, --hex:
    When displaying CPU features and flags, display in hexadecimal format.

  5. -y, --extended=KEY:
    Display extended CPU information. KEY can be one of the following: cache, cpu, flags, topology.

  6. -e, --online:
    Only display information about online CPUs, that is, running CPUs.

  7. -V, --version:
    Display the version information of the lscpu command.

use lscpu:

insert image description here

Memory usage: free

The free command can display the memory usage of the system, including physical memory, swap partition, buffer and cache, etc. It has the following commonly used options:

  • -h: Display information in human-readable format, such as KB, MB, GB, etc.
  • -m: display information in MB
  • -g: display information in GB
  • -t: display total information

For example, input free -h, you can get output similar to the following:

root@CQUPTLEI:~# free
              total        used        free      shared  buff/cache   available
Mem:        2030144      986392      108608       66064      935144      785052
Swap:       1049596      793048      256548

System real-time process status: top

The top command can display the process status of the system in real time, such as CPU usage, memory usage, running time, etc. It has the following commonly used options:

  • -u: only show the processes of the specified user
  • -p: only display the process with the specified process ID
  • -c: display the complete command line
  • -d : set the refresh interval in seconds

For example, input top, you can get output similar to the following:

insert image description here

This means that the current system has been running for 42 days, there are 2 users logged in, and the average load is 0.46, 0.39, and 0.26.

There are 249 processes, one running and 248 sleeping.

The CPU usage is 2.0% in user state, 2.2% in system state, and 95.5% idle.

The memory usage is xxxxxx (see picture). The usage of the swap partition is xxxx.

Finally, the information of each process is displayed, such as process ID, user, priority, virtual memory, physical memory, shared memory, status, CPU usage, memory usage, running time, commands, etc.

Network interface information: ifconfig

The ifconfig command can display and configure network interface information, such as IP address, subnet mask, broadcast address, MAC address, etc. It has the following commonly used options:

  • -a: Display information about all interfaces, including inactive ones
  • -s: only display summary information, no detailed information
  • : Only display the information of the specified interface

For example, input ifconfig, you can get output similar to the following:

This means that the current system has 3 network interfaces.

eth0 is an Ethernet interface, its IP address is xxx.xxx.xxx.xxx, its subnet mask is xxx.xxx.xxx.xxx, its broadcast address is xxx.xxx.xxx.xxx, and its MAC address is xx:xx: xx:xx:xx:xx.

lo is the local loopback interface, docker0 is docker.

Network connection related information: netstat

netstat can display active TCP\UDP connections, listening ports, routing tables, interface statistics, multicast members, etc.

Common parameters:

  • -a: Display all active TCP connections and listening TCP and UDP ports
  • -b: Display the name of the executable involved in each connection or listening port. In some cases, an executable may contain multiple independent components, in which case the sequence of components involved in creating a connection or listening port is shown. This option can be time consuming and requires sufficient permissions to use.
  • -e: Display Ethernet statistics such as bytes and packets sent and received. This parameter can be used with -s.
  • -n: Display addresses and port numbers in numeric form instead of trying to resolve hostnames.
  • -o: Display the process identifier (PID) of each connection. You can find the corresponding application according to the PID in the task manager. This parameter can be used with -a, -n, -p.
  • -p : Only display connections of the specified protocol. Protocol can be tcp, udp, tcpv6, or udpv6. If this parameter is used with -s to display protocol statistics, the protocol can be tcp, udp, icmp, ip, tcpv6, udpv6, icmpv6, or ipv6.
  • -r: Display the IP routing table. This is equivalent to the route print command.
  • -s: Display statistics by protocol. By default, statistics for TCP, UDP, ICMP, and IP protocols are displayed. If the IPv6 protocol is installed, it will also display statistics for TCP over IPv6, UDP over IPv6, ICMPv6, and IPv6 protocols. The -p parameter can be used to specify a set of protocols.
  • -t: Display the current connection status.
  • <interval>: Redisplay the selected information every specified number of seconds. Press CTRL+C to stop the redisplay. If this parameter is omitted, the command will only print the selected information once.

insert image description here

Wireless network interface information: iwconfig

Display and configure the information of the wireless network interface, including the name, frequency and connection status of the wireless network card.

The server is not connected to the wireless network, so the example will not be shown (too lazy to switch between dual systems).

Disk partition information: fdisk

List information about all disk partitions on the system, including disk devices, partition types, and partition sizes.

For disk-related commands, you can read my previous article. The commonly used parameters here are-l

For example, this server has 2 disks: vda and vdb, each divided into 2 areas.
insert image description here

disk usage: df

The df command can display the disk usage of the system, including total capacity, used space, free space, usage percentage, etc. It has the following commonly used options:

  • -h: Display information in human-readable format, such as KB, MB, GB, etc.
  • -m: display information in MB
  • -g: display information in GB
  • -T: display file system type
  • -a: display all filesystems, including special ones
  • < directory >: Only display the file system where the specified directory is located

For example, input df -hT, you can get output similar to the following:
insert image description here

System host name and other information: hostnamectl

View and set the hostname of the system, including information such as hostname, operating system version, architecture, and system time zone.

parameter:

  1. status:
    Display information such as the host name, operating system version, architecture, and time zone of the current system.

  2. set-hostname NAME:
    Set the hostname of the system to the specified name. Requires root privileges or use sudo to do this.

  3. set-chassis TYPE:
    Set the chassis type of the system. Available types include desktop (desktop), laptop (notebook), server (server), and vm (virtual machine), among others.

  4. set-deployment DEPLOYMENT:
    Set the deployment environment of the system. Available deployment environments include production (production environment), development (development environment), testing (test environment), and custom (custom environment).

  5. set-icon-name NAME:
    Set the icon name of the system host. The icon name usually corresponds to the host icon displayed in the graphical user interface (GUI).

  6. set-location LOCATION:
    Set the location information of the system. It can be a custom location string, such as office name or computer room number.

  7. set-timezone TIMEZONE:
    Set the time zone of the system. A valid time zone identifier needs to be specified, such as "Asia/Shanghai" or "America/New_York", etc.

  8. set-volatile BOOL:
    Sets whether the hostname is ephemeral (volatile). If set to "yes", the hostname will be reset to default after system restart.

  9. set-static-hostname NAME:
    Set the static hostname of the system. Static hostnames persist across system reboots.

  10. set-pretty NAME:
    Set a hostname for pretty purposes. Can be a friendly name to be displayed to the user in a specific context.

  11. set-transient-hostname NAME:
    Set the system's temporary hostname. The temporary hostname resets to the default after a system restart.

example:hostnamectl status

insert image description here

PCI device information: lspci

lspci is a command used to display information about all PCI buses in the system and all devices connected to them. By default it shows a brief list of devices.

Here: PCI means Peripheral Component Interconnect, which is a personal computer bus used to connect various peripheral devices on the motherboard, such as graphics cards, sound cards, network cards, etc.

You can request more verbose output or output suitable for parsing by other programs using some of the common parameters below:

  • -m: display output in a backward compatible machine-readable format
  • -mm: Display output in a machine-readable format for easy script parsing
  • -t: Display the output in the form of a tree diagram, including all buses, bridges, devices and connections between them
  • -v: Display verbose output, including device class, vendor, subsystem, IRQ, etc.
  • -vv: Display more detailed output, including capability list, PCI configuration space, etc.
  • -vvv: show the most verbose output, including all parseable information, even if it doesn't look very interesting (e.g. undefined memory regions)
  • -k: Display kernel drivers and modules for each device
  • -x: Display the standard part of the PCI configuration space in hexadecimal format (first 64 bytes or first 128 bytes for CardBus bridges)
  • -xxx: Display the entire PCI configuration space (256 bytes) in hexadecimal format. This option is only available as root, because some PCI devices will error out when you try to read certain parts of the configuration space (this behavior may not violate the PCI standard, but at least it is stupid). Such devices are rare, though, so you don't have to worry too much.
  • -xxxx: Displays the extended (4096 bytes) PCI configuration space in hexadecimal format, which is available on the PCI-X 2.0 and PCI Express buses.
  • -b: Display all IRQ numbers and addresses in a bus-centric view, not as seen by the kernel.
  • -D: Always display the PCI domain number. By default, on machines with only domain 0, lspci suppresses them.
  • -P: Identify PCI devices by their path to each bridge, rather than by bus number.
  • -PP: Identify PCI devices by the path of each bridge, and display the bus number and device number at the same time.
  • -n: Display PCI vendor and device codes numerically instead of looking them up in a list of PCI IDs.
  • -nn: Display PCI vendor and device codes as numbers and names.
  • -q: If a device is not found in the local pci.ids file, use DNS to query the central PCI ID database and save the results in the local cache. If the DNS query succeeds, subsequent runs will recognize the result even if this option is not given. Use this option sparingly only in automation scripts to avoid overloading the database server.
  • -qq: Whether or not a device is found in the local pci.ids file, use DNS to query the central PCI ID database and reset the local cache.
  • -Q: Use DNS to query the central PCI ID database even if a device is found in the local pci.ids file. Use this option if you suspect that the displayed entry is wrong.

insert image description here

USB device information: lsusb

Lists information about all USB devices connected to the system, including device ID, manufacturer, and device speed, etc.

Common parameters:

  • -v: Display detailed information, including device class, vendor, subsystem, configuration descriptor, etc.
  • -t: Display the output in the form of a tree diagram, including all buses, devices and connections between them
  • -s [ [ bus]: ] [ devnum]: Show only devices with the specified bus and/or device number. Both numbers are decimal and can be omitted.
  • -d [vendor]: [product]: Only display devices with the specified vendor and product ID. Both IDs are in hexadecimal.
  • -D device: Do not scan the /dev/bus/usb directory, but only display information about the given device file. The device file should look something like /dev/bus/usb/001/001. This option displays verbose information, similar to the -v option; you must be root to use this option.
  • -V: Print version information and exit successfully.

insert image description here

System hardware details: dmidecode

Displays detailed information about system hardware such as motherboard, BIOS, memory, processor, etc.

dmidecode is a command for parsing the contents of a system's DMI (also known as SMBIOS) tables and displaying them in a human-readable format. The DMI table contains descriptions of system hardware components, as well as some other useful information, such as serial numbers and BIOS versions. You can control the output with some common parameters:

  • -d, --dev-mem FILE: read memory from the specified device file (default is /dev/mem)
  • -h, --help: display help and exit
  • -q, --quiet: show more terse output, do not show unknown, inactive and OEM-specific entries
  • -s, --string KEYWORD: Only display the value of the DMI string corresponding to the specified keyword. The keyword must be one of the following list: bios-vendor, bios-version, bios-release-date, system-manufacturer, system-product-name, system-version , system-serial-number, system-uuid, baseboard- manufacturer, baseboard-product-name, baseboard-version, baseboard-serial-number, baseboard-asset-tag, chassis-manufacturer, chassis-type, chassis-version, chassis-serial-number, chassis-asset-tag, processor- family, processor-manufacturer, processor-version, processor-frequency.
  • -t, --type TYPE: Only display DMI entries of the specified type. Type can be a number, or a comma-separated list of numbers, or a range of numbers, such as 0-4. Type can also be one of the following keywords: bios, system, baseboard, chassis, processor, memory, cache, connector, slot.
  • -u: Display unparsed entry content, in hexadecimal format.
  • -V: Print version information and exit successfully.

insert image description here

View through environment variables

In addition to commands, you can also print environment variables to view some system configurations, such as terminal type, shell type, etc.:

  • PATH: defines the list of directories that the system searches for executable files when executing commands, separated by colons. If a command does not contain a complete path, the system will search for the existence of the command in turn according to this list.
    insert image description here

  • HOME: Defines the home directory of the current user, which is the directory entered by default when logging in. This directory usually contains the user's configuration files, data files, etc.
    insert image description here

  • USER: Defines the username of the current user, which is the username entered when logging in.
    insert image description here

  • SHELL: defines the shell program used by the current user, that is, the interactive command interpreter started when logging in. Common shell programs include bash, zsh, csh, etc.
    insert image description here

  • LANG: Defines the language and character encoding used by the current user, that is, the language rules and character sets used when displaying and inputting text. This variable affects the language and locale supported by the system, such as date format, currency symbol, collation, etc.
    insert image description here

  • PS1: defines the prompt of the current shell, that is, the string displayed in front of the command line each time a command is entered. This variable can contain some special escape sequences, such as \urepresenting the user name, \hrepresenting the host name, \wrepresenting the current working directory, etc.
    insert image description here

  • TERM: defines the type of the current terminal, that is, the terminal emulator or console used when displaying and entering text. This variable affects the terminal features supported by the system, such as colors, cursors, keymaps, etc.
    insert image description here

  • EDITOR: Defines the text editor used by the current user, that is, the program that starts by default when editing a text file. Common text editors include vim, nano, emacs, etc.

  • LD_LIBRARY_PATH: Defines the list of directories that the system searches for dynamic link libraries when executing programs, separated by colons. If a program depends on some dynamic link libraries, the system will check whether the libraries exist in order according to this list.

  • TMPDIR: defines the directory where the system stores temporary files at runtime. If this variable is not defined, the system will use the default temporary file directory, usually /tmp or /var/tmp.


In addition to the commonly used commands above, there are many other commands, as well as third-party tools. For example top, the upgraded version htop.

insert image description here


Write love you forever into the end of the poem ~

Guess you like

Origin blog.csdn.net/weixin_43764974/article/details/131645637