13.1 服务概述
1.服务是通过运⾏程序提供的
2.在linux中 - 通常由守护进程提供服务
windows中的服务查看 - services.msc
程序类型:
#可进行网络通信的程序
#1. 客户端-程序 client - - 提供客户端访问请求功能
1.1 客户端普通程序 - 提供客户端访问请求功能
无需持续开启 - 只需要在进⾏访问时开启即可
1.2 客户端服务程序 - 提供客户端访问请求功能/服务
需要持续打开 - - 持续在内存驻留 - 持续提供服务
dhcp client
#2. 服务端-服务程序 service/server - 提供接受-处理从客户端发送来的访问请求-服务
#不可进行网络通信的程序
# 1. 本地普通程序
无需持续开启 - 只需要在进行访问时开启即可 例:c:\windows\notepad.exe
# 2. 本地服务程序
需要持续打开 - 持续在内存驻留 - 持续提供服务
系统用户身份鉴权服务
#普通程序 和 服务程序区别 在于: 普通程序随⽤随开, 服务程序持续打开
#如果服务程序没有打开-无法提供该服务
#系统本地的服务和服务之间可以进行关联
(服务1可以保护服务2 , 通过服务1可以开启服务2,通过程序实现)
#服务程序类型
1.客户端服务程序
2.服务端服务程序
3.本地无网络服务程序
#重点关注的服务程序
linux 服务器系统/windows服务器系统 ----- 对外提供服务 |系统中安装⼤量的服务端服务程序
* 普通个人主机 - 主要安装客户端(浏览器客户端,APP客户端)
* 服务器 (服务端服务程序 Web-apache(httpd) )
#服务一定是一个程序 (运行中的 - 进程 / 停⽌的)
#程序不⼀定是服务
#服务访问方式
C/S - 架构 | 普通客户端 - 访问服务器
B/S - 架构 | 浏览器客户端 - 访问服务器
13.2 systemd-服务核心框架
#1.系统中服务 - 拉活方式
系统在开机过程中,需要拉活必要的服务
在linux中系统 - 系统服务可以通过守护进程拉活,通常是由系统中第⼀个守护进程拉活
#2.系统中第⼀个守护进程
不同linux版本 - 系统中第⼀个守护进程不同 | linux kernal version
/usr/lib/systemd/systemd
/sbin/init
kernel. 守护进程
2.6.32 - init (systemV - init)
3.10.0 - systemd
5.4.0 - systemd
[clh@localhost ~]$ uname -a #查看内核
Linux localhost.localdomain 3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[clh@localhost ~]$ uname -r -v #查看内核简要信息
3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022 #内核版本3.10.0
内核版本3.0以后 由systemd 拉活,内核版本2.0(centos 6)由/sbin/init 拉活 例:
[clh@localhost ~]$ ps aux | head -2
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 193972 6004 ? Ss 8月24 0:09 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 #进程PID 1,命令是/usr/lib/systemd/systemd
#3. init - systemd 区别
init - 串行拉活 / 服务控制/管理命令 - service
[root@centos6 ~]# service sshd status
[root@centos6 ~]# /etc/init.d/sshd status
systemd - 并行拉活 / 服务控制/管理命令 - systemctl (system control)
[root@centos7 tom]# systemctl status sshd.service #*补充: 部分操作系统 - systemd系统管理中 可以向下兼容init管理命令
[root@centos7 tom]# service sshd status Redirecting to /bin/systemctl status sshd.service
[clh@localhost ~]$ sudo systemctl status sshd.service
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since 日 2025-08-24 00:18:38 CST; 2 days ago
#*补充: 部分操作系统 - systemd系统管理中 可以向下兼容init管理命令
[root@centos7 tom]# service sshd status #不是管理,是命令重定向转换/bin/ 如下:
Redirecting to /bin/systemctl status sshd.service
#4. systemd中引入了 [unit] 单元
系统启动过程中 - 开启大量初始化工作任务 -每个任务叫做一个unit 单元 - 每个单元就是systemd管理对象
1.开启各种服务进程/
2.挂载文件系统/
3.开启网络socket监
# 服务是systemd中的单元类型,但是单元类型不只包括服务,单元类型件#7
#5. systemd [unit] 单元查看
[clh@localhost ~]$ systemctl list-unit-files #列出所有可用单元,将出现400多个单元(401) 有static(静态的)/disabled(开机禁用的)/ enabled(开机启用的)/ indirect
[clh@localhost ~]$ systemctl list-units #列出所有运⾏单元
[clh@localhost ~]$ systemctl list-unit-files --type=service #列出所有可⽤服务单元
[clh@localhost ~]$ systemctl list-units --type=service #列出所有运⾏服务单元
[clh@localhost ~]$ systemctl list-units --type=service #查看所有运行服务单元--并列出状态
[clh@localhost ~]$ systemctl list-units --type=service --all #查看所有服务单元,包括为运行的,失败的,死亡的
#6. systemd [unit] 单元配置文件 - 启动脚本(*了解)
/usr/lib/systemd/system
ExecStart=/usr/sbin/NetworkManager --no-daemon
[root@localhost ~]# ls /usr/lib/systemd/system
[root@localhost ~]# ls /usr/lib/systemd/system | grep -i NetworkManager
[root@localhost ~]# ls /usr/lib/systemd/system | grep service
[root@localhost ~]# ls /usr/lib/systemd/system | grep -i NetworkManager
NetworkManager-dispatcher.service
NetworkManager.service
NetworkManager-wait-online.service
[root@localhost system]# cat /usr/lib/systemd/system NetworkManager.service
#每一个.service都是一个配置文件,配置文件是一个启动脚本,systemd作为第一个守护进程去拉活NetworkManager,找到启动脚本,启动脚本有个unit单元叫NetworkManager(配置文件)
#7. systemd [unit] 单元类型 (*了解) (重点关注服务单元类型)
[root@localhost system]# systemctl -t help # -t 查看类型 help 查看帮助
Available unit types:
service #服务
socket #服务
busname #总线
target #目标器
snapshot #快照
device #设备
mount #挂载
automount
swap
timer
path
slice
scope
device unit : ⽤于定义内核识别的设备单元
service unit : ⽤于定义普通系统服务单元
target unit : 模拟系统“运⾏级别”
socket unit : ⽤于表示进程之间通信的socker⽹络⽂件。可以在系统启动时,设置延时启动服务,也可以根据实际⽹络需求-按需启动。
[root@localhost system]# systemctl list-dependencies #查看单元之间依赖关系
#8. systemd[unit] 单元状态查看
[root@localhost system]# systemctl list-units --type=service
enabled --- 开机⾃动启动
disabled --- 开机禁⽌⾃动启动
static --- 开机不⾃动启动,可以被其他服务拉活
LOAD
loaded ------------ 已经成功加载unit-单元配置⽂件
unloaded(not-found) --- 加载 unit-单元配置⽂件失败
ACTIVE
active - 处于运⾏状态
inactive - 处于⾮运⾏状态
[root@localhost system]# systemctl is-active sshd.service #查看sshd这个服务是否active
active
SUB
running(持续运⾏)
exited (已经成功完成⼀次运⾏)
waiting(正在等待事件) dead (死亡)
13.3 systemd-服务基础管理
# 1.服务状态查看
[clh@localhost ~]$ systemctl status bluetooth.service #查看蓝牙服务状态
[clh@localhost ~]$ service bluetooth.service status #查看蓝牙服务状态,老版本命令
Redirecting to /bin/systemctl status bluetooth.service #上面命令重定向
● bluetooth.service - Bluetooth service
Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since 日 2025-08-24 00:18:30 CST; 2 days ago #运行状态,时间、时长
Docs: man:bluetoothd(8)
Main PID: 721 (bluetoothd) #进程主ID
Status: "Running" #运行状态
Tasks: 1 #任务数量
CGroup: /system.slice/bluetooth.service #服务的资源隔离,控制群组
└─721 /usr/libexec/bluetooth/bluetoothd
8月 24 00:18:30 localhost.localdomain systemd[1]: Starting Bluetooth service... #服务的日志信息
8月 24 00:18:30 localhost.localdomain bluetoothd[721]: Bluetooth daemon 5.44
8月 24 00:18:30 localhost.localdomain bluetoothd[721]: Starting SDP server
8月 24 00:18:30 localhost.localdomain systemd[1]: Started Bluetooth service.
*服务名称/服务描述
*服务加载的配置⽂件 - 其他相关信息
*服务状态详细信息 - 服务启动时间,运⾏时间
*服务man文档
*服务主进程ID
*控制群组
*服务日志
[root@localhost ~]# systemctl is-active bluetooth.service #查看服务是否开启
active
[root@localhost ~]# systemctl is-active sshd.service #查看服务是否开启
active
# 2.服务启用/开启
[root@localhost ~]# systemctl start bluetooth.service
[root@localhost ~]# service bluetooth.service start
Redirecting to /bin/systemctl start bluetooth.service
# 3.服务禁用/关闭
[root@localhost ~]# systemctl stop bluetooth.service
[root@localhost ~]# service bluetooth.service stop
Redirecting to /bin/systemctl stop bluetooth.service
*默认情况下-向对应的主进程ID-发送kill -15 (KILL SIGTERM)
正常关闭如下:
Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 二 2025-08-26 13:06:57 CST; 1min 35s ago
Docs: man:bluetoothd(8)
Process: 721 ExecStart=/usr/libexec/bluetooth/bluetoothd (code=exited, status=0/SUCCESS)
Main PID: 721 (code=exited, status=0/SUCCESS)
Status: "Quitting"
# 4.服务强制关闭 - 当系统无法进行关闭/重启,服务又无法正常关闭时。
[root@localhost ~]# systemctl kill -s 15 bluetooth.service #发送kill 15信号,正常关闭
[root@localhost ~]# systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 二 2025-08-26 13:15:46 CST; 4s ago
Docs: man:bluetoothd(8)
Process: 49772 ExecStart=/usr/libexec/bluetooth/bluetoothd (code=exited, status=0/SUCCESS)
[root@localhost ~]# systemctl start bluetooth.service
[root@localhost ~]# systemctl kill bluetooth.service #采用kill默认也是正常关闭,如下红色
[root@localhost ~]# systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 二 2025-08-26 13:17:11 CST; 5s ago
Docs: man:bluetoothd(8)
Process: 49892 ExecStart=/usr/libexec/bluetooth/bluetoothd (code=exited, status=0/SUCCESS)
[root@localhost ~]# systemctl start bluetooth.service
[root@localhost ~]# systemctl kill -s 9 bluetooth.service #-9 强杀,强制关闭,如下红色标示
[root@localhost ~]# systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: failed (Result: signal) since 二 2025-08-26 13:20:00 CST; 4s ago
Docs: man:bluetoothd(8)
Process: 49945 ExecStart=/usr/libexec/bluetooth/bluetoothd (code=killed, signal=KILL)
Main PID: 49945 (code=killed, signal=KILL)
Status: "Running"
# 5.服务重启 - (stop /start)服务中断
[root@localhost ~]# systemctl restart bluetooth.service
[root@localhost ~]# service bluetooth.service restart
Redirecting to /bin/systemctl restart bluetooth.service
# 6.服务重载 - 重新加载 (服务所对应的配置⽂件)-服务不中断 (服务配置文件需要包含Execreload命令参数)
[root@localhost ~]# systemctl reload bluetooth.service
[root@localhost ~]# service bluetooth.service reload
Redirecting to /bin/systemctl reload bluetooth.service
Failed to reload bluetooth.service: Job type reload is not applicable for unit bluetooth.service.
See system logs and 'systemctl status bluetooth.service' for details. #Failed to reload
[root@localhost ~]# systemctl reload NetworkManager.service #重载成功
[root@localhost ~]#
[root@localhost ~]# more /usr/lib/systemd/system/bluetooth.service #查看配置文件
[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth
[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/libexec/bluetooth/bluetoothd #配置文件中只有ExecStar,没有Execreload
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
ProtectHome=true
ProtectSystem=full
[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service
[root@localhost ~]# more /usr/lib/systemd/system/NetworkManager.service
ExecReload=/usr/bin/dbus-send --print-reply --system --type=method_call --dest=org.freede
sktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.Reloa
d uint32:0
#ExecReload=/bin/kill -HUP $MAINPID #配置文件中有reload,所以可以重载成功
ExecStart=/usr/sbin/NetworkManager --no-daemon
13.4 systemd-服务自动启动管理
主机系统在开机时,是否自动加载-开启对应的服务
#1 查看自动启动状态
[root@localhost ~]# systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled) # enabled 开机自启动
[root@localhost ~]# systemctl is-enabled bluetooth.service #查看服务是否 开机自启动
enabled
#2 禁⽌⾃动启动 (可以手动拉活)
[root@localhost ~]# systemctl disable bluetooth.service #禁止开机自启动
Removed symlink /etc/systemd/system/dbus-org.bluez.service. #Removed开机自启动相关配置文件,移除的是etc的配置文件,并不是lib下的配置文件
Removed symlink /etc/systemd/system/bluetooth.target.wants/bluetooth.service.
[root@localhost ~]#
[root@localhost ~]# systemctl is-enabled bluetooth.service #再次查看,此时disabled
disabled
[root@centos7 tom]# chkconfig bluetooth off #老版本用chkconfig命令
#3 开启⾃动启动
[root@localhost ~]# systemctl enable bluetooth.service
Created symlink from /etc/systemd/system/dbus-org.bluez.service to /usr/lib/systemd/system/bluetooth.service.
Created symlink from /etc/systemd/system/bluetooth.target.wants/bluetooth.service to /usr/lib/systemd/system/bluetooth.service.
[root@localhost ~]# systemctl is-enabled bluetooth.service
enabled
[root@centos7 tom]# chkconfig bluetooth on #老版本用chkconfig命令
13.5 systemd-服务冲突管理
network / NetworkManager
Firewalld / iptables
发⽣冲突服务建议禁⽤掉其中⼀个
#禁用服务 (⽆法⼿动拉活)
[root@centos7 tom]# systemctl mask NetworkManager.service
Created symlink from /etc/systemd/system/NetworkManager.service to /dev/null.
#取消禁⽤服务
[root@centos7 tom]# systemctl unmask NetworkManager.service
Removed symlink /etc/systemd/system/NetworkManager.service.