Linux下systemctl和service和chkconfig的区别
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命令。
1 | [root@localhost ~]# service httpd start |
systemctl命令的用法
Systemctl命令简介:
Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。
Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。
systemd即为system daemon,是linux下的一种init软件。
Systemctl命令常见用法
列出所有可用单元
1
2
3
4
5
6
7
8
9
10
11[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列出所有可用单元
1
2
3
4
5
6
7
8
9
10
11[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
..............列出所有失败单元
1
2
3
4
5
6
7
8
9
10
11[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'.检查某个单元是否启动
1
2[root@localhost ~]# systemctl is-enabled httpd.service
enabled检查某个服务的运行状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18[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列出所有服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15[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启动,停止,重启服务等
1
2
3
4
5[root@localhost ~]# systemctl restart httpd.service
# systemctl restart httpd.service
# systemctl stop httpd.service
# systemctl reload httpd.service
# systemctl status httpd.service查询服务是否激活,和配置是否开机启动
1
2
3
4
5
6[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.使用systemctl命令杀死服务
1
[root@localhost ~]# systemctl kill httpd
列出系统的各项服务,挂载,设备等
1
2
3[root@localhost ~]# systemctl list-unit-files --type
automount device path snapshot swap timer
busname mount service socket target获得系统默认启动级别和设置默认启动级别
1
2
3[root@localhost ~]# systemctl get-default
graphical.target
[root@localhost ~]# systemctl set-default multi-user.target启动运行等级
1
systemctl isolate multiuser.target
重启、停止,挂起、休眠系统等
1
2
3
4
5# systemctl reboot
# systemctl halt
# systemctl suspend
# systemctl hibernate
# systemctl hybrid-sleep
Service命令用法
service命令可以启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。
service命令的作用是去/etc/init.d目录下寻找相应的服务,进行开启和关闭等操作。
使用示例
开启关闭一个服务:service httpd start/stop
1 | [root@localhost ~]# service httpd start |
查看系统服务的状态:service –status-all
1 | [root@localhost ~]# service --status-all |
chkconfig命令用法
chkconfig是管理系统服务(service)的命令行工具。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。
chkconfig可以更新(启动或停止)和查询系统服务(service)运行级信息。更简单一点,chkconfig是一个用于维护/etc/rc[0-6].d目录的命令行工具。
chkconfig常见用法
1 | [root@localhost ~]# chkconfig --help |
设置service开机是否启动
1 | chkconfig name on/off/reset |
- on、off、reset用于改变service的启动信息。
- on表示开启,off表示关闭,reset表示重置。
- 默认情况下,on和off开关只对运行级2,3,4,5有效,reset可以对所有运行级有效。
1 | [root@localhost ~]# chkconfig httpd on |
在Redhat7上,运行chkconfig命令,都会被转到systemcle命令上。
设置service运行级别
1 | chkconfig --level levels |
该命令可以用来指定服务的运行级别,即指定运行级别2,3,4,5等。
- 等级0表示:表示关机
- 等级1表示:单用户模式
- 等级2表示:无网络连接的多用户命令行模式
- 等级3表示:有网络连接的多用户命令行模式
- 等级4表示:不可用
- 等级5表示:带图形界面的多用户模式
- 等级6表示:重新启动
例如:
1 | [root@localhost ~]# chkconfig --level 5 httpd on |
列出service启动信息
1 | # chkconfig --list [name] |
如果不指定name,会列出所有services的信息。
每个service每个运行级别都会有一个启动和停止脚本;当切换运行级别时,init不会重启已经启动的service,也不会重新停止已经停止的service。
例如:
1 | [root@localhost ~]# chkconfig --list |
总结:service命令的功能基本都被systemctl取代。直接使用systemctl命令即可。