单位类型 | 文件扩展 | 描述 |
---|---|---|
服务单元 | .service | 系统服务。 |
目标单元 | .target | 一组 systemd 单元。 |
Automount 单元 | .automount | 文件系统自动挂载点。 |
设备单元 | .device | 内核可识别的设备文件。 |
挂载单位 | .mount | 文件系统挂载点。 |
路径单元 | .path | 文件系统中的一个文件或者目录。 |
Scope 单元 | .scope | 外部创建的进程。 |
Slice 单元 | .slice | 一组管理系统进程的分层组织单元。 |
快照单元 | .snapshot | 已保存的 systemd 管理器状态。 |
套接字单元 | .socket | 进程间的通信套接字。 |
Swap 单元 | .swap | 一个交换设备或者一个交换文件。 |
计时器单元 | .timer | systemd 计时器。 |
systemd 单元文件位置
目录 | 描述 |
---|---|
/usr/lib/systemd/system/ | 安装的 RPM 软件包中的 systemd 单元文件。 |
/run/systemd/system/ | 在运行时创建的 systemd 单元文件。该目录优先于安装了的服务单元文件的目录。 |
/etc/systemd/system/ | Systemd 单元文件由 systemctl enable 命令创建,并添加用于扩展服务的单元文件。这个目录优先于带有运行时单元文件的目录。 |
使用 system.conf 覆盖默认 systemd 配置
默认 systemd 配置是在编译过程中定义的,可以在 /etc/systemd/system.conf
中的 systemd 配置文件中找到。如果您想与那些默认值分离,并全局覆盖所选的 systemd 单元默认值,请使用这个文件。
例如,若要覆盖设为 90 秒的超时限制的默认值,可使用 DefaultTimeoutStartSec
参数输入所需的值(以秒为单位)。
DefaultTimeoutStartSec=required value
Masked 的作用
masked
用于阻止某服务启动,通常用于解决服务冲突问题。
服务实用程序与 systemctl 的比较
service | systemctl | 描述 |
---|---|---|
chkconfig --level 3 httpd on | systemctl enable httpd.service | 使某服务自动启动 |
chkconfig --level 3 httpd off | systemctl disable httpd.service | 使某服务不自动启动 |
service name start | systemctl start name.service | 启动一个服务。 |
service name stop | systemctl stop name.service | 停止服务。 |
service name restart | systemctl restart name.service | 重启服务。 |
service name condrestart | systemctl try-restart name.service | 仅在运行时重启服务。 |
service name reload | systemctl reload name.service | 重新加载配置。 |
service name status | systemctl status name.service systemctl is-active name.service | 检查服务是否在运行。 |
service --status-all | systemctl list-units --type service --all | 显示所有服务的状态。 |
systemctl try-restart httpd.service | 只会在服务已经运行的状态下重启服务 |
sysvinit 命令 | systemd 命令 | 备 注 |
---|---|---|
service httpd start | systemctl start httpd.service | 启动httpd服务 |
service httpd stop | systemctl stop httpd.service | 关闭httpd服务 |
service httpdrestart | systemctl restart httpd.service | 重启httpd服务,而不管httpd服务当前是否启动或关闭状态 |
service httpdreload | systemctl reload httpd.service | 重新载入httpd配置信息而不中断服务 |
service httpdcondrestart | systemctl condrestart httpd.service | 也表示重启httpd服务,但是condrestart选项会检查服务当前是否正在运行,如果已在运行,那么它可以重启 这个服务,但是如果服务没有运行,condrestart是无法启动这个服务的 |
service httpd status | systemctl status httpd.service | 查看httpd服务的运行状态 |
chkconfig httpd on | systemctl enable httpd.service | 设置httpd服务开机自启动 |
chkconfig httpd off | systemctl disable httpd.service | 禁止httpd服务开机自启动 |
chkconfig httpd | systemctl is-enabled httpd.service | 检查httpd服务在当前环境下是启用还是禁用 |
chkconfig--list | systemctl list-unit-files --type=service | 输出在各个运行级别下所有服务的启用和禁用情况 |
chkconfig httpd --list | Is /etc/systemd/system/*.wants/httpd.service | 查看httpd服务在各个运行级别下的启用和禁用情况 |
chkconfig httpd --add | systemctl daemon-reload | 创建一个新服务文件或者变更配置时使用 |
chkconfig 实用程序与 systemctl 的比较
chkconfig | systemctl | 描述 |
---|---|---|
chkconfig name on | systemctl enable name.service | 启用服务。 |
chkconfig name off | systemctl disable name.service | 禁用服务。 |
chkconfig --list name | systemctl status name.service systemctl is-enabled name.service | 检查是否启用了服务。 |
chkconfig --list | systemctl list-unit-files --type service | 列出所有服务并检查是否启用它们。 |
chkconfig --list | systemctl list-dependencies --after | 列出在指定单元前排序启动的服务。 |
chkconfig --list | systemctl list-dependencies --before | 列出在指定单元之后排序启动的服务。 |
指定服务单元
systemctl命令 | 含 义 |
---|---|
systemctl poweroff | 关闭系统 |
systemctl reboot | 重启系统 |
systemctl suspend | 进入待机模式 |
systemctl hibernate | 进入休眠模式 |
systemctl hybrid-sleep | 进入混合休眠模式 |
为清楚起见,本节其余部分中的所有命令示例都使用带有 .service
文件扩展名的完整单元名称,例如:
[root@localhost ~]# systemctl stop nfs-server.service
但是,可以省略文件扩展名,在这种情况下,system
ctl 实用程序假定参数是服务单元。以下命令等同于以上命令:
[root@localhost ~]# systemctl stop nfs-server
此外,某些单元具有别名名称。这些名称的名称比单元短,可以使用它们,而不是实际的单元名称。要查找可用于特定单元的所有别名,请使用:
[root@localhost ~]# systemctl show nfs-server.service -p Names
systemctl 在 chroot 环境中的行为
如果您使用 chroot
命令更改根目录,大多数 systemctl
命令会拒绝执行任何操作。原因在于 systemd
进程和使用 chroot 命令的用户对
文件系统没有相同的视图。当从 kickstart
文件中调用 systemctl
时会出现这种情况。
一个例外是单元文件命令,如 systemctl enable
和 systemctl disable
命令。这些命令不需要运行中的系统,也不会影响正在运行的进程,但它们确实会影响单元文件。因此,即使在 chroot
环境中也可以运行这些命令。例如:要在 /srv/website1/
目录下的系统中启用 httpd
服务:
[root@localhost ~]# chroot /srv/website1
[root@localhost ~]# systemctl enable httpd.service
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service, pointing to /usr/lib/systemd/system/httpd.service.
要列出所有目前载入的服务单元
systemctl list-units --type service
[root@localhost ~]# systemctl list-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
dracut-shutdown.service loaded active exited Restore /run/initramfs on shutdown
getty@tty1.service loaded active running Getty on tty1
import-state.service loaded active exited Import network configuration from initramfs
irqbalance.service loaded active running irqbalance daemon
kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel
ldconfig.service loaded active exited Rebuild Dynamic Linker Cache
lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling
lvm2-pvscan@8:3.service loaded active exited LVM event activation on device 8:3
NetworkManager-wait-online.service loaded active exited Network Manager Wait Online
NetworkManager.service loaded active running Network Manager
nis-domainname.service loaded active exited Read and set NIS domainname from /etc/sysconfig/network
plymouth-quit-wait.service loaded active exited Hold until boot process finishes up
plymouth-quit.service loaded active exited Terminate Plymouth Boot Screen
plymouth-read-write.service loaded active exited Tell Plymouth To Write Out Runtime Data
plymouth-start.service loaded active exited Show Plymouth Boot Screen
polkit.service loaded active running Authorization Manager
rsyslog.service loaded active running System Logging Service
sshd.service loaded active running OpenSSH server daemon
sssd.service loaded active running System Security Services Daemon
systemd-fsck@dev-disk-by\x2duuid-08D7\x2dF291.service loaded active exited File System Check on /dev/disk/by-uuid/08D7-F291
UNIT:服务单元的名称
LOAD:systemd是否正确解析了单元的配置,并将该单元加载到內存中
ACTIVE:单元的高级别激活状态,此信息表明单元是否已成功启动
SUB:单元的低级别激活状态,信息视单元类型、状态以及单元的执行方式而异。
DESCRIPTION:单元的简单描述
对于每个服务单元文件,此命令会显示其全名(UNIT
),后跟一个备注,该单元文件是否已加载(LOAD
)、其高级(ACTIVE)
和低级(SU
B)单元文件激活状态,以及简短描述(DESCRIPTION
)。
默认情况下,systemctl list-units
命令只显示活跃的单位。如果您想列出所有载入的单元,无论它们的状态如何,请使用 --all
或 -a
选项。
[root@localhost ~]# systemctl list-units --type service --all
您还可以列出所有可用服务单元以查看是否启用了它们。要做到这一点,请键入:
[root@localhost ~]# systemctl list-unit-files --type service
列出所有目前载入的服务单元,请运行以下命令:
[root@localhost ~]# systemctl list-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-vmcore.service loaded active exited Harvest vmcores for ABRT
abrt-xorg.service loaded active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
...
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
tog-pegasus.service loaded active running OpenPegasus CIM Server
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.
46 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'
UNIT:服务单元的名称
LOAD:systemd是否正确解析了单元的配置,并将该单元加载到內存中
ACTIVE:单元的高级别激活状态,此信息表明单元是否已成功启动
SUB:单元的低级别激活状态,信息视单元类型、状态以及单元的执行方式而异。
DESCRIPTION:单元的简单描述
列出所有安装的服务单元文件以确定它们是否启用,请输入:
[root@localhost ~]# systemctl list-unit-files --type service
UNIT FILE STATE
abrt-ccpp.service enabled
abrt-oops.service enabled
abrt-vmcore.service enabled
abrt-xorg.service enabled
abrtd.service enabled
...
wpa_supplicant.service disabled
ypbind.service disabled
208 unit files listed.
STATE的类型
enabled:系统引导时启动
disabled:未设置为系统引导时启动
static:无法启动,但是可以由其他启动的单元引导启动
masked:无法启动,也无法被其他单元引导启动
显示与系统服务对应的服务单元的详细信息,请在 shell 提示符后输入以下内容:
[root@localhost ~]# systemctl status name.service
可用服务单元信息
项 | 描述 |
---|---|
Loaded | 是否载入了服务单元、到这个单元文件的绝对路径,以及是否启用该单位的信息。 |
Active | 服务单元是否在运行的信息,后面有一个时间戳。 |
Main PID | 对应系统服务的 PID 及其名称。 |
Status | 相关系统服务的额外信息。 |
Process | 有关相关进程的附加信息。 |
CGroup | 有关相关控制组群(cgroups)的附加信息。 |
要只验证某个服务单元是否正在运行,运行以下命令:
[root@localhost ~]# systemctl is-active name.service
要确定某个服务单元是否启用,运行:
[root@localhost ~]# systemctl is-enabled name.service
请注意,如果指定的服务单元正在运行或已启用,则 systemctl is-active
和 systemctl is-enabled
的返回退出状态为 0
。
GNOME 显示管理器的服务单元名为 gdm.service
。要确定这个服务单元的当前状态,在 shell 提示下键入以下内容:
[root@localhost ~]# systemctl status gdm.service
gdm.service - GNOME Display Manager
Loaded: loaded (/usr/lib/systemd/system/gdm.service; enabled)
Active: active (running) since Thu 2013-10-17 17:31:23 CEST; 5min ago
Main PID: 1029 (gdm)
CGroup: /system.slice/gdm.service
├─1029 /usr/sbin/gdm
├─1037 /usr/libexec/gdm-simple-slave --display-id /org/gno...
└─1047 /usr/bin/Xorg :0 -background none -verbose -auth /r...
Oct 17 17:31:23 localhost systemd[1]: Started GNOME Display Manager.
在服务前按顺序显示服务
要确定在指定服务前调度什么服务启动,在 shell 提示下键入以下内容:
[root@localhost ~]# systemctl list-dependencies --after gdm.service
gdm.service
├─dbus.socket
├─getty@tty1.service
├─livesys.service
├─plymouth-quit.service
├─system.slice
├─systemd-journald.socket
├─systemd-user-sessions.service
└─basic.target
[output truncated]
在服务后显示被启动的服务
要确定在指定服务后调度的服务启动,在 shell 提示符后输入以下内容:
[root@localhost ~]# systemctl list-dependencies --before gdm.service
gdm.service
├─dracut-shutdown.service
├─graphical.target
│ ├─systemd-readahead-done.service
│ ├─systemd-readahead-done.timer
│ └─systemd-update-utmp-runlevel.service
└─shutdown.target
├─systemd-reboot.service
└─final.target
└─systemd-reboot.service
要启动与系统服务对应的服务单元,以 root
用户身份在 shell 提示符后输入以下内容:
[root@localhost ~]# systemctl start name.service
使用您要启动的服务单元的名称替换 name(例如: gdm
)。这个命令会在当前会话中启动所选服务单元。
启动服务
Apache HTTP 服务器的服务单元名为 httpd.service
。要激活这个服务单元并在当前会话中启动 httpd
守护进程,以 root
用户身份运行以下命令:
[root@localhost ~]# systemctl start httpd.service
要停止与系统服务对应的服务单元,以 root
用户身份在 shell 提示符后输入以下内容:
[root@localhost ~]# systemctl stop name.service
使用您要停止的服务单元的名称替换 name(例如: bluetooth
)。该命令将在当前会话中停止所选服务单元。
停止服务
bluetoothd
守护进程的服务单元名为 bluetooth.service
。要取消激活这个服务单元并在当前会话中停止 bluetoothd
守护进程,以 root
用户身份运行以下命令:
[root@localhost ~]# systemctl stop bluetooth.service
重启服务
要重启与系统服务对应的服务单元,以 root
用户身份在 shell 提示符后输入以下内容:
[root@localhost ~]# systemctl restart name.service
使用您要重启的服务单元的名称替换 name(例如 httpd
)。这个命令可在当前会话中停止所选服务单元,并立即重新启动。最重要的是,如果所选服务单元没有运行,这个命令也会启动它。要让 systemd 仅在相应服务已在运行时重启服务单元,以 root
用户身份运行以下命令:
[root@localhost ~]# systemctl try-restart name.service
某些系统服务还允许您在不中断其执行的情况下重新载入其配置。要做到这一点,以 root
用户身份输入:
[root@localhost ~]# systemctl reload name.service
[root@localhost ~]# systemctl reload-or-restart name.service
为了防止用户遇到不必要的错误消息或部分呈现的 Web 页面,Apache HTTP 服务器允许您编辑和重新加载其配置,而无需重新启动它并中断主动处理的请求。要做到这一点,以 root
根用户身份在 shell 提示符后输入以下内容:
[root@localhost ~]# systemctl reload httpd.service
启用服务
要配置与系统服务对应的服务单元,在引导时自动启动,以 root
用户身份在 shell 提示符后输入以下内容:
[root@localhost ~]# systemctl enable name.service
使用您要启用的服务单元的名称替换 name(例如 httpd
)。该命令读取所选服务单元的 [Install]
部分,并在 /etc/systemd/system/
目录和其子目录中创建到 /usr/lib/systemd/system/name.service
的符号链接。但是,这个命令不会重写已经存在的链接。如果要确保重新创建符号链接,以 root
用户身份使用以下命令:
[root@localhost ~]# systemctl reenable name.service
该命令禁用所选服务单元,并立即再次启用。
启用服务
要将 Apache HTTP 服务器配置为在引导时自动启动,以 root
用户身份运行以下命令:
[root@localhost ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
要防止与系统服务对应的服务单元在引导时自动启动,以 root
用户身份在 shell 提示符后输入以下内容:
[root@localhost ~]# systemctl disable name.service
使用您要禁用的服务单元的名称替换 name(例如: bluetooth
)。该命令读取所选服务单元的 [Install]
部分,并从 /etc/systemd/system/
目录及其子目录中删除到 /usr/lib/systemd/system/name.service
文件的符号链接。另外,您可以屏蔽所有服务单元,以防止手动启动或者由其他服务启动。要做到这一点,以 root
运行以下命令:
[root@localhost ~]# systemctl mask name.service
这个命令将 /etc/systemd/system/name.service
文件替换为 /dev/null
的符号链接,从而导致 systemd 无法访问实际的单元文件。要恢复这个动作并取消掩码一个服务单元,以 root
用户身份输入:
[root@localhost ~]# systemctl unmask name.service
禁用服务
演示如何在当前会话中停止 bluetooth.service
单元。要防止这个服务单元在引导时启动,以 root
用户身份在 shell 提示符后输入以下内容:
[root@localhost ~]# systemctl disable bluetooth.service
Removed symlink /etc/systemd/system/bluetooth.target.wants/bluetooth.service.
Removed symlink /etc/systemd/system/dbus-org.bluez.service.
启动冲突服务
在 Systemd 中,不同服务间会存在正或负的依赖关系。启动特定的服务可能需要启动一个或多个其他服务(正向依赖项)或者停止一个或多个服务(负依赖项)。
当您试图启动新服务时,systemd 会自动解析所有依赖项。请注意,这是在没有向用户发出显式通知的情况下完成的。如果您已经运行了服务,且您试图使用负依赖项启动另一个服务,则第一个服务会自动停止。
例如,如果您运行 postfix
服务,并且尝试启动 sendmail
服务,systemd 首先会自动停止 postfix
,因为这两个服务彼此冲突且无法在同一个端口上运行。
查看默认启动目标
[root@localhost ~]# systemctl get-default
查看系统上可用的所有启动目标
[root@localhost ~]# systemctl list-units --type=target
常见的启动目标有:
multi-user.target
:多用户模式,类似于传统的运行级别3,没有图形界面。graphical.target
:图形模式,类似于传统的运行级别5,带图形界面。rescue.target
:救援模式,类似于单用户模式。emergency.target
:紧急模式,最小化的系统环境。
3. 设置默认启动目标
要更改系统的默认启动目标,可以使用以下命令:
[root@localhost ~]# systemctl set-default multi-user.target
或者,如果你想使用图形界面启动:
[root@localhost ~]# systemctl set-default graphical.target
4. 立即切换启动目标
如果你希望立即切换到另一启动目标(例如从命令行切换到图形界面),可以使用以下命令:
[root@localhost ~]# systemctl isolate graphical.target
或者切换到命令行模式:
[root@localhost ~]# systemctl isolate multi-user.target
这样就可以在CentOS 8中设置和更改启动目标了。
通过GRUB引导菜单临时指定启动目标
- 启动系统并进入GRUB菜单:
- 在系统启动时,按下
Esc
或Shift
键进入GRUB引导菜单。
- 在系统启动时,按下
- 选择内核版本:
- 在GRUB菜单中,选择你想要启动的内核版本,但不要按回车键。
- 编辑启动命令:
- 按下
e
键进入编辑模式。
- 按下
- 添加启动目标:
- 在编辑界面中,找到以
linux
或linuxefi
开头的行。通常这一行会包含root=
参数。 - 在这一行的末尾,添加你想要的启动目标。例如:
systemd.unit=multi-user.target
:进入多用户模式(无图形界面)。systemd.unit=graphical.target
:进入图形界面模式。systemd.unit=rescue.target
:进入救援模式。systemd.unit=emergency.target
:进入紧急模式。
linux /vmlinuz-4.18.0-240.el8.x86_64 root=/dev/mapper/cl-root ro crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet systemd.unit=multi-user.target
- 在编辑界面中,找到以
- 启动系统:
- 编辑完成后,按下
Ctrl + X
或F10
来启动系统。
- 编辑完成后,按下
Comments NOTHING