systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一块儿。linux
任务 | 旧指令 | 新指令 |
使某服务自动启动 | chkconfig --level 3 httpd on | systemctl enable httpd.service |
使某服务不自动启动 | chkconfig --level 3 httpd off | systemctl disable httpd.service |
检查服务状态 | service httpd status | systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service (仅显示是否 Active) |
显示全部已启动的服务 | chkconfig --list | systemctl list-units --type=service |
启动某服务 | service httpd start | systemctl start httpd.service |
中止某服务 | service httpd stop | systemctl stop httpd.service |
重启某服务 | service httpd restart | systemctl restart httpd.service |
CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令所有由systemctl命令来代替。centos
daemon命令 | systemctl命令 | 说明 |
---|---|---|
service [服务] start | systemctl start [unit type] | 启动服务 |
service [服务] stop | systemctl stop [unit type] | 中止服务 |
service [服务] restart | systemctl restart [unit type] | 重启服务 |
此外还有二个systemctl参数没有与service命令参数对应网络
应用举例:ssh
#启动网络服务 systemctl start network.service #中止网络服务 systemctl stop network.service #重启网络服务 systemctl restart network.service #查看网络服务状态 systemctl status network.serivce
daemon命令 | systemctl命令 | 说明 |
---|---|---|
chkconfig [服务] on | systemctl enable [服务] | 设置服务开机启动 |
chkconfig [服务] off | systemctl disable [服务] | 设备服务禁止开机启动 |
应用举例:socket
#中止cup电源管理服务 systemctl stop cups.service #禁止cups服务开机启动 systemctl disable cups.service #查看cups服务状态 systemctl status cups.service #从新设置cups服务开机启动 systemctl enable cups.service
命令格式:工具
systemctl [command] [–type=TYPE] [–all]
oop
参数详解:spa
command
- list-units:依据unit列出全部启动的unit。加上 –all 才会列出没启动的unit; - list-unit-files:依据/usr/lib/systemd/system/ 内的启动文件,列出启动文件列表.net
–type=TYPE
- 为unit type, 主要有service, socket, targethibernate
应用举例:
systemctl命令 | 说明 |
---|---|
systemctl | 列出全部的系统服务 |
systemctl list-units | 列出全部启动unit |
systemctl list-unit-files | 列出全部启动文件 |
systemctl list-units –type=service –all | 列出全部service类型的unit |
systemctl list-units –type=service –all grep cpu | 列出 cpu电源管理机制的服务 |
systemctl list-units –type=target –all | 列出全部target |
systemctl命令 | 说明 |
---|---|
systemctl is-active [服务] | 查看服务是否运行 |
systemctl is-enable [服务] | 查看服务是否设置为开机启动 |
systemctl mask [服务] | 注销指定服务 |
systemctl unmask [服务] | 取消注销指定服务 |
应用举例:
#查看网络服务是否启动 systemctl is-active network.service #检查网络服务是否设置为开机启动 systemctl is-enable network.service #中止cups服务 systemctl stop cups.service #注销cups服务 systemctl mask cups.service #查看cups服务状态 systemctl status cups.service #取消注销cups服务 systemctl unmask cups.service
init命令 | systemctl命令 | 说明 |
---|---|---|
init 0 | systemctl poweroff | 系统关机 |
init 6 | systemctl reboot | 从新启动 |
与开关机相关的其余命令:
systemctl命令 | 说明 |
---|---|
systemctl suspend | 进入睡眠模式 |
systemctl hibernate | 进入休眠模式 |
systemctl rescue | 强制进入救援模式 |
systemctl emergency | 强制进入紧急救援模式 |
init级别 | systemctl target |
---|---|
0 | shutdown.target |
1 | emergency.target |
2 | rescure.target |
3 | multi-user.target |
4 | 无 |
5 | graphical.target |
6 | 无 |
此外仍是一个getty.target用来设置tty的数量。
命令格式:
systemctl [command] [unit.target]
参数详解:
command:
systemctl命令 | 说明 |
---|---|
systemctl get-default | 得到当前的运行级别 |
systemctl set-default multi-user.target | 设置默认的运行级别为mulit-user |
systemctl isolate multi-user.target | 在不重启的状况下,切换到运行级别mulit-user下 |
systemctl isolate graphical.target | 在不重启的状况下,切换到图形界面下 |
命令格式:
systemctl list-dependencies [unit] [–reverse]
–reverse
是用来检查寻哪一个unit使用了这个unit
应用举例:
#得到当前运行级别的target [root@www ~]# systemctl get-default multi-user.target #查看当前运行级别target(mult-user)启动了哪些服务 [root@www ~]# systemctl list-dependencies default.target ├─abrt-ccpp.service ├─abrt-oops.service ├─vsftpd.service ├─basic.target │ ├─alsa-restore.service │ ├─alsa-state.service .....(中间省略)..... │ ├─sockets.target │ │ ├─avahi-daemon.socket │ │ ├─dbus.socket .....(中间省略)..... │ ├─sysinit.target │ │ ├─dev-hugepages.mount │ │ ├─dev-mqueue.mount .....(中间省略)..... │ └─timers.target │ └─systemd-tmpfiles-clean.timer ├─getty.target │ └─getty@tty1.service └─remote-fs.target #查看哪些target引用了当前运行级别的target [root@www ~]# systemctl list-dependencies --reverse default.target └─graphical.target
在使用systemctl关闭网络服务时有一些特殊 须要同时关闭unit.servce和unit.socket
使用systemctl查看开启的sshd服务
[root@www system]# systemctl list-units --all | grep sshd sshd-keygen.service loaded inactive dead OpenSSH Server Key Generation sshd.service loaded active running OpenSSH server daemon sshd.socket loaded inactive dead OpenSSH Server Socket
能够看到系统同时开启了 sshd.service
和 sshd.socket
, 若是只闭关了 sshd.service
那么 sshd.socket
还在监听网络,在网络上有要求链接 sshd 时就会启动 sshd.service
。所以若是想彻底关闭sshd服务的话,须要同时停用 sshd.service
和 sshd.socket
。
systemctl stop sshd.service systemctl stop sshd.socket systemctl disable sshd.service sshd.socket
因为centos 7.x默认没有安装net-tools,所以没法使用netstat 来查看主机开发的商品。须要经过yum安装来得到该工具包:
yum -y install net-tools
查看是否关闭22端口
netstat -lnp |grep sshd
Centos 7.x 中取消了iptables, 用firewall取而代之。要关闭防火墙并禁止开机启动服务使用下面的命令:
systemctl stop firewalld.service systemctl disable firewalld.service