0

0

Linux下systemctl、service与chkconfig命令的区别详解

藏色散人

藏色散人

发布时间:2023-01-29 17:01:13

|

2611人浏览过

|

来源于github

转载

本篇文章给大家带来了关于linux的相关知识,其中主要介绍了systemctl和service、chkconfig命令的关系,以及区别介绍,下面一起来看一下,希望对大家有帮助。

Linux下systemctl、service与chkconfig命令的区别详解

systemctl和service、chkconfig命令的关系

  • systemctl命令:是一个systemd工具,主要负责控制systemd系统和服务管理器。
  • service命令:可以启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。
  • chkconfig命令:是管理系统服务(service)的命令行工具。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。

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

systemctl是RHEL 7 的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。可以使用它永久性或只在当前会话中启用/禁用服务。

所以systemctl命令是service命令和chkconfig命令的集合和代替。

例如:使用service启动服务实际上也是调用systemctl命令。

[root@localhost ~]# service httpd start
Redirecting to /bin/systemctl start  httpd.service

systemctl命令的用法

Systemctl命令简介:

Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。

Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

systemd即为system daemon,是linux下的一种init软件。

Systemctl命令常见用法:

(1)列出所有可用单元:

[root@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  
sys-kernel-config.mount                    static  
sys-kernel-debug.mount                     static  
tmp.mount                                  disabled
var-lib-nfs-rpc_pipefs.mount               static  
brandbot.path                              disabled
cups.path                                  enabled

(2)列出所有可用单元:

[root@localhost ~]# systemctl list-units
  UNIT                      LOAD      ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount loaded    active waiting   Arbitrary 
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb
  sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loade
  sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device lo
 ..............

(3)列出所有失败单元:

[root@localhost ~]# systemctl --failed 
  UNIT                LOAD   ACTIVE SUB    DESCRIPTION
● network.service     loaded failed failed LSB: Bring up/down networking
● teamd@team0.service loaded failed failed Team Daemon for device team0

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB
SUB    = The low-level unit activation state, values depend on unit type.

2 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

(4)检查某个单元是否启动:

[root@localhost ~]# systemctl is-enabled httpd.service 
enabled

(5)检查某个服务的运行状态:

[root@localhost ~]# systemctl status httpd.service     
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2018-10-14 18:21:46 CST; 1 day 2h ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 19020 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─19020 /usr/sbin/httpd -DFOREGROUND
           ├─27310 /usr/sbin/httpd -DFOREGROUND
           ├─27311 /usr/sbin/httpd -DFOREGROUND
           ├─27312 /usr/sbin/httpd -DFOREGROUND
           ├─27313 /usr/sbin/httpd -DFOREGROUND
           └─27314 /usr/sbin/httpd -DFOREGROUND

10月 14 18:21:46 localhost systemd[1]: Starting The Apache HTTP Serv....
10月 14 18:21:46 localhost httpd[19020]: AH00558: httpd: Could not r...e

(6)列出所有服务:

[root@localhost ~]# systemctl list-unit-files --type=service
UNIT FILE                                  STATE   
abrt-ccpp.service                          enabled 
abrt-oops.service                          enabled 
abrt-pstoreoops.service                    disabled
abrt-xorg.service                          enabled 
abrtd.service                              enabled 
accounts-daemon.service                    enabled 
alsa-restore.service                       static  
alsa-state.service                         static  
alsa-store.service                         static  
arp-ethers.service                         disabled
atd.service                                disabled
auditd.service                             enabled 
auth-rpcgss-module.service                 static

(7)启动,停止,重启服务等:

[root@localhost ~]# systemctl restart httpd.service
# systemctl restart httpd.service
# systemctl stop httpd.service
# systemctl reload httpd.service
# systemctl status httpd.service

(8)查询服务是否激活,和配置是否开机启动:

[root@localhost ~]# systemctl is-active httpd
active
[root@localhost ~]# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
[root@localhost ~]# systemctl enable httpd 
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

(9)使用systemctl命令杀死服务:

[root@localhost ~]# systemctl kill httpd

(10)列出系统的各项服务,挂载,设备等:

[root@localhost ~]# systemctl list-unit-files --type 
automount  device     path       snapshot   swap       timer
busname    mount      service    socket     target

(11)获得系统默认启动级别和设置默认启动级别:

[root@localhost ~]# systemctl get-default 
graphical.target
[root@localhost ~]# systemctl set-default multi-user.target

(12)启动运行等级:

红墨
红墨

一站式小红书图文生成器

下载
systemctl isolate multiuser.target

(13)重启、停止,挂起、休眠系统等:

# systemctl reboot
# systemctl halt
# systemctl suspend
# systemctl hibernate
# systemctl hybrid-sleep

Service命令用法

service命令可以启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。

service命令的作用是去/etc/init.d目录下寻找相应的服务,进行开启和关闭等操作。

使用示例:

开启关闭一个服务:service  httpd  start/stop

[root@localhost ~]# service httpd start
Redirecting to /bin/systemctl start  httpd.service

查看系统服务的状态:service –status-all

[root@localhost ~]# service --status-all
未加载 netconsole 模块
已配置设备:
lo ens33 ens33.old team0 team0-port1 team0-port1.old team0-port2 team0-port2.old team0.old
当前活跃设备:
lo ens33 virbr0 ens38 ens39 team0
● rhnsd.service - LSB: Starts the Spacewalk Daemon
   Loaded: loaded (/etc/rc.d/init.d/rhnsd; bad; vendor preset: disabled)
   Active: active (running) since 五 2018-10-12 14:53:19 CST; 3 days ago
     Docs: man:systemd-sysv-generator(8)
 Main PID: 1380 (rhnsd)
   CGroup: /system.slice/rhnsd.service
           └─1380 rhnsd

chkconfig命令用法

chkconfig是管理系统服务(service)的命令行工具。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。

chkconfig可以更新(启动或停止)和查询系统服务(service)运行级信息。更简单一点,chkconfig是一个用于维护/etc/rc[0-6].d目录的命令行工具。

chkconfig常见用法:

[root@localhost ~]# chkconfig  --help
chkconfig 版本 1.7.2 - 版权 (C) 1997-2000 红帽公司
在 GNU 公共许可条款下,本软件可以自由重发行。

用法:   chkconfig [--list] [--type <类型>] [名称]
         chkconfig --add <名称>
         chkconfig --del <名称>
         chkconfig --override <名称>
         chkconfig [--level <级别>] [--type <类型>] <名称> 

(一)设置service开机是否启动:

chkconfig name on/off/reset
  • on、off、reset用于改变service的启动信息。
  • on表示开启,off表示关闭,reset表示重置。
  • 默认情况下,on和off开关只对运行级2,3,4,5有效,reset可以对所有运行级有效。

[root@localhost ~]# chkconfig httpd on
注意:正在将请求转发到“systemctl enable httpd.service”。
在Redhat7上,运行chkconfig命令,都会被转到systemcle命令上。

(2)设置service运行级别:

chkconfig --level levels

该命令可以用来指定服务的运行级别,即指定运行级别2,3,4,5等。

  • 等级0表示:表示关机
  • 等级1表示:单用户模式
  • 等级2表示:无网络连接的多用户命令行模式
  • 等级3表示:有网络连接的多用户命令行模式
  • 等级4表示:不可用
  • 等级5表示:带图形界面的多用户模式
  • 等级6表示:重新启动

例如:

[root@localhost ~]# chkconfig --level 5 httpd on
注意:正在将请求转发到“systemctl enable httpd.service”

(三)列出service启动信息:

# chkconfig --list [name]

如果不指定name,会列出所有services的信息。

每个service每个运行级别都会有一个启动和停止脚本;当切换运行级别时,init不会重启已经启动的service,也不会重新停止已经停止的service。

例如:

[root@localhost ~]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
rhnsd           0:关    1:关    2:开    3:开    4:开    5:开    6:关

总结:service命令的功能基本都被systemct取代。直接使用systemctl命令即可。

推荐学习:《Linux视频教程

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

4

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

7

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

42

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

4

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6.3万人学习

Git 教程
Git 教程

共21课时 | 2.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号