1、alternatives
1.1 概述
update-alternatives 和 alternatives 都是Linux系统中用于管理命令链接符的工具,它们允许用户在系统中存在多个软件版本时,方便地设置和切换默认使用的版本。
update-alternatives 是最初在Debian Linux下开发的项目,用于管理多版本。RHEL重写了这个项目,并将其命名为 alternatives ,在基于Fedora的分发版本中发行和传播。在某些RHEL或CentOS版本中,update-alternatives 可能作为一个软链接存在,指向alternatives命令。
由于update-alternatives和alternatives都涉及系统级别的命令链接符管理,因此通常需要root权限来执行相关命令。
1.2 安装与配置
基于RPM的系统(如Red Hat、Fedora、CentOS),alternatives功能是通过chkconfig包提供的,并且通常已经默认安装
sudo apt-get update
sudo apt-get install alternatives
1.3 语法
[appuser@localhost app]$ update-alternatives -help
alternatives(备用)版本 1.7.6 - 版权 (C) 2001 红帽公司
在 GNU 公共许可条款下,本软件可被自由地重发行。
用法:alternatives --install <链接> <名称> <路径> <优先度>
[--initscript <服务>]
[--family <family>]
[--slave <链接> <名称> <路径>]*
alternatives --remove <名称> <路径>
alternatives --auto <名称>
alternatives --config <名称>
alternatives --display <名称>
alternatives --set <名称> <路径>
alternatives --list
--install <链接> <名称> <路径> <优先度>
⇒ 安装一个新的替代项。- <链接> 是指向 /etc/alternatives 目录下符号链接的路径。
- <名称> 是替代项的通用名称。
- <路径> 是实际可执行文件的路径。
- <优先度> 是这个替代项的优先级,数字越大优先级越高。
[--initscript <服务>]
⇒ 指定一个初始化脚本,当替代项被更新时,这个脚本会被调用。[--family <family>]
⇒ 将替代项分组到一个家族中。[--slave <链接> <名称> <路径>]
⇒ 为一个主替代项添加一个或多个从属替代项。--remove <名称> <路径>
⇒ 从系统中移除一个替代项。--auto <名称>
⇒ 自动选择优先级最高的替代项。--config <名称>
⇒ 显示替代项的配置菜单,允许用户手动选择一个替代项。--display <名称>
⇒ 显示有关替代项的信息,包括所有可用的替代项及其优先级。--set <名称> <路径>
⇒ 手动选择一个特定的替代项。--list
⇒ 列出所有替代项的名称
示例
- 添加新的程序版本
sudo alternatives --install /usr/bin/python python /usr/bin/python3.8 100 sudo alternatives --install /usr/bin/python python /usr/bin/python3.9 200
- 列出所有注册到 python 名称下的替代程序及其优先级
sudo alternatives --display python
- 切换Python版本
# 列出所有可用版本,输入对应的编码来切换版本 sudo alternatives --config python # 自动切换,系统将根据程序的优先级自动选择默认版本 sudo alternatives --auto python # 手动切换指定版本 sudo alternatives --set python /usr/bin/python3.8
- 删除已注册的命令版本。
sudo alternatives --remove python /usr/bin/python3.8
2、update-alternatives
参见上述的 alternatives
3、scp
概述
scp
(Secure Copy)是 Linux 系统中一个基于 SSH(Secure Shell)协议的文件传输命令。它允许用户在不安全的网络环境中安全地传输文件。由于使用了 SSH 协议,scp
命令在传输过程中会加密数据,确保数据的安全性和完整性。
语法
zhangsan@MacBook-Pro ~ % scp
usage: scp [-346ABCOpqRrsTv] [-c cipher] [-D sftp_server_path] [-F ssh_config]
[-i identity_file] [-J destination] [-l limit] [-o ssh_option]
[-P port] [-S program] [-X sftp_option] source ... target
参数说明:
-3: 通过 SSH 连接到第三个主机,然后复制文件。这通常用于通过跳板机进行文件传输。
-4: 强制使用 IPv4 地址。
-6: 强制使用 IPv6 地址。
-B: 使用批处理模式(此选项已过时,不推荐使用)。
-C: 允许压缩数据,在传输过程中进行压缩以提高效率。
-p: 保留文件的修改时间、访问时间和权限。
-q: 静默模式,不显示传输过程中的信息。
-r: 递归复制整个目录及其内容。
-T: 禁用伪终端分配(这通常用于脚本或命令行)。
-v: 详细模式,显示传输过程中的详细信息。
-c cipher: 指定加密算法。
-F ssh_config: 指定 SSH 配置文件的位置。
-i identity_file: 使用指定的私钥文件进行身份验证。
-J destination: 跳板机选项,用于通过指定的主机连接到目标主机。
-l limit: 限制传输带宽。
-o ssh_option: 传递单个 SSH 选项。
-P port: 指定 SSH 服务的端口号(默认是 22)。
-S program: 用于连接的主机密钥验证的程序。
source ...: 这是你想要复制的文件或目录的列表。可以使用通配符(如 *)来匹配多个文件。
target: 这是你希望将文件或目录复制到的位置。这可以是一个本地路径(如果目标主机是本地),也可以是一个远程主机的路径(格式为 [user@]host:path)。
示例
-
从本地复制到远程主机:
# 将 文件 `/Users/zhangsan/Downloads/a.txt` 复制到 `/root` 下 zhangsan@MacBook-Pro ~ % scp Downloads/a.txt root@192.168.10.1:~ root@192.168.10.1's password: a.txt 100% 6850 17.9MB/s 00:00 # 将 文件夹 `/Users/zhangsan/Downloads/a` 复制到 `/app` 下 lisi@MacBook-Pro ~ % scp -r Downloads/a root@192.168.10.1:/app root@192.168.10.1's password: a.txt 100% 2 6.0KB/s 00:00 b.txt 100% 2 8.1KB/s 00:00
-
从远程主机复制到本地
# 将 文件夹 `/app/a` 复制到 `/Users/wangwu/Downloads` 下 wangwu@MacBook-Pro ~ % scp -r root@192.168.10.1:/app/a ./Downloads root@192.168.10.1's password: a.txt 100% 2 3.3KB/s 00:00 b.txt 100% 2 4.6KB/s 00:00 # 将 文件 `/root/a.txt` 复制到 `/Users/zhaoliu/Downloads` 下 zhaoliu@MacBook-Pro ~ % scp root@192.168.10.1:~/a.txt ./Downloads root@192.168.10.1's password: a.txt 100% 6850 6.9MB/s 00:00
-
指定 SSH 端口
# 如果远程主机的 SSH 端口不是默认的 22,可以使用 `-P` 选项指定端口: scp -P 2222 user@remote_host:/path/on/remote/file.txt /path/to/local/
4、curl
概述
curl 是一个非常强大且灵活的工具,支持多种协议(如 HTTP、HTTPS、FTP 等),并通过各种选项支持不同的请求方式、认证机制、代理设置、传输限制等。这些参数可以极大地提高网络请求中的效率和灵活性。
语法
[root@localhost ~]# curl --help
Usage: curl [options...] <url>
Options: (H) means HTTP/HTTPS only, (F) means FTP only
--anyauth Pick "any" authentication method (H)
-a, --append Append to target file when uploading (F/SFTP)
--basic Use HTTP Basic Authentication (H)
--cacert FILE CA certificate to verify peer against (SSL)
--capath DIR CA directory to verify peer against (SSL)
-E, --cert CERT[:PASSWD] Client certificate file and password (SSL)
--cert-type TYPE Certificate file type (DER/PEM/ENG) (SSL)
--ciphers LIST SSL ciphers to use (SSL)
--compressed Request compressed response (using deflate or gzip)
-K, --config FILE Specify which config file to read
--connect-timeout SECONDS Maximum time allowed for connection
-C, --continue-at OFFSET Resumed transfer offset
-b, --cookie STRING/FILE String or file to read cookies from (H)
-c, --cookie-jar FILE Write cookies to this file after operation (H)
--create-dirs Create necessary local directory hierarchy
--crlf Convert LF to CRLF in upload
--crlfile FILE Get a CRL list in PEM format from the given file
-d, --data DATA HTTP POST data (H)
--data-ascii DATA HTTP POST ASCII data (H)
--data-binary DATA HTTP POST binary data (H)
--data-urlencode DATA HTTP POST data url encoded (H)
--delegation STRING GSS-API delegation permission
--digest Use HTTP Digest Authentication (H)
--disable-eprt Inhibit using EPRT or LPRT (F)
--disable-epsv Inhibit using EPSV (F)
-D, --dump-header FILE Write the headers to this file
--egd-file FILE EGD socket path for random data (SSL)
--engine ENGINGE Crypto engine (SSL). "--engine list" for list
-f, --fail Fail silently (no output at all) on HTTP errors (H)
-F, --form CONTENT Specify HTTP multipart POST data (H)
--form-string STRING Specify HTTP multipart POST data (H)
--ftp-account DATA Account data string (F)
--ftp-alternative-to-user COMMAND String to replace "USER [name]" (F)
--ftp-create-dirs Create the remote dirs if not present (F)
--ftp-method [MULTICWD/NOCWD/SINGLECWD] Control CWD usage (F)
--ftp-pasv Use PASV/EPSV instead of PORT (F)
-P, --ftp-port ADR Use PORT with given address instead of PASV (F)
--ftp-skip-pasv-ip Skip the IP address for PASV (F)
--ftp-pret Send PRET before PASV (for drftpd) (F)
--ftp-ssl-ccc Send CCC after authenticating (F)
--ftp-ssl-ccc-mode ACTIVE/PASSIVE Set CCC mode (F)
--ftp-ssl-control Require SSL/TLS for ftp login, clear for transfer (F)
-G, --get Send the -d data with a HTTP GET (H)
-g, --globoff Disable URL sequences and ranges using {} and []
-H, --header LINE Custom header to pass to server (H)
-I, --head Show document info only
-h, --help This help text
--hostpubmd5 MD5 Hex encoded MD5 string of the host public key. (SSH)
-0, --http1.0 Use HTTP 1.0 (H)
--ignore-content-length Ignore the HTTP Content-Length header
-i, --include Include protocol headers in the output (H/F)
-k, --insecure Allow connections to SSL sites without certs (H)
--interface INTERFACE Specify network interface/address to use
-4, --ipv4 Resolve name to IPv4 address
-6, --ipv6 Resolve name to IPv6 address
-j, --junk-session-cookies Ignore session cookies read from file (H)
--keepalive-time SECONDS Interval between keepalive probes
--key KEY Private key file name (SSL/SSH)
--key-type TYPE Private key file type (DER/PEM/ENG) (SSL)
--krb LEVEL Enable Kerberos with specified security level (F)
--libcurl FILE Dump libcurl equivalent code of this command line
--limit-rate RATE Limit transfer speed to this rate
-l, --list-only List only names of an FTP directory (F)
--local-port RANGE Force use of these local port numbers
-L, --location Follow redirects (H)
--location-trusted like --location and send auth to other hosts (H)
-M, --manual Display the full manual
--mail-from FROM Mail from this address
--mail-rcpt TO Mail to this receiver(s)
--mail-auth AUTH Originator address of the original email
--max-filesize BYTES Maximum file size to download (H/F)
--max-redirs NUM Maximum number of redirects allowed (H)
-m, --max-time SECONDS Maximum time allowed for the transfer
--metalink Process given URLs as metalink XML file
--negotiate Use HTTP Negotiate Authentication (H)
-n, --netrc Must read .netrc for user name and password
--netrc-optional Use either .netrc or URL; overrides -n
--netrc-file FILE Set up the netrc filename to use
-N, --no-buffer Disable buffering of the output stream
--no-keepalive Disable keepalive use on the connection
--no-sessionid Disable SSL session-ID reusing (SSL)
--noproxy List of hosts which do not use proxy
--ntlm Use HTTP NTLM authentication (H)
-o, --output FILE Write output to <file> instead of stdout
--pass PASS Pass phrase for the private key (SSL/SSH)
--post301 Do not switch to GET after following a 301 redirect (H)
--post302 Do not switch to GET after following a 302 redirect (H)
--post303 Do not switch to GET after following a 303 redirect (H)
-#, --progress-bar Display transfer progress as a progress bar
--proto PROTOCOLS Enable/disable specified protocols
--proto-redir PROTOCOLS Enable/disable specified protocols on redirect
-x, --proxy [PROTOCOL://]HOST[:PORT] Use proxy on given port
--proxy-anyauth Pick "any" proxy authentication method (H)
--proxy-basic Use Basic authentication on the proxy (H)
--proxy-digest Use Digest authentication on the proxy (H)
--proxy-negotiate Use Negotiate authentication on the proxy (H)
--proxy-ntlm Use NTLM authentication on the proxy (H)
-U, --proxy-user USER[:PASSWORD] Proxy user and password
--proxy1.0 HOST[:PORT] Use HTTP/1.0 proxy on given port
-p, --proxytunnel Operate through a HTTP proxy tunnel (using CONNECT)
--pubkey KEY Public key file name (SSH)
-Q, --quote CMD Send command(s) to server before transfer (F/SFTP)
--random-file FILE File for reading random data from (SSL)
-r, --range RANGE Retrieve only the bytes within a range
--raw Do HTTP "raw", without any transfer decoding (H)
-e, --referer Referer URL (H)
-J, --remote-header-name Use the header-provided filename (H)
-O, --remote-name Write output to a file named as the remote file
--remote-name-all Use the remote file name for all URLs
-R, --remote-time Set the remote file's time on the local output
-X, --request COMMAND Specify request command to use
--resolve HOST:PORT:ADDRESS Force resolve of HOST:PORT to ADDRESS
--retry NUM Retry request NUM times if transient problems occur
--retry-delay SECONDS When retrying, wait this many seconds between each
--retry-max-time SECONDS Retry only within this period
-S, --show-error Show error. With -s, make curl show errors when they occur
-s, --silent Silent mode. Don't output anything
--socks4 HOST[:PORT] SOCKS4 proxy on given host + port
--socks4a HOST[:PORT] SOCKS4a proxy on given host + port
--socks5 HOST[:PORT] SOCKS5 proxy on given host + port
--socks5-basic Enable username/password auth for SOCKS5 proxies
--socks5-gssapi Enable GSS-API auth for SOCKS5 proxies
--socks5-hostname HOST[:PORT] SOCKS5 proxy, pass host name to proxy
--socks5-gssapi-service NAME SOCKS5 proxy service name for gssapi
--socks5-gssapi-nec Compatibility with NEC SOCKS5 server
-Y, --speed-limit RATE Stop transfers below speed-limit for 'speed-time' secs
-y, --speed-time SECONDS Time for trig speed-limit abort. Defaults to 30
--ssl Try SSL/TLS (FTP, IMAP, POP3, SMTP)
--ssl-reqd Require SSL/TLS (FTP, IMAP, POP3, SMTP)
-2, --sslv2 Use SSLv2 (SSL)
-3, --sslv3 Use SSLv3 (SSL)
--ssl-allow-beast Allow security flaw to improve interop (SSL)
--stderr FILE Where to redirect stderr. - means stdout
--tcp-nodelay Use the TCP_NODELAY option
-t, --telnet-option OPT=VAL Set telnet option
--tftp-blksize VALUE Set TFTP BLKSIZE option (must be >512)
-z, --time-cond TIME Transfer based on a time condition
-1, --tlsv1 Use => TLSv1 (SSL)
--tlsv1.0 Use TLSv1.0 (SSL)
--tlsv1.1 Use TLSv1.1 (SSL)
--tlsv1.2 Use TLSv1.2 (SSL)
--tlsv1.3 Use TLSv1.3 (SSL)
--tls-max VERSION Use TLS up to VERSION (SSL)
--trace FILE Write a debug trace to the given file
--trace-ascii FILE Like --trace but without the hex output
--trace-time Add time stamps to trace/verbose output
--tr-encoding Request compressed transfer encoding (H)
-T, --upload-file FILE Transfer FILE to destination
--url URL URL to work with
-B, --use-ascii Use ASCII/text transfer
-u, --user USER[:PASSWORD] Server user and password
--tlsuser USER TLS username
--tlspassword STRING TLS password
--tlsauthtype STRING TLS authentication type (default SRP)
--unix-socket FILE Connect through this UNIX domain socket
-A, --user-agent STRING User-Agent to send to server (H)
-v, --verbose Make the operation more talkative
-V, --version Show version number and quit
-w, --write-out FORMAT What to output after completion
--xattr Store metadata in extended file attributes
-q If used as the first parameter disables .curlrc
参数说明:
-
数据输出相关选项
-
-o [file]
:将响应内容保存到指定文件。curl -o output.html https://example.com
-
-O:将文件下载并保留其原始文件名。
curl -O https://example.com/file.zip
-
-J
:根据 Content-Disposition 头部信息保存文件名(与 -O 配合使用)。curl -OJ https://example.com/download
-
-C -
:断点续传。继续从上次中断的位置下载文件。curl -C - -O https://example.com/largefile.zip
-
-L
:跟随重定向(3xx 状态码)。curl -L https://short.url/redirect
-
-s
:静默模式,不输出错误和进度信息。curl -s https://example.com
-
-S
:与 -s 一起使用,在静默模式下显示错误信息。curl -sS https://example.com
-
-v
:详细模式,显示请求和响应的详细信息,用于调试。curl -v https://example.com
-
-i
:显示响应头和响应体。curl -i https://example.com
-
-I
:仅显示响应头。curl -I https://example.com
-
-k
:忽略 SSL 证书验证错误。curl -k https://self-signed.badssl.com/
-
-
数据发送相关选项
-
-d
[data]:使用 POST 方法发送数据。数据可以是键值对或 JSON 格式。curl -d "key1=value1&key2=value2" https://api.example.com/submit curl -d '{"name":"John","age":30}' -H "Content-Type: application/json" https://api.example.com/submit
-
-F [name=content]
:提交表单数据,可以上传文件。curl -F "file=@/path/to/file.jpg" https://api.example.com/upload
-
-X [method]
:指定 HTTP 请求方法,如 GET、POST、PUT、DELETE 等。curl -X DELETE https://api.example.com/resource/1
-
-
请求头相关选项
-
-H [header]
:自定义 HTTP 请求头信息,如 Content-Type 或 Authorization。curl -H "Authorization: Bearer token123" https://api.example.com/data
-
-A [user-agent]
:设置用户代理(User-Agent),模拟特定浏览器请求。curl -A "Mozilla/5.0" https://example.com
-
-e [referer]
:设置 Referer 请求头,用于模拟从特定页面跳转的请求。curl -e https://example.com https://other.example.com
-
--compressed
:请求压缩内容并自动解压。curl --compressed https://example.com
-
-b [cookie]
:发送带有指定 Cookie 的请求。curl -b "name=value" https://example.com
-
-c [cookie-file]
:将服务器返回的 Cookie 保存到指定文件中。curl -c cookies.txt https://example.com
-
-
认证与安全相关选项
-
-u [user:password]
:用于 HTTP 基本认证,发送用户名和密码。curl -u user:pass https://example.com/protected
-
--proxy-user [user:password]
:在使用代理服务器时,指定代理的用户名和密码。curl --proxy-user proxyuser:password -x http://proxy.example.com:8080 https://example.com
-
--key [key-file]
:使用指定的私钥文件(用于 HTTPS)。curl --key /path/to/private.key https://example.com
-
--cert [cert-file]
:使用指定的客户端证书文件。curl --cert /path/to/cert.pem https://example.com
-
--cacert [CA-cert-file]
:使用指定的 CA 证书文件验证服务器证书。curl --cacert /path/to/ca.crt https://example.com
-
--ssl-reqd
:强制使用 SSL/TLS。curl --ssl-reqd https://example.com
-
-
代理相关选项
-
-x [proxy]
:使用指定的代理服务器发送请求。curl -x http://proxy.example.com:8080 https://example.com
-
--proxy-user [user:password]
:在使用代理服务器时,指定代理的用户名和密码。curl --proxy-user proxyuser:password -x http://proxy.example.com:8080 https://example.com
-
--noproxy [no-proxy-list]
:对于指定的主机,不使用代理。curl --noproxy "example.com" https://example.com
-
-
限制与超时选项
-
--max-time [seconds]
:设置最大请求时间,超过此时间 curl 会自动终止。curl --max-time 10 https://example.com
-
--connect-timeout [seconds]
:设置最大连接时间。curl --connect-timeout 5 https://example.com
-
--limit-rate [speed]
:限制传输速度,可以用 k 或 m 表示千字节/秒或兆字节/秒。curl --limit-rate 100k https://example.com
-
-m [seconds]
:设置整个操作的超时时间,与 --max-time 类似。curl -m 20 https://example.com
-
-
多文件与并行处理选项
-
-Z
:启用并行处理多个 URL 请求。curl -Z https://example.com/page1 https://example.com/page2
-
-K [config-file]
:从指定的配置文件中读取 curl 参数。curl -K config.txt
-
-
调试与开发选项
-
--trace [file]
:记录请求和响应的详细信息到文件。curl --trace trace.log https://example.com
-
--trace-ascii [file]
:以 ASCII 格式记录 trace 信息。curl --trace-ascii trace.txt https://example.com
-
--stderr [file]
:将错误信息重定向到指定文件。curl --stderr errors.log https://example.com
-
-w [format]
:定义如何显示最终状态的格式,如 HTTP 状态码、传输时间等。curl -w "HTTP Code: %{http_code}\nTotal Time: %{time_total}\n" https://example.com
-
-
其他选项
-
-q
:禁用 .curlrc 配置文件。curl -q https://example.com
-
-h
:显示帮助信息,列出所有可用选项。curl -h
-
-V
:显示 curl 的版本信息。curl -V
-
5、top
概述
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器。
top命令可以动态显示进程的资源使用情况,即可以通过用户按键来不断刷新当前状态。
如果在前台执行该命令,它将独占前台,直到用户终止该程序为止。比较准确的说,top命令提供了实时的对系统处理器的状态监视,它将显示系统中CPU最“敏感”的任务列表,该命令可以按CPU使用、内存使用和执行时间来对任务进行排序,而且该命令的很多特性都可以通过交互式命令或者在个人定制文件中进行设定。
语法
hh@MacBook-Pro ~ % top -help
procps-ng version 3.3.10
Usage:
top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]
top 命令的基本用法:
- 无参数运行:直接输入 top 并回车,将显示当前系统中所有进程的实时资源占用情况。
- -h 或 --help:显示帮助信息(但注意,有些版本的 top 可能不支持 -help,而是使用 --help 或没有参数时直接显示帮助)。
- -v 或 --version:显示 top 命令的版本信息。
- -b:以批处理模式运行,通常用于将输出重定向到文件。
- -c:显示完整的命令行,而不是只显示命令名。
- -H:以线程模式运行,显示各个线程的资源占用情况。
- -i:忽略闲置和僵尸进程。
- -O 或 --field-separator:指定字段分隔符,用于输出格式化。
- -S:以累计模式显示CPU时间,即包括子进程的CPU时间。
- -s:以安全模式运行,限制某些交互命令。
- -d secs:设置刷新间隔,单位为秒。
- -n max:设置最大刷新次数,之后 top 将自动退出。
- -u user 或 -U user:只显示指定用户的进程。
- -p pid(s):只显示指定PID(s)的进程。
- -o field:指定输出字段的顺序。
- -w [cols]:设置输出宽度,单位为字符。
top 运行界面说明
无参运行top界面如下:
top - 19:45:25 up 3 days, 4 min, 1 user, load average: 0.00, 0.03, 0.00
Tasks: 224 total, 1 running, 105 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1494124 total, 1209276 free, 136400 used, 148448 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1275224 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
298 root 20 0 0 0 0 I 0.3 0.0 0:00.21 kworker/0:+
1689 root 20 0 112328 4000 3244 R 0.3 0.3 0:00.07 top
1 root 20 0 92584 9436 5140 S 0.0 0.6 0:00.47 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
5 root 20 0 0 0 0 I 0.0 0.0 0:00.01 kworker/0:+
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:+
7 root 20 0 0 0 0 I 0.0 0.0 0:00.00 kworker/u4+
8 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_+
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_+
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_tasks_+
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
12 root 20 0 0 0 0 I 0.0 0.0 0:00.01 rcu_sched
13 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
14 root 20 0 0 0 0 I 0.0 0.0 0:00.00 kworker/0:+
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuhp/0
第一部分是前5行,是系统整体的统计信息。第二部分是第8行开始的进程信息。
第二部分的排序默认是按CPU占用,若想按内存排序,输入 M 回车即可。
- 第一行,
top - 19:45:25 up 3 days, 4 min, 1 user, load average: 0.00, 0.03, 0.00
- top:表示这是个top命令
- 19:45:25:表示当前系统时间
- up 3 days, 4 min:表示系统运行时间
- 1user:表示当前登录用户人数
- load average: 0.00, 0.03, 0.00:表示 1分钟/5分钟/15分钟 系统负载情况,对于单核cpu 1.0 表示满负载
- 第二行,
Tasks: 224 total, 1 running, 105 sleeping, 0 stopped, 0 zombie
- tasks:表示这行表示的是进程情况
- 224 total:总任务数
- 1 running:运行进程数
- 105 sleeping:当前睡眠状态数量
- 0 stopped:停止进程数量
- 0 zombie:僵尸进程数量
- 第三行,
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
- %Cpu(s): 表示这行表示cpu状态
- 0.0 us:进程在用户地址空间中消耗CPU时间的百分比。像shell程序、各种语言的编译器、数据库应用、web服务器和各种桌面应用都算是运行在用户地址空间的进程,这些程序如果不是处于idle状态,那么绝大多数的CPU时间都是运行在用户态。
- 0.2 sy:进程在内核地址空间中消耗CPU时间的百分比。所有进程要使用的系统资源都是由Linux内核处理的。当处于用户态(用户地址空间)的进程需要使用系统的资源时,比如需要分配一些内存、或是执行IO操作、再或者是去创建一个子进程,此时就会进入内核态(内核地址空间)运行。事实上,决定进程在下一时刻是否会被运行的进程调度程序就运行在内核态。对于操作系统的设计来说,消耗在内核态的时间应该是越少越好,在实践中有一类典型的情况会使sy变大,那就是大量的IO操作,因此在调查IO相关的问题时需要着重关注它。
- 0.0 ni:用户进程空间内改变过优先级的进程占用CPU百分比。ni是nice的缩写,可以通过nice值调整进程用户态的优先级,这里显示的ni表示调整过nice值的进程消耗掉的CPU时间。如果系统中没有进程被调整过nice值,那么ni就显示为0。
- 98.8 id:空闲CPU百分比
- 0.0 wa:CPU等待磁盘IO操作的时间。和CPU的处理速度相比,磁盘IO操作是非常慢的,有很多这样的操作,比如:CPU在启动一个磁盘读写操作后,需要等待磁盘读写操作的结果。在磁盘读写操作完成前,CPU只能处于空闲状态。Linux系统在计算系统平均负载时会把CPU等待IO操作的时间也计算进去,所以在我们看到系统平均负载过高时,可以通过wa来判断系统的性能瓶颈是不是过多的IO操作造成的。
- 0.0 hi:硬中断占用百分比。
- 0.0 si:软中断占用百分比。这两个值表示系统处理中断消耗的时间。中断分为硬中断和软中断,hi表示处理硬中断消耗的时间,si表示处理软中断消耗的时间。硬中断是硬盘、网卡等硬件设备发送给CPU的中断消息,当CPU收到中断消息后需要进行适当的处理(消耗CPU时间)。软中断是由程序发出的中断,最终也会执行相应的处理程序(消耗CPU时间)。
- 0.0 st:虚拟机占用百分比
- 第四行,
KiB Mem : 1494124 total, 1209276 free, 136400 used, 148448 buff/cache
- KiB Mem:kib 表示单位 mem表示物理内存
- total:物理内存总量
- free:空闲的内存总量
- used:已使用的物理内存总量
- buff/cache: 用作内核缓存的内存量
- 第五行,
KiB Swap: 2097148 total, 2097148 free, 0 used. 1275224 avail Mem
- KiB Swap:kib 表示单位 Swap表示交换区
- total:交换区内存总量
- free:空闲
- used:已使用
- avail Mem:可用内存,这部分指标与Swap没关系
- 第六行,
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
- PID:进程ID。
- USER:进程所有者的用户名
- PR:优先级
- NI:NICE 值,取值 -20 到19 数值越小优先级越高
- VIRT:进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES。
- RES:进程使用的驻留内存
- SHR:共享内存大小,单位kb
- S:进程状态(D=不可中断的睡眠状态,R=运行,S=睡眠,T=跟踪/停止,Z=僵尸进程)
- %CPU:进程使用的CPU的百分比。
- %MEM:进程使用的物理内存百分比。
- TIME+:进程使用的CPU时间总计,单位1/100秒。
- COMMAND:执行的命令
- 第六行可能出现的其他参数:
- PPID 父进程id。
- RUSER Real user name。
- UID 进程所有者的用户id。
- GROUP 进程所有者的组名。
- TTY 启动进程的终端名。不是从终端启动的进程则显示为 ?
- P 最后使用的CPU,仅在多CPU环境下有意义
- TIME 进程使用的CPU时间总计,单位秒
- SWAP 进程使用的虚拟内存中,被换出的大小,单位kb。
- CODE 可执行代码占用的物理内存大小,单位kb
- DATA 可执行代码以外的部分(数据段+栈)占用的物理内存大小,单位kb
- nFLT 页面错误次数
- nDRT 最后一次写入到现在,被修改过的页面数。
- WCHAN 若该进程在睡眠,则显示睡眠中的系统函数名
- Flags 任务标志,参考 sched.h