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