Linux运维常用工具

本文最后更新于:2022年9月4日 下午

gdb调试

gdb调试:一个简单的入门

ldd

  • 查看程序运行所需的共享库(.so文件),常用来解决因缺少某个库文件而不能运行的问题
  • 使用方式:ldd + 文件名
  • 输出三列,分别是依赖的库、库的位置、库加载的起始地址

top

实时显示系统中各个进程的资源占用状况

交互指令:

  • i:只显示正在运行的进程
  • h:帮助命令
  • q:退出
  • u:指定显示用户进程
  • P:按%CPU使用率排行
  • T:按MITE+排行
  • M:按%MEM排行
  • c:显示命令完全模式
  • s:设置刷新时间间隔
  • 按键盘数字“1”,可监控每个逻辑CPU的状况

一些缩写:

  • PID:进程的ID
  • USER:进程所有者
  • PR:进程的优先级别,越小越优先被执行
  • VIRT:进程占用的虚拟内存
  • RES:进程占用的物理内存
  • SHR:进程使用的共享内存
  • %CPU:进程占用CPU的使用率
  • %MEM:进程使用的物理内存和总内存的百分比
  • TIME+:该进程启动后占用的总的CPU时间,即占用CPU使用时间的累加值
  • COMMAND:进程启动命令名称

cpu:

  • us 用户空间占用CPU百分比
  • sy 内核空间占用CPU百分比
  • ni 用户进程空间内改变过优先级的进程占用CPU百分比
  • id 空闲CPU百分比
  • wa 等待输入输出的CPU时间百分比
  • hi 硬件中断
  • si 软件中断

lsof

lsof(list open files)
通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Lists open files and the corresponding processes.
Note: Root privileges (or sudo) is required to list files opened by others.
More information: https://manned.org/lsof.

- Find the processes that have a given file open:
lsof path/to/file

- Find the process that opened a local internet port:
lsof -i :port

- Only output the process ID (PID):
lsof -t path/to/file

- List files opened by the given user:
lsof -u username

- List files opened by the given command or process:
lsof -c process_or_command_name

- List files opened by a specific process, given its PID:
lsof -p PID

- List open files in a directory:
lsof +D path/to/directory

- Find the process that is listening on a local IPv6 TCP port and don't convert network or port numbers:
lsof -i6TCP:port -sTCP:LISTEN -n -P

ps

查看进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

ps

Information about running processes.
More information: https://manned.org/ps.

- List all running processes:
ps aux

- List all running processes including the full command string:
ps auxww

- Search for a process that matches a string:
ps aux | grep string

- List all processes of the current user in extra full format:
ps --user $(id -u) -F

- List all processes of the current user as a tree:
ps --user $(id -u) f

- Get the parent PID of a process:
ps -o ppid= -p pid

- Sort processes by memory consumption:
ps --sort size

pstack

  • 显示每个进程的栈跟踪
  • 通过sudo apt install pstack进行安装

strace

追踪系统调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

- Start tracing a specific process by its PID:
strace -p pid

- Trace a process and filter output by system call:
strace -p pid -e system_call_name

- Count time, calls, and errors for each system call and report a summary on program exit:
strace -p pid -c

- Show the time spent in every system call:
strace -p pid -T

- Start tracing a program by executing it:
strace program

- Start tracing file operations of a program:
strace -e trace=file program

readelf

  • ELF(Executable and Linking Format)
  • elf文件的几种类型
    • 可重定位的对象文件(Relocatable file),比如由汇编器汇编生成的 .o 文件
    • 可执行的对象文件(Executable file)
    • 动态库文件,.so文件(Shared object file)

readelf指令的几个常用选项:

  • -a列出全部
  • -h列出文件头
  • -S列出段头
  • -s符号表

objdump

  • 直接输入objdump可以显示指令选项,man指令可以更详细一些
  • -S 尽可能反汇编出源代码,尤其当编译的时候指定了-g这种调试参数时,效果比较明显。隐含了-d参数
  • -d 反汇编
  • -f 显示文件头信息

    nm

    nm 命令显示关于指定 File 中符号的信息

    wget

  • wget [参数] [URL地址]
  • wegt url 从网络下载一个文件并保存在当前目录
  • -O选项可以指定文件名
  • -c重新启动下载中断的文件
  • -b后台下载

scp

secure copy,从远程主机处复制
常用命令:

  • -r 递归复制整个目录
  • scp path/to/local_file emote_username@remote_host:path/to/remote_directory
  • scp -i ~/.ssh/private_key local_file remote_host:/path/remote_file

crontab

定时任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
crontab

Schedule cron jobs to run on a time interval for the current user.
Job definition format: "(min) (hour) (day_of_month) (month) (day_of_week) command_to_execute".
More information: https://manned.org/crontab.

- Edit the crontab file for the current user:
crontab -e

- Edit the crontab file for a specific user:
sudo crontab -e -u user

- Replace the current crontab with the contents of the given file:
crontab path/to/file

- View a list of existing cron jobs for current user:
crontab -l

- Remove all cron jobs for the current user:
crontab -r

- Sample job which runs at 10:00 every day (* means any value):
0 10 * * * command_to_execute

- Sample job which runs every minute on the 3rd of April:
* * 3 Apr * command_to_execute

- Sample job which runs a certain script at 02:30 every Friday:
30 2 * * Fri /absolute/path/to/script.sh

Linux运维常用工具
http://gls.show/p/8329f3e9/
作者
郭佳明
发布于
2022年9月4日
许可协议