百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

其他管理-2

bigegpt 2024-08-18 13:53 2 浏览

systemctl命令的使用

管理服务

我们一起来看一下在服务管理方面systemctl这个工具如何使用

[root@localhost ~]# systemctl start servername
start 启动服务
stop 停止服务
restart 重启服务(没启动的服务会启动)
try-restart 只重启正在运行的服务(没有运行则不启动)
reload 重载配置文件(修改完服务的配置文件后使用)
status 检查服务状态
is-active 检查服务是否已经启动
enable 设置服务开机时启动
disable 设置服务开机时不启动
is-enabled 查看服务是否开机自动启动
mask 屏蔽一个服务
unmask 取消屏蔽
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server 服务名称
Loaded: loaded 加载到内存中;error加载失败;bad-setting无法理解单元配置文件中的设置;masked被屏蔽 (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) 服务开机时是否启动 enabled为启动;disabled为不启动
Active: active (running) since Mon 2019-11-18 15:56:46 CST; 1h 11min ago 服务当前的状态,active(running)为运行;active(exited)运行一次就退出了;active(waiting)运行中,但是再等待其它程序结束才能继续;inactive(dead)为没有运行;activating为启动中,deactivating停止中;failed启动失败
Docs: man:httpd.service(8) 服务的帮助文档 
Main PID: 57779 (httpd) 服务的主进程号
Status: "Running, listening on: port 80" 额外的状态信息
Tasks: 213 (limit: 24882) 任务数量,含进程+线程
Memory: 22.4M 当前占用的内存
CGroup: /system.slice/httpd.service
├─57779 /usr/sbin/httpd -DFOREGROUND
├─57780 /usr/sbin/httpd -DFOREGROUND
├─57781 /usr/sbin/httpd -DFOREGROUND
├─57782 /usr/sbin/httpd -DFOREGROUND
└─57783 /usr/sbin/httpd -DFOREGROUND
Control Groups额外信息
11月 18 15:56:46 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
11月 18 15:56:46 localhost.localdomain httpd[57779]: AH00558: httpd: Could not reliably determ>
11月 18 15:56:46 localhost.localdomain httpd[57779]: Server configured, listening on: port 80
11月 18 15:56:46 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
例:
[root@localhost ~]# systemctl is-active atd 
inactive
[root@localhost ~]# systemctl start atd
[root@localhost ~]# systemctl status atd
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-11-18 18:36:26 CST; 7s ago
Main PID: 60620 (atd)
Tasks: 1 (limit: 24882)
Memory: 504.0K
CGroup: /system.slice/atd.service
└─60620 /usr/sbin/atd -f
11月 18 18:36:26 localhost.localdomain systemd[1]: Started Job spooling tools.
[root@localhost ~]# systemctl stop atd
[root@localhost ~]# systemctl status atd
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Mon 2019-11-18 18:36:43 CST; 6s ago
Process: 60620 ExecStart=/usr/sbin/atd -f $OPTS (code=exited, status=0/SUCCESS)
Main PID: 60620 (code=exited, status=0/SUCCESS)
11月 18 18:36:26 localhost.localdomain systemd[1]: Started Job spooling tools.
11月 18 18:36:43 localhost.localdomain systemd[1]: Stopping Job spooling tools...
11月 18 18:36:43 localhost.localdomain systemd[1]: Stopped Job spooling tools.
[root@localhost ~]# systemctl is-enabled atd
enabled
[root@localhost ~]# systemctl disable atd
Removed /etc/systemd/system/multi-user.target.wants/atd.service.
[root@localhost ~]# systemctl is-enabled atd
disabled
[root@localhost ~]# systemctl enable atd
Created symlink /etc/systemd/system/multi-user.target.wants/atd.service → /usr/lib/systemd/system/atd.service.
[root@localhost ~]# systemctl is-enabled atd
enabled
[root@localhost ~]# systemctl stop atd
[root@localhost ~]# systemctl mask atd
Created symlink /etc/systemd/system/atd.service → /dev/null.
[root@localhost ~]# systemctl status atd
● atd.service
Loaded: masked (Reason: Unit atd.service is masked.)
Active: inactive (dead)
11月 05 18:12:41 localhost.localdomain systemd[1]: Started Job spooling tools.
11月 18 18:34:15 localhost.localdomain systemd[1]: Stopping Job spooling tools...
11月 18 18:34:15 localhost.localdomain systemd[1]: Stopped Job spooling tools.
11月 18 18:36:26 localhost.localdomain systemd[1]: Started Job spooling tools.
11月 18 18:36:43 localhost.localdomain systemd[1]: Stopping Job spooling tools...
11月 18 18:36:43 localhost.localdomain systemd[1]: Stopped Job spooling tools.
[root@localhost ~]# systemctl unmask atd
Removed /etc/systemd/system/atd.service.
[root@localhost ~]# systemctl status atd
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: inactive (dead)
11月 05 18:12:41 localhost.localdomain systemd[1]: Started Job spooling tools.
11月 18 18:34:15 localhost.localdomain systemd[1]: Stopping Job spooling tools...
11月 18 18:34:15 localhost.localdomain systemd[1]: Stopped Job spooling tools.
11月 18 18:36:26 localhost.localdomain systemd[1]: Started Job spooling tools.
11月 18 18:36:43 localhost.localdomain systemd[1]: Stopping Job spooling tools...
11月 18 18:36:43 localhost.localdomain systemd[1]: Stopped Job spooling tools

查看服务

[root@localhost ~]#systemctl command --type=xxx --all
list-units 查看所有加载到内存中的单元 
list-unit-files 查看系统中所有安装的单元文件(存放在/usr/lib/systemd/system)的启用状态
--type=xxx 单元类型
--all 列出系统中所有加载的,不管状态为何
[root@localhost ~]# systemctl 
UNIT LOAD ACTIVE SUB DESCRIPTION 
sys.. loaded active plugged /sys.....
sys.. loaded active plugged /sys.....
sys.. loaded active plugged /sys.....
sys.. loaded active plugged /sys.....
sys.. loaded active plugged /sys.....
sys.. loaded active plugged /sys.....
UNIT: 单元名称
LOAD: 是否被加载
ACTIVE: 高优先级单元状态,与SUB结合就是使用status查看的状态
SUB: 低优先级单元状态 
DESCRIPTION:简介
[root@localhost ~]# systemctl list-unit-files 
UNIT FILE STATE 
proc... static 
-.mount generated
boot.mount generated
dev-hugepages.mount static 
dev-mqueue.mount static 
proc-fs-nfsd.mount static 
proc-... static 
sys-f... static 
sys-... static 
sys-ke.. static 
tmp.mount static 
var-lib.. static 
var-lib.. static 
cups.path.. enabled 
systemd.. static
查看指定类型的服务
[root@localhost ~]# systemctl --type target --all
UNIT LOAD ACTIVE SUB DESCRIPTION 
basic.target loaded active active Basic System 
bluetooth.target loaded active active Bluetooth 
cryptsetup.target loaded active active Local... 
● dbus.target not-found inactive dead dbus.target 
[root@localhost ~]# systemctl --type=target --all
UNIT LOAD ACTIVE SUB DESCRIPTION 
basic.target loaded active active Basic System 
bluetooth.target loaded active active Bluetooth 
cryptsetup.target loaded active active Local...
● dbus.target not-found inactive dead dbus.target
[root@localhost ~]# systemctl list-units --type=target --all
UNIT LOAD ACTIVE SUB DESCRIPTION 
basic.target loaded active active Basic System 
bluetooth.target loaded active active Bluetooth 
cryptsetup.target loaded active active Local...
● dbus.target not-found inactive dead dbus.target 
使用systemctl切换运行级别
[root@localhost ~]# systemctl list-units --type target --all
UNIT LOAD ACTIVE SUB DESCRIPTION 
basic.target loaded active active Basic System 
bluetooth.target loaded inactive dead Bluetooth 
cryptsetup.target loaded active active Local ..
● dbus.target not-found inactive dead dbus.target 
emergency.target loaded inactive dead Emergency Mode 
getty-pre.target loaded inactive dead Login..
getty.target loaded active active Login Prompts 
graphical.target loaded inactive dead Graphica..Interface 
常用的target
graphical.target:图形模式
multi-user.target:字符模式
rescue.target:救援模式
emergency.target:紧急模式,无法进入到救援模式时使用
shutdown.target:关机
[root@localhost ~]# systemctl get-default 查看默认运行级别
graphical.target 
[root@localhost ~]# systemctl set-default multi-user.target 设置 
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/multi-user.target.
[root@localhost ~]# systemctl isolate multi-user.target 切换
除了可以使用上述方法设置和查看之外 系统还给我们提供了几个简单的命令,方便操作
[root@localhost ~]# systemctl poweroff 关机
[root@localhost ~]# systemctl reboot 重启
[root@localhost ~]# systemctl suspend 挂起
[root@localhost ~]# systemctl hibernate 休眠
[root@localhost ~]# systemctl rescue 进入到救援模式
[root@localhost ~]# systemctl emergency 进入到紧急模式
查看服务之间的依赖关系
[root@localhost ~]# systemctl list-dependencies multi-user.target
查看multi-user.target依赖谁
multi-user.target
● ├─atd.service
.
.
.
● ├─basic.target
● │ ├─-.mount
● │ ├─microcode.service
● │ ├─paths.target
● │ ├─slices.target
● │ │ ├─-.slice
● │ │ └─system.slice
● │ ├─sockets.target
● │ │ ├─avahi-daemon.socket
.
.
.
[root@localhost ~]# systemctl list-dependencies multi-user.target --reverse 查看谁依赖multi-user.target
multi-user.target
● └─graphical.target

小结

systemctl命令的使用

服务的启动

服务的停止

服务的状态

服务的自启动

systemctl查看服务信息

运行级别切换及常用的target单元

sytemctl的额外功能

依赖关系的查看

这些内容,包括回显内容都是重点,一定要熟记,都是以后要经常用到的命令。

难点:命令较多,建议在理解的基础上进行记忆,更容易一些

相关推荐

了解Linux目录,那你就了解了一半的Linux系统

大到公司或者社群再小到个人要利用Linux来开发产品的人实在是多如牛毛,每个人都用自己的标准来配置文件或者设置目录,那么未来的Linux则就是一团乱麻,也对管理造成许多麻烦。后来,就有所谓的FHS(F...

Linux命令,这些操作要注意!(linux命令?)

刚玩Linux的人总觉得自己在演黑客电影,直到手滑输错命令把公司服务器删库,这才发现命令行根本不是随便乱用的,而是“生死簿”。今天直接上干货,告诉你哪些命令用好了封神!喜欢的一键三连,谢谢观众老爷!!...

Linux 命令速查手册:这 30 个高频指令,拯救 90% 的运维小白!

在Linux系统的世界里,命令行是强大的武器。对于运维小白而言,掌握一些高频使用的Linux命令,能极大提升工作效率,轻松应对各种系统管理任务。今天,就为大家奉上精心整理的30个Linu...

linux必学的60个命令(linux必学的20个命令)

以下是Linux必学的20个基础命令:1.cd:切换目录2.ls:列出文件和目录3.mkdir:创建目录4.rm:删除文件或目录5.cp:复制文件或目录6.mv:移动/重命名文件或目录7....

提高工作效率的--Linux常用命令,能够决解95%以上的问题

点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf为什么要学习Linux命令?1、因为Li...

15 个实用 Linux 命令(linux命令用法及举例)

Linux命令行是系统管理员、开发者和技术爱好者的强大工具。掌握实用命令不仅能提高效率,还能解锁Linux系统的无限潜力,本文将深入介绍15个实用Linux命令。ls-列出目录内容l...

Linux 常用命令集合(linux常用命令全集)

系统信息arch显示机器的处理器架构(1)uname-m显示机器的处理器架构(2)uname-r显示正在使用的内核版本dmidecode-q显示硬件系统部件-(SMBIOS/DM...

Linux的常用命令就是记不住,怎么办?

1.帮助命令1.1help命令#语法格式:命令--help#作用:查看某个命令的帮助信息#示例:#ls--help查看ls命令的帮助信息#netst...

Linux常用文件操作命令(linux常用文件操作命令有哪些)

ls命令在Linux维护工作中,经常使用ls这个命令,这是最基本的命令,来写几条常用的ls命令。先来查看一下使用的ls版本#ls--versionls(GNUcoreutils)8.4...

Linux 常用命令(linux常用命令)

日志排查类操作命令查看日志cat/var/log/messages、tail-fxxx.log搜索关键词grep"error"xxx.log多条件过滤`grep-E&#...

简单粗暴收藏版:Linux常用命令大汇总

号主:老杨丨11年资深网络工程师,更多网工提升干货,请关注公众号:网络工程师俱乐部下午好,我的网工朋友在Linux系统中,命令行界面(CLI)是管理员和开发人员最常用的工具之一。通过命令行,用户可...

「Linux」linux常用基本命令(linux常用基本命令和用法)

Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们。总结送免费学习资料(包含视频、技术学习路线图谱、文档等)1、显示日期的指令:d...

Linux的常用命令就是记不住,怎么办?于是推出了这套教程

1.帮助命令1.1help命令#语法格式:命令--help#作用:查看某个命令的帮助信息#示例:#ls--help查看ls命令的帮助信息#netst...

Linux的30个常用命令汇总,运维大神必掌握技能!

以下是Linux系统中最常用的30个命令,精简版覆盖日常操作核心需求,适合快速掌握:一、文件/目录操作1.`ls`-列出目录内容`ls-l`(详细信息)|`ls-a`(显示隐藏文件)...

Linux/Unix 系统中非常常用的命令

Linux/Unix系统中非常常用的命令,它们是进行文件操作、文本处理、权限管理等任务的基础。下面是对这些命令的简要说明:**文件操作类:*****`ls`(list):**列出目录内容,显...