欢迎光临
我们一直在努力

Linux 服务管理深度拆解|systemd、systemctl 与 Unit 配置全解

Linux 服务管理

windows 开机的时候,有些程序自动启动了。

命令行运行services.msc

systemd 介绍

基本概念

CentOS 7 使用 Systemd 引导系统启动 ,速度最快,所有进程无论有无依赖关系则都是并行启动(很多时候进程没有真正启动而是只有一个信号或者说是标记而已,在真正利用的时候才会真正启动)。Systemd为了解决上文的问题而诞生。它的目标是,为系统的启动和管理提供一套完整的解决方案。

系统引导程序:

  • 用户空间,systemd,pid为1

  • 内核空间,kthreadd,pid为2

  • 服务 :从业务角度来称呼,例如 web 服务,数据库服务。

    守护进程(daemon) :web 服务器对外提供 web 服务,由 web 相关的进程提供支持。

    以web服务为例:

    • 服务:web服务

    • 守护进程:httpd

    万和提供培训服务

    • 服务:万培训云计算

    • 守护进程:课程顾问、代课老师、就业老师等。

    例如:

    # 安装软件包
    [root@centos7 ~ 19:04:32]# yum install -y httpd

    # 启动服务
    [root@centos7 ~ 19:05:18]# systemctl start httpd

    # 查看进程
    [root@centos7 ~ 19:05:47]# ps -C httpd f
    PID TTY STAT TIME COMMAND
    5271 ? Ss 0:00 /usr/sbin/httpd -DFOREGROUND
    5288 ? S 0:00 \\_ /usr/sbin/httpd -DFOREGROUND
    5289 ? S 0:00 \\_ /usr/sbin/httpd -DFOREGROUND
    5290 ? S 0:00 \\_ /usr/sbin/httpd -DFOREGROUND
    5291 ? S 0:00 \\_ /usr/sbin/httpd -DFOREGROUND
    5292 ? S 0:00 \\_ /usr/sbin/httpd -DFOREGROUND

    httpd 服务对应的守护进程是5271、5288…

    systemd 架构

    守护进程:systemd,由守护进程干活。

    工具:systemctl,提供给用户要、用来根机器交互的工具。

    在这里插入图片描述

    unit 类型

    systemctl 命令用于管理不同类型的系统对象,这些对象称之为 units 。

    • Service unit :用于定义系统服务,文件扩展名为**.service** ,例如httpd.service

    • Socket unit :用于标识进程间通信用的 socket文件,文件扩展名为.socket

    • Target unit :用于模拟实现“运行级别”,文件扩展名为.target

    • Timer unit :用于管理计划任务,文件扩展名为.timer

    • Device unit:用于定义内核识别的设备,文件扩展名为.device

    • Mount unit:用于定义文件系统挂载点,文件扩展名为.mount

    • Snapshot unit:管理系统快照,文件扩展名为.snapshot

    • Swap unit:用于标识swap设备,文件扩展名为.swap

    • Automount unit:文件系统的自动挂载点,文件扩展名为.automount

    • Path unit:用于根据文件系统上特定对象的变化来启动其他服务,文件扩展名为.path

    • Slice unit:用于资源管理,文件扩展名为.slice

    查看 unit 列表信息

    # 列出状态为loaded units
    [root@centos7 ~ 19:06:23]# systemctl list-units

    systemctl list-units命令输出说明:

    • UNIT :服务单元名称。

    • LOAD :systemd是否正确解析了单元的配置并将该单元加载到内存中。

    • ACTIVE :单元的高级别激活状态。此信息表明单元是否已成功启动。

    • SUB :单元的低级别激活状态。此信息指示有关该单元的更多详细信息。信息视单元类型、 状态以及单元的执行方式而异。

    • DESCRIPTION :单元的简短描述。

    # -t选项查看特定类型unit 清单
    [deng@centos7 ~ 19:07:55]$ systemctl list-units -t timer
    UNIT LOAD ACTIVE SUB DESCRIPTION
    systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Directories
    unbound-anchor.timer loaded active waiting daily update of the root trust anchor for DNSSEC

    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.

    2 loaded units listed. Pass –all to see loaded but inactive units, too.
    To show all installed unit files use 'systemctl list-unit-files'.

    # 列出类型为service,状态为active和inactive unit
    [root@centos7 ~ 19:09:12]# systemctl list-units –type service –all

    # 列出系统中所有unit,包括未loaded的unit
    [root@centos7 ~ 19:10:38]# systemctl list-unit-files

    # 查看失败的服务
    [root@centos7 ~ 19:11:05]# systemctl –failed –type service

    查看单个 unit 信息

    [deng@centos7 ~ 19:12:44]$ systemctl status sshd.service
    ● sshd.service – OpenSSH server daemon
    Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
    Active: `active` (`running`) since 三 2026-07-22 17:37:45 CST; 5h 50min ago
    Docs: man:sshd(8)
    man:sshd_config(5)
    Main PID: 1167 (sshd)
    Tasks: 1
    CGroup: /system.slice/sshd.service
    └─1167 /usr/sbin/sshd -D
    ……

    关键字概述
    loaded 单元配置文件已处理
    active(running) 正在运行
    active(exited) 已成功完成一次性配置
    active(waiting) 运行中,正在等待事件
    inactive 不再运行
    enabled 系统引导时启动
    disabled 系统引导时不启动
    static 无法启动,依赖其他单元启动

    控制系统服务

    命令任务
    systemctl status UNIT 查看单元状态的详细信息。
    systemctl stop UNIT 在运行中的系统上停止一项服务。
    systemctl start UNIT 在运行中的系统上启动一项服务。
    systemctl restart UNIT 在运行中的系统上重新启动一项服务。
    systemctl reload UNIT 重新加载运行中服务的配置文件。
    systemctl mask UNIT 禁用服务,使其无法手动启动或在系统引导时启动。
    systemctl unmask UNIT 使屏蔽的服务变为可用。
    systemctl enable UNIT 将服务配置为在系统引导时启动。使用 –now 选项也会启动该服务。
    systemctl disable UNIT 禁止服务在系统引导时启动。使用 –now选项也会停止该服务。

    # 停止服务
    [root@centos7 ~ 19:14:21]# systemctl stop sshd.service

    # 客户端连接测试
    [root@centos7 ~ 19:14:56]# ssh deng@centos7 hostname

    # 启动服务
    [root@centos7 ~ 19:15:22]# systemctl start sshd.service
    # 客户端连接测试
    [root@centos7 ~ 19:15:48]# ssh deng@centos7 hostname

    # 重启服务,相当于stop再start
    [root@centos7 ~ 19:16:15]# systemctl restart sshd.service

    # 一般用于配置文件变动后,重新加载
    [root@centos7 ~ 19:16:43]# systemctl reload sshd.service
    # 重新加载服务,服务对应的主进程不会重启,只会重新加载一次配置文件。

    # 禁止服务开机自启
    [root@centos7 ~ 19:17:09]# systemctl disable httpd.service

    # 允许服务开机自启
    [root@centos7 ~ 19:17:35]# systemctl enable httpd.service

    # 查看服务是否开机自启
    [root@centos7 ~ 19:18:02]# systemctl is-enabled httpd.service
    enabled

    # 查看服务是否正在运行
    [root@centos7 ~ 19:18:28]# systemctl is-active httpd.service
    active

    # 屏蔽服务(禁止启动)
    [root@centos7 ~ 19:18:54]# systemctl mask httpd.service

    # 取消屏蔽
    [root@centos7 ~ 19:19:21]# systemctl unmask httpd.service

    unit 配置文件

    配置文件说明

    systemd 单元配置文件通常位于以下目录:

    • /usr/lib/systemd/system/ :软件包安装的单元文件(不要修改)

    • /etc/systemd/system/ :管理员创建或修改的单元文件(优先使用)

    • /run/systemd/system/ :运行时生成的临时单元文件

    配置文件结构:

    [Unit]
    Description=描述信息
    After=network.target
    Wants=network.target

    [Service]
    Type=simple
    ExecStart=/path/to/executable
    Restart=always
    User=deng
    Group=deng

    [Install]
    WantedBy=multi-user.target

    段说明
    [Unit] 定义单元的元数据和依赖关系
    [Service] 服务单元的配置(仅适用于.service文件)
    [Install] 定义单元的安装信息

    开发一个 study 服务

    创建一个自定义服务,实现每隔1秒向日志文件写入一条消息。

    # 创建脚本
    [deng@centos7 ~ 19:20:47]$ cat > /usr/local/bin/study.sh << 'EOF'
    #!/bin/bash
    while true
    do
    echo "$(date): I'm studying Linux service management" >> /var/log/study.log
    sleep 1
    done
    EOF

    [deng@centos7 ~ 19:21:14]$ chmod +x /usr/local/bin/study.sh

    # 创建服务配置文件
    [root@centos7 ~ 19:21:40]# cat > /etc/systemd/system/study.service << 'EOF'
    [Unit]
    Description=Study Service
    After=network.target

    [Service]
    Type=simple
    ExecStart=/usr/local/bin/study.sh
    Restart=always
    User=deng
    Group=deng

    [Install]
    WantedBy=multi-user.target
    EOF

    # 重新加载systemd配置
    [root@centos7 ~ 19:22:06]# systemctl daemon-reload

    # 启动服务
    [root@centos7 ~ 19:22:33]# systemctl start study.service

    # 查看服务状态
    [root@centos7 ~ 19:22:59]# systemctl status study.service
    ● study.service – Study Service
    Loaded: loaded (/etc/systemd/system/study.service; disabled; vendor preset: enabled)
    Active: active (running) since 三 2026-07-22 19:22:33 CST; 26s ago
    Main PID: 12345 (study.sh)
    Tasks: 2
    CGroup: /system.slice/study.service
    ├─12345 /bin/bash /usr/local/bin/study.sh
    └─12367 sleep 1

    # 查看日志
    [deng@centos7 ~ 19:23:25]$ tail -f /var/log/study.log
    2026年 07月 22日 星期三 19:22:33 CST: I'm studying Linux service management
    2026年 07月 22日 星期三 19:22:34 CST: I'm studying Linux service management
    2026年 07月 22日 星期三 19:22:35 CST: I'm studying Linux service management
    ……

    # 设置开机自启
    [root@centos7 ~ 19:24:01]# systemctl enable study.service

    # 停止服务
    [root@centos7 ~ 19:24:28]# systemctl stop study.service

    验证日志:

    [deng@centos7 ~ 19:25:04]$ cat /var/log/study.log
    2026年 07月 22日 星期三 19:22:33 CST: I'm studying Linux service management
    2026年 07月 22日 星期三 19:22:34 CST: I'm studying Linux service management
    2026年 07月 22日 星期三 19:22:35 CST: I'm studying Linux service management
    2026年 07月 22日 星期三 19:22:36 CST: I'm studying Linux service management
    2026年 07月 22日 星期三 19:22:37 CST: I'm studying Linux service management
    ……
    studying Linux service management
    2026年 07月 22日 星期三 19:22:34 CST: I'm studying Linux service management
    2026年 07月 22日 星期三 19:22:35 CST: I'm studying Linux service management
    2026年 07月 22日 星期三 19:22:36 CST: I'm studying Linux service management
    2026年 07月 22日 星期三 19:22:37 CST: I'm studying Linux service management
    ……

    赞(0)
    未经允许不得转载:171主机测评 » Linux 服务管理深度拆解|systemd、systemctl 与 Unit 配置全解
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址