Chapter 9: 服务
Section 9.1: 列出在Ubuntu上运行的服务
要获取系统上的服务列表,你可以运行service --status-all命令,运行的结果如下:
demouser@demohosts:~$ service --status-all
[ + ] alsa-utils
[ - ] anacron
[ + ] apparmor
[ + ] apport
[ + ] atop
[ + ] atopacct
[ - ] bluetooth
[ - ] console-setup.sh
[ + ] cron
[ + ] cups
[ + ] dbus
[ + ] docker
[ + ] gdm3
[ - ] grub-common
[ + ] kerneloops
[ - ] keyboard-setup.sh
[ + ] kmod
[ + ] openvpn
[ - ] plymouth
[ + ] plymouth-log
[ + ] procps
[ - ] rsync
[ - ] saned
[ - ] speech-dispatcher
[ - ] spice-vdagent
[ + ] ssh
[ - ] sssd
[ + ] sysstat
[ - ] ubuntu-fan
[ + ] ufw
[ + ] unattended-upgrades
[ - ] uuidd
[ - ] whoopsie
[ - ] x11-common
service --status-all的输出结果列出了 System V 所控制的服务状态。
“+”表示服务正在运行,“-”表示服务已停止。您可以通过service SERVICENAME status 来查看服务的状态,SERVICENAME 为 [ + ] 或者 [ - ] 后面的服务名称。
最近几年的发行版本基本放弃了 system v 改为使用systemd。
Section 9.2: Systemd 服务管理
列出正在运行的服务
systemctl 列出正在运行的服务
demouser@demohosts:~$ systemctl
UNIT LOA>
proc-sys-fs-binfmt_misc.automount loa>
sys-devices-pci0000:00-0000:00:10.0-host32-target32:0:0-32:0:0:0-block-sda-sda1.device loa>
sys-devices-pci0000:00-0000:00:10.0-host32-target32:0:0-32:0:0:0-block-sda-sda2.device loa>
…… 省略 ……
update-notifier-download.timer loa>
update-notifier-motd.timer loa>
Legend: 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.
227 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
systemctl --failed 列出运行失败的服务
demouser@demohosts:~$ systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
0 loaded units listed.
管理目标(类似于System V中的运行级)
systemctl get-default 获取系统的默认目标
demouser@demohosts:~$ systemctl get-default
graphical.target
systemctl set-default <target-name> 设置系统的默认目标,运行级别常用为:mulit-user.target 多用户有网络和 graphical.target图形化界面。
demouser@demohosts:~$ systemctl set-default graphical.target
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ====
管理系统服务或单元文件需要认证。
Authenticating as: demouser
Password:
==== AUTHENTICATION COMPLETE ====
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/graphical.target.
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ====
Authentication is required to reload the systemd state.
Authenticating as: demouser
Password:
==== AUTHENTICATION COMPLETE ====
运行时(runtime)管理服务
systemctl start [service-name] 启动一个服务
demouser@demohosts:~$ systemctl start docker
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
启动“docker.service”需要认证。
Authenticating as: demouser
Password:
==== AUTHENTICATION COMPLETE ====
systemctl stop [service-name] 停止一个服务
demouser@demohosts:~$ systemctl stop docker
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
停止“docker.service”需要认证。
Authenticating as: demouser
Password:
==== AUTHENTICATION COMPLETE ====
Stopping 'docker.service', but its triggering units are still active:
docker.socket
systemctl restart [service-name] 重启一个服务
demouser@demohosts:~$ systemctl restart docker
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
重新启动“docker.service”需要认证。
Authenticating as: demouser
Password:
==== AUTHENTICATION COMPLETE ====
demouser@demohosts:~$ systemctl restart docker
systemctl reload [service-name] 请求服务重新加载其配置,在配置文件有修改的时候使用
demouser@demohosts:~$ systemctl reload docker
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
重新载入“docker.service”需要认证。
Authenticating as: demouser
Password:
==== AUTHENTICATION COMPLETE ====
systemctl status [service-name] 显示服务的当前状态
demouser@demohosts:~$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; preset: enabled)
Active: active (running) since Tue 2024-11-05 15:15:06 CST; 5s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 10312 (dockerd)
Tasks: 10
Memory: 21.9M (peak: 23.3M)
CPU: 702ms
CGroup: /system.slice/docker.service
└─10312 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
11月 05 15:15:05 demohosts dockerd[10312]: time="2024-11-05T15:15:05.368545637+08:00" level=info msg=">
11月 05 15:15:06 demohosts dockerd[10312]: time="2024-11-05T15:15:06.002243724+08:00" level=info msg=">
11月 05 15:15:06 demohosts dockerd[10312]: time="2024-11-05T15:15:06.209206517+08:00" level=warning ms>
11月 05 15:15:06 demohosts dockerd[10312]: time="2024-11-05T15:15:06.209337426+08:00" level=info msg=">
11月 05 15:15:06 demohosts dockerd[10312]: time="2024-11-05T15:15:06.233515356+08:00" level=warning ms>
11月 05 15:15:06 demohosts dockerd[10312]: time="2024-11-05T15:15:06.233547627+08:00" level=warning ms>
11月 05 15:15:06 demohosts dockerd[10312]: time="2024-11-05T15:15:06.233572887+08:00" level=info msg=">
11月 05 15:15:06 demohosts dockerd[10312]: time="2024-11-05T15:15:06.233644909+08:00" level=info msg=">
11月 05 15:15:06 demohosts dockerd[10312]: time="2024-11-05T15:15:06.282569011+08:00" level=info msg=">
11月 05 15:15:06 demohosts systemd[1]: Started docker.service - Docker Application Container Engine.
管理服务的自动启动
systemctl is-enabled [service-name] 显示指定的服务当前是否设置为系统启动时自启动:
- enabled :自启动状态
- disabled :非自启动状态
demouser@demohosts:~$ systemctl is-enabled docker
enabled
systemctl is-active [service-name] 显示指定的服务当前是否活动(正在运行)
- active
- inactive
demouser@demohosts:~$ systemctl is-active docker
active
systemctl enable [service-name] 启用该服务在系统启动时自启动
demouser@demohosts:~$ sudo systemctl enable docker
Synchronizing state of docker.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
systemctl disable [service-name] 在系统启动时禁用该服务自启动
demouser@demohosts:~$ sudo systemctl disable docker
Synchronizing state of docker.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install disable docker
Removed "/etc/systemd/system/multi-user.target.wants/docker.service".
Disabling 'docker.service', but its triggering units are still active:
docker.socket
屏蔽/注销服务
systemctl mask [service-name] 屏蔽/注销服务(被屏蔽/注销的服务无法使用start、stop、anable等操作)
demouser@demohosts:~$ sudo systemctl mask docker
Created symlink /etc/systemd/system/docker.service → /dev/null.
systemctl unmask [service-name] 取消服务的屏蔽/注销
demouser@demohosts:~$ sudo systemctl unmask docker
Removed "/etc/systemd/system/docker.service".
重启systemd
systemctl daemon-reload
demouser@demohosts:~$ systemctl daemon-reload
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ====
Authentication is required to reload the systemd state.
Authenticating as: demouser
Password:
==== AUTHENTICATION COMPLETE ====