Linux之systemd管理

一、Systemd的由来

历史上,Linux 的启动一直采用init进程。
下面的命令用来启动服务。

$ sudo /etc/init.d/httpd start
# 
$ service httpd start

这种方法有两个缺点:

  1. 是启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
  2. 是启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。

二、Systemd的概述

Systemd 就是为了解决这些问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。
根据 Linux 惯例,字母d是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。

使用了 Systemd,就不需要再用init了。Systemd 取代了initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。

Systemd 的优点是功能强大,使用方便,缺点是体系庞大,非常复杂。事实上,现在还有很多人反对使用 Systemd,理由就是它过于复杂,与操作系统的其他部分强耦合,违反Llinux的哲学之一

三、Systemd的特性

  1. 基于socket的激活机制,systemd为支持此机制的服务监听socket,当接收到来自客户端的socket通信时,由systemd激活对应的服务,应答客户端的请求;

  2. 基于bus的激活机制。

  3. 基于device的激活机制,当有设备接入到系统时,systemd会自动激活device、mount、automount等unit来识别、挂载、接入对应的设备;

  4. 基于path的激活机制,当某个文件路径变得可用时或路径出现相应的文件时,激活相应的服务;

  5. 系统快照机制,保存各unit的当前状态信息到持久存储中,在下次开机时可恢复之前某次快照时的系统状态,必要时可自动载入;

  6. 兼容Centos 5的SysV init 以及Centos 6的upstart机子,能够继续使用/etc/rc.d/init.d目录中的服务管理脚本。

  7. 系统引导时,其服务的启动时并行的;

  8. 按需激活进程;

  9. 基于依赖关系定义了服务控制逻辑;

四、systemctl命令详解

systemctl是 Systemd 的主命令,用于管理系统服务,它实际上将 service 和 chkconfig 这两个命令组合到一起。

任务 CentOS 6 CentOS 7
使某服务自动启动 chkconfig –level 3 httpd on systemctl enable httpd.service
使某服务不自动启动 chkconfig –level 3 httpd off systemctl disable httpd.service
检查服务状态 service httpd status systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service (仅显示是否 Active)
显示所有已启动的服务 chkconfig –list systemctl list-units –type=service
启动某服务 service httpd start systemctl start httpd.service
停止某服务 service httpd stop systemctl stop httpd.service
重启某服务 service httpd restart systemctl restart httpd.service

实例

1.启动httpd服务

systemctl start httpd-server.service

2.设置开机自启动

systemctl enable httpd-server.service

3.停止开机自启动

systemctl disable httpd-server.service

4.查看服务当前状态

systemctl status httpd-server.service

5.重新启动某服务

systemctl restart httpd-server.service

6.查看所有已启动的服务

systemctl list -units --type=service

7. 重启Linux系统

systemctl reboot

8. 关闭linux系统

systemctl halt | poweroff

9. 重新加载systemd配置文件

systemctl daemon-reload

服务出错原因:

  • 防火墙未关闭

  • selinux未关闭

.
彻底关闭防火墙:

  1. 查看防火墙当前状态

    sudo systemctl status firewalld.service
    
  2. 停止防火墙服务

    sudo systemctl stop firewalld.service          
    
  3. 使防火墙开机不启动

    sudo systemctl disable firewalld.service
    

    开启防火墙80端口

    iptables -I INPUT -p tcp --dport 80 -j accept
    

关闭SElinux:

修改/etc/selinux/config文件中的SELINUX=””为disabled,然后重启。

五、Unit 详解

Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位)。

Unit 一共分成12种。

  • service:用于定义系统服务, 文件后缀为.service

  • socket:用于标识进程间通信使用的socket文件, 文件后缀为.socket

  • target: 用于模拟实现“运行级别”, 文件后缀.target

  • snapshot:用于管理系统快照, 文件后缀.snapshot

  • device:用于定义内核识别的设备, 文件后缀.device

  • mount:用于定义文件系统挂载点, 文件后缀.mount

  • automount:用于定义文件系统的自动挂载点, 文件后缀.automount

  • swap:用于标识swap设备, 文件后缀.swap

  • timer:用于管理计划任务, 文件后缀.timer

  • path:用于根据文件系统上特定对象的变化来启动其他服务, 文件后缀.path

  • slice:用于资源管理, 文件后缀.slice

  • scope:用于外部创建的进程, 文件后缀.scope

unit文件的组成

Systemd的unit文件通常由三部分组成:[Unit],[Service] 和[Install],其分别代表:

[Unit]:定义与unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等;

[Service]:与特定类型相关的专用选项,与unit类型相同,此处为service类型;

[Install]:定义由”systemctl enable|disable”命令在实现服务启动或禁用时用到的一些选项;

  • [Unit]的内容包括:
Description:描述信息;
After:定义unit的启动次序;表示当前unit应该晚于哪些unit启动;其功能与Before相反;
Requires:依赖的units,此为强依赖,当被依赖的units无法激活时,当前unit即无法激活;
Wants:依赖的units,弱依赖;
Conflicts:定义冲突关系;
  • [Service]的内容包括:
Type:用于定义影响ExecStart及相关参数的功能的unit进程启动类型;其类型包括:simple,forking,oneshot,dbus,notify,idle等等;
EnvironmentFile:环境配置文件;
ExecStart:指定启动unit要运行的命令或脚本;其中ExecStartPre表示启动unit前运行的脚本或命令;ExecStartPost表示启动unit后要运行的命令或脚本;
ExecStop:指明停止unit要运行的命令或脚本;
Restart:指定
  • [Install]的内容包括:
Alias:别名;
RequiredBy:该服务被哪些units所依赖;强依赖;
WantedBy:该服务被哪些units所依赖;弱依赖;

systemctl list-units命令可以查看当前系统的所有 Unit

# 列出正在运行的 Unit
$ systemctl list-units

# 列出所有Unit,包括没有找到配置文件的或者启动失败的
$ systemctl list-units --all

# 列出所有没有运行的 Unit
$ systemctl list-units --all --state=inactive

# 列出所有加载失败的 Unit
$ systemctl list-units --failed

# 列出所有正在运行的、类型为 service 的 Unit
$ systemctl list-units --type=service

猜你喜欢

转载自blog.csdn.net/lv8549510/article/details/80766022