【Linux】一步一步学Linux——systemctl命令(147)

00. 目录

01. 命令概述

systemctl命令是系统服务管理器指令,它实际上将 servicechkconfig这两个命令组合到一起。

Centos7之后从init完全换成了systemd的启动方式,systemd 启动服务的机制主要是通过 systemctl 的这个系统服务管理指令来处理。systemctl在用法上也囊括 service / chkconfig / setup / init 的大部分功能。

任务旧指令新指令
使某服务自动启动chkconfig --level 3 httpd onsystemctl enable httpd.service
使某服务不自动启动chkconfig --level 3 httpd offsystemctl disable httpd.service
检查服务状态service httpd statussystemctl status httpd.service (服务详细信息) systemctl is-enabled httpd.service (仅显示是否 Active)
显示所有已启动的服务chkconfig --listsystemctl list-units --type=service
启动某服务service httpd startsystemctl start httpd.service
停止某服务service httpd stopsystemctl stop httpd.service
重启某服务service httpd restartsystemctl restart httpd.service
某服务重新加载配置文件service httpd reloadsystemctl reload httpd.service

02. 命令格式

格式:systemctl [选项...] {命令} ...

03. 常用选项

-start	启动服务
-stop	停止服务
-restart	重启服务
-enable	使某服务开机自启
-disable	关闭某服务开机自启
-status	查看服务状态
-list-units –type=service	列举所有已启动服务

04. 参考示例

4.1 输出所有已经启动服务

[deng@localhost ~]$ systemctl 

4.2 输出所有已经启动的单元

[deng@localhost ~]$ systemctl list-units

4.3 显示启动失败的服务

[deng@localhost ~]$ systemctl --failed
  UNIT            LOAD   ACTIVE SUB    DESCRIPTION
● network.service loaded failed failed LSB: Bring up/down networking
● postfix.service loaded failed failed Postfix Mail Transport Agent

4.4 查看所有已经安装的服务

[deng@localhost ~]$ systemctl list-unit-files
UNIT FILE                                     STATE   
proc-sys-fs-binfmt_misc.automount             static  
dev-hugepages.mount                           static  
dev-mqueue.mount                              static  
proc-fs-nfsd.mount                            static  
proc-sys-fs-binfmt_misc.mount                 static  
sys-fs-fuse-connections.mount                 static  

4.5 启动服务

[root@localhost ~]# systemctl start sshd

4.6 停止服务

[root@localhost ~]# systemctl stop sshd

4.7 重启服务

[root@localhost ~]# systemctl restart sshd

4.8 重新加载服务配置

[root@localhost ~]# systemctl reload sshd

4.9 查看服务状态

[root@localhost ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since 六 2019-08-17 20:56:41 CST; 1min 10s ago

4.10 查看服务是否开机启动

[root@localhost ~]# systemctl is-enabled sshd
enabled
[root@localhost ~]# 

4.11 开机自动启动服务

[root@localhost ~]# systemctl enable sshd
[root@localhost ~]# 

4.12 开机不启动服务

[root@localhost ~]# systemctl disable sshd
Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service.
[root@localhost ~]# 

4.13 显示服务帮助信息

[root@localhost ~]# systemctl help sshd

4.14 重新加载systemd

[root@localhost ~]# systemctl daemon-reload

4.15 重新启动系统

[root@localhost ~]# systemctl reboot

4.16 关机

[root@localhost ~]# systemctl poweroff 

4.17 休眠

[root@localhost ~]# systemctl hibernate 

hibernate:休眠模式则是将系统状态保存到硬盘当中,保存完毕后,将计算机关机。当用户尝试唤醒系统时,系统会开始正常运行,然后将保存在硬盘中的系统状态恢复回来。因为数据需要从硬盘读取,因此唤醒的速度比较慢(如果你使用的是 SSD 磁盘,唤醒的速度也是非常快的)。

4.18 待机

[root@localhost ~]# systemctl suspend 

suspend:暂停模式会将系统的状态保存到内存中,然后关闭掉大部分的系统硬件,当然,并没有实际关机。当用户按下唤醒机器的按钮,系统数据会从内存中回复,然后重新驱动被大部分关闭的硬件,所以唤醒系统的速度比较快。

4.19 混合休眠模式(同时休眠到硬盘并待机)

[root@localhost ~]# systemctl hybrid-sleep 

4.20 相当于telinit 3 或 telinit 5

[root@localhost ~]# systemctl isolate graphical.target 

graphical.target:就是文字界面再加上图形界面,这个 target 已经包含了下面的 multi-user.target。

multi-user.target:纯文本模式!

rescue.target:在无法使用 root 登陆的情况下,systemd 在开机时会多加一个额外的临时系统,与你原本的系统无关。这时你可以取得 root 的权限来维护你的系统。

emergency.target:紧急处理系统的错误,在无法使用 rescue.target 时,可以尝试使用这种模式!

shutdown.target:就是执行关机。

getty.target:可以设置 tty 的配置。

正常的模式是 multi-user.target 和 graphical.target 两个,救援方面的模式主要是 rescue.target 以及更严重的 emergency.target。如果要修改可提供登陆的 tty 数量,则修改 getty.target。

4.21 列出所有已经启动的服务

[root@localhost ~]# systemctl list-units --type=service

4.22 查看目前是否运行指定服务

[root@localhost ~]# systemctl is-active sshd
active
[root@localhost ~]# 

4.23 列出unit的配置

[root@localhost ~]# systemctl show

4.24 查看服务的配置

[root@localhost ~]# systemctl show sshd.service 

4.25 注销服务

[root@localhost ~]# systemctl mask sshd.service 
Created symlink from /etc/systemd/system/sshd.service to /dev/null.
[root@localhost ~]# 

4.26 反注销服务

[root@localhost ~]# systemctl unmask sshd.service 
Removed symlink /etc/systemd/system/sshd.service.
[root@localhost ~]# 

4.27 只查看服务类型的unit

[root@localhost ~]# systemctl list-units  --type=service --all
  UNIT                         LOAD      ACTIVE   SUB     DESCRIPTION
  abrt-ccpp.service            loaded    active   exited  Install ABRT coredump hoo
  abrt-oops.service            loaded    active   running ABRT kernel log watcher
  abrt-vmcore.service          loaded    inactive dead    Harvest vmcores for ABRT
  abrt-xorg.service            loaded    active   running ABRT Xorg log watcher
  abrtd.service                loaded    active   running ABRT Automated Bug Report
  accounts-daemon.service      loaded    active   running Accounts Service
  alsa-restore.service         loaded    inactive dead    Save/Restore Sound Card S
  alsa-state.service           loaded    active   running Manage Sound Card State (
● apparmor.service             not-found inactive dead    apparmor.service

4.28 进入救援模式

[root@localhost ~]# systemctl rescue 

4.29 进入紧急救援模式

[root@localhost ~]# systemctl emergency 

4.30 本机 socket 的服务

[root@localhost ~]# systemctl list-sockets 

05. 附录

参考:【Linux】一步一步学Linux系列教程汇总

参考:https://blog.csdn.net/skh2015java/article/details/94012643

  • 8
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
第11章 邮件服务器配置 《Linux服务器配置与管理》教课件—第-11-章--MySQL-服务器配置全文共59页,当前为第1页。 目录 11.6 数据库的备份与恢复 11.7 任务实战 11.5 MySQL 基本使用 11.4 MySQL 客户端 11.2 MySQL 软件的安装与运行 11.1 MySQL 简介 11.3 MySQL 服务器的配置与优化 《Linux服务器配置与管理》教课件—第-11-章--MySQL-服务器配置全文共59页,当前为第2页。 MySQL 简介 11.1 《Linux服务器配置与管理》教课件—第-11-章--MySQL-服务器配置全文共59页,当前为第3页。 MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle公司旗下的产品。由于 MySQL 性能高、成本低、可靠性好,已经成为目前非常流行的开源数据库,因此 MySQL 被广泛地应用在中小型网站中。随着 MySQL 的不断发展,它逐渐被用于更多大型集群网站和应用中,比如维基百科、Google 和 Facebook 等网站。非常流行的开源软件组合 LAMP 中的"M"指的就是 MySQL。 11.1 MySQL 简介 《Linux服务器配置与管理》教课件—第-11-章--MySQL-服务器配置全文共59页,当前为第4页。 MySQL软件的安装与运行 11.2 《Linux服务器配置与管理》教课件—第-11-章--MySQL-服务器配置全文共59页,当前为第5页。 1.添加系统 MySQL 5.7 仓库 CentOS 7系统默认仓库不再提供MySQL软件包,取而代之的是MySQL的分支——MariaDB。所以为了安装MySQL,需要添加一个yum仓库,MySQL官方提供了一个RPM软件包来创建yum仓库,执行如下命令进行安装: [root@kangvcar ~]# rpm -ivh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 11.2 MySQL软件的安装与运行 《Linux服务器配置与管理》教课件—第-11-章--MySQL-服务器配置全文共59页,当前为第6页。 2.启用仓库 在MySQL 5.7仓库安装完成后,会在"/etc/yum.repos.d/"目录下生成两个文件,即mysql-community.repo文件和mysql-community-source.repo文件,我们主要查看mysql-community.repo文件中的配置,在该文件中默认会包含几个版本(如mysql55-community、mysql56-community、mysql57-community、mysql80-community)的仓库条目以方便我们使用指定版本的MySQL。 此处选择安装MySQL 5.7,我们需要检查mysql-community.repo文件以确保mysql57-community条目下的enabled参数设置为1,命令如下: [root@kangvcar ~]# grep -A5 'mysql57' /etc/yum.repos.d/mysql-community.repo [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ enabled=1 ### 将参数的值设置为1,并将其他版本条目下的enabled参数的值设置为0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 在yum仓库配置完成后,可以使用如下命令进行验证: [root@kangvcar ~]# yum clean all ### 清除yum缓存[root@kangvcar ~]# yum repolist ### 检查yum源是否正常 11.2 MySQL软件的安装与运行 《Linux服务器配置与管理》教课件—第-11-章--MySQL-服务器配置全文共59页,当前为第7页。 3.安装 mysql-server [root@kangvcar ~]# yum -y install mysql-server 在执行该命令后,会安装mysql-server(服务器端软件)和mysql-client(客户端软件) 4.启动 MySQL 服务 [root@kangvcar ~]# systemctl start mysqld ### 启动 MySQL 服务器 [roo

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值