service命令,顾名思义,就是用于管理Linux操作系统中服务的命令。
这个命令不是在所有的linux发行版本中都有。主要是在redhat、fedora、mandriva和centos中。
查看所有服务当前的运行状态:
┌──(root㉿kali)-[~/dahe]
└─# service --status-all
[ - ] apache-htcacheclean
[ - ] apache2
[ - ] apparmor
[ + ] atd
[ - ] atftpd
[ - ] avahi-daemon
[ + ] binfmt-support
[ - ] bluetooth
[ - ] console-setup.sh
...
查看指定服务的运行状态:
┌──(root㉿kali)-[~/dahe]
└─# service sudo status
○ sudo.service
Loaded: masked (Reason: Unit sudo.service is masked.)
Active: inactive (dead)
停止指定服务:
service 服务名 stop
重启某个服务:
service 服务名 restart
chkconfig实用程序是一个命令行工具,允许你在指定运行级别下启动所选服务,以及列出所有可用服务及其当前设置。
此外,它还允许我们从启动中启用或禁用服务。前提是你有超级管理员权限(root 或者sudo)运行这个命令。
所有的服务脚本位于
/etc/init.d文件中🫒
设定mysqld在等级3和5为开机运行服务,--level 35表示操作只在等级3和5执行,on表示启动,off表示关闭:
chkconfig --level 35 mysqld on
设定mysqld在各等级为on,“各等级”包括2、3、4、5等级:
chkconfig mysqld on
systemd核心概念unit(单元)类型:unit表示不同类型的systemd对象,通过配置文件进行标识和配置; 文件中主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息
systemd的配置文件目录:
/usr/lib/systemd/system/
查看所有的systemd指令管理的服务:
┌──(root💀kali)-[~/Desktop]
└─# ls -l /usr/lib/systemd/system/ 1 ⨯
total 1236
-rw-r--r-- 1 root root 419 Nov 13 2020 apache2.service
-rw-r--r-- 1 root root 491 Nov 13 2020 apache2@.service
-rw-r--r-- 1 root root 603 Sep 1 2020 apache-htcacheclean.service
-rw-r--r-- 1 root root 612 Sep 1 2020 apache-htcacheclean@.service
-rw-r--r-- 1 root root 1162 Apr 3 2021 apparmor.service
-rw-r--r-- 1 root root 326 Apr 13 2021 apt-daily.service
-rw-r--r-- 1 root root 156 Apr 13 2021 apt-daily.timer
-rw-r--r-- 1 root root 389 Apr 13 2021 apt-daily-upgrade.service
-rw-r--r-- 1 root root 184 Apr 13 2021 apt-daily-upgrade.timer
-rw-r--r-- 1 root root 686 Aug 3 2016 auth-rpcgss-module.service
lrwxrwxrwx 1 root root 14 Apr 12 2021 autovt@.service -> getty@.service
-rw-r--r-- 1 root root 1044 Feb 6 2021 avahi-daemon.service
-rw-r--r-- 1 root root 870 Feb 6 2021 avahi-daemon.socket
-rw-r--r-- 1 root root 927 Feb 2 2021 basic.target
-rw-r--r-- 1 root root 1159 Apr 17 2020 binfmt-support.service
-rw-r--r-- 1 root root 449 Feb 2 2021 blockdev@.target
...
查看服务的状态:
查看httpd服务的状态:
systemctl status httpd
运行状态:
启动状态:

查看服务的开机启动状态:
查看关于ssh的服务的运行状态:
┌──(root💀kali)-[~/Desktop]
└─# systemctl list-unit-files | grep ssh
ssh.service disabled disabled
ssh@.service static -
ssh.socket disabled disabled
rescue-ssh.target static -
停止某个服务:
systemctl stop 服务名
启动某个服务:
systemctl start 服务名
查看某个服务是否是开机启动的:
查看ssh服务是否开机自启:(如下表示开机不自启)
┌──(root💀kali)-[~/Desktop]
└─# systemctl is-enabled ssh
disabled
设置某一服务开机自启:
设置ssh服务开机自启:
┌──(root💀kali)-[~/Desktop]
└─# systemctl enable ssh 1 ⨯
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ssh
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
┌──(root💀kali)-[~/Desktop]
└─# systemctl is-enabled ssh
enabled
设置某一服务开机不自启动:
设置ssh服务开机不自启动:
┌──(root💀kali)-[~/Desktop]
└─# systemctl disable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable ssh
Removed /etc/systemd/system/sshd.service.
Removed /etc/systemd/system/multi-user.target.wants/ssh.service.
┌──(root💀kali)-[~/Desktop]
└─# systemctl is-enabled ssh
disabled