Linux的shell命令 - 实践

1.进程管理

        进程是一个独立可调度的任务,是一个程序一次的执行过程。进程有四种状态:运行态:此时进程正在运行或准备运行,等待态:此时的进程正在等待一个事件的发生或者系统的某种资源,有可中断和不可中断两种。停止态:此时进程被停止。死亡态:这是一个已经终止的进程,但是还在进程向量数组中占有一个stack_struct结构。

        下面我们来看与进程有关的shell命令:

linux@ubuntu:~$ ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  5.7  0.2 101328 11632 ?        Ss   10:15   0:02 /sbin/init auto noprompt splash
root           2  0.1  0.0      0     0 ?        S    10:15   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        S    10:15   0:00 [pool_workqueue_release]
root           4  0.0  0.0      0     0 ?        I<   10:15   0:00 [kworker/R-rcu_g]
root           5  0.0  0.0      0     0 ?        I<   10:15   0:00 [kworker/R-rcu_p]
root           6  0.0  0.0      0     0 ?        I<   10:15   0:00 [kworker/R-slub_]

ps aux:显示进程。其中USER:进程的所有者,哪个用户运行的进程。PID:进程的进程号,1是系统的守护进程。%CPU:占用cpu的百分比。%MEM:占用内存的百分比。VSZ:虚拟内存的大小。RSS:常驻内存集,表示进程分配的内存大小。TTY:所属的终端。STAT:进程的状态。START:进程开始时间。TIME:该进程所占用的处理时间。COMMAND:执行的程序。

linux@ubuntu:~$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 10:15 ?        00:00:02 /sbin/init auto noprompt splash
root           2       0  0 10:15 ?        00:00:00 [kthreadd]
root           3       2  0 10:15 ?        00:00:00 [pool_workqueue_release]
root           4       2  0 10:15 ?        00:00:00 [kworker/R-rcu_g]
root           5       2  0 10:15 ?        00:00:00 [kworker/R-rcu_p]
root           6       2  0 10:15 ?        00:00:00 [kworker/R-slub_]
root           7       2  0 10:15 ?        00:00:00 [kworker/R-netns]

ps -ef:查看进程,显示进程号和父进程号。UID:用户名。PPID:父进程号。

linux@ubuntu:~$ ps -ef | grep bluetoothd
root         935       1  0 10:15 ?        00:00:00 /usr/lib/bluetooth/bluetoothd
linux       2294    2041  0 10:26 pts/0    00:00:00 grep --color=auto bluetoothd
linux@ubuntu:~$ sudo kill 935
[sudo] linux 的密码:
linux@ubuntu:~$ ps -ef | grep bluetoothd
linux       2304    2041  0 10:27 pts/0    00:00:00 grep --color=auto bluetoothd
linux@ubuntu:~$ kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX
linux@ubuntu:~$ kill -9 -1

kill:杀死指定的进程。kill -l:列出系统支持的所有信号。kill -9 -1:杀死系统所有的进程,系统会立即重新启动。

linux@ubuntu:~$ pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager───2*[{NetworkManager}]
        ├─VGAuthService
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─acpid
        ├─avahi-daemon───avahi-daemon
        ├─blkmapd
        ├─colord───2*[{colord}]
        ├─cron
linux@ubuntu:~$ pstree -a
systemd auto noprompt splash
  ├─ModemManager
  │   └─2*[{ModemManager}]
  ├─NetworkManager --no-daemon
  │   └─2*[{NetworkManager}]
  ├─VGAuthService
  ├─accounts-daemon
  │   └─2*[{accounts-daemon}]
  ├─acpid
  ├─avahi-daemon
  │   └─avahi-daemon
linux@ubuntu:~$ top
top - 10:33:46 up 18 min,  1 user,  load average: 0.00, 0.04, 0.17
任务: 333 total,   1 running, 332 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.5 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   3868.5 total,   1958.0 free,   1019.6 used,    890.9 buff/cache
MiB Swap:   3898.0 total,   3898.0 free,      0.0 used.   2567.9 avail Mem
 进程号 USER      PR  NI    VIRT    RES    SHR    %CPU  %MEM     TIME+ COMMAND
   1387 linux     20   0 4579740 284564 150788 S   3.3   7.2   0:19.24 gnome-shell
   1999 linux     20   0  799492  81584  63564 S   1.0   2.1   0:06.14 gnome-terminal-
     31 root      20   0       0      0      0 I   0.3   0.0   0:00.88 kworker/2:0-events
   2322 linux     20   0   16472   4224   3328 R   0.3   0.1   0:00.07 top
      1 root      20   0  167016  11632   8048 S   0.0   0.3   0:02.69 systemd

pstree:进程以树状图的形式显示出来。pstree -a:以树状图的形式显示进程的详细信息。top:任务管理器指令,类似于windows的任务管理器。fg:把在前台运行的任务切换到后台。bg:把在后台运行的任务切换到前台。

2.网络管理

        网络管理对于linux系统也是非常重要的,下面我们来看看与网络管理相关的shell命令:

linux@ubuntu:~$ ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.5.243  netmask 255.255.255.0  broadcast 192.168.5.255
        inet6 fe80::2ae1:f767:633a:328f  prefixlen 64  scopeid 0x20
        ether 00:50:56:31:2c:f0  txqueuelen 1000  (以太网)
        RX packets 348  bytes 145037 (145.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 337  bytes 49046 (49.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (本地环回)
        RX packets 140  bytes 13483 (13.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 140  bytes 13483 (13.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
linux@ubuntu:~$ ifconfig ens33
ens33: flags=4163  mtu 1500
        inet 192.168.5.243  netmask 255.255.255.0  broadcast 192.168.5.255
        inet6 fe80::2ae1:f767:633a:328f  prefixlen 64  scopeid 0x20
        ether 00:50:56:31:2c:f0  txqueuelen 1000  (以太网)
        RX packets 348  bytes 145037 (145.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 338  bytes 49135 (49.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
linux@ubuntu:~$ sudo ifconfig ens33 192.168.5.200 netmask 255.255.255.0
linux@ubuntu:~$ ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.5.200  netmask 255.255.255.0  broadcast 192.168.5.255
        inet6 fe80::2ae1:f767:633a:328f  prefixlen 64  scopeid 0x20
        ether 00:50:56:31:2c:f0  txqueuelen 1000  (以太网)
        RX packets 351  bytes 145554 (145.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 354  bytes 52347 (52.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (本地环回)
        RX packets 140  bytes 13483 (13.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 140  bytes 13483 (13.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ifconfig:用来查看和配置网络信息。ifconfig (网卡):用来查看指定网卡的信息。ifconfig (网卡) (ip)(子网掩码):用来配置网卡信息,在重启后会失效。

linux@ubuntu:~$ sudo ifconfig ens33 down
linux@ubuntu:~$ ifconfig ens33
ens33: flags=4098  mtu 1500
        ether 00:50:56:31:2c:f0  txqueuelen 1000  (以太网)
        RX packets 437  bytes 166488 (166.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 385  bytes 56182 (56.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
linux@ubuntu:~$ sudo ifconfig ens33 up
linux@ubuntu:~$ ifconfig ens33
ens33: flags=4163  mtu 1500
        inet 192.168.5.243  netmask 255.255.255.0  broadcast 192.168.5.255
        inet6 fe80::2ae1:f767:633a:328f  prefixlen 64  scopeid 0x20
        ether 00:50:56:31:2c:f0  txqueuelen 1000  (以太网)
        RX packets 449  bytes 168340 (168.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 434  bytes 63504 (63.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
linux@ubuntu:~$ route
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
default         _gateway        0.0.0.0         UG    100    0        0 ens33
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 ens33
192.168.5.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33
sudo route add -net 192.168.10.0 netmask 255.255.255.0 dev ens33
linux@ubuntu:~$ route
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
default         _gateway        0.0.0.0         UG    100    0        0 ens33
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 ens33
192.168.5.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 ens33
linux@ubuntu:~$ sudo route del -net 192.168.10.0 netmask 255.255.255.0 dev ens33
linux@ubuntu:~$ route
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
default         _gateway        0.0.0.0         UG    100    0        0 ens33
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 ens33
192.168.5.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33

ifconfig (网卡)down:关闭网卡。ifconfig (网卡)up:启动网卡。route:显示路由信息。route add:添加路由信息。route del:删除路由信息。

linux@ubuntu:~$ hostname
ubuntu
linux@ubuntu:~$ hostname -i
127.0.1.1
linux@ubuntu:~$ hostname ubuntu22.04
hostname: you must be root to change the host name
linux@ubuntu:~$ sudo hostname ubuntu22.04
linux@ubuntu:~$ hostname
ubuntu22.04
linux@ubuntu:~$ cat /etc/hostname
ubuntu

hostname:查看和修改主机名,使用该命令修改的主机名重启后失效,想要永久改变主机名要改变/etc/hostname中的名字。hostname -i :显示主机的ip地址。

linux@ubuntu:~$ netstat -a
激活Internet连接 (服务器和已建立连接的)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:33493           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:netbios-ssn     0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:nfs             0.0.0.0:*               LISTEN
linux@ubuntu:~$ netstat -nu
激活Internet连接 (w/o 服务器)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 192.168.5.243:68        192.168.5.191:67        ESTABLISHED
linux@ubuntu:~$ sudo netstat -apu
激活Internet连接 (服务器和已建立连接的)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
udp        0      0 0.0.0.0:53125           0.0.0.0:*                           1115/rpc.statd
udp        0      0 localhost:domain        0.0.0.0:*                           643/systemd-resolve
udp        0      0 ubuntu22.04:bootpc      _gateway:bootps         ESTABLISHED 939/NetworkManager
udp        0      0 0.0.0.0:tftp            0.0.0.0:*                           1194/xinetd
udp        0      0 0.0.0.0:41061           0.0.0.0:*                           933/avahi-daemon: r
linux@ubuntu:~$ netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33     1500      616      0      0 0           579      0      0      0 BMRU
lo       65536      177      0      0 0           177      0      0      0 LRU
linux@ubuntu:~$ netstat -r
内核 IP 路由表
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         _gateway        0.0.0.0         UG        0 0          0 ens33
link-local      0.0.0.0         255.255.0.0     U         0 0          0 ens33
192.168.5.0     0.0.0.0         255.255.255.0   U         0 0          0 ens33
linux@ubuntu:~$ netstat -ap | grep ssh
(并非所有进程都能被检测到,所有非本用户的进程信息将不会显示,如果想看到所有信息,则必须切换到 root 用户)
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      -
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      -
unix  2      [ ACC ]     流        LISTENING     19643    1190/systemd         /run/user/1000/gnupg/S.gpg-agent.ssh
unix  2      [ ACC ]     流        LISTENING     19775    1228/gnome-keyring-  /run/user/1000/keyring/ssh

netstat -a:显示系统网络中所有连接信息。netstat -nu:显示系统网络中所有udp连接信息。netstat -apu:显示系统网络中upd连接端口号使用信息。netstat -i:显示网卡当前信息。netstat -r:显示路由表状态信息。netstat -ap | grep ssh:找到某个服务器对应的连接信息。

linux@ubuntu:~$ ping www.baidu.com
PING www.a.shifen.com (183.2.172.17) 56(84) bytes of data.
64 bytes from 183.2.172.17 (183.2.172.17): icmp_seq=1 ttl=46 time=31.4 ms
64 bytes from 183.2.172.17 (183.2.172.17): icmp_seq=2 ttl=46 time=59.7 ms
64 bytes from 183.2.172.17 (183.2.172.17): icmp_seq=3 ttl=46 time=58.9 ms
64 bytes from 183.2.172.17 (183.2.172.17): icmp_seq=4 ttl=46 time=56.3 ms
linux@ubuntu:~$ ping -c 3 www.baidu.com
PING www.a.shifen.com (183.2.172.177) 56(84) bytes of data.
64 bytes from 183.2.172.177 (183.2.172.177): icmp_seq=1 ttl=46 time=27.1 ms
64 bytes from 183.2.172.177 (183.2.172.177): icmp_seq=2 ttl=46 time=25.6 ms
64 bytes from 183.2.172.177 (183.2.172.177): icmp_seq=3 ttl=46 time=55.4 ms
linux@ubuntu:~$ traceroute www.baidu.com
traceroute to www.a.shifen.com (183.2.172.177), 64 hops max
  1   192.168.5.191  29.474ms  2.298ms  2.363ms
  2   *  *  *
  3   *  *  *
  4   172.31.4.2  20.339ms  9.116ms  13.631ms
  5   172.31.4.18  10.079ms  8.679ms  6.655ms
  6   *  *  *

ping:用来测试主机间的连通性。ping -c:指定发送的数据包个数。traceroute:用来跟踪数据包在网络上传输的全部路径。

3.时间管理

        在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。所有Linux相关指令与函数都是读取系统时钟的设定。

linux@ubuntu:~$ date
2025年 12月 18日 星期四 11:31:22 CST
linux@ubuntu:~$ date "+%Y-%m-%d %H:%M:%S"
2025-12-18 11:32:13
linux@ubuntu:~$ date -s "2024-12-13 13:14:15"
date: 无法设置日期: 不允许的操作
2024年 12月 13日 星期五 13:14:15 CST
linux@ubuntu:~$ sudo date -s "2024-12-13 13:14:15"
[sudo] linux 的密码:
2024年 12月 13日 星期五 13:14:15 CST

date:用于显示时间。date -s:修改时间。

linux@ubuntu:~$ hwclock
hwclock: 无法通过已知的任何方式访问硬件时钟。
hwclock: Use the --verbose option to see the details of our search for an access method.
linux@ubuntu:~$ sudo hwclock
2025-12-18 11:36:33.990583+08:00
linux@ubuntu:~$ sudo hwclock --hctosys
linux@ubuntu:~$ date
2025年 12月 18日 星期四 11:37:10 CST
linux@ubuntu:~$ sudo hwclock --set --date="2024-12-13 12:00:00"
linux@ubuntu:~$ hwclock
hwclock: 无法通过已知的任何方式访问硬件时钟。
hwclock: Use the --verbose option to see the details of our search for an access method.
linux@ubuntu:~$ sudo hwclock
2024-12-13 12:00:14.995906+08:00
linux@ubuntu:~$ sudo hwclock --systohc
linux@ubuntu:~$ sudo hwclock
2025-12-18 11:39:20.998052+08:00

hwclock:查看硬件时钟信息。hwclock --hctosys:将硬件时钟信息同步到系统时钟。hwclock --set --date= :修改硬件时钟信息。hwclock --systohc:将系统时钟同步到硬件时钟。

linux@ubuntu:~$ tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa							     7) Europe
2) Americas							     8) Indian Ocean
3) Antarctica							     9) Pacific Ocean
linux@ubuntu:~$ sudo ntpdate ntp.tencent.com
18 Dec 11:44:21 ntpdate[2871]: adjust time server 106.55.184.199 offset +0.035655 sec

tzselcet:设置时区。ntpdate:从服务器中获取时间来设置本地的时间信息。

4.系统服务管理

        下面我们来看看关于系统服务管理的相关shell命令:

        首先对于系统的管理有:#重启系统sudo systemctl reboot,#关闭系统,切断电源sudo systemctl poweroff,#CPU停止工作sudo systemctl halt,#暂停系统sudo systemctl suspend,#让系统进入冬眠状态sudo systemctl hibernate,#让系统进入交互式休眠状态sudo systemctl hybrid-sleep,#启动进入救援状态(单用户状态)sudo systemctl rescue。

linux@ubuntu:~$ systemd-analyze
Startup finished in 8.267s (kernel) + 29.423s (userspace) = 37.691s
graphical.target reached after 28.760s in userspace
linux@ubuntu:~$ systemd-analyze blame
21.680s plymouth-quit-wait.service
 3.374s snapd.seeded.service
 3.178s snapd.service
 3.103s fwupd-refresh.service
 1.962s apt-daily.service
 1.705s dev-sda3.device
linux@ubuntu:~$ systemd-analyze critical-chain
The time when unit became active or started is printed after the "@" character.
The time the unit took to start is printed after the "+" character.
graphical.target @28.760s
└─multi-user.target @28.760s
  └─plymouth-quit-wait.service @7.079s +21.680s
    └─systemd-user-sessions.service @6.931s +75ms
      └─network.target @6.760s
        └─NetworkManager.service @6.214s +536ms
          └─dbus.service @6.208s
linux@ubuntu:~$ hostnamectl
 Static hostname: ubuntu
       Icon name: computer-vm
         Chassis: vm
      Machine ID: 83e9717b89f1408caa846de940a434f4
         Boot ID: 75b3f8c306ca4423863ba81b6b8f4284
  Virtualization: vmware
Operating System: Ubuntu 22.04.5 LTS
          Kernel: Linux 6.8.0-87-generic
    Architecture: x86-64
 Hardware Vendor: VMware, Inc.
  Hardware Model: VMware Virtual Platform
linux@ubuntu:~$ sudo hostnamectl set-hostname ys

systemd-analyze:显示系统启动时间。systemd-analyze blame:查看每个时间的启动耗时。systemd-analyze critical-chain:显示瀑布式的启动过程流。hostnamectl:显示主机信息。hostnamectl set-hostname:修改主机名。

linux@ubuntu:~$ localectl
   System Locale: LANG=zh_CN.UTF-8
                  LANGUAGE=zh_CN:en_US:en
       VC Keymap: n/a
      X11 Layout: cn
       X11 Model: pc105
linux@ubuntu:~$ timedatectl
               Local time: 四 2025-12-18 11:58:06 CST
           Universal time: 四 2025-12-18 03:58:06 UTC
                 RTC time: 四 2025-12-18 03:58:06
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
linux@ubuntu:~$ timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
linux@ubuntu:~$ loginctl list-sessions
SESSION  UID USER  SEAT  TTY
      1 1000 linux seat0 tty2
1 sessions listed.
linux@ubuntu:~$ loginctl list-users
 UID USER
1000 linux
1 users listed.
linux@ubuntu:~$ loginctl show-user linux
UID=1000
GID=1000
Name=linux
Timestamp=Thu 2025-12-18 10:16:00 CST
TimestampMonotonic=16087456
RuntimePath=/run/user/1000
Service=user@1000.service
Slice=user-1000.slice
Display=1
State=active
Sessions=1
IdleHint=no
IdleSinceHint=0
IdleSinceHintMonotonic=0
Linger=no

localectl:查看或修改本地化设置。timedatectl:查看和设置时区信息。loginctl list-sessions:列列出当前sessions。loginctl list-users:列出当前登录用户。loginctl show-user :显示指定用户信息。

        systemd可以管理所有的系统资源,不同的系统资源成为Unit(单位),Unit一共有下列12种。

Service unit:系统服务
Target unit:多个Unit构成的一个组
Device Unit:硬件设备
Mount Unit:文件系统的挂载点
Automount Unit:自动挂载点
Path Unit:文件或路径
Scope Unit:不是由Systemd启动的外部进程
Slice Unit:进程组
Snapshot Unit:Systemd快照,可以切回某个快照
Socket Unit:进程间通信的
socketSwap Unit:swap文件
Timer Unit:定时器

下面我们来看与Unit相关的命令:

linux@ubuntu:~$ systemctl list-units
  UNIT                                                                                             LOAD   ACTIVE SUB       DESCRIPTION                           >
  proc-sys-fs-binfmt_misc.automount                                                                loaded active running   Arbitrary Executable File Formats File>
  sys-devices-pci0000:00-0000:00:10.0-host5-target5:0:0-5:0:0:0-block-sda-sda1.device              loaded active plugged   VMware_Virtual_S 1
  sys-devices-pci0000:00-0000:00:10.0-host5-target5:0:0-5:0:0:0-block-sda-sda2.device              loaded active plugged   VMware_Virtual_S EFI\x20System\x20Part>
  sys-devices-pci0000:00-0000:00:10.0-host5-target5:0:0-5:0:0:0-block-sda-sda3.device              loaded active plugged   VMware_Virtual_S 3
linux@ubuntu:~$ systemctl list-units --all
  UNIT                                                                                             LOAD      ACTIVE   SUB       DESCRIPTION                      >
  proc-sys-fs-binfmt_misc.automount                                                                loaded    active   running   Arbitrary Executable File Formats>
  dev-cdrom.device                                                                                 loaded    active   plugged   VMware_Virtual_SATA_CDRW_Drive CD>
  dev-disk-by\x2did-ata\x2dVMware_Virtual_SATA_CDRW_Drive_00000000000000000001.device              loaded    active   plugged   VMware_Virtual_SATA_CDRW_Drive CD>
  dev-disk-by\x2dlabel-CDROM.device                                                                loaded    active   plugged   VMware_Virtual_SATA_CDRW_Drive CD>
  dev-disk-by\x2dpartlabel-EFI\x5cx20System\x5cx20Partition.device                                 loaded    active   plugged   VMware_Virtual_S EFI\x20System\x2>
  dev-disk-by\x2dpartuuid-0c199cf4\x2d7201\x2d4d32\x2d8d63\x2d3365b5ab0094.device                  loaded    active   plugged   VMware_Virtual_S 3
linux@ubuntu:~$ systemctl list-units --all --state=inactive
  UNIT                                                                                 LOAD      ACTIVE   SUB  DESCRIPTION                                       >
● home.mount                                                                           not-found inactive dead home.mount
● tmp.mount                                                                            not-found inactive dead tmp.mount                                         >
  systemd-ask-password-console.path                                                    loaded    inactive dead Dispatch Password Requests to Console Directory Wa>
  alsa-state.service                                                                   loaded    inactive dead Manage Sound Card State (restore and store)
  anacron.service                                                                      loaded    inactive dead Run anacron jobs
linux@ubuntu:~$ systemctl list-units --failed
  UNIT                      LOAD   ACTIVE SUB    DESCRIPTION
● apport-autoreport.service loaded failed failed Process error reports when automatic reporting is enabled
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
1 loaded units listed.
linux@ubuntu:~$ systemctl list-units --type=service
  UNIT                                                  LOAD   ACTIVE SUB     DESCRIPTION
  accounts-daemon.service                               loaded active running Accounts Service
  acpid.service                                         loaded active running ACPI event daemon
  alsa-restore.service                                  loaded active exited  Save/Restore Sound Card State
  apparmor.service                                      loaded active exited  Load AppArmor profiles

systemctl list-units:列出正在运行的unit。systemctl list-units --all:列出所有的units。systemctl list-units --all --state=inactive:列出所有没有运行的unit。systemctl list-units --failed:列出所有加载失败的unit。systemctl list-units --type=service:列出所有正在运行且类型为service的unit。

linux@ubuntu:~$ systemctl status
● ubuntu
    State: degraded
     Jobs: 0 queued
   Failed: 1 units
    Since: Thu 2025-12-18 10:15:52 CST; 1h 59min ago
linux@ubuntu:~$ systemctl status bluetooth.service
○ bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Thu 2025-12-18 10:27:35 CST; 1h 48min ago
       Docs: man:bluetoothd(8)
    Process: 935 ExecStart=/usr/lib/bluetooth/bluetoothd (code=exited, status=0/SUCCESS)
linux@ubuntu:~$ systemctl start bluetooth.service
linux@ubuntu:~$ systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2025-12-18 12:16:47 CST; 4s ago
       Docs: man:bluetoothd(8)
   Main PID: 3091 (bluetoothd)
linux@ubuntu:~$ systemctl stop bluetooth.service
linux@ubuntu:~$ systemctl status bluetooth.service
○ bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Thu 2025-12-18 12:17:45 CST; 4s ago
       Docs: man:bluetoothd(8)
    Process: 3091 ExecStart=/usr/lib/bluetooth/bluetoothd (code=exited, status=0/SUCCESS)
   Main PID: 3091 (code=exited, status=0/SUCCESS)
     Status: "Powering down"
        CPU: 116ms
12月 18 12:16:47 ubuntu bluetoothd[3091]: Bluetooth daemon 5.64
12月 18 12:16:47 ubuntu bluetoothd[3091]: Starting SDP server
12月 18 12:16:47 ubuntu systemd[1]: Started Bluetooth service.
12月 18 12:16:47 ubuntu bluetoothd[3091]: Bluetooth management interface 1.22 initialized
12月 18 12:17:45 ubuntu systemd[1]: Stopping Bluetooth service...
12月 18 12:17:45 ubuntu bluetoothd[3091]: Terminating
12月 18 12:17:45 ubuntu bluetoothd[3091]: Stopping SDP server
12月 18 12:17:45 ubuntu bluetoothd[3091]: Exit
12月 18 12:17:45 ubuntu systemd[1]: bluetooth.service: Deactivated successfully.
12月 18 12:17:45 ubuntu systemd[1]: Stopped Bluetooth service.
linux@ubuntu:~$ systemctl restart bluetooth.service
linux@ubuntu:~$ systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2025-12-18 12:18:22 CST; 3s ago

systemctl status:显示系统状态。systemctl status bluetooth.service:显示单个unit状态。systemctl start bluetooth.service:启动指定服务。systemctl stop bluetooth.service:停止指定服务。systemctl restart bluetooth.service:重启指定服务。

posted @ 2026-01-13 16:54  yangykaifa  阅读(5)  评论(0)    收藏  举报