Linux常用命令速查

Journalctl

  1. 查询指定系统单元服务的日志
    journalctl -u network
    journalctl -u sshd
    
  2. 查询内核日志
    journalctl -k
    
  3. 查询指定日志级别的日志
    journalctl -p err
    journalctl -p 0..3
    journalctl -p warning..emerg
    
    # 0: emerg
    # 1: alert
    # 2: crit
    # 3: err
    # 4: warning
    # 5: notice
    # 6: info
    # 7: debug
    
  4. 查询指定时间范围内的日志
    journalctl --since "2023-01-01 00:00:00" --until "2023-01-02 23:59:59"
    journalctl --since yesterday
    
  5. 实时跟踪日志
    journalctl -f
    
  6. 跳到日志末尾
    journalctl -e
    
  7. 日志倒序显示
    journalctl -r
    
  8. 指定日志条目数量
    journalctl -n 20
    
  9. 格式化输出日志
    journalctl -o json-pretty
    

Ausearch

  1. 查询指定审计事件键值(key)的审计记录
    ausearch -k my_key
    
  2. 查询指定用户名相关的审计记录
    ausearch -ua root
    ausearch -ui 0
    
  3. 查询指定开始时间的审计记录
    ausearch -te 2025-02-02 12:00:00
    ausearch -te yesterday
    
  4. 查询指定结束时间的审计记录
    ausearch -te 2025-02-02 12:00:00
    ausearch -te today
    
  5. 查询指定审计 ID(audit ID)的审计记录
    ausearch -a 12345
    
  6. 查询指定进程 ID(PID/PPID)相关的审计记录
    ausearch -p 1000
    ausearch -pp 1
    
  7. 查询指定系统调用相关的审计记录
    ausearch -sc openat
    
  8. 将结果解释为人类可读(将审计事件中的数字代码转换为可读的文本)
    ausearch -i
    
  9. 查询指定命令相关的审计记录
    ausearch -i -c ls
    ausearch -i -c iptables
    
  10. 查询指定消息类型的审计记录
    ausearch -i -m PROCTITLE -ts today
    

Ethtool

  1. 查看和设置指定网络接口(eth0)的标准信息,如速度、双工模式等
    ethtool eth0  
    ethtool -r eth0  # 重新自协商  
    ethtool -s eth0 speed 10000 duplex full
    
  2. 查看和设置接口的RX/TX环的参数
    ethtool -S eth0 | grep fifo  
    ethtool -g eth0  
    ethtool -G eth0 rx 2048 tx 2048
    
  3. 查看和设置流控
    ethtool -S eth0 | grep control  
    ethtool -a eth0  
    ethtool -A eth0 autoneg off  # 自协商关闭  
    ethtool -A eth0 tx off  # 发送模块关闭  
    ethtool -A eth0 rx off  # 接收模块关闭
    
  4. 显示或设置接口的协议卸载和其他功能,如TCP/IP校验和卸载、数据包分段和重组
    ethtool -k eth0  
    ethtool -K eth0 tso off  # TCP分段  
    ethtool -K eth0 gso off  # 通用分段  
    ethtool -K eth0 gro off  # 通用重组
    
  5. 显示指定网络接口的驱动信息
    ethtool -i eth0
    
  6. 显示网络接口的统计信息
    ethtool -S eth0
    
  7. 查看和设置网卡RSS队列
    ethtool -x eth0  
    ethtool -X eth0 xxxx
    
  8. 查看和设置网卡中断自适应功能
    ethtool -c eth0  
    ethtool -C eth0 adaptive-rx on adaptive-tx on
    
  9. 查看和设置网卡队列
    ethtool -l eth0
    ethtool -L eth0 rx 64 tx 64
    
  10. 显示时间戳功能
    ethtool -T eth0
    

NetworkManager管理

  1. 网络控制

    nmcli networking
    nmcli networking connectivity
    nmcli networking on
    nmcli networking off
    
  2. 连接管理

    nmcli connection show
    nmcli connection show --active
    nmcli connection show eth0
    nmcli connection up eth0
    nmcli connection down eth0
    nmcli connection reload eth0
    nmcli connection delete eth0
    
    nmcli connection add con-name eth0 ifname eth0 type ethernet
    nmcli connection add con-name eth0 ifname eth0 type ethernet ipv4.method auto
    nmcli connection add con-name eth0 ifname eth0 type ethernet ipv4.method manual ipv4.addresses 192.168.200.128/24 ipv4.gateway 192.168.200.2 ipv4.dns 192.168.200.2
    
    nmcli connection modify eth0 ipv4.method auto
    nmcli connection modify eth0 ipv4.addresses 192.168.200.129/24 ipv4.gateway 192.168.200.1
    nmcli connection modify eth0 +ipv4.addresses 192.168.200.130/24
    nmcli connection modify eth0 +ipv4.routes "192.168.1.0/24 10.0.0.1,192.168.2.0/24 10.0.0.2"
    nmcli connection modify eth0 +ipv4.dns 114.114.114.114
    
  3. 设备管理

    nmcli device status
    nmcli device show eth0
    nmcli device connect eth0
    nmcli device disconnect eth0
    nmcli device reapply eth0
    
  4. 设置物理网卡参数

    # 示例
    nmcli connection modify eth0 ethtool.coalesce-adaptive-tx on
    nmcli connection modify eth0 ethtool.coalesce-adaptive-rx on
    nmcli connection modify eth0 ethtool.coalesce-tx-usecs 100
    nmcli connection modify eth0 ethtool.coalesce-rx-usecs 100
    nmcli connection modify eth0 ethtool.feature-gro on
    nmcli connection modify eth0 ethtool.feature-gso on
    nmcli connection modify eth0 ethtool.feature-tso on
    nmcli connection modify eth0 ethtool.ring-rx 4096
    nmcli connection modify eth0 ethtool.ring-tx 4096
    
    # 生成的配置文件内容
    ETHTOOL_OPTS="-K eth0 gro on gso on tso on ; -C eth0 rx-usecs 100 tx-usecs 100 ; -G eth0 rx 4096 tx 4096"
    

    4.1 ethtool.coalesce 相关参数

    ethtool.coalesce-adaptive-rx
    
    • 用于控制网卡接收中断合并的自适应功能。当启用时,网卡可以根据网络流量情况自动调整接收中断合并的参数,以优化性能和降低 CPU 使用率。
    ethtool.coalesce-adaptive-tx
    
    • 与接收类似,但针对发送中断合并的自适应功能,根据发送流量自动调整发送中断合并的参数,有助于提高网络传输效率和 CPU 资源利用。
    ethtool.coalesce-pkt-rate-high
    
    • 设定在高数据包速率情况下中断合并的一个阈值,高于该速率时可能会触发特定的中断合并策略调整。
    ethtool.coalesce-pkt-rate-low
    
    • 设定在低数据包速率情况下中断合并的一个阈值,低于该速率时可能会触发不同的中断合并策略调整。
    ethtool.coalesce-rx-frames
    
    • 配置接收中断合并时的最大帧数,当接收帧数达到此值时,将触发一个中断,以减少频繁中断对 CPU 的影响。
    ethtool.coalesce-rx-frames-high
    
    • 类似于 ethtool.coalesce-rx-frames,但适用于较高的接收负载场景,可能会更激进地合并中断以减轻 CPU 负担。
    ethtool.coalesce-rx-frames-irq
    
    • 与接收中断相关,可能是指定每个中断处理的接收帧数,以优化中断处理的性能。
    ethtool.coalescal-rx-frames-low
    
    • 用于较低接收负载下的接收中断合并的帧数阈值,避免在低负载时过度合并中断影响性能。
    ethtool.coalesce-rx-usecs
    
    • 接收中断合并的时间间隔,以微秒为单位,即经过该时间间隔后触发一个接收中断,即使未达到 ethtool.coalesce-rx-frames 的帧数要求。
    ethtool.coalesce-rx-usecs-high
    
    • 在高接收负载下的接收中断合并时间间隔,可根据负载情况动态调整中断触发的时间间隔。
    ethtool.coalesce-rx-usecs-irq
    
    • 可能是与接收中断相关的微秒级时间间隔,用于调整接收中断的触发时间。
    ethtool.coalesce-rx-usecs-low
    
    • 低接收负载下的接收中断合并的微秒级时间间隔,确保低负载时中断处理的合理性。
    ethtool.coalesce-sample-interval
    
    • 中断合并策略调整的采样时间间隔,用于评估网络流量情况,以便根据实际流量调整中断合并的参数。
    ethtool.coalesce-stats-block-usecs
    
    • 统计块更新的时间间隔,以微秒为单位,用于记录中断合并的统计信息。
    ethtool.coalesce-tx-frames
    
    • 发送中断合并的最大帧数,达到此帧数时触发一个发送中断,以优化发送中断处理。
    ethtool.coalesce-tx-frames-high
    
    • 高发送负载下的发送中断合并的最大帧数,适应高负载情况的发送中断优化。
    ethtool.coalesce-tx-frames-irq
    
    • 可能是每个发送中断处理的帧数,确保发送中断处理的性能和效率。
    ethtool.coalesce-tx-frames-low
    
    • 低发送负载下的发送中断合并的最大帧数,避免在低负载时不必要的中断合并。
    ethtool.coalesce-tx-usecs
    
    • 发送中断合并的时间间隔,以微秒为单位,在达到该时间间隔后触发发送中断。
    ethtool.coalesce-tx-usecs-high
    
    • 高发送负载下的发送中断合并的时间间隔,根据负载调整发送中断的触发时间。
    ethtool.coalesce-tx-usecs-irq
    
    • 与发送中断相关的微秒级时间间隔,可用于精细调整发送中断触发。
    ethtool.coalesce-tx-usecs-low
    
    • 低发送负载下的发送中断合并的时间间隔,以优化低负载时的发送中断处理。

    4.2 ethtool.feature 相关参数

    ethtool.feature-esp-hw-offload
    
    • 表示是否启用硬件加速 ESP(Encapsulating Security Payload)处理,提高网络加密的性能。
    ethtool.feature-esp-tx-csum-hw-offload
    
    • 启用硬件发送 ESP 数据包的校验和计算,减少 CPU 负担。
    ethtool.feature-fcoe-mtu
    
    • 支持 FCoE(Fibre Channel over Ethernet)的最大传输单元(MTU)相关功能,可能与存储网络在以太网上的传输相关。
    ethtool.feature-gro
    
    • 通用接收卸载(Generic Receive Offload),通过将多个小数据包合并成一个大的数据包来减少 CPU 处理的开销。
    ethtool.feature-gso
    
    • 通用分段卸载(Generic Segmentation Offload),将大的数据包分成较小的片段,以提高网络传输性能。
    ethtool.feature-highdma
    
    • 可能涉及高级直接内存访问(DMA)功能,以提高数据传输的效率和性能。
    ethtool.feature-hw-tc-offload
    
    • 硬件流量控制卸载,利用硬件实现部分流量控制功能,减少 CPU 负载。
    ethtool.feature-l2-fwd-offload
    
    • 二层转发卸载,将部分二层网络转发任务交给硬件处理,提高转发性能。
    ethtool.feature-loopback
    
    • 网卡的环回功能,可用于测试和诊断网络接口。
    ethtool.feature-lro
    
    • 大接收卸载(Large Receive Offload),将多个小的接收数据包合并为一个大的数据包,提高接收性能。
    ethtool.feature-ntuple
    
    • 支持元组过滤,允许更复杂的网络流量过滤规则。
    ethtool.feature-rx
    
    • 接收功能的总开关,启用或禁用接收相关的功能。
    ethtool.feature-rx-all
    
    • 可能是启用所有接收相关的高级功能。
    ethtool.feature-rx-fcs
    
    • 接收时处理帧校验序列(FCS),确保数据完整性。
    ethtool.feature-rx-gro-hw
    
    • 硬件层面的通用接收卸载,提高接收性能。
    ethtool.feature-rxhash
    
    • 接收哈希功能,可用于负载均衡和流量分类。
    ethtool.feature-rx-udp_tunnel-port-offload
    
    • 对 UDP 隧道端口的接收卸载功能,提高 UDP 隧道的性能。
    ethtool.feature-rxvlan
    
    • 接收 VLAN 帧的功能,确保能够处理带有 VLAN 标签的帧。
    ethtool.feature-rx-vlan-filter
    
    • 对接收的 VLAN 帧进行过滤,根据 VLAN ID 进行过滤和处理。
    ethtool.feature-rx-vlan-stag-filter
    
    • 对接收的双层 VLAN 帧(QinQ)进行过滤和处理。
    ethtool.feature-rx-vlan-stag-hw-parse
    
    • 硬件解析接收的双层 VLAN 帧,提高性能。
    ethtool.feature-sg
    
    • 散列 / 聚集(Scatter/Gather)功能,可用于更高效的数据传输。
    ethtool.feature-tls-hw-record
    
    • 硬件记录 TLS 信息,可能用于加速 TLS 加密的处理。
    ethtool.feature-tls-hw-tx-offload
    
    • 硬件 TLS 发送卸载,提高 TLS 发送的性能。
    ethtool.feature-tso
    
    • TCP 分段卸载(TCP Segmentation Offload),将 TCP 数据包的分段任务交给硬件处理。
    ethtool.feature-tx
    
    • 发送功能的总开关,启用或禁用发送相关的功能。
    ethtool.feature-tx-checksum-fcoe-crc
    
    • 发送 FCoE 数据包的 CRC 校验和计算的功能,确保数据完整性。
    ethtool.feature-tx-checksum-ip-generic
    
    • 发送 IP 数据包的通用校验和计算,可能涉及不同 IP 协议的校验和。
    ethtool.feature-tx-checksum-ipv4
    
    • 发送 IPv4 数据包的校验和计算,由硬件或软件完成。
    ethtool.feature-tx-checksum-ipv6
    
    • 发送 IPv6 数据包的校验和计算,确保 IPv6 数据包的正确性。
    ethtool.feature-tx-checksum-sctp
    
    • 发送 SCTP 数据包的校验和计算,保证 SCTP 协议的传输质量。
    ethtool.feature-tx-esp-segmentation
    
    • 对 ESP 数据包进行分段,可能与加密和传输相关。
    ethtool.feature-tx-fcoe-segmentation
    
    • 对 FCoE 数据包进行分段,以适应网络传输要求。
    ethtool.feature-tx-gre-csum-segmentation
    
    • 对 GRE 数据包进行校验和计算和分段,优化 GRE 协议的传输。
    ethtool.feature-tx-gre-segmentation
    
    • 对 GRE 数据包进行分段,提高 GRE 隧道的性能。
    ethtool.feature-tx-gso-partial
    
    • 部分通用分段卸载,可能是针对特定情况的 GSO 功能。
    ethtool.feature-tx-gso-robust
    
    • 更健壮的通用分段卸载功能,确保在复杂情况下 GSO 的性能。
    ethtool.feature-tx-ipxip4-segmentation
    
    • 对 IP-in-IP 封装的 IPv4 数据包进行分段,提高传输性能。
    ethtool.feature-tx-ipxip6-segmentation
    
    • 对 IP-in-IP 封装的 IPv6 数据包进行分段,以适应网络传输。
    ethtool.feature-tx-nocache-copy
    
    • 避免发送时的缓存复制,提高数据传输的速度和效率。
    ethtool.feature-tx-scatter-gather
    
    • 发送时的散列 / 聚集功能,提高发送性能。
    ethtool.feature-tx-scatter-gather-fraglist
    
    • 可能是发送时使用碎片列表进行散列 / 聚集,以优化数据传输。
    ethtool.feature-tx-sctp-segmentation
    
    • 对 SCTP 数据包进行分段,提高 SCTP 传输的性能。
    ethtool.feature-tx-tcp6-segmentation
    
    • 对 TCPv6 数据包进行分段,优化 TCPv6 的传输。
    ethtool.feature-tx-tcp-ecn-segmentation
    
    • 对带有 ECN(Explicit Congestion Notification)的 TCP 数据包进行分段,以处理拥塞。
    ethtool.feature-tx-tcp-mangleid-segmentation
    
    • 可能是对 TCP 数据包进行标识处理和分段,以适应不同的网络要求。
    ethtool.feature-tx-tcp-segmentation
    
    • 对 TCP 数据包进行分段,将大的 TCP 数据包分成较小的段。
    ethtool.feature-tx-udp-segmentation
    
    • 对 UDP 数据包进行分段,提高 UDP 传输性能。
    ethtool.feature-tx-udp_tnl-csum-segmentation
    
    • 对 UDP 隧道数据包进行校验和计算和分段,确保传输质量。
    ethtool.feature-tx-udp_tnl-segmentation
    
    • 对 UDP 隧道数据包进行分段,优化 UDP 隧道的性能。
    ethtool.feature-txvlan
    
    • 发送 VLAN 帧的功能,允许发送带有 VLAN 标签的帧。
    ethtool.feature-tx-vlan-stag-hw-insert
    
    • 硬件插入 VLAN 标签,提高发送 VLAN 帧的性能。

    4.3 ethtool.ring 相关参数

    ethtool.ring-rx
    
    • 接收环的大小,影响接收队列的性能,较大的接收环可以容纳更多的接收数据包,减少数据包丢失的可能性。
    ethtool.ring-rx-jumbo
    
    • 接收巨帧的环大小,对于处理大型数据包(如 Jumbo 帧)的接收性能有影响。
    ethtool.ring-rx-mini
    
    • 接收小型数据包的环大小,优化小型数据包的接收处理。
    ethtool.ring-tx
    
    • 发送环的大小,影响发送队列的性能,较大的发送环可以提高发送性能,减少发送队列的拥塞。

网络相关信息

  • 查询网卡信息
  1. 查看网卡的统计信息
    netstat -s
    ethtool -S eth0
    cat /proc/net/dev
    cat /sys/class/net/eth0/statistics/*
    
  2. 查看网络接收处理队列的统计信息
    cat /proc/net/softnet_stat
    
  • 查询网络套接字信息
  1. 列出所有活动的TCP和UDP套接字以及其他非UNIX域套接字
    ss -ap | grep -vE "^(nl|u_)"
    
  2. 列出所有建立连接的TCP套接字
    netstat -tnp | grep ESTABLISHED
    lsof -i tcp -s TCP:ESTABLISHED -n
    
  3. 列出所有监听和连接的tcp/udp/raw套接字
    ss -altuwenp
    netstat -altuwenp
    
    # -a 显示所有的sockets
    # -a 显示监听的sockets
    # -t 只显示tcp sockets
    # -u 只显示ucp sockets
    # -w 只显示raw sockets
    # -e 显示更详细的socket信息
    # -n 不要解析名称
    # -p 使用socket显示进程
    
    #define IPPROTO_IP 0 /* dummy for IP */
    #define IPPROTO_ICMP 1 /* control message protocol */
    #define IPPROTO_IGMP 2 /* internet group management protocol */
    #define IPPROTO_GGP 3 /* gateway^2 (deprecated) */
    #define IPPROTO_TCP 6 /* tcp */
    #define IPPROTO_PUP 12 /* pup */
    #define IPPROTO_UDP 17 /* user datagram protocol */
    #define IPPROTO_IDP 22 /* xns idp */
    #define IPPROTO_ND 77 /* UNOFFICIAL net disk proto */
    #define IPPROTO_RAW 255 /* raw IP packet */
    #define IPPROTO_MAX 256
    #58代表ICMPv6的协议号
    

操作系统相关信息

  • 缓存缓冲区清理
  1. 清除页面缓存、dentries/inodes(目录项/索引节点)缓存
    ansible all -m shell -a "sync && echo 1 > /proc/sys/vm/drop_caches"
    ansible all -m shell -a "sync && echo 2 > /proc/sys/vm/drop_caches"
    ansible all -m shell -a "sync && echo 3 > /proc/sys/vm/drop_caches"
    
  • 查看系统信息
  1. 按进程名合并统计内存占用百分比最高的前20个进程
    top -b -n 1 | awk '{if(NF==12)arr[$12]+=$10}END{for(i in arr)print arr[i]"\t"i}' | sort -nr | head -20
    
  2. 统计内存占用百分比最高的前20个进程PID及其PPID
    ps -e -o user,pid,ppid,%cpu,%mem,vsz,rss,command --sort=-rss | head -20
    
  3. 统计文件打开数目最多的前20个进程名及其PID
    lsof -w| awk '{print $1" "$2}' | sort | uniq -c | sort -nr | awk '{print $2, "PID: "$3, "OpenFileNum: "$1}' | column -t | head -20
    
  4. 列出系统上所有自定义定时任务
    for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l 2>/dev/null; done; awk '/^[0-9*]+[[:space:]]/' /etc/crontab
    
  • 文本处理
  1. 查找最近一分钟内被修改过的文件/含特定字符串/特定权限的文件
    find / \( -path /sys -o -path /proc -o -path /dev -o -path /run -o -path /tmp \) -prune -o -type f -mmin -1 -print
    find / \( -path /sys -o -path /proc -o -path /dev -o -path /run -o -path /tmp \) -prune -o -type f -exec grep -l "eth0" {} +
    find /etc -type f -exec bash -c 'lsattr {} | grep "\----i---------"' \;
    find / \( -path /sys -o -path /proc -o -path /dev -o -path /run -o -path /tmp \) -prune -o -type f -exec bash -c 'lsattr {} | grep "\----i---------"' \;
    
  2. 文件权限修复
    # 递归找到当前文件夹下的所有文件,将权限改为644
    find . -type f -exec chmod 644 {} +
    
    # 递归找到当前文件夹下的所有目录,将权限改为755
    find . -type d -exec chmod 755 {} +
    
    # 递归找到当前文件夹下的所有文件,如果文件以.sh结尾或文件第一行为#!/bin/开头,就将权限改为755
    find . -type f -name "*.sh" -exec chmod 755 {} +
    find . -type f -exec sh -c 'head -n1 "$1" | grep -q "#!/bin/bash" && chmod 755 "$1"' sh {} \;
    # 将上面两条整合为一条命令:
    find . -type f \( -name "*.sh" -o -exec sh -c 'head -n1 "$1" | grep -q "^#!/bin/"' sh {} \; \) -exec chmod 755 {} +
    
    # 递归找到当前文件夹下的所有文件,如果文件是二进制文件,将权限改为755
    find . -type f -exec sh -c 'file "$1" | grep -q "executable"' sh {} \; -exec chmod 755 {} +
    
  3. 将空格分隔的文件转换为制表符分隔的文件
    awk -v OFS='\t' '{$1=$1; print}' /filepath  # 方式1
    awk 'OFS="\t" {$1=$1}1' /filepath  # 方式2
    
    # OFS="\t" # set output separator as a tab
    # {$1=$1}  # remove extra spaces and set OFS as tab
    # 1        # with awk, true, so print the current line
    
posted @ 2024-07-12 10:18  wanghongwei-dev  阅读(26)  评论(0编辑  收藏  举报