本文档风哥主要介绍Linux系统中无用服务的禁用方法和系统轻量化实战,包括服务分析、禁用步骤、系统优化、生产环境实战案例等内容,参考Red Hat Enterprise Linux 10官方文档中的System administration章节,适合系统管理员在生产环境中使用。更多视频教程www.fgedu.net.cn
Part01-基础概念与理论知识
1.1 系统轻量化概念
系统轻量化是指通过禁用不必要的服务和进程,减少系统资源占用,提高系统性能和安全性。轻量化的系统具有启动速度快、资源占用低、安全性高等优点。学习交流加群风哥微信: itpux-com
- 减少系统资源占用
- 提高系统启动速度
- 增强系统安全性
- 降低系统维护成本
- 延长硬件使用寿命
1.2 服务管理基础
服务管理是系统轻量化的核心,通过systemctl命令可以管理系统服务的启动、停止、启用和禁用。
$ systemctl status [service]
# 启动服务
$ sudo systemctl start [service]
# 停止服务
$ sudo systemctl stop [service]
# 重启服务
$ sudo systemctl restart [service]
# 启用服务(开机自启)
$ sudo systemctl enable [service]
# 禁用服务(禁止开机自启)
$ sudo systemctl disable [service]
# 查看所有服务状态
$ systemctl list-units –type=service
# 查看所有已启用的服务
$ systemctl list-unit-files –type=service –state=enabled
1.3 服务分类
系统服务可以分为以下几类:
- 必要服务:系统运行必需的服务,如systemd、journald等
- 常用服务:根据系统用途需要的服务,如httpd、mysql等
- 可选服务:根据具体需求选择启用的服务
- 无用服务:不需要的服务,如bluetooth、avahi等
Part02-生产环境规划与建议
2.1 规划策略
规划策略:
- 需求分析:分析系统的具体用途和所需服务
- 服务评估:评估每个服务的必要性
- 风险评估:评估禁用服务可能带来的风险
- 实施计划:制定服务禁用的实施计划
- 验证测试:验证禁用服务后的系统状态
2.2 服务评估
服务评估方法:
- 功能分析:分析服务的功能和用途
- 依赖分析:分析服务的依赖关系
- 使用频率:评估服务的使用频率
- 资源占用:评估服务的资源占用情况
- 安全风险:评估服务的安全风险
2.3 风险评估
风险评估:
$ systemctl list-dependencies [service]
# 查看服务的启动顺序
$ systemctl list-dependencies –before [service]
# 查看服务的依赖服务
$ systemctl list-dependencies –after [service]
# 测试禁用服务后的系统状态
$ sudo systemctl disable [service]
$ sudo systemctl stop [service]
# 验证系统功能
$ systemctl status [critical-service]
Part03-生产环境项目实施方案
3.1 服务分析
服务分析步骤:
$ systemctl list-units –type=service
# 查看已启用的服务
$ systemctl list-unit-files –type=service –state=enabled
# 查看服务的资源占用
$ ps aux | grep [service]
# 查看服务的启动时间
$ systemctl show [service] | grep ActiveEnterTimestamp
# 查看服务的日志
$ journalctl -u [service]
3.2 服务禁用
服务禁用步骤:
$ sudo systemctl disable –now bluetooth
sudo systemctl disable –now avahi-daemon
sudo systemctl disable –now cups
sudo systemctl disable –now postfix
sudo systemctl disable –now firewalld
# 验证服务状态
$ systemctl status bluetooth avahi-daemon cups postfix firewalld
● bluetooth.service – Bluetooth service
Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; disabled; vendor preset: enabled)
Active: inactive (dead)
● avahi-daemon.service – Avahi mDNS/DNS-SD Stack
Loaded: loaded (/usr/lib/systemd/system/avahi-daemon.service; disabled; vendor preset: enabled)
Active: inactive (dead)
● cups.service – CUPS Scheduler
Loaded: loaded (/usr/lib/systemd/system/cups.service; disabled; vendor preset: enabled)
Active: inactive (dead)
● postfix.service – Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled; vendor preset: disabled)
Active: inactive (dead)
● firewalld.service – firewalld – dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
3.3 系统优化
系统优化步骤:
$ sudo find /tmp -type f -atime +7 -delete
# 清理日志文件
$ sudo journalctl –vacuum-time=1d
# 优化系统启动
$ sudo systemctl mask syslog.service
# 禁用不需要的系统定时器
$ sudo systemctl list-timers
$ sudo systemctl disable [timer]
# 优化内核参数
$ sudo vim /etc/sysctl.d/99-optimization.conf
# 添加以下内容
vm.swappiness = 10
vm.overcommit_memory = 1
fs.file-max = 65536
net.core.somaxconn = 65535
# 加载内核参数
$ sudo sysctl -p
# 查看系统启动时间
$ systemd-analyze
Startup finished in 1.234s (kernel) + 2.345s (userspace) = 3.579s
# 查看启动过程中耗时较长的服务
$ systemd-analyze blame
1.234s httpd.service
567ms mariadb.service
345ms sshd.service
234ms NetworkManager.service
Part04-生产案例与实战讲解
4.1 Web服务器优化案例
案例:Web服务器优化
$ systemctl list-unit-files –type=service –state=enabled
# 禁用不需要的服务
$ sudo systemctl disable –now bluetooth
sudo systemctl disable –now avahi-daemon
sudo systemctl disable –now cups
sudo systemctl disable –now postfix
sudo systemctl disable –now firewalld
# 只保留必要的服务
$ systemctl list-unit-files –type=service –state=enabled
UNIT FILE STATE
autovt@.service enabled
getty@.service enabled
httpd.service enabled
mariadb.service enabled
sshd.service enabled
systemd-journald.service enabled
systemd-logind.service enabled
systemd-networkd.service enabled
systemd-resolved.service enabled
systemd-timesyncd.service enabled
# 优化Apache配置
$ sudo vim /etc/httpd/conf/httpd.conf
# 调整以下参数
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 256
MaxConnectionsPerChild 10000
# 重启Apache服务
$ sudo systemctl restart httpd
# 验证服务状态
$ systemctl status httpd
● httpd.service – The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2026-04-06 13:00:00 CST; 1min ago
# 测试Web服务器性能
$ ab -n 1000 -c 100 http://localhost/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 1000 requests
Finished 1000 requests
Server Software: Apache/2.4.53
Server Hostname: localhost
Server Port: 80
Document Path: /
Document Length: 612 bytes
Concurrency Level: 100
Time taken for tests: 0.123 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 838000 bytes
HTML transferred: 612000 bytes
Requests per second: 8130.08 [#/sec] (mean)
Time per request: 12.300 [ms] (mean)
Time per request: 0.123 [ms] (mean, across all concurrent requests)
Transfer rate: 6653.04 [Kbytes/sec] received
4.2 数据库服务器优化案例
案例:数据库服务器优化
$ systemctl list-unit-files –type=service –state=enabled
# 禁用不需要的服务
$ sudo systemctl disable –now bluetooth
sudo systemctl disable –now avahi-daemon
sudo systemctl disable –now cups
sudo systemctl disable –now postfix
sudo systemctl disable –now firewalld
sudo systemctl disable –now httpd
# 只保留必要的服务
$ systemctl list-unit-files –type=service –state=enabled
UNIT FILE STATE
autovt@.service enabled
getty@.service enabled
mariadb.service enabled
sshd.service enabled
systemd-journald.service enabled
systemd-logind.service enabled
systemd-networkd.service enabled
systemd-resolved.service enabled
systemd-timesyncd.service enabled
# 优化MySQL配置
$ sudo vim /etc/my.cnf
# 添加以下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve
key_buffer_size=256M
max_allowed_packet=16M
table_open_cache=256
sort_buffer_size=1M
read_buffer_size=1M
read_rnd_buffer_size=4M
myisam_sort_buffer_size=64M
thread_cache_size=8
query_cache_size=16M
max_connections=100
# 重启MySQL服务
$ sudo systemctl restart mariadb
# 验证服务状态
$ systemctl status mariadb
● mariadb.service – MariaDB 10.5 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2026-04-06 13:30:00 CST; 1min ago
# 测试MySQL性能
$ mysqlslap –user=root –password=password –host=localhost –concurrency=50 –iterations=10 –query=”SELECT * FROM fgedu_test_table”
Benchmark
Average number of seconds to run all queries: 0.056 seconds
Minimum number of seconds to run all queries: 0.045 seconds
Maximum number of seconds to run all queries: 0.078 seconds
Number of clients running queries: 50
Average number of queries per client: 1
4.3 最小化服务器配置案例
案例:最小化服务器配置
# 在安装过程中选择”Minimal Install”
# 安装后查看服务状态
$ systemctl list-unit-files –type=service –state=enabled
# 禁用不需要的服务
$ sudo systemctl disable –now bluetooth
sudo systemctl disable –now avahi-daemon
sudo systemctl disable –now cups
sudo systemctl disable –now postfix
sudo systemctl disable –now firewalld
sudo systemctl disable –now NetworkManager
sudo systemctl enable –now network
sudo systemctl disable –now wpa_supplicant
# 安装必要的软件包
$ sudo dnf install -y openssh-server vim curl wget
# 配置网络
$ sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0
# 添加以下内容
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
# 重启网络服务
$ sudo systemctl restart network
# 验证网络连接
$ ping -c 3 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=12.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=11.9 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=118 time=12.1 ms
— 8.8.8.8 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 11.932/12.114/12.308/0.168 ms
# 查看系统启动时间
$ systemd-analyze
Startup finished in 0.876s (kernel) + 1.234s (userspace) = 2.110s
# 查看系统资源占用
$ top -b -n 1 | head -20
top – 14:00:00 up 5 min, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 89 total, 1 running, 88 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3945.1 total, 3700.0 free, 120.0 used, 125.1 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 3725.0 avail Mem
Part05-风哥经验总结与分享
5.1 优化技巧
优化技巧:
- 服务评估:定期评估系统服务的必要性
- 逐步禁用:逐步禁用不需要的服务,避免一次性禁用过多
- 监控验证:禁用服务后监控系统状态
- 备份配置:备份服务配置文件,便于恢复
- 文档记录:记录禁用的服务和原因
5.2 最佳实践
最佳实践:
- 按需启用:只启用必要的服务
- 定期清理:定期清理临时文件和日志
- 优化配置:优化服务配置参数
- 监控系统:监控系统资源使用情况
- 安全加固:禁用不必要的服务提高安全性
5.3 风哥建议
风哥建议:
- 制定标准:制定服务管理的标准流程
- 培训团队:培训团队成员的服务管理技能
- 持续优化:根据系统需求不断调整服务配置
- 安全第一:优先考虑系统安全性
- 文档化:建立服务管理文档
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
