Linux

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > Linux > Linux服务器Systemctl命令

Linux服务器Systemctl命令完全使用指南

作者:岚叔运维

这篇文章主要介绍了Linux服务器Systemctl命令完全使用指南,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

以下是 systemctl 使用指南,涵盖服务管理、单元操作、运行级别控制、电源管理及常用示例,帮助您高效管理 Linux 系统服务和进程。

1. 基本语法

systemctl    [选项...]   命令   [服务名|.target|.mount等]

2. 基础命令速查表

命令作用示例

systemctl start <服务名>

启动指定服务

systemctl start nginx

systemctl stop <服务名>

停止指定服务

systemctl stop nginx

systemctl restart <服务名>

重启服务(先停止再启动)

systemctl restart nginx

systemctl reload <服务名>

重新加载配置(不中断服务,更轻量)

systemctl reload nginx

systemctl try-restart <服务名>

仅当服务运行时才重启,否则不操作

systemctl try-restart mysql

systemctl   status  <服务名>

查看服务详细状态

systemctl  status  nginx

systemctl enable <服务名>

配置服务开机自启

systemctl enable mysql

systemctl disable <服务名>

禁用开机自启(仍可手动启动)

systemctl disable mysql

systemctl enable --now <服务名>

立即启动并配置开机自启(合并操作)

systemctl enable --now redis

systemctl   is-active  <服务名>仅检查服务是否活跃systemctl is-active nginx
systemctl  is-enabled <服务名>检查服务是否开机自启systemctl is-enabled nginx

systemctl mask <服务名>

彻底禁用服务(禁止手动 / 自动启动)

systemctl mask cups

systemctl unmask <服务名>

取消彻底禁用

systemctl unmask cups

systemctl    list-units 列出当前活跃的所有单元(默认)
systemctl    list-units --all列出所有单元(包括未运行、失败的)
systemctl    list-units   --type=service仅显示服务单元
systemctl    list-units   --type=timer仅显示定时器单元
systemctl    list-unit-files列出所有单元文件
systemctl   cat     <服务名>查看单元文件内容(如服务的配置脚本)systemctl cat nginx.service
systemctl   show   <服务名>查看单元的详细属性(配置、依赖等)systemctl show sshd.service
systemctl   list-dependencies   <服务名>查看<服务名>依赖的服务systemctl list-dependencies  nginx.service
systemctl   list-dependencies --reverse  <服务名>查看依赖<服务名>的服务systemctl list-dependencies --reverse nginx.service
systemctl reboot重启系统
systemctl poweroff关机并切断电源
systemctl halt关闭系统(与 poweroff 功能类似)
systemctl suspend进入休眠(数据存内存,需持续供电)
systemctl hibernate进入休眠(数据存硬盘,可断电)
systemctl rescue进入救援模式(单用户,用于修复)
systemctl emergency进入紧急模式(最小环境,极端修复)
systemctl get-default  查看当前系统目标
systemctl isolate <目标名>.target切换系统目标

(切换到图形界面)

systemctl isolate graphical.target

systemctl set-default <目标名>.target设置默认启动目标

(设置为多用户模式)

systemctl set-default multi-user.target

PS:

# 查看系统启动耗时
systemd-analyze
# 查看每个服务启动的耗时
systemd-analyze blame

3. 常用示例

3.1 部署新服务后,设置开机自启并启动

systemctl enable  myservice   --now

3.2 检查系统中所有失败的服务并尝试修复

systemctl list-units --failed --type=service
systemctl reset-failed  # 重置失败状态
systemctl restart 失败的服务名

3.3 查看系统中所有开机自启的服务

systemctl list-unit-files --type=service | grep enabled

4. 总结

systemctl 是 Linux 系统管理的核心工具,掌握以下技能将极大提升您的效率:

到此这篇关于Linux服务器Systemctl命令完全使用指南的文章就介绍到这了,更多相关Linux服务器Systemctl命令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文