启用和禁用服务

在早期版本的 RHEL 中,chkconfig 实用程序用于启用和禁用服务。
在 RHEL 7 中,systemctl 实用程序提供了一组等效的子命令。
下表显示了 chkconfig 实用程序与 systemctl 的比较。

chkconfig 实用程序systemctl 实用程序描述
chkconfig name onsystemctl enable name启用服务
chkconfig name offsystemctl disable name禁用服务
chkconfig -list namesystemctl status name, systemctl is-enabled name检查服务是否已启用
chkconfig -listsystemctl list-unit-files -type service列出所有服务并检查它们是否已启用
CentOS/RHEL 7:systemd 服务单元

以前版本的 Oracle Linux 使用位于 /etc/rc.d/init 目录中的 init 脚本来启动和停止服务。
在 RHEL 7 中,这些 init 脚本已替换为 systemd 服务单元。
服务单位有一个 .service 扩展名。
使用 systemctl 命令列出所有加载的服务单元:

# systemctl list-units --type service --all
UNIT                                 LOAD   ACTIVE   SUB     DESCRIPTION
auditd.service                       loaded active   running Security Auditing Service
avahi-daemon.service                 loaded active   running Avahi mDNS/DNS-SD Stack
.....

其中:
LOAD 服务负载状态
高级 (ACTIVE) 和低级 (SUB) 单元激活状态
DESCRIPTION 服务单元的描述。

省略 -all 选项以仅列出活动服务单元。
使用 list-unit-files 选项查看启用了哪些服务单元:

# systemctl list-unit-files --type service
欢迎来到之路教程(on itroad-com)

启动和停止服务

在以前的 RHEL 版本中,服务实用程序用于停止和启动服务。
在 RHEL 7 中,systemctl 实用程序提供了一组等效的子命令。
下表显示了 service 实用程序与 systemctl 的比较。

service 实用程序systemctl 实用程序描述
service name startsystemctl start name启动服务
service name stopsystemctl stop name停止服务
service name restartsystemctl restart name重新启动服务
service name condrestartsystemctl try- restart name仅当服务正在运行时才重新启动服务
service name reloadsystemctl reload name重新加载配置
service name statussystemctl status name检查服务是否正在运行
service -status- allsystemctl list-units -type service -all显示所有服务的状态

显示服务状态

systemd 服务单元对应系统服务。
使用以下命令显示有关服务单元的详细信息。
此示例显示有关 sshd 服务单元的信息。

# systemctl status sshd

以下信息可用于指定的服务单位:

Loaded:如果已加载服务,则为服务单元文件的绝对路径,如果
服务单元已启用
Active:如果服务单元正在运行并且有时间戳
主PID:对应系统服务的进程ID和服务名称
Status:有关相应系统服务的其他信息
Process:有关相关进程的其他信息
CGroup:有关相关控制组的其他信息

要检查服务是否正在运行(活动)或者未运行(非活动):

# systemctl is-active sshd
active

要检查服务是否已启用:

# systemctl is-enabled sshd
enabled
日期:2020-09-17 00:12:47 来源:oir作者:oir