Linux命令速查整理(篇1)
日志与审计
Journalctl
- 查询指定系统单元服务的日志
journalctl -u network journalctl -u sshd - 查询内核日志
journalctl -k - 查询指定日志级别的日志
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 - 查询指定时间范围内的日志
journalctl --since "2023-01-01 00:00:00" --until "2023-01-02 23:59:59" journalctl --since yesterday - 实时跟踪日志
journalctl -f - 跳到日志末尾
journalctl -e - 日志倒序显示
journalctl -r - 指定日志条目数量
journalctl -n 20
Ausearch
- 查询指定审计事件键值(key)的审计记录
ausearch -k my_key - 查询指定用户名相关的审计记录
ausearch -ua root ausearch -ui 0 - 查询指定开始时间的审计记录
ausearch -te 2025-02-02 12:00:00 ausearch -te yesterday - 查询指定结束时间的审计记录
ausearch -te 2025-02-02 12:00:00 ausearch -te today - 查询指定审计 ID(audit ID)的审计记录
ausearch -a 12345 - 查询指定进程 ID(PID/PPID)相关的审计记录
ausearch -p 1000 ausearch -pp 1 - 查询指定系统调用相关的审计记录
ausearch -sc openat - 将结果解释为人类可读(将审计事件中的数字代码转换为可读的文本)
ausearch -i - 查询指定命令相关的审计记录
ausearch -i -c ls ausearch -i -c iptables - 查询指定消息类型的审计记录
ausearch -i -m PROCTITLE -ts today ausearch -i -m EXECVE -p <PID>
网络管理
Ethtool
- 查看和设置指定网络接口(eth0)的标准信息,如速度、双工模式等
ethtool eth0 # 查询网卡eth0的参数配置 ethtool -r eth0 # 重启网卡的自动协商功能(即重新进行链路协商) ethtool -s eth0 autoneg on # 开启eth0接口的链路自动协商功能(默认启用) ethtool -s eth0 autoneg off # 关闭eth0接口的链路自动协商功能 ethtool -s eth0 speed 10000 duplex full # 手动指定eth0接口的速率为10000Mbps(即10Gbps),双工模式为全双工(full duplex) - 查看和设置接口的RX/TX环的参数
ethtool -S eth0 | grep fifo # 查看eth0网卡的统计信息中与FIFO缓冲区相关的指标 ethtool -g eth0 # 显示eth0网卡的接收(rx)和发送(tx)环形缓冲区(ring buffer)的当前大小 ethtool -G eth0 rx 2048 tx 2048 # 手动设置eth0网卡的发送和接收环形缓冲区大小为2048 - 查看和设置网卡的流量控制
ethtool -a eth0 # 查看网卡eth0的流量控制配置 ethtool -A eth0 autoneg on # 开启流量控制的自动协商功能(默认启用) ethtool -A eth0 autoneg off # 关闭流量控制的自动协商功能 ethtool -A eth0 tx off # 关闭发送方向(Tx)的流量控制功能 ethtool -A eth0 rx off # 关闭接收方向(Rx)的流量控制功能 - 显示或设置接口的协议卸载和其他功能,如TCP/IP校验和卸载、数据包分段和重组
ethtool -k eth0 # 显示eth0网卡的各种硬件卸载(offload)功能的当前状态(如TSO、GSO、GRO等) ethtool -K eth0 tso off # 关闭eth0网卡的TSO(TCP Segmentation Offload,TCP分段卸载)功能 ethtool -K eth0 gso off # 关闭eth0网卡的GSO(Generic Segmentation Offload,通用分段卸载)功能 ethtool -K eth0 gro off # 关闭eth0网卡的GRO(Generic Receive Offload,通用接收卸载)功能 - 显示指定网络接口的驱动信息
ethtool -i eth0 # 查看eth0网卡的硬件与驱动相关信息 - 显示网络接口的统计信息
ethtool -S eth0 # 显示eth0网卡的详细统计信息 - 查看和设置网卡RSS队列
ethtool -x eth0 # 显示eth0网卡的接收队列(Rx queue)的中断亲和性(IRQ affinity)配置 ethtool -X eth0 equal 4 # 将eth0的接收队列均衡分配到4个CPU核心(0-3) - 查看和设置网卡中断合并
ethtool -c eth0 # 显示eth0网卡的中断合并(Interrupt Coalescing)参数的详细配置 ethtool -C eth0 adaptive-rx on adaptive-tx on # 为eth0网卡开启自适应中断合并功能 - 查看和设置网卡队列
ethtool -l eth0 # 显示eth0网卡的队列数量配置信息 ethtool -L eth0 rx 64 tx 64 # 手动配置eth0网卡的队列数量,将发送和接收队列(rx queues)都设置为64个 - 显示时间戳功能
ethtool -T eth0 # 显示eth0网卡的时间同步相关信息
NetworkManager
- 网络控制
nmcli networking nmcli networking connectivity nmcli networking on nmcli networking off - 连接管理
nmcli connection show nmcli -f NAME,UUID,TYPE,DEVICE,FILENAME 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 - 设备管理
nmcli device status nmcli device show eth0 nmcli device connect eth0 nmcli device disconnect eth0 nmcli device reapply eth0 - 设置物理网卡参数
# 示例 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"
Tcpdump
- 常见协议抓包速查
协议 抓包命令 说明 ARP tcpdump -i eth0 'ether proto 0x0806' 或 tcpdump -i eth0 arp 地址解析协议 RARP tcpdump -i eth0 'ether proto 0x8035' 或 tcpdump -i eth0 rarp 反向地址解析协议 LLDP tcpdump -i eth0 'ether proto 0x88cc' 链路层发现协议 LACP tcpdump -i eth0 'ether proto 0x8809' 链路聚合控制协议 ICMP tcpdump -i eth0 'ip proto 1' 或 tcpdump -i eth0 icmp 互联网控制消息协议 IGMP tcpdump -i eth0 'ip proto 2' 或 tcpdump -i eth0 igmp 互联网组管理协议 VRRP tcpdump -i eth0 'ip proto 112' 或 tcpdump -i eth0 vrrp 虚拟路由器冗余协议 IPsec ESP tcpdump -i eth0 'ip proto 50' IPsec 封装安全载荷 IPsec AH tcpdump -i eth0 'ip proto 51' IPsec 认证头 IKEv2 tcpdump -i eth0 'udp port 500 or udp port 4500' 互联网密钥交换协议 OSPF tcpdump -i eth0 'ip proto 89' 开放式最短路径优先协议 BGP tcpdump -i eth0 'tcp port 179' 边界网关协议 DNS tcpdump -i eth0 port 53 域名系统 DHCP tcpdump -i eth0 'udp port 67 or udp port 68' 动态主机配置协议 DHCPv6 tcpdump -i eth0 'udp port 546 or udp port 547' IPv6 动态主机配置协议 SSH tcpdump -i eth0 'tcp port 22' 安全外壳协议 Telnet tcpdump -i eth0 'tcp port 23' 远程终端协议 RDP tcpdump -i eth0 'tcp port 3389' 远程桌面协议 NTP tcpdump -i eth0 'udp port 123' 网络时间协议 FTP tcpdump -i eth0 'tcp port 21 or tcp port 20' 文件传输协议 TFTP tcpdump -i eth0 'udp port 69' 简单文件传输协议 SMTP tcpdump -i eth0 'tcp port 25 or tcp port 465 or tcp port 587' 简单邮件传输协议 IMAP4 tcpdump -i eth0 'tcp port 143 or tcp port 993' 互联网邮件访问协议 POP3 tcpdump -i eth0 'tcp port 110 or tcp port 995' 邮局协议 LDAP tcpdump -i eth0 'tcp port 389 or tcp port 636' 轻量级目录访问协议 SNMP tcpdump -i eth0 'udp port 161 or udp port 162' 简单网络管理协议 MQTT tcpdump -i eth0 'tcp port 1883 or tcp port 8883' 消息队列遥测传输协议
信息查询
- 查询网卡信息
- 查看网卡的统计信息
netstat -s ethtool -S eth0 cat /proc/net/dev cat /sys/class/net/eth0/statistics/* - 查看网络接收处理队列的统计信息
cat /proc/net/softnet_stat
- 查询网络套接字信息
- 列出所有活动的TCP和UDP套接字以及其他非UNIX域套接字
ss -ap | grep -vE "^(nl|u_)" - 列出所有建立连接的TCP套接字
netstat -tnp | grep ESTABLISHED lsof -i tcp -s TCP:ESTABLISHED -n - 列出所有监听和连接的tcp/udp/raw套接字
ss -altuwenp netstat -altuwenp # -a 显示所有的sockets # -l 显示监听的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的协议号
操作系统管理
- 缓存缓冲区清理
- 清除页面缓存、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"
- 查看系统信息
- 按进程名合并统计内存占用百分比最高的前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 - 统计内存占用百分比最高的前20个进程PID及其PPID
ps -e -o user,pid,ppid,%cpu,%mem,vsz,rss,command --sort=-rss | head -20 - 统计文件打开数目最多的前20个进程名及其PID
lsof -w| awk '{print $1" "$2}' | sort | uniq -c | sort -nr | awk '{print $2, "PID: "$3, "OpenFileNum: "$1}' | column -t | head -20 - 列出系统上所有自定义定时任务
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l 2>/dev/null; done; awk '/^[0-9*]+[[:space:]]/' /etc/crontab
- 文本处理
- 查找最近一分钟内被修改过的文件/含特定字符串/特定权限的文件
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---------"' \; - 文件权限修复
# 递归找到当前文件夹下的所有文件,将权限改为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 {} + - 将空格分隔的文件转换为制表符分隔的文件
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
作者:wanghongwei
版权声明:本作品遵循<CC BY-NC-ND 4.0>版权协议,商业转载请联系作者获得授权,非商业转载请附上原文出处链接及本声明。

浙公网安备 33010602011771号