Linux:常见工具、命令使用集锦

1. 前言

限于作者能力水平,本文可能存在谬误,因此而给读者带来的损失,作者不做任何承诺。

2. Linux 命令和工具

2.1 CPU 工具

lscpu
/proc/cpuinfo
/sys/devices/system/cpu/*

2.2 内存工具

sudo dmidecode -t memory | grep Speed # 查询 DDR 速度
sudo lshw -short -C memory
pcstat # 查看文件 page cache 状态,https://github.com/tobert/pcstat

2.3 磁盘工具

fdisk # 查询、格式华磁盘等
gparted # 磁盘扩容等
df # 磁盘已用、剩余容量
du # 查看目录树、文件占用磁盘容量
lsblk # 磁盘块信息
iostat # io 行为观测
iotop # io 行为观测
losetup # loop device 工具
......

2.3.1 查看路径所属文件系统信息

  • 方法 1:通过 df 命令查找
$ df -T /
Filesystem     Type 1K-blocks     Used Available Use% Mounted on
/dev/sda1      ext4  32728952 19078872  11964480  62% /

$ df -T /sys
Filesystem     Type  1K-blocks  Used Available Use% Mounted on
sysfs          sysfs         0     0         0    - /sys

$ df -T /sys/fs/cgroup
Filesystem     Type  1K-blocks  Used Available Use% Mounted on
tmpfs          tmpfs   2007948     0   2007948   0% /sys/fs/cgroup
$ df -T /sys/fs/cgroup/cpu
Filesystem     Type   1K-blocks  Used Available Use% Mounted on
cgroup         cgroup         0     0         0    - /sys/fs/cgroup/cpu,cpuacct
$ df -T /sys/fs/cgroup/systemd
Filesystem     Type   1K-blocks  Used Available Use% Mounted on
cgroup         cgroup         0     0         0    - /sys/fs/cgroup/systemd
  • 方法 2:通过 stat 命令
$ stat -f -c %T /sys/kernel/debug
debugfs
  • 方法 3:通过 findmnt 命令
$ findmnt --target /dev
TARGET SOURCE FSTYPE   OPTIONS
/dev   udev   devtmpfs rw,nosuid,relatime,size=1977184k,nr_inodes=494296,mode=755

2.4 查询进程信息

# 查询进程命名空间信息
ps -e -o pidns,pid,args
lsns

# 查看系统进程信息
ps -ef
top

2.5 虚拟化相关工具

# 查看系统是否运行在 虚拟机下: lscpu, systemd-detect-virt
# . lscpu 是来自于 util-linux 工具的程序,通过 CPUID 指令来获取的信息来判定系统是否运行于虚拟机下;
# . systemd-detect-virt 来自于 systemd 包的一个程序,具体实现没探究过。
$ lscpu
Architecture:            x86_64
...
Virtualization features:
  Hypervisor vendor:     KVM
  Virtualization type:   full
...
$ systemd-detect-virt
none
$ systemd-detect-virt
kvm
$ systemd-detect-virt
vmware
# 通过查看系统首号进程(PID=1)的 cgroup 属性节点,查看系统是否运行于容器环境
$ cat /proc/1/cgroup

2.6 ELF 工具

readelf
objdump
nm
patchelf: patchelf --set-interpreter /lib64/ld-linux-x86-64.so.2 main
strings
......

2.6.1 获取 ELF 目标文件的编译器信息

有时候,需要知道已经编译好的 ELF 目标文件 所用的编译器版本,可以尝试以下方法:

readelf -p .comment 目标文件
objdump -s --section .comment 目标文件
strings 目标文件 | grep 'GCC' // 针对GCC

上面的基本原理都是提取 ELF .comment 段 的信息。该段是用来保存版本控制信息的。

2.7 系统版本信息查询

uname
/proc/version
/proc/version_signature
/proc/sys/kernel/version
/proc/sys/kernel/osrelease
/proc/sys/kernel/ostype
/proc/sys/kernel/bootloader_type
/proc/sys/kernel/bootloader_version

2.8 远程操作

## scp(secure copy) 是一个基于 ssh 的远程拷贝工具,windows 下的 WinSCP 工具就是基于它。

## 1. 从本地复制到远程
scp local_file remote_username@remote_ip:remote_file

## 2. 从远程复制到本地
scp remote_username@remote_ip:remote_file local_file

2.9 搜索查找

# grep 时排除多个目录
grep -E -R 'google|Animal|hunting' --exclude-dir=my-first-dir --exclude-dir=my-second-dir
grep -E -R 'google|Animal|hunting' --exclude-dir={my-first-dir,my-second-dir}
# 指定搜索目录
grep -nrw "buffer_size" sound/

How To Exclude Matches, Directories Or Files with Grep

2.10 控制台、登录信息查询

# tty
not a tty # 在嵌入式环境,我们从系统的调试串口登录

# tty # 使用 ssh 登录
/dev/pts/0
# who am i
root     pts/0        Jan  1 04:08 (192.168.0.15)

# who
root     pts/0        Jan  1 04:08 (192.168.0.15)

# w
USER            TTY             IDLE    TIME             HOST
root            pts/0           00:00   Jan  1 04:08:49  192.168.0.15

2.11 安装包管理工具

sudo apt-get update # 按 /etc/apt/sources.list 的包源,更新系统软件包源信息数据库
apt-cache search <package-name> # 从系统软件包源信息数据库,搜索给定名称的软件包
sudo apt-get remove <package-name> # 删除已安装的软件包,但保留其配置文件
sudo apt-get purge <package-name> # 同时删除软件包 <package-name> 及其配置文件
sudo apt-get remove --purge <package-name> # 同时删除软件包及其配置文件,同 apt-get purge
sudo apt-get autoremove # 卸载软件包后,有时会留下无用的依赖包,使用 autoremove 来清除这些无用的依赖包
sudo apt-get autoclean # 清理目录 /var/cache/apt/archives/ 下不再需要的软件安装包
sudo apt-get clean # 清理目录 /var/cache/apt/archives/ 下下载的所有软件安装包
dpkg-query -S <absolute path of file> # 搜索指定文件所在的、本地已安装的 package 名称
dpkg-query -L <package-name> # 列举指定文件包包含的文件列表
apt-cache policy gcc-arm-linux-gnueabihf # 查看候选安装包信息(包括版本等信息)

sudo dpkg --add-architecture architecture # 更新 /var/lib/dpkg/arch
sudo dpkg --remove-architecture architecture
sudo dpkg --print-architecture
sudo dpkg --print-foreign-architectures
#$ sudo dpkg --add-architecture arm64 # 在 host 上添加 arm64 架构包
#$ sudo dpkg --print-architecture
#amd64
#$ sudo dpkg --print-foreign-architectures
#i386
#arm64
#$ sudo apt-get update
## /etc/apt/sources.list
## Standard amd64 repositories
#deb [arch=amd64] http://cn.archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse
#deb [arch=amd64] http://cn.archive.ubuntu.com/ubuntu/ jammy-updates main restricted universe multiverse
#deb [arch=amd64] http://cn.archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
#deb [arch=amd64] http://security.ubuntu.com/ubuntu jammy-security main restricted universe multiverse
#
# arm64 repositories
#deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ jammy main restricted universe multiverse
#deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ jammy-updates main restricted universe multiverse
#deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports/ jammy-security main restricted universe multiverse
# sudo apt-get clean
# sudo apt-get update
#$ sudo apt install g++-aarch64-linux-gnu libdb-dev:arm64

# 从系统软件包源信息数据库,搜索哪些软件包包含指定文件
sudo apt install apt-file
sudo apt-file update
apt-file search <file-name>

apt-cache depends <package-name> # 查询 package-name 依赖的包
apt-cache show <package-name> # 查询 package-name 的详细信息,包括依赖关系
apt-cache showpkg <package-name> # 查询 package-name 的详细信息及依赖关系和反向依赖关系
dpkg -I <deb-package-name> # 查询 deb-package-name 依赖的包

# 递归地列出 package-name 及其所有依赖包
sudo apt install apt-rdepends
apt-rdepends -r <package-name>

# 修复丢失或损坏的包
sudo apt-get update --fix-missing

sources.list 格式:SOURCES.LIST(5)

# snap
sudo snap install <snap-name>
snap list <snap-name> # 列出已经安装的 snap 包
snap find <keyword> # 查找 snap 安装包的信息
snap info <snap-name>

snap list <snap-name>
#$ snap list chromium
#Name      Version         Rev   Tracking       Publisher   Notes
#chromium  140.0.7339.185  3251  latest/stable  canonical✓  -
#WARNING: There is 1 new warning. See 'snap warnings'.
ls /snap/<snap-name>/<revision> 
#$ ls /snap/chromium/current
#$ ls /snap/chromium/3251
...
# 查看所有通过 snap 安装的包
$ snap list
Name                       Version           Rev    Tracking         Publisher   Notes
bare                       1.0               5      latest/stable    canonical✓  base
core20                     20241206          2496   latest/stable    canonical✓  base
core22                     20241119          1722   latest/stable    canonical✓  base
docker                     27.5.1            3064   latest/stable    canonical✓  -
firefox                    135.0-2           5751   latest/stable/…  mozilla✓    -
gnome-42-2204              0+git.38ea591     202    latest/stable/…  canonical✓  -
gtk-common-themes          0.1-81-g442e511   1535   latest/stable/…  canonical✓  -
lz4                        1.9.4             4      latest/stable    hopem       -
snap-store                 41.3-72-g80e7130  1216   latest/stable/…  canonical✓  -
snapd                      2.67.1            23771  latest/stable    canonical✓  snapd
snapd-desktop-integration  0.9               253    latest/stable/…  canonical✓  -

# 查看 snap 安装的包的文件列表
#
# 方法1:
# snap list | grep "包名"
# ls /snap/包名/版本号
#
# 方法2:查看挂载点
# mount | grep snap | grep "包名"

$ ls /snap/docker/3064 -l
total 0
drwxr-xr-x 2 root root 343  1月 28 03:27 bin
drwxr-xr-x 2 root root  34  1月 28 03:27 config
drwxr-xr-x 2 root root  27  1月 28 03:27 dev
drwxr-xr-x 9 root root 123  1月 28 03:27 etc
drwxr-xr-x 6 root root  88  1月 28 03:27 lib
drwxr-xr-x 4 root root  56  1月 28 03:27 meta
drwxr-xr-x 2 root root 188  1月 28 03:27 sbin
drwxr-xr-x 3 root root  71  1月 28 03:27 snap
drwxr-xr-x 8 root root 126  1月 28 03:27 usr
drwxr-xr-x 3 root root  26  1月 28 03:24 var

$ mount | grep snap | grep "docker"
var/lib/snapd/snaps/docker_3064.snap on /snap/docker/3064 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
nsfs on /run/snapd/ns/docker.mnt type nsfs (rw)
$ ls /snap/docker/3064
bin  config  dev  etc  lib  meta  sbin  snap  usr  var

2.12 其它

# screen 是一个窗口管理程序,可以用来分离 终端 和 运行程序, screen 典型应用场景之一是 ssh 远程登录编译。
# 在我们使用 ssh 登录到远程服务器,编译耗时长的大的工程时(如编译 android 系统),中间可能出现登录终端电脑
# 和 服务器短线的情况,由于我们发起的编译命令隶属于 ssh 登录的 session,当登录终端断开,隶属于该登录 session
# 的进程都将随之退出,我们的编译工作也就被中断了,此时我们可以通过 screen 将我们的编译命令放到服务器电脑的后台
# 运行,这样我们在需要时直接退出 ssh 登录中断,编译工作也不会受到影响,再次 ssh 登录再切入到服务器电脑后台看看
# 编译工作是否完成。使用 ssh 远程登录服务器后:
$ screen -S test # 首次,我们创建一个服务器后台窗口,会进入到这个后台窗口
$ make # 在 服务器后台窗口 执行耗时的编译命令,接着按 Ctrl + A,D 退出后台窗口,最后 断开 ssh 连接,然后再次登录
$ screen -ls # 查看现有的服务器后台窗口, 3282678 为后台窗口程序的 PID
There is a screen on:
        3282678.test     (12/23/2023 03:38:02 PM)        (Detached)
$ screen -r 3282678 # 切回服务器后台窗口
$ kill 3282678 # 即使服务器后台窗口的命令执行完毕,后台窗口一直存在,不需要的时候用 kill 移除它
// test 程序代码

#include <unistd.h>

void main(void)
{
	while (1) sleep(1);
}
# nohup COMMAND [ARG]...
# nohup OPTION
#
# nohup 让运行的程序忽略 SIGHUP 信号。SIGHUP 的默认处理动作是退出程序。
# nohup 默认会生成一个 nohup.out 输出文件,记录程序的输出内容。
#
# 更多关于 nohup 的细节:
# https://www.man7.org/linux/man-pages/man1/nohup.1.html

# 1. 用 ssh 登录主机,然后在 ssh 连接上下文直接运行 test,
#    然后在主机一侧查询 test 的运行情况
$ ./test &
$ ps -ef | grep -v grep | grep test
bill       3132   3100  0 11:01 pts/17   00:00:00 ./test

# 2. 退出 ssh 登录连接,然后再在主机侧查询 test 运行情况,
#    我们会发现,这时候查不到 test,也就是 test 进程随着
#    ssh 连接的断开也退出了
$ ps -ef | grep -v grep | grep "test"

# 3. 再次用 ssh 登录主机,然后在 ssh 连接上下文用 nohup 运行 
#    test,然后在主机一侧查询 test 的运行情况
$ nohup ./test &
[1] 3224
nohup: ignoring input and appending output to 'nohup.out'
$ ps -ef | grep -v grep | grep "test"
bill       3224   3208  0 11:08 pts/17   00:00:00 ./test

# 4. 退出 ssh 登录连接,然后再在主机侧查询 test 运行情况,
#    这时候 test 并没有像前面退出,依然在运行,只是 test
#    程序的中断由 pts/17 变成了一个 ? 显示
$ $ ps -ef | grep -v grep | grep "test"
bill       3224      1  0 11:08 ?        00:00:00 ./test

3. 后记

本着积少成多、集腋成裘的想法,着力将本文打造成一篇 Linux 用户常见命令的常见用法快捷参考,将持续更新。

posted @ 2025-04-07 13:49  JiMoKuangXiangQu  阅读(35)  评论(0)    收藏  举报