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

service 命令

service命令是Redhat Linux兼容的发行版中用来控制系统服务的实用工具,它以启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。

语法: service < option > | --status-all | [ service_name [ command | --full-restart ] ]

option 的值

  • -h:显示 service 的帮助信息

  • -status:显示所服务的状态

  • --status-all:查看所有服务的状态

  • service_name:服务名,即 /etc/init.d 目录下的脚本文件名

  • command:系统服务脚本支持的控制命令,如:start、stop 和 restart

  • --full-restart:重启所有服务

实例:查看 service 的帮助信息

1

2

3

[root@localhost ~]# service -h

Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]

[root@localhost ~]#

实例2:查看所有的服务状态

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

[root@centos-x64 ~]# service --status-all

auditd (pid  1299) is running...

Stopped

cgred is stopped

crond (pid  1481) is running...

Table: filter

Chain INPUT (policy ACCEPT)

num  target     prot opt source               destination

1    ACCEPT     all      ::/0                 ::/0                state RELATED,ESTABLISHED

2    ACCEPT     icmpv6    ::/0                 ::/0

3    ACCEPT     all      ::/0                 ::/0

4    ACCEPT     tcp      ::/0                 ::/0                state NEW tcp dpt:22

5    ACCEPT     tcp      ::/0                 ::/0                state NEW tcp dpt:80

6    REJECT     all      ::/0                 ::/0                reject-with icmp6-adm-prohibited

Chain FORWARD (policy ACCEPT)

num  target     prot opt source               destination

1    REJECT     all      ::/0                 ::/0                reject-with icmp6-adm-prohibited

实例3: 使用 service 启动/重启/停止网络服务

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

[root@centos-x64 ~]# service network restart

Shutting down interface eth0:                              [  OK  ]

Shutting down loopback interface:                          [  OK  ]

Bringing up loopback interface:                            [  OK  ]

Bringing up interface eth0:                                [  OK  ]

[root@centos-x64 ~]# service network start

Bringing up loopback interface:               [ OK ]

Bringing up interface eth0:                   [ OK ]

[root@centos-x64 ~]# service network stop

Bringing dwon interface eth0:                   [ OK ]

Bringing down loopback interface:               [ OK ]

[root@centos-x64 ~]# service network status

Configured devices:

lo eth0

Currently active devices:

lo eth0

systemctl 命令

历史上,Linux 的启动一直采用init进程。下面的命令用来启动服务。

1

2

3

sudo /etc/init.d/apache2 start

# 或者

$ service apache2 start

这种方法有两个缺点:

  • 一是启动时间长。init 进程是串行启动,只有前一个进程启动完,才会启动下一个进程。

  • 二是启动脚本复杂。init 进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长

Systemd 就是为了解决上面问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。根据 Linux 惯例,字母 d 是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。使用了 Systemd,就不需要再用 init 了。Systemd 取代了 initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。

1

2

3

4

[root@localhost ~]# systemctl --version

systemd 219

+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

[root@localhost ~]#

Systemd 的优点是功能强大,使用方便,缺点是体系庞大,非常复杂。事实上,现在还有很多人反对使用 Systemd,理由就是它过于复杂,与操作系统的其他部分强耦合,违反 “keep simple, keep stupid” 的Unix 哲学。

实例1:systemctl常用命令

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# 重启系统

sudo systemctl reboot

# 关闭系统,切断电源

sudo systemctl poweroff

# CPU停止工作

sudo systemctl halt

# 暂停系统

sudo systemctl suspend

# 让系统进入冬眠状态

sudo systemctl hibernate

# 让系统进入交互式休眠状态

sudo systemctl hybrid-sleep

# 启动进入救援状态(单用户状态)

sudo systemctl rescue

实例2:检查systemd和systemctl的二进制文件和库的安装位置。

1

2

3

4

# whereis systemd 

systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz

# whereis systemctl

systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

实例3:检查systemd是否正在运行

1

2

3

4

5

6

# ps -eaf | grep [s]ystemd

root         1     0  0 16:27 ?        00:00:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 23

root       444     1  0 16:27 ?        00:00:00 /usr/lib/systemd/systemd-journald

root       469     1  0 16:27 ?        00:00:00 /usr/lib/systemd/systemd-udevd

root       555     1  0 16:27 ?        00:00:00 /usr/lib/systemd/systemd-logind

dbus       556     1  0 16:27 ?        00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation

实例4:列出所有服务(包括启用和禁用)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# systemctl list-unit-files --type=service

UNIT FILE                                   STATE

arp-ethers.service                          disabled

auditd.service                              enabled

autovt@.service                             disabled

blk-availability.service                    disabled

brandbot.service                            static

collectd.service                            disabled

console-getty.service                       disabled

console-shell.service                       disabled

cpupower.service                            disabled

crond.service                               enabled

dbus-org.fedoraproject.FirewallD1.service   enabled

....

service 与 systemctl 命令对比

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

systemctl和service区别相关推荐

  1. systemctl和service

    一直以为这两个功能是一样的,某些情况下systemctl比service功能还要强,但是工作中遇到了问题 在这里,用service操作libvirt-bin,但是无法用systemctl处理.查阅资料 ...

  2. 查看systemctl或service启动服务日志

    查看systemctl或service启动服务日志_mikelv01的博客-CSDN博客_systemctl 查看日志 journalctl -u minio.service 微信扫一扫:关注我个人订 ...

  3. SQL内置系统账户:Local system/Network service/Local Service 区别

    内置系统账户:Local system/Network service/Local Service 区别 参考文献: http://www.cnblogs.com/xianspace/archive/ ...

  4. SQL2008系统账户:Local system/Network service/Local Service 区别

    内置系统账户:Local system/Network service/Local Service 区别 LocalSystem   账户 LocalSystem是预设的拥有本机所有权限的本地账户,这 ...

  5. centos7重新加载服务的命令_Centos7 systemctl添加service服务参数说明

    Centos7可以通过systemctl执行服务命令,同时支持自定义service服务文件来进行一系列的标准执行. 常用命令 systemctl daemon-reload systemctl ena ...

  6. systemctl自定义service

    应用场景:开启自启动运行脚本/usr/local/manage.sh 一.服务介绍 CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户 ...

  7. CentOS7防火墙设置;Linux防火墙设置;systemctl -- firewalld.service;firewall;firewall-cmd

    1.查看firewall服务状态 systemctl status firewalld 2.查看firewall的状态 firewall-cmd --state 3.开启.重启.关闭.firewall ...

  8. systemctl与service

    1. 引言 首先简单说一下service命令,systemctl比他的功能更丰富一些,service命令是Redhat Linux兼容的发行版中用来控制系统服务的实用工具,它以启动.停止.重新启动和关 ...

  9. 内置系统账户:Local system/Network service/Local Service 区别

    参考文献: http://www.cnblogs.com/xianspace/archive/2009/04/05/1429835.html 前言 今天在安装sqlserver2008 r2的时候,在 ...

最新文章

  1. java list_Java集合-List
  2. JPA(二)之CRUD操作
  3. Linux学习笔记01
  4. [bzoj2159]Crash 的文明世界
  5. jboss ejb_使用JBoss AS 7进行SSL加密的EJB调用
  6. python更新es数据_python操作es增删改查
  7. android listview 向上自动滚动效果,Android通过代码控制ListView上下滚动的方法
  8. Flutter教程app
  9. vuejs 外部嵌套from表单
  10. 【原创】公司各个阶段 CTO 需要做什么?(上篇)
  11. 简述Java三大特性
  12. linux hping3命令,系列H - hping3 - 测试网络及主机的安全 - 《Linux命令大全》 - 技术池(jishuchi.com)...
  13. 10个python办公黑科技,助你办公效率提高100倍
  14. 如何制作ISO镜像文件
  15. Pytorch系列之——nn网络层
  16. 【触动精灵】开发手册学习整理(一)
  17. 三维形体的数据结构(1)半边数据结构
  18. 7.25 10figting!
  19. *java面试题**
  20. 数睿数据深度 | 中国软件网对话数睿数据总裁刘超:深挖数据驱动、企业级无代码

热门文章

  1. 2022年数维杯国际赛C题 如何利用大脑结构诊断阿尔茨海默氏病
  2. 2719 sheldon数
  3. 华为云实战 之 对象存储的使用以及与腾讯云COS对比
  4. 多位博士毕业去了三四流高校,目前惨不忍睹……
  5. 十进制转化成二、八、十六进制的一个小程序
  6. 【GDOI 2016】第四题 疯狂动物城
  7. 客户端访问网站的整个流程图_如何阻止整个国家访问您的网站
  8. 在Linux系统下实现进程,在Linux2.6内核下实现进程隐藏
  9. 黑马Python教程实战项目--美多商城(五)
  10. BYOD策略的制定关乎企业网络安全