标题: CentOS 实用命令备忘 分类: 工具 创建: 2024-12-26 22:56 修改: 链接: http://0x2531.tech/tools/202412262256.txt -------------------------------------------------------------------------------- 在工作当中,为了完成某些特定任务,如:排障、应急处置等,需要执行一些系统命令。但由于使用的不频繁,常常 需要上网搜索,挺麻烦的。所以,索性找个地方集中记录下,方便日后查看。 1、进程相关 1-1 查看进程是否存在 [root@elk-test3 ~]# ps aux | grep titanagent root 1237 0.4 1.2 574600 96812 ? Sl 12月24 5:34 ./titanagent -d 1-2 查看进程详情 [root@elk-test3 ~]# cat /proc/1237/status Name: titanagent Umask: 0000 State: T (stopped) Tgid: 1237 Ngid: 0 Pid: 1237 PPid: 1 ... 1-3 查看进程打开的文件 [root@elk-test3 ~]# lsof -p 1237 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME titanagen 1237 root cwd DIR 253,0 4096 4451435 /titan/agent titanagen 1237 root rtd DIR 253,0 249 64 / titanagen 1237 root txt REG 253,0 13875376 4451407 /titan/agent/titanagent titanagen 1237 root mem REG 253,0 547568 33426 /usr/lib64/libfreeblpriv3.so titanagen 1237 root mem REG 253,0 11031312 137732839 /var/lib/sss/mc/initgroups titanagen 1237 root mem REG 253,0 37168 240026 /usr/lib64/libnss_sss.so.2 titanagen 1237 root mem REG 253,0 1096192 134860732 /titan/agent/plugin/libFileMonitor.so ... 1-4 查看进程使用的 CPU 和内存 [root@elk-test3 ~]# top -p 1237 ... PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1237 root 20 0 574600 96876 13792 T 0.0 1.2 5:35.83 titanagent 1-5 查看进程的环境变量 [root@elk-test3 ~]# cat /proc/1237/environ | tr '\0' '\n' LD_PRELOAD= XDG_SESSION_ID=514081 SHELL=/bin/sh OLDPWD=/titan/agent ... 2、网络相关 2-1 查看网络连接(netstat) # 查看监听端口 [root@elk-test3 ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5601 0.0.0.0:* LISTEN 697/node tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 688/rpcbind ... # 只查看TCP连接 netstat -antp # 只查看UDP连接 netstat -anup 2-2 查看网络连接(lsof) # 查看所有网络连接 [root@elk-test3 ~]# lsof -i COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 688 rpc 6u IPv4 22033 0t0 UDP *:sunrpc rpcbind 688 rpc 7u IPv4 22034 0t0 UDP *:twamp-control rpcbind 688 rpc 8u IPv4 22035 0t0 TCP *:sunrpc (LISTEN) rpcbind 688 rpc 9u IPv6 22036 0t0 UDP *:sunrpc rpcbind 688 rpc 10u IPv6 22037 0t0 UDP *:twamp-control rpcbind 688 rpc 11u IPv6 22038 0t0 TCP *:sunrpc (LISTEN) node 697 kibana 18u IPv4 141849809 0t0 TCP elk-test3:52334->elk-test3:wap-wsp (ESTABLISHED) node 697 kibana 19u IPv4 141847847 0t0 TCP elk-test3:52336->elk-test3:wap-wsp (ESTABLISHED) ... # 查看特定端口 lsof -i:80 # 查看特定协议 lsof -i tcp lsof -i udp 2-3 实时监控网络连接 [root@elk-test3 ~]# watch netstat -antp Every 2.0s: netstat -antp Wed Dec 25 14:56:27 2024 Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5601 0.0.0.0:* LISTEN 697/node tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 688/rpcbind ... [root@elk-test3 ~]# watch ss -t Every 2.0s: ss -t Wed Dec 25 14:57:20 2024 State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 0 0.0.0.0:52332 0.0.0.0:wap-wsp ESTAB 0 0 0.0.0.0:56870 0.0.0.0:wap-wsp ESTAB 0 0 0.0.0.0:40062 0.0.0.0:7788 ... 2-4 抓包 # 抓指定网口全部网络包 [root@elk-test3 ~]# tcpdump -i ens160 -w /tmp/hids.pcap tcpdump: listening on ens160, link-type EN10MB (Ethernet), capture size 262144 bytes 注:得到 pcap 文件后,导入到 Wireshark 中分析 # 抓指定网口特定 IP 网络包 [root@elk-test3 ~]# tcpdump -i ens160 host 192.168.1.100 -w /tmp/hids.pcap tcpdump: listening on ens160, link-type EN10MB (Ethernet), capture size 262144 bytes # 只抓取发往特定 IP 的数据包(目标IP) tcpdump dst host 192.168.1.100 # 只抓取来自特定 IP 的数据包(源IP) tcpdump src host 192.168.1.100 3、系统相关 3-1 查看 CPU 信息 [root@elk-test3 ~]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 1 Core(s) per socket: 1 ... 3-2 查看 CPU 使用 TOP3 进程 [root@elk-test3 ~]# ps -eo pid,comm,%cpu --sort=-%cpu | head -n 4 PID COMMAND %CPU 986 java 0.9 1237 titanagent 0.3 697 node 0.2 3-3 查看内存使用情况 [root@elk-test3 ~]# free -m total used free shared buff/cache available Mem: 7822 3305 456 378 4060 3717 Swap: 1023 884 139 3-4 查看内存使用 TOP3 进程 [root@elk-test3 ~]# ps -eo pid,comm,%mem --sort=-%mem | head -n 4 PID COMMAND %MEM 986 java 16.8 4042 java 4.9 27653 java 4.6 3-5 查看磁盘使用情况 [root@elk-test3 ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root 99G 58G 42G 59% / devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm ... 3-6 查看目录大小 [root@elk-test3 ~]# du -sh /titan/agent/ 109M /titan/agent/ 注:去掉 -s 参数,则显示目录下所有文件大小 3-7 查看指定阈值 TOP10 大文件并显示大小 [root@elk-test3 ~]# find / -type f -size +100M -exec du -sh {} + | sort -rh | head -n 10 2.5G /root/sec.tar.gz 690M /var/log/suricata/access.log-20240928 ... 3-8 搜索文件 # 按文件名搜索 [root@elk-test3 ~]# find / -name "access.log" /var/log/nginx/access.log /var/log/suricata/access.log # 按文件内容搜索 [root@elk-test3 ~]# grep -r "error" /var/log/ /var/log/anaconda/syslog:02:56:55,787 ERR multipathd:uevent trigger error ...