当前位置: 代码网 > 服务器>服务器>Linux > Linux命令之systemctl用法详解

Linux命令之systemctl用法详解

2024年05月15日 Linux 我要评论
一、systemctl命令简介centos 5使用sysv init;centos 6使用upstart,centos 7使用systemd管理守护进程。centos7采用 systemd管理,服务独

一、systemctl命令简介

centos 5使用sysv init;centos 6使用upstart,centos 7使用systemd管理守护进程。centos7采用 systemd管理,服务独立的运行在内存中,服务响应速度快,但占用更多内存。独立服务的服务启动脚本都在目录 /usr/lib/systemd/system里。systend的新特性:

  • 系统引导时实现服务的并行启动;
  • 按需激活进程;
  • 系统实现快照;
  • 基于依赖关系定义服务的控制逻辑;

systemctl可用于内省和控制“systemd”系统和服务管理器的状态。centos7.x系统环境下我们经常使用此命令启停服务,实际上此命令除了其他独立服务还有很多其他用途。

二、systemctl使用示例

1、查看命令帮助

[root@s153 system]# systemctl --help
systemctl [options…] {command} …

2、启动服务

接下来的操作实例以管理xinetd服务为例。

[root@s153 system]# systemctl start xinetd

3、查看服务状态

[root@s153 system]# systemctl status xinetd
● xinetd.service - xinetd a powerful replacement for inetd
loaded: loaded (/usr/lib/systemd/system/xinetd.service; enabled; vendor preset: enabled)
active: active (running) since 三 2022-07-20 10:29:26 cst; 1min 53s ago
process: 15831 execstart=/usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid $extraoptions (code=exited, status=0/success)
main pid: 15832 (xinetd)
cgroup: /system.slice/xinetd.service
└─15832 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid

4、停止服务

[root@s153 system]# systemctl stop xinetd

5、查看服务是否活跃

[root@s153 system]# systemctl is-active xinetd
inactive
[root@s153 system]# systemctl start xinetd
[root@s153 system]# systemctl is-active xinetd
active

6、重新加载服务配置

reload是在不重启服务的情况下重新加载配置文件。

[root@s153 system]# systemctl reload xinetd

7、重启服务

restart命令实际上是先stop,然后start。

[root@s153 system]# systemctl restart xinetd

8、列出所有可用单元

在这里插入图片描述

[root@s153 system]# systemctl list-unit-files
unit file state
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
…

9、列出所有已加载单元

在这里插入图片描述

[root@s153 system]# systemctl list-units
unit load active sub description
proc-sys-fs-binfmt_misc.automount loaded active waiting arbitrary executable file formats file system automount point
sys-devices-pci0000:00-0000:00:01.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged qemu_dvd-rom centos_7_x86_64
sys-devices-pci0000:00-0000:00:05.0-virtio1-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged qemu_harddisk 1

10、查看可用systemctl管理的所有服务

在这里插入图片描述

systemctl可用管理单元分很多种,日常工作中我们仅仅用于管理服务,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, 用于定义文件系统中的一文件或目录;

11、注销服务

服务被注销后该服务就无法通过systemctl进行启停管理。

[root@s153 system]# systemctl mask firewalld
created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@s153 system]# systemctl start firewalld
failed to start firewalld.service: unit is masked.

12、取消注销服务

[root@s153 system]# systemctl unmask firewalld
removed symlink /etc/systemd/system/firewalld.service.
[root@s153 system]# systemctl start firewalld

13、设置服务开机自启动

[root@s153 system]# systemctl enable xinetd.service
created symlink from /etc/systemd/system/multi-user.target.wants/xinetd.service to /usr/lib/systemd/system/xinetd.service.

14、取消服务开机自启动

[root@s153 system]# systemctl disable xinetd.service
removed symlink /etc/systemd/system/multi-user.target.wants/xinetd.service.

15、查看机器信息

[root@s153 system]# systemctl list-machines
name state failed jobs
s153 (host) running 0 0

1 machines listed.

16、查看系统环境变量

[root@s153 system]# systemctl show-environment
lang=zh_cn.utf-8
path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

17、重新加载unit文件

如果手动修改了unit文件,可以使用此命令重新加载。

[root@s153 system]# systemctl daemon-reload

18、创建一个系统快照

[root@s153 system]# systemctl snapshot wuhs
wuhs.snapshot

19、删除指定快照

[root@s153 system]# systemctl delete wuhs

20、查看服务是否开机自启动

[root@s153 system]# systemctl is-enabled xinetd.service
enabled

21、杀死服务

[root@s153 system]# systemctl kill xinetd
[root@s153 system]# systemctl is-failed xinetd
inactive

22、进入救援模式

[root@s153 system]# systemctl rescue

broadcast message from root@s153 on pts/1 (三 2022-07-20 13:08:30 cst):

the system is going down to rescue mode now!
#执行完命令后系统就进入了救援模式
#救援模式下切换到默认模式
[root@s153 ~]# systemctl default

在这里插入图片描述

23、关闭系统

[root@s153 ~]# systemctl poweroff

24、重启机器

[root@s153 ~]# systemctl reboot

25、系统睡眠

suspend暂停模式,类似window环境的睡眠模式,会将系统的状态数据保存到内存中,然后关闭掉大部分的系统硬件,当然,并没有实际关机。当用户按下唤醒机器的按钮,系统数据会重内存中回复,然后重新驱动被大部分关闭的硬件,就开始正常运作!唤醒的速度较快。

[root@s153 ~]# systemctl suspend

26、查看系统启动模式

[root@s153 boot]# systemctl get-default
multi-user.target

27、设置系统为图形界面启动

[root@s153 system]# systemctl set-default graphical.target
removed symlink /etc/systemd/system/default.target.
created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.

三、systemctl参数说明

1、使用语法

用法:systemctl [options…] {command} …

2 、参数说明

参数参数说明
start立刻启动后面接的unit
stop立刻关闭后面接的unit
restart立刻关闭后启动后面接的unit,亦即执行stop再start的意思
reload不关闭后面接的unit的情况下,重载配置文件,让设定生效
enable设定下次开机时,后面接的unit会被启动
disable设定下次开机时,后面接的unit 不会被启动
status目前后面接的这个unit 的状态,会列出是否正在执行、是否开机启动等信息。
is-active目前有没有正在运行中
is-enable开机时有没有预设要启用这个unit
kill不要被kill这个名字吓着了,它其实是向运行unit的进程发送信号
show列出unit的配置。
mask注销unit,注销后你就无法启动这个unit了
unmask取消对unit的注销
list-units依据unit列出目前有启动的unit。若加上–all才会列出没启动的。(等价于无参数)
list-unit-files列出所有以安装unit以及他们的开机启动状态(enabled、disabled、static、mask)。
–type=type就是unit type,主要有service,socket,target等
get-default取得目前的 target
set-default设定后面接的 target 成为默认的操作模式
isolate切换到后面接的模式

3、unit file结构

文件通常由三部分组成:

  • [unit]: 定义与unit类型无关的通用选项;用于提供unit的描述信息,unit行为及依赖关系等。
  • [service]:与特定类型相关的专用选项;此处为service类型。
  • [install]:定义由"systemctl enable"及"systemctl disable"命令在实现服务启用或禁用时用到的一些选项。

4、unit段的常用选项

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

5、service段的常用选项

  • type:用于定义影响execstart及相关参数的功能的unit进程类型;
    类型有:simple、forking、oneshot、dbus、notify、idle。
  • environmentfile:环境配置文件;
  • execstart:指明启动unit要运行的命令或脚本;execstart, execstartpost
  • execstop:指明停止unit要运行的命令或脚本;
  • restart:

6、install段的常用配置:

  • alias:
  • requiredby:被哪些unit所依赖;
  • wantby:被哪些unit所依赖;

7、unit文件样例

[root@s153 system]# cat chronyd.service
[unit]
description=ntp client/server
documentation=man:chronyd(8) man:chrony.conf(5)
after=ntpdate.service sntp.service ntpd.service
conflicts=ntpd.service systemd-timesyncd.service
conditioncapability=cap_sys_time

[service]
type=forking
pidfile=/var/run/chronyd.pid
environmentfile=-/etc/sysconfig/chronyd
execstart=/usr/sbin/chronyd $options
execstartpost=/usr/libexec/chrony-helper update-daemon
privatetmp=yes
protecthome=yes
protectsystem=full

[install]
wantedby=multi-user.target

以上就是linux命令之systemctl用法详解的详细内容,更多关于linux systemctl命令的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com