温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Centos7之Systemd知识简要备忘录

发布时间:2020-04-03 05:52:21 来源:网络 阅读:120 作者:tomshen 栏目:系统运维


Systemd:Systemd 是 Linux 系统中最新的初始化系统(init)

    概要解释参考:https://www.ibm.com/developerworks/cn/linux/1407_liuming_init3/index.html

POST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) --> rootfs --> /sbin/init

init:

CentOS 5: SysV init

CentOS 6: Upstart

CentOS 7: Systemd

为了减少系统启动时间,systemd 的目标是:

    尽可能启动更少的进程

    尽可能将更多进程并行启动


Systemd新特性:

系统引导时实现服务并行启动;

按需激活进程;

系统状态快照;

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





systemd 自带日志服务 journald,该日志服务的设计初衷是克服现有的 syslog 服务的缺点


核心概念:unit

单元的概念

    系统初始化需要做的事情非常多。需要启动后台服务,比如启动 SSHD 服务;需要做配置工作,比如挂载文件系统。这个过程中的每一步都被 systemd 抽象为一个配置单元,

即 unit。可以认为一个服务是一个配置单元;一个挂载点是一个配置单元;一个交换分区的配置是一个配置单元;等等。systemd 将配置单元归纳为以下一些不同的类型。

每个配置单元都有一个对应的配置文件,系统管理员的任务就是编写和维护这些不同的配置文件,比如一个 MySQL 服务对应一个 mysql.service 文件。这种配置文件的语法非常简单,用户不需要再编写和维护复杂的系统脚本了。

配置文件进行标识和配置;文件中主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息;

当 systemd 以系统实例(--system)运行时,加载单元的先后顺序(较前的目录优先级较高):

系统单元目录     描述

/etc/systemd/system 本地配置的系统单元

/run/systemd/system 运行时配置的系统单元

/usr/lib/systemd/system 软件包安装的系统单元



Unit的类型:

Service unit: 文件扩展名为.service, 用于定义系统服务;

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

Device unit: .device, 用于定义内核识别的设备;

Mount unit: .mount, 定义文件系统挂载点;

Socket unit: .socket, 用于标识进程间通信用的socket文件;

Snapshot unit: .snapshot, 管理系统快照;

Swap unit: .swap, 用于标识swap设备;

Automount unit: .automount,文件系统的自动挂载点;

Path unit: .path,用于定义文件系统中的一个文件或目录;






管理系统服务:

CentOS 7: service unit

注意:能兼容早期的服务脚本


命令:systemctl COMMAND name.service


启动:service name start ==> systemctl start name.service

停止:service name stop ==> systemctl stop name.service

重启:service name restart ==> systemctl restart name.service

状态:service name status ==> systemctl status name.service

条件式重启:service name condrestart ==> systemctl try-restart name.service

重载或重启服务:systemctl reload-or-restart name.service

重载或条件式重启服务:systemctl reload-or-try-restart name.service

禁止设定为开机自启:systemctl mask name.service

取消禁止设定为开机自启:systemctl unmask name.service



查看某服务当前激活与否的状态:systemctl is-active name.service

查看所有已经激活的服务:

systemctl list-units --type service 

查看所有服务:

systemctl list-units --type service --all


chkconfig命令的对应关系:

设定某服务开机自启:chkconfig name on ==> systemctl enable name.service

禁止:chkconfig name off ==> systemctl disable name.service

查看所有服务的开机自启状态:

chkconfig --list ==> systemctl list-unit-files --type service 

查看服务是否开机自启:systemctl is-enabled name.service


其它命令:

查看服务的依赖关系:systemctl list-dependencies name.service


    Target 和运行级别

    systemd 用目标(target)替代了运行级别的概念,提供了更大的灵活性


target units:

unit配置文件:.target


运行级别:

0  ==> runlevel0.target, poweroff.target

1  ==> runlevel1.target, rescue.target

2  ==> runlevel2.target, multi-user.target

3  ==> runlevel3.target, multi-user.target

4  ==> runlevel4.target, multi-user.target

5  ==> runlevel5.target, graphical.target

6  ==> runlevel6.target, reboot.target


(对systemd来说运行级别2,3,4没有区别)


Centos7不建议使用:runlevel命令


级别切换:

init N ==> systemctl isolate name.target   (.target不可省略)


查看级别:

runlevel ==> systemctl list-units --type target


获取默认运行级别:

/etc/inittab ==> systemctl get-default


修改默认级别:

/etc/inittab ==> systemctl set-default name.target


切换至紧急救援模式:

systemctl rescue


切换至emergency模式:

systemctl emergency

(emergency:驱动不加载,系统不做初始化,服务不启动)


其它常用命令:

关机:systemctl halt、systemctl poweroff

重启:systemctl reboot

挂起:systemctl suspend

快照:systemctl hibernate

快照并挂起:systemctl hybrid-sleep





向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI