Linux下systemctl命令和service、chkconfig命令的区别


Linux下systemctl命令和service、chkconfig命令的区别

 

Systemctl和service、chkconfig命令的关系

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

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

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

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

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

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

systemctl命令的用法
Systemctl命令简介:
Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。

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

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

Systemctl命令常见用法:
(1)列出所有可用单元:

 1 [root@alwen ~]# systemctl list-unit-files
 2 UNIT FILE                                  STATE    
 3 proc-sys-fs-binfmt_misc.automount          static   
 4 -.mount                                    generated
 5 boot.mount                                 generated
 6 dev-hugepages.mount                        static   
 7 dev-mqueue.mount                           static   
 8 proc-sys-fs-binfmt_misc.mount              static   
 9 sys-fs-fuse-connections.mount              static   
10 sys-kernel-config.mount                    static   
11 sys-kernel-debug.mount                     static   
12 tmp.mount                                  disabled 
13 systemd-ask-password-console.path          static   
14 systemd-ask-password-wall.path             static   
15 session-1.scope                            transient
16 session-3.scope                            transient
17 session-4.scope                            transient
18 session-5.scope                            transient
19 session-6.scope                            transient
20 session-7.scope                            transient
21 arp-ethers.service                         disabled 
22 atd.service                                enabled  
23 auditd.service                             enabled  
24 autovt@.service                            enabled  
25 blk-availability.service                   disabled 
26 chrony-dnssrv@.service                     static   
27 chrony-wait.service                        disabled 
28 chronyd.service                            disabled 
29 cockpit-motd.service                       static   
30 cockpit-wsinstance-http-redirect.service   static

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

 1 [root@alwen ~]# systemctl list-units
 2 UNIT                                                            LOAD   ACTIVE SUB       DESCRIPTION              
 3 proc-sys-fs-binfmt_misc.automount                               loaded active waiting   Arbitrary Executable File>
 4 sys-devices-pci0000:00-0000:00:07.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged   VMwar>
 5 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged   VMwar>
 6 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plugged   VMwar>
 7 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged   VMware_Vir>
 8 sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loaded active plugged   82545EM Gigabit Etherne>
 9 sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device loaded active plugged   ES1371/ES1373 / Creat>
10 sys-devices-platform-serial8250-tty-ttyS1.device                loaded active plugged   /sys/devices/platform/ser>
11 sys-devices-platform-serial8250-tty-ttyS2.device                loaded active plugged   /sys/devices/platform/ser>
12 sys-devices-platform-serial8250-tty-ttyS3.device                loaded active plugged   /sys/devices/platform/ser>
13 sys-devices-pnp0-00:05-tty-ttyS0.device                         loaded active plugged   /sys/devices/pnp0/00:05/t>
14 sys-devices-virtual-block-dm\x2d0.device                        loaded active plugged   /sys/devices/virtual/bloc>
15 sys-devices-virtual-block-dm\x2d1.device                        loaded active plugged   /sys/devices/virtual/bloc>
16 sys-module-configfs.device                                      loaded active plugged   /sys/module/configfs     
17 sys-subsystem-net-devices-ens33.device                          loaded active plugged   82545EM Gigabit Ethernet >
18 -.mount                                                         loaded active mounted   Root Mount               
19 boot.mount                                                      loaded active mounted   /boot                    
20 dev-hugepages.mount                                             loaded active mounted   Huge Pages File System   
21 dev-mqueue.mount                                                loaded active mounted   POSIX Message Queue File >
22 run-user-0.mount                                                loaded active mounted   /run/user/0              
23 sys-kernel-config.mount                                         loaded active mounted   Kernel Configuration File>
24 sys-kernel-debug.mount                                          loaded active mounted   Kernel Debug File System 
25 systemd-ask-password-console.path                               loaded active waiting   Dispatch Pass

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

[root@alwen ~]# systemctl  --failed
0 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

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

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

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

 1 [root@alwen ~]# systemctl status httpd.service
 2 ● httpd.service - The Apache HTTP Server
 3    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
 4    Active: active (running) since Thu 2021-06-10 19:58:25 CST; 34min ago
 5      Docs: man:httpd.service(8)
 6  Main PID: 1919 (httpd)
 7    Status: "Total requests: 2; Idle/Busy workers 100/0;Requests/sec: 0.00096; Bytes served/sec:  96 B/sec"
 8     Tasks: 278 (limit: 11210)
 9    Memory: 48.0M
10    CGroup: /system.slice/httpd.service
11            ├─1919 /usr/sbin/httpd -DFOREGROUND
12            ├─1920 /usr/sbin/httpd -DFOREGROUND
13            ├─1921 /usr/sbin/httpd -DFOREGROUND
14            ├─1922 /usr/sbin/httpd -DFOREGROUND
15            ├─1923 /usr/sbin/httpd -DFOREGROUND
16            └─2148 /usr/sbin/httpd -DFOREGROUND
17 
18 Jun 10 19:58:25 alwen.com systemd[1]: Starting The Apache HTTP Server...
19 Jun 10 19:58:25 alwen.com systemd[1]: Started The Apache HTTP Server.
20 Jun 10 19:58:25 alwen.com httpd[1919]: Server configured, listening on: port 80

(6)列出所有服务:

 1 [root@alwen ~]# systemctl list-unit-files  --type=service
 2 UNIT FILE                                  STATE   
 3 arp-ethers.service                         disabled
 4 atd.service                                enabled 
 5 auditd.service                             enabled 
 6 autovt@.service                            enabled 
 7 blk-availability.service                   disabled
 8 chrony-dnssrv@.service                     static  
 9 chrony-wait.service                        disabled
10 chronyd.service                            disabled
11 cockpit-motd.service                       static  
12 cockpit-wsinstance-http-redirect.service   static  
13 cockpit-wsinstance-http.service            static  
14 cockpit-wsinstance-https-factory@.service  static  
15 cockpit-wsinstance-https@.service          static  
16 cockpit.service                            static  
17 console-getty.service                      disabled
18 container-getty@.service                   static  
19 cpupower.service                           disabled
20 crond.service                              enabled 
21 dbus-org.freedesktop.hostname1.service     static  
22 dbus-org.freedesktop.locale1.service       static  
23 dbus-org.freedesktop.login1.service        static  
24 dbus-org.freedesktop.nm-dispatcher.service enabled 
25 dbus-org.freedesktop.portable1.service     static  
26 dbus-org.freedesktop.timedate1.service     enabled 
27 dbus.service                               static  
28 debug-shell.service                        disabled
29 dm-event.service                           static  
30 dnf-makecache.service                      static 

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

1 [root@localhost ~]# systemctl restart httpd.service
2 # systemctl restart httpd.service
3 # systemctl stop httpd.service
4 # systemctl reload httpd.service
5 # systemctl status httpd.service

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

1 [root@alwen ~]# systemctl is-active httpd
2 active
3 [root@alwen ~]# systemctl disable httpd
4 [root@alwen ~]# systemctl enable httpd
5 Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
6 [root@alwen ~]# 

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

[root@alwen ~]# systemctl kill httpd

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

 1 [root@alwen ~]# systemctl list-unit-files 
 2 UNIT FILE                                  STATE    
 3 proc-sys-fs-binfmt_misc.automount          static   
 4 -.mount                                    generated
 5 boot.mount                                 generated
 6 dev-hugepages.mount                        static   
 7 dev-mqueue.mount                           static   
 8 proc-sys-fs-binfmt_misc.mount              static   
 9 sys-fs-fuse-connections.mount              static   
10 sys-kernel-config.mount                    static   
11 sys-kernel-debug.mount                     static   
12 tmp.mount                                  disabled 
13 systemd-ask-password-console.path          static   
14 systemd-ask-password-wall.path             static   
15 session-1.scope                            transient
16 session-3.scope                            transient
17 session-4.scope                            transient
18 session-5.scope                            transient
19 session-6.scope                            transient
20 session-7.scope                            transient
21 arp-ethers.service                         disabled 
22 atd.service                                enabled  

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

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

12)启动运行等级:

1 [root@alwen ~]# systemctl isolate multiuser.target
2 Failed to start multiuser.target: Operation refused, unit may not be isolated.
3 See system logs and 'systemctl status multiuser.target' for details.

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

1 # systemctl reboot
2 # systemctl halt
3 # systemctl suspend
4 # systemctl hibernate
5 # systemctl hybrid-sleep

Service命令用法

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

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

使用示例:

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

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

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

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

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

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

chkconfig常见用法:

1 [root@alwen ~]# chkconfig --help
2 chkconfig version 1.13 - Copyright (C) 1997-2000 Red Hat, Inc.
3 This may be freely redistributed under the terms of the GNU Public License.
4 
5 usage:   chkconfig [--list] [--type <type>] [name]
6          chkconfig --add <name>
7          chkconfig --del <name>
8          chkconfig --override <name>
9          chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>

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

1  chkconfig name on/off/reset
  • on、off、reset用于改变service的启动信息。
  • on表示开启,off表示关闭,reset表示重置。
  • 默认情况下,on和off开关只对运行级2,3,4,5有效,reset可以对所有运行级有效。
    1 [root@alwen ~]# chkconfig httpd on
    2 Note: Forwarding request to 'systemctl enable httpd.service'.
    [root@localhost ~]# chkconfig httpd on
    注意:正在将请求转发到“systemctl enable httpd.service”。

  在Redhat7上,运行chkconfig命令,都会被转到systemcle命令上。

2)设置service运行级别:

1 [root@alwen ~]# chkconfig --level levels
2 chkconfig version 1.13 - Copyright (C) 1997-2000 Red Hat, Inc.
3 This may be freely redistributed under the terms of the GNU Public License.
4 
5 usage:   chkconfig [--list] [--type <type>] [name]
6          chkconfig --add <name>
7          chkconfig --del <name>
8          chkconfig --override <name>
9          chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>

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

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

例如:

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

(三)列出service启动信息:

1 # chkconfig --list [name]

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

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

例如:

 1 [root@localhost ~]# chkconfig --list
 2 
 3 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
 4       如果您想列出 systemd 服务,请执行 'systemctl list-unit-files' 5       欲查看对特定 target 启用的服务请执行
 6       'systemctl list-dependencies [target]' 7 
 8 netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
 9 network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
10 rhnsd           0:关    1:关    2:开    3:开    4:开    5:开    6:关
 1 [root@alwen ~]# systemctl list-dependencies
 2 default.target
 3 ● ├─atd.service
 4 ● ├─auditd.service
 5 ● ├─crond.service
 6 ● ├─dbus.service
 7 ● ├─httpd.service
 8 ● ├─irqbalance.service
 9 ● ├─kdump.service
10 ● ├─libstoragemgmt.service
11 ● ├─mcelog.service
12 ● ├─mdmonitor.service
13 ● ├─NetworkManager.service
14 ● ├─nvmefc-boot-connections.service
15 ● ├─smartd.service
16 ● ├─sshd.service
17 ● ├─sssd.service
18 ● ├─systemd-ask-password-wall.path
19 ● ├─systemd-logind.service
20 ● ├─systemd-update-utmp-runlevel.service
21 ● ├─systemd-user-sessions.service
22 ● ├─tuned.service
23 ● ├─vdo.service
24 ● ├─basic.target
25 ● │ ├─-.mount
26 ● │ ├─microcode.service
27 ● │ ├─paths.target
28 ● │ ├─slices.target
29 ● │ │ ├─-.slice
30 ● │ │ └─system.slice

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

网址:https://cshihong.blog.csdn.net/article/details/83081881?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujs&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujs

 

查看linux系统的运行级别

 

查看当前系统的运行级别
[root@apenglinux ~]# runlevel
3 5
查看系统的默认级别
[root@apenglinux ~]# systemctl get-default
graphical.target


设置系统的默认运行级别为3
[root@apenglinux ~]# systemctl set-default multi-user.target

[root@apenglinux ~]# rm -f /etc/systemd/system/default.target
[root@apenglinux ~]# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target


设置系统的默认运行级别为5
[root@apenglinux ~]# systemctl set-default graphical.target
[root@apenglinux ~]# rm -f /etc/systemd/system/default.target
[root@apenglinux ~]# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target


从3级别切换到5级别
init 5
systemctl isolate graphical.target


从5级别切换到3级别
init 3
systemctl isolate multi-user.target


查看系统的运行级别
[root@apenglinux ~]# ls -l /lib/systemd/system/runlevel*target
lrwxrwxrwx. 1 root root 15 1月 18 17:03 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 1月 18 17:03 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 1月 18 17:03 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 1月 18 17:03 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 1月 18 17:03 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 1月 18 17:03 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 1月 18 17:03 /lib/systemd/system/runlevel6.target -> reboot.target

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM