一、概念梳理:
1、systemd:所有进程的父进程(Linux内核 3.0以上版本),通过 pstree 显示系统中的进程树,可以看到 systemd 是第一个。

2、systemctl:服务控制命令。用户管理各类型的 systemd 对象,这些对象称之为:单元。这些对象可以是:.service(服务单元)、.socket(套接字)、.path(路径单元)。其中最为常用.service(服务单元)。
3、ssh:远程协议,可以让远程主机通过网络访问 sshd 服务,开始一个安全shell,并对其进行操控。
4、sshd:一个服务,允许远程主机通过网络连接到服务器中使用 shell 的服务。
5、守护进程是一个在后台运行并且不受任何终端控制的进程。它们执行不同的管理任务。很多守护进程在系统引导的时候启动,并且一直运行直到系统关闭。有一些只在需要的时候才启动,完成任务后就自动结束。
二、服务控制命令
1、status:查看服务状态。
2、start:启动服务。
3、stop:暂停服务。
4、restart:重启加载服务。
5、 reload:重新加载服务配置文件(进程暂停,然后把配置文件加载进去后,继续执行后续操作。进程的 PID 不会发生变化。start、restart PID 会发生改变。)
6、 enable:设置开机自启服务。
7、 disable:设置开机不自启服务。
三、通过 sshd 服务做实验。
1、status:查看服务状态。
systemctl status sshd.service 查看 sshd 的服务状态(.service 可以省略,效果一样。)
红色下划线:sshd.service - OpenSSH server daemon 是一个守护进程。
黄色下划线:系统服务初始化完成,配置已加载
/usr/lib/systemd/system/sshd.service; enabled; —— sshd 服务开机自启(enabled:启动、disabled:开机不启动、static:无法启动,依赖于某个服务才能启动。)。
vendor preset: enabled —— 厂家预设:启动。
蓝色下划线:active (running) —— 正在运行
atcive(exited)—— 仅执行一次就正常结束的服务, 目前并沒有任何程序在系統中执行。在操作系统中,有某些服务只需要初始化一下就可以了;不需要在服务器中启动一个守护进程。这种服务初始化完成后就直接退出,其服务的状态就是 active(exited)。
active(waiting) —— 运行中,但正在等待事件才能继续。
Inactive—— 不在运行。
绿色下划线:PID 进程 ID 和 执行的命令、参数。

2、stop:暂停服务。
1)、暂停服务前先 systemctl status sshd 查看一下 sshd 的 PID,是 952。

2)、(1)、红色下划线:systemctl stop sshd 暂停 sshd 服务。
(2)、黄色下划线:systemctl status sshd 查看 sshd 的服务状态。此时,Active: inactive (dead) 不运行。

(3)、暂停了 sshd 服务,已经连接上 sshd 服务的远程终端(Xshell)可以继续运行。tail 和 ls 都可以继续操作系统。

(4)、但重新建立一个远程连接则失败,无法连接。

3、start:启动服务。
1)、暂停了的 sshd 服务 PID 是 952(蓝色下划线)。

2)、(1)、红色下划线:systemctl start sshd 启动 sshd 服务
(2)、黄色下划线:systemctl status sshd:查看 sshd 的服务状态。Active: active (running) 正在运行。PID变成了 1381。

(3)、重新启动的 sshd 服务,Xshell 已经可以远程连接登录。

tips:暂停了服务再启动服务 PID 会改变。
4、restart:重启加载服务。
1)、现在 sshd 的 PID 是 1381。

2)、(1)、红色下划线:systemctl restart sshd 重启加载 sshd 服务。
(2)、黄色下划线:systemctl status sshd 查看 sshd 的服务状态。Active: active (running) 正在运行。PID变成了 1435。

tips:重启加载服务 PID 会改变。
5、 reload:重新加载服务配置文件。
1)、现在 sshd 的 PID 是 1435。

2)、(1)、红色下划线:systemctl reload sshd 重新加载 sshd 服务配置文件。
(2)、黄色下划线:systemctl status sshd 查看 sshd 的服务状态。Active: active (running) 正在运行。PID还是 1435,没有改变。

tips:重新加载服务配置文件 PID 不会改变。
6、 disable:设置开机不自启服务。
1)、sshd 服务现在的开机启动状态是 enabled (启动状态)。

2)、(1)、红色下划线:systemctl disable sshd:设置开机不自启服务。
(2)、黄色下划线:提示删除 sshd.service 符号链接。
(3)、蓝色下划线:开机自启项变为 disabled:开机不启动。

3)、重启 linux 系统,试试设置了 sshd 开机不自启的情况下,Xshell 能不能远程连接。

4)、(1)、Linux 已经重启进入系统。由于设置开机不自启 sshd 服务,显然的重启后的 Linux,Xshell 不能远程连接。


(2)、Linux 上用 systemctl status sshd 查看 sshd 的服务状态。Active:inactive(dead)不运行。

(3)、只能手动开启 sshd 服务,Xshell 才能远程连接。


7、 enable:设置开机自启服务。
1)、红色下划线:disabled 开机不自启 sshd 服务。

2)、(1)、红色下划线:systemctl enable sshd 设置 sshd 服务开机自启。Created symlink 创建符号连接。
(2)、systemctl status sshd 查看 sshd 的服务状态,disabled 改为 enabled 开机自启。此时,开机时 sshd 服务都会自动启动。

systemctl list-units:列出已经开启的服务状态。有很多内容,需要翻页。

众多内容当中,可以找到例子里的 sshd 服务。

也可以通过 systemctl list-units | grep sshd 只列出 sshd 的服务状态。

systemctl list-unit-files:列出所有服务开机启动的状态。有很多内容,需要翻页。

同样,在众多内容当中,也可以找到例子里的 sshd 服务。enabled 开机自启的状态。

也可以通过 systemctl list-unit-files | grep sshd 只列出 sshd 的开机启动的状态。

tips:红帽 5 和 6 查看服务状态是:Service sshd status。
开机自启是:Chkconfig ssh on/off。
网友评论