CentOS 7开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替。

1、原来的 service 命令与 systemctl 命令对比

daemon命令 systemctl命令 说明
service [服务] start systemctl start [unit type] 启动服务
service [服务] stop systemctl stop [unit type] 停止服务
service [服务] restart systemctl restart [unit type] 重启服务

此外还是二个systemctl参数没有与service命令参数对应

  • status:参数来查看服务运行情况
  • reload:重新加载服务,加载更新后的配置文件(并不是所有服务都支持这个参数,比如network.service)

举例:

#启动网络服务
systemctl start network.service #停止网络服务
systemctl stop network.service #重启网络服务
systemctl restart network.service #查看网络服务状态
systemctl status network.serivce

2、原来的chkconfig 命令与 systemctl 命令对比

2.1、设置开机启动/不启动

daemon命令 systemctl命令 说明
chkconfig [服务] on systemctl enable [unit type] 设置服务开机启动
chkconfig [服务] off systemctl disable [unit type] 设备服务禁止开机启动

举例:

#停止cup电源管理服务
systemctl stop cups.service #禁止cups服务开机启动
systemctl disable cups.service #查看cups服务状态
systemctl status cups.service #重新设置cups服务开机启动
systemctl enable cups.service

2.2、查看系统上上所有的服务

命令格式:

systemctl [command] [–type=TYPE] [–all]

参数详解:

command - list-units:依据unit列出所有启动的unit。加上 –all 才会列出没启动的unit; - list-unit-files:依据/usr/lib/systemd/system/ 内的启动文件,列出启动文件列表

–type=TYPE - 为unit type, 主要有service, socket, target

举例:

systemctl命令 说明
systemctl 列出所有的系统服务
systemctl list-units 列出所有启动unit
systemctl list-unit-files 列出所有启动文件
systemctl list-units –type=service –all 列出所有service类型的unit
systemctl list-units –type=service –all grep cpu 列出 cpu电源管理机制的服务
systemctl list-units –type=target –all 列出所有target

3、systemctl特殊的用法

systemctl命令 说明
systemctl is-active [unit type] 查看服务是否运行
systemctl is-enable [unit type] 查看服务是否设置为开机启动
systemctl mask [unit type] 注销指定服务
systemctl unmask [unit type] 取消注销指定服务

举例:

#查看网络服务是否启动
systemctl is-active network.service #检查网络服务是否设置为开机启动
systemctl is-enable network.service #停止cups服务
systemctl stop cups.service #注销cups服务
systemctl mask cups.service #查看cups服务状态
systemctl status cups.service #取消注销cups服务
systemctl unmask cups.service

4、init 命令与systemctl命令对比

init命令 systemctl命令 说明
init 0 systemctl poweroff 系统关机
init 6 systemctl reboot 重新启动

与开关机相关的其他命令:

systemctl命令 说明
systemctl suspend 进入睡眠模式
systemctl hibernate 进入休眠模式
systemctl rescue 强制进入救援模式
systemctl emergency 强制进入紧急救援模式

5、设置系统运行级别

5.1、运行级别对应表

init级别 systemctl target
0 shutdown.target
1 emergency.target
2 rescure.target
3 multi-user.target
4
5 graphical.target
6

此外还是一个getty.target用来设置tty的数量。

5.2、设置运行级别

命令格式:

systemctl [command] [unit.target]

参数详情:

command

  • get-default :取得当前的target
  • set-default :设置指定的target为默认的运行级别
  • isolate :切换到指定的运行级别
  • unit.target :为5.1表中列出的运行级别
systemctl命令 说明
systemctl get-default 获得当前的运行级别
systemctl set-default multi-user.target 设置默认的运行级别为mulit-user
systemctl isolate multi-user.target 在不重启的情况下,切换到运行级别mulit-user下
systemctl isolate graphical.target 在不重启的情况下,切换到图形界面下

6、使用systemctl分析各服务之前的依赖关系

命令格式:

systemctl list-dependencies [unit] [–reverse]

–reverse是用来检查寻哪个unit使用了这个unit

举例:

#获得当前运行级别的target
[root@www ~]# systemctl get-default
multi-user.target #查看当前运行级别target(mult-user)启动了哪些服务
[root@www ~]# systemctl list-dependencies
default.target
├─abrt-ccpp.service
├─abrt-oops.service
├─vsftpd.service
├─basic.target
│ ├─alsa-restore.service
│ ├─alsa-state.service
.....(中间省略).....
│ ├─sockets.target
│ │ ├─avahi-daemon.socket
│ │ ├─dbus.socket
.....(中间省略).....
│ ├─sysinit.target
│ │ ├─dev-hugepages.mount
│ │ ├─dev-mqueue.mount
.....(中间省略).....
│ └─timers.target
│ └─systemd-tmpfiles-clean.timer
├─getty.target
│ └─getty@tty1.service
└─remote-fs.target #查看哪些target引用了当前运行级别的target
[root@www ~]# systemctl list-dependencies --reverse
default.target
└─graphical.target

7、关闭网络服务

在使用systemctl关闭网络服务时有一些特殊 需要同时关闭unit.servce和unit.socket

使用systemctl查看开启的sshd服务

[root@www system]#  systemctl list-units --all | grep sshd
sshd-keygen.service loaded inactive dead OpenSSH Server Key Generation
sshd.service loaded active running OpenSSH server daemon
sshd.socket loaded inactive dead OpenSSH Server Socket

可以看到系统同时开启了 sshd.service 和 sshd.socket , 如果只闭关了 sshd.service 那么 sshd.socket还在监听网络,在网络上有要求连接 sshd 时就会启动 sshd.service 。因此如果想完全关闭sshd服务的话,需要同时停用 sshd.service 和 sshd.socket 。

systemctl stop sshd.service
systemctl stop sshd.socket
systemctl disable sshd.service sshd.socket

由于centos 7.x默认没有安装net-tools,因此无法使用netstat 来查看主机开发的商品。需要通过yum安装来获得该工具包:

yum -y install net-tools

查看是否关闭22端口

netstat -lnp |grep sshd

8、关闭防火墙firewall

Centos 7.x 中取消了iptables, 用firewall取而代之。要关闭防火墙并禁止开机启动服务使用下面的命令:

systemctl stop firewalld.service
systemctl disable firewalld.service

CentOS7的systemctl使用的更多相关文章

  1. CentOS7利用systemctl添加自定义系统服务【转】

    systemctl enable name.service 设置开机启 systemctl disable name.service 删除开机启动指令 systemctl list-units --t ...

  2. CentOS7 tomcat systemctl脚本

    1,centos7 使用 systemctl 替换了 service命令 参考:redhat文档: https://access.redhat.com/documentation/en-US/Red_ ...

  3. [转帖]备忘:CentOS-7 使用systemctl 管理的服务,文件打开数上限1024要改

    备忘:CentOS-7 使用systemctl 管理的服务,文件打开数上限1024要改 https://blog.csdn.net/toontong/article/details/50440272 ...

  4. [转]CentOS7利用systemctl添加自定义系统服务

    原文:https://www.cnblogs.com/saneri/p/7778756.html CentOS7自定义系统服务 CentOS7的服务systemctl脚本存放在:/usr/lib/sy ...

  5. CentOS7利用systemctl添加自定义系统服务

    CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit].[Service]和[Install] 转载于互联网 [Unit] 部分主要是对这个服务的说明,内容包括Descrip ...

  6. Centos7 自定义systemctl服务脚本

    序言篇: 之前工作环境一直使用Centos6版本,脚本一直在使用/etc/init.d/xxx:系统升级到Cento7后,虽然之前的启动脚本也可以使用,但一直没有使用systemctl 的自定义脚本. ...

  7. CentOS7使用systemctl添加自定义服务

    一.简介 Centos7开机第一个程序从init完全换成了systemd这种启动方式,同centos 5 6已经是实质差别.systemd是靠管理unit的方式来控制开机服务,开机级别等功能. 在/u ...

  8. CentOS7 上systemctl

    CentOS 上systemctl 的用法 [日期:--] 来源:Linux社区 作者:Linux [字体:大 中 小] 我们对service和chkconfig两个命令都不陌生,systemctl ...

  9. Linux学习笔记之Centos7 自定义systemctl服务脚本

    0x00 概述 之前工作环境一直使用Centos6版本,脚本一直在使用/etc/init.d/xxx:系统升级到Cento7后,虽然之前的启动脚本也可以使用,但一直没有使用systemctl 的自定义 ...

  10. 对比centos7的systemctl和其他service+chkconfig

    syetemctl就是service和chkconfig这两个命令的整合,在CentOS 7就开始被使用了.systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig ...

随机推荐

  1. Django-Rest-Framework的版本和认证

    Django-Rest-Framework的版本和认证   restful framework DRF的版本 版本控制是做什么用的,我们为什么要用 首先要知道版本是干嘛用的,我们知道开发项目的时候有多 ...

  2. 题解 BZOJ 1037 & Luogu P2592 [ZJOI2008]生日聚会

    BZOJ & Luogu 老师说是背包?并没看出来QAQ 设f[i][j][o][p]表示已经选了i个人,j个男生,男生比女生最多多o个,女生比男生最多多p个时的方案数 两种转移: <= ...

  3. 2017"百度之星"程序设计大赛 - 初赛(B)小小粉丝度度熊

    Problem Description 度度熊喜欢着喵哈哈村的大明星——星星小姐. 为什么度度熊会喜欢星星小姐呢? 首先星星小姐笑起来非常动人,其次星星小姐唱歌也非常好听. 但这都不是最重要的,最重要 ...

  4. CoreCLR

    CoreCLR 在这篇中我将讲述GC Collector内部的实现, 这是CoreCLR中除了JIT以外最复杂部分,下面一些概念目前尚未有公开的文档和书籍讲到. 为了分析这部分我花了一个多月的时间,期 ...

  5. h5点击区域和实际区域对不上

    点击区域和实际区域对不上 然后点击后触发的其实是上面的区域,会导致事件触发错误

  6. c#基础2-out-ref

    //out参数要求在方法的内部 ; JiangJin(ref salary1); Console.WriteLine(salary1); Console.ReadKey(); 必须为其赋值 out.r ...

  7. AJPFX辨析Java中堆内存和栈内存的区别

    Java把内存分成两种,一种叫做栈内存,一种叫做堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,java就在栈中为这个变量分配内存空间 ...

  8. jsoup爬虫,项目实战,欢迎收看

    import com.mongodb.BasicDBObject import com.mongodb.DBCollection import org.jsoup.Jsoup import org.j ...

  9. css:focus伪类的使用

    css中:focus伪类的使用,即给已获取焦点的元素设置样式 示例一 <!DOCTYPE html> <html lang="en"> <head&g ...

  10. apache官方供下载所有项目所有版本的官方网站地址

    Apache官网有一个列举apache所有发布的项目的各个版本的官方网站,现在在此记录下来供大家快速浏览使用. 网站地址如下: http://archive.apache.org/dist/