• Linux基础 - 服务管理(systemd)


    ‍‍🏡博客主页:  Passerby_Wang的博客_CSDN博客-系统运维,云计算,Linux基础领域博主

    🌐所属专栏:『Linux基础

    🌌上期文章:  Linux基础 - 系统安全(SELinux与Firewalld)

    📰如觉得博主文章写的不错或对你有所帮助的话,还望大家多多支持呀! 关注、点赞、收藏、评论。

    目录

    一、systemd的由来

    二、介绍

    1、Linux系统和服务管理器

    三、主要的管理工具systemctl

    1、配置文件

    2、unit分类

    3、管理unit

    1)常见命令

    2)实例

    4、系统管理(请勿轻易尝试)

    5、查看系统服务

    1)命令

    2)实例

    6、运行级别

    1)级别划分

    2)常见命令(谨慎使用)

    3)实例

    7、设置默认级别

    1)查看默认级别

    2)设置默认级别


    一、systemd的由来

            systemd即为system daemon,是linux下的一种init软件,由Lennart Poettering带头开发,systemd这一名字源于Unix中的一个惯例:在Unix中常以"d"作为系统守护进程(英语:daemon,亦称后台进程)的后缀标识。除此以外,systemd亦是借代英文术语D体系,而这一术语即是用于描述一个人具有快速地适应环境并解决困难的能力。systemd已纳入众多Linux发行版的软件源中。

    二、介绍

    1、Linux系统和服务管理器

            Systemd是一个更高效的系统和服务管理器,是内核引导之后加载的第一个初始化进程(PID=1),负责掌控整个Linux的运行/服务资源组合,在开机时服务并行启动,各系统服务间的精确依赖。

    三、主要的管理工具systemctl

    1、配置文件

    配置目录:/etc/systemd/system

    脚本目录:/run/systemd/system

    服务目录:/lib/systemd/system

    2、unit分类

    Systemd 可以管理的系统资源。不同的资源统称为 unit(单元),unit有13种。

    序号

    资源名称

    含义

    1

    service

    系统服务

    2

    socket

    用于标识进程间通信的socket

    3

    busname

    用于设定与此服务通信所使用的D-Bus名称

    4

    target

    用于模拟实现“运行级别”

    5

    snapshot

    管理系统快照

    6

    device

    硬件设备

    7

    mount

    文件系统挂载点

    8

    automount

    自动挂载点

    9

    swap

    用于标识swap设备(交换空间)

    10

    timer

    定时器

    11

    path

    文件或路径

    12

    slice

    进程组

    13

    scope

    不是由systemd启动的外部进程

    3、管理unit

    1)常见命令

    systemctl start unit #启动服务

    systemctl stop unit #停止服务

    systemctl restart unit #重启服务

    systemctl enable unit #开机自启服务

    systemctl enable --now unit #设置开机自启并立即开启服务

    systemctl is-enable unit #查看服务是否开机自启

    systemctl disable unit #禁止开机自启服务

    systemctl kill unit #杀死unit的所有进程

    systemctl reload unit #重新加载unit的配置文件

    systemctl daemon-reload #重新加载所有修改过的配置文件

    2)实例

    [root@wangwu ~]# systemctl start httpd
    1. [root@wangwu ~]# systemctl status httpd
    2. ● httpd.service - The Apache HTTP Server
    3.    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
    4.    Active: active (running) since Sun 2022-10-23 21:08:08 CST; 1min 2s ago
    5.      Docs: man:httpd(8)
    6.            man:apachectl(8)
    7.  Main PID: 110860 (httpd)
    8.    Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    9.     Tasks: 6
    10.    CGroup: /system.slice/httpd.service
    11.            ├─110860 /usr/sbin/httpd -DFOREGROUND
    12.            ├─110861 /usr/sbin/httpd -DFOREGROUND
    13.            ├─110862 /usr/sbin/httpd -DFOREGROUND
    14.            ├─110863 /usr/sbin/httpd -DFOREGROUND
    15.            ├─110864 /usr/sbin/httpd -DFOREGROUND
    16.            └─110865 /usr/sbin/httpd -DFOREGROUND
    17. Oct 23 21:08:08 wangwu systemd[1]: Starting The Apache HTTP Server...
    18. Oct 23 21:08:08 wangwu httpd[110860]: AH00558: httpd: Could not reliably d...ge
    19. Oct 23 21:08:08 wangwu systemd[1]: Started The Apache HTTP Server.

    注:

    Loaded行:配置文件的位置,是否设为开机启动

    Active行:表示正在运行

    Main PID行:主进程ID

    Status行:由应用本身提供的软件当前状态

    CGroup块:应用的所有子进程

    日志块:应用的日志

    [root@wangwu ~]# systemctl stop httpd
    1. [root@wangwu ~]# systemctl status httpd
    2. ● httpd.service - The Apache HTTP Server
    3.    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
    4.    Active: inactive (dead)
    5.      Docs: man:httpd(8)
    6.            man:apachectl(8)
    7. Sep 29 17:45:02 wangwu systemd[1]: Unit httpd.service cannot be reloaded b...e.
    8. Oct 23 21:08:08 wangwu systemd[1]: Starting The Apache HTTP Server...
    9. Oct 23 21:08:08 wangwu httpd[110860]: AH00558: httpd: Could not reliably d...ge
    10. Oct 23 21:08:08 wangwu systemd[1]: Started The Apache HTTP Server.
    11. Oct 23 21:24:02 wangwu httpd[116627]: AH00558: httpd: Could not reliably d...ge
    12. Oct 23 21:24:02 wangwu systemd[1]: Reloaded The Apache HTTP Server.
    13. Oct 23 21:24:09 wangwu systemd[1]: Stopping The Apache HTTP Server...
    14. Oct 23 21:24:10 wangwu systemd[1]: Stopped The Apache HTTP Server.
    [root@wangwu ~]# systemctl restart httpd
    1. [root@wangwu ~]# systemctl enable httpd
    2. Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
    3. [root@wangwu ~]# systemctl enable --now httpd
    4. [root@wangwu ~]# systemctl is-enabled httpd
    5. enabled
    1. [root@wangwu ~]# systemctl disable httpd
    2. Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
    3. [root@wangwu ~]# systemctl is-enabled httpd
    4. disabled
    1. [root@wangwu ~]# systemctl kill httpd
    2. [root@wangwu ~]# systemctl reload httpd
    3. [root@wangwu ~]# systemctl daemon-reload

    4、系统管理(请勿轻易尝试)

    systemctl reboot #重启系统

    systemctl poweroff #退出系统并关闭电源

    systemctl halt #CPU停止工作

    systemctl suspend #挂起系统

    systemctl hibernate #系统休眠

    systemctl hybrid-sleep #系统休眠并挂起:

    5、查看系统服务

    1)命令

    systemctl -t service #查看活动的系统服务

    systemctl -t service --all #列出所有系统服务(包括不活跃的)

    2)实例

    [root@wangwu ~]# systemctl -t service

      UNIT                        LOAD   ACTIVE SUB     DESCRIPTION

      abrt-ccpp.service           loaded active exited  Install ABRT coredump hook

    注:

    UNIT  #单元 

    LOA #是否存在

    ACTIVE  #是否激活

    SUB #状态

    DESCRIPTION #描述

    1. [root@wangwu ~]# systemctl -t service --all
    2.   UNIT                       LOAD      ACTIVE   SUB     DESCRIPTION
    3. ● apparmor.service           not-found inactive dead    apparmor.service

    6、运行级别

    1)级别划分

    系统的运行级别,分为七个级别,每种运行级别代表特定的操作模式,分别用数字0-6表示。

    RHEL5、6

    RHEL7

    级别

    含义

    对应级别

    含义

    0

    关机

    poweroff.target

    关机

    1

    单用户模式

    rescure.target

    救援模式

    2

    不完全的命令行模式,不包含NFS

    multi-user.target

    非图形界面的多用户方式

    3

    完全命令行模式,标准字符界面

    multi-user.target

    非图形界面的多用户方式

    4

    系统保留

    multi-user.target

    非图形界面的多用户方式

    5

    图形模式

    graphical.target

    图形界面的多用户方式

    6

    重启

    reboot.target

    重启

    2)常见命令(谨慎使用)

    systemctl -t target #查看可用运行级别

    systemctl isolate multi-user.target #切换到文本模式

    systemctl isolate graphical.target #切换到图形模式

    3)实例

    1. [root@wangwu ~]# systemctl -t target
    2. UNIT                   LOAD   ACTIVE SUB    DESCRIPTION
    3. basic.target           loaded active active Basic System
    4. cryptsetup.target      loaded active active Local Encrypted Volumes
    5. getty-pre.target       loaded active active Login Prompts (Pre)
    6. getty.target           loaded active active Login Prompts
    7. graphical.target       loaded active active Graphical Interface
    8. local-fs-pre.target    loaded active active Local File Systems (Pre)
    9. local-fs.target        loaded active active Local File Systems
    10. multi-user.target      loaded active active Multi-User System
    11. network-online.target  loaded active active Network is Online
    12. network-pre.target     loaded active active Network (Pre)
    13. network.target         loaded active active Network
    14. nfs-client.target      loaded active active NFS client services
    15. nss-user-lookup.target loaded active active User and Group Name Lookups
    16. paths.target           loaded active active Paths
    17. remote-fs-pre.target   loaded active active Remote File Systems (Pre)
    18. rpc_pipefs.target      loaded active active rpc_pipefs.target
    19. slices.target          loaded active active Slices
    20. sockets.target         loaded active active Sockets
    21. swap.target            loaded active active Swap
    22. sysinit.target         loaded active active System Initialization
    23. timers.target          loaded active active Timers
    1. [root@wangwu ~]# systemctl isolate multi-user.target
    2. [root@wangwu ~]# systemctl isolate graphical.target

    7、设置默认级别

    1)查看默认级别

    命令

    systemctl get-default

    实例

    1. [root@wangwu ~]# systemctl get-default #查看默认级别
    2. graphical.target #图形模式

    2)设置默认级别

    常用命令

    systemctl set-default multi-user.target

    systemctl set-default graphica.target

    实例

    1. [root@wangwu ~]# systemctl set-default multi-user.target #设置默认级别为文本模式
    2. Removed symlink /etc/systemd/system/default.target.
    3. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
    4. [root@wangwu ~]# systemctl get-default
    5. multi-user.target #文本模式
    1. [root@wangwu ~]# systemctl set-default graphical.target #设置默认级别为图形模式
    2. Removed symlink /etc/systemd/system/default.target.
    3. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
    4. [root@wangwu ~]# systemctl get-default
    5. graphical.target #图形模式
  • 相关阅读:
    Java进阶01进阶版基础知识 IDEA快捷键+常用插件
    数据结构与算法——19.红黑树
    网络安全(黑客)自学
    海量数据分析更快、更稳、更准。GaussDB(for MySQL) HTAP只读分析特性详解
    操作系统知识图谱
    Spring原理:PostProcessor与AOP原理
    【目标检测算法】利用wandb可视化YOLO-V5模型的训练
    5分钟搞懂MySQL - 行转列
    全球南方《乡村振兴战略下传统村落文化旅游设计》许少辉八一新枝——2023学生开学季辉少许
    详解 http 鉴权
  • 原文地址:https://blog.csdn.net/Passerby_Wang/article/details/127482392