systemd命令



systemd

核心概念 unit

​ unit由相关配置文件进行标识,识别和配置 主要包含了系统服务 监听socket 保存的快照和其他init信息,这些配置文件主要保存在:

  • /usr/lib/systemd/system

    每个 服务最主要的启动脚本设置 ,类似于之前的/etc/init.d/

  • /run/systemd/system

    系统执行过程中所产生的服务脚本,比上面目录优先运行

  • /etc/systemd/system

    管理员建立的执行脚本,类似于 于/etc/rc.d/rcN.d/Sxx 类的功能,比上面目录优先运行


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 定义文件系统中的一文件或者目录

关键特性

​ 基于socket的激活机制: socket与程序分离

​ 基于bus的激活机制

​ 基于device的激活机制

​ 基于path的激活机制

​ 系统快照 保存各个unit当前的状态信息在持久存储设备中

​ 向后兼容sysv init脚本

不兼容

​ systemctl的命令是固定不变的

​ 不是由systemd启动的服务,systemctl无法与之通信


管理系统服务

​ CentOS 7:service类型的unit文件来管控的

systemctl命令:

​ —control the systemd system and service manager

systemctk [OPTION..] COMMEND [NAME…]

​ 启动:

            service NAME start ====>systemctl start                                  NAME.service

​ 停止:

            service NAME stop ====>systemctl stop NAM.service

​ 重启:

            service NAME restart ===>systemctl restart                                NAM.service

​ 状态:

            service NAME status====>systemd status NAM.service 

​ 条件式重启:

            service NAME condrestart===>systemctl try-restart NAME.service

​ 重载或重启服务:

            systemctl reload | restart NAME.service

​ 重载或条件式重启服务

systemctl reload | try-restart NAME.service

查看当前服务激活状态与否

systemctl is-active NAME.service

查看所有已经激活的服务

systemctl list-units --type service

查看所有服务

chkconfig --list ====>systemctl list-units -t service --all | -a

设置开机自启动

chkconfig NAME on =====>systemctl enable NAME.service

禁止开机自启动

chkconfig NAME off =====>systemctl disable NAME.service

查看某服务是否开机自启动

chkconfig --list NAME =====>systemctl is-enabled NAME.service

禁止某服务设定开机自启动

systemctl mask NAME.service

取消禁止

systemctl unmask NAME.service

查看服务的依赖关系

systemctl list-dependencies NAME.service

管理target units

​ 运行级别:

  • 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-user.target
  • 6====> runlevel6.target reboot.target

级别切换

init N =====>systemctl isolate NAME.target

查看级别

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

查看所有级别

systemctl list-units -t target -a

获取默认运行级别

systemctl get-default

设定默认运行级别

systemctl set-default NAME.target

切换到紧急救援模式

systemctl rescue

切换到emergency模式

systemctl emergency

其他常用命令

systemctl halt | poweroff |reboot |suspend |hibernate |hybrid-sleep

service unit file

文件通常是由3个部分组成

​ [unit] 定义unit类型无关的通用选项,提供unit的描述信息 unit的行为和依赖关系

​ [service] 与特定类型的专用选项,此处为service类型

​ [install] 定义由”systemctl enables”和”systemctl disable”命令在实现服务启用或禁用时候用到$一些选项

unit段常用选项:

  • description:描述信息;意义性描述
  • after 定义unit$启动次序,表示当前unit应该晚于哪些unit启动,其功能与before相反
  • requies:依赖到其他unit,强依赖,被依赖的units无法激活时,当前unit无法激活
  • wants 依赖到的其他unit 弱依赖
  • confilcts:定义units间的冲突关系

service段的常用选项

type 用于定义影响execstart和相关参数的功能unit进程启动类型

  • simple
  • forking
  • oneshot
  • dbus
  • notify 类型与simple
  • idle

execstart 指明unit要运行命令和脚本 execstartpre ExecStartPost

Restart

install段常用选项

ailas

RequiredBy 被哪些units依赖

WantedBy

重载生效

systemctl daemon-reload

systemd 是一个 Linux 系统下的系统管理守护进程,它提供了一整套系统管理功能,如进程管理、服务管理、挂载管理、网络管理等。以下是一些常用的 systemd 命令: 1. systemctl:用于控制 systemd 系统和服务管理器。 - `systemctl start service`:启动服务。 - `systemctl stop service`:停止服务。 - `systemctl restart service`:重启服务。 - `systemctl enable service`:设置服务开机自启。 - `systemctl disable service`:取消服务开机自启。 - `systemctl status service`:查看服务状态。 - `systemctl list-unit-files`:列出所有可用的 unit 文件。 2. journalctl:用于管理 systemd 的日志。 - `journalctl`:查看所有日志。 - `journalctl -u service`:查看某个服务的日志。 - `journalctl --since "2021-01-01"`:查看某个时间点之后的日志。 - `journalctl -f`:实时查看日志。 - `journalctl -n 100`:查看最近的 100 条日志。 3. hostnamectl:用于管理主机名和相关设置。 - `hostnamectl set-hostname new_hostname`:设置主机名。 - `hostnamectl status`:查看主机名和其他设置。 4. timedatectl:用于管理系统时间和时区。 - `timedatectl set-time "2021-01-01 00:00:00"`:设置系统时间。 - `timedatectl set-timezone Asia/Shanghai`:设置时区。 - `timedatectl status`:查看时间和时区设置。 5. networkctl:用于管理网络接口和连接。 - `networkctl list`:列出所有网络接口和连接。 - `networkctl status eth0`:查看 eth0 网络接口状态。 - `networkctl enable eth0`:启用 eth0 网络接口。 - `networkctl disable eth0`:禁用 eth0 网络接口。 6. systemctl-analyze:用于分析启动性能。 - `systemctl-analyze`:查看系统启动总时间。 - `systemctl-analyze blame`:列出启动过程中占用时间最长的服务。 - `systemctl-analyze critical-chain`:列出启动过程中最耗时的服务及其依赖关系。 以上是一些常用的 systemd 命令,更多命令可以通过 `man` 命令查看相关文档。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值