服务器电源配置实战教程:新手也能一次成功
准备工作:你需要知道的信息和工具
开始前先确认三件事:你的服务器操作系统(这里以 CentOS 7 / Ubuntu 22.04 为例)、是否已安装 cpupower 或 tuned 等工具、以及是否具有 root 权限(或 sudo 权限)。
如果还没安装工具,可以执行下面命令补上:
# CentOS / RHEL 系列
yum install -y kernel-tools tuned
# Ubuntu / Debian 系列
sudo apt update
sudo apt install -y linux-tools-common tuned
另外,建议先备份关键配置,避免误操作影响业务。
---
查看当前服务器电源状态
很多新手最容易忽略第一步——先看清当前运行模式。
使用 cpupower 可以快速查看 CPU 频率和调速器(governor):
sudo cpupower frequency-info
关注输出中的 current policy 和 available governors 部分。
比如看到 powersave 或 ondemand 就说明目前偏节能。
如果你想要最大性能,需要切换到 performance 模式。
除此之外,还可以用 tuned-adm 查看系统级的电源优化方案:
sudo tuned-adm list
输出的列表里会标明当前激活的 profile(前面有 * 号),比如 throughput-performance 或 powersave。
---
调整电源模式,提升性能或节能
确认当前状态后,根据实际需求修改。
这里讲两种常用方式:
方法一:手动设置 CPU 调速器
# 将所有 CPU 核心设为 performance 模式
sudo cpupower frequency-set -g performance
如果要恢复默认,可以用 -g ondemand 或 -g schedutil(取决于内核版本)。
这个修改会在重启后失效,如果想持久化,需要写进 rc.local 或 systemd 服务。
方法二:使用 tuned 切换整体电源方案
# 切换到高性能方案(推荐服务器场景)
sudo tuned-adm profile throughput-performance
# 或 balance 方案(兼顾性能与功耗)
sudo tuned-adm profile balanced
# 查看当前 profile
sudo tuned-adm active
throughput-performance 会自动把 CPU governor 设为 performance,并调整磁盘、网络等参数,比手动改更全面。
适合数据库、Web 服务等对响应速度敏感的场景。
---
常见错误和避坑要点
错误 1:修改后无权限
Operation not permitted
这是缺少 root 权限或 BIOS 锁定了电源管理。
检查是否使用 sudo,或者进 BIOS 开启 Intel Turbo Boost 和 C-States。
错误 2:tuned 服务未启动
Failed to connect to Tuned socket
执行 sudo systemctl start tuned && sudo systemctl enable tuned 即可。
避坑提示:
- 生产服务器不要直接设为
performance模式,建议先用balanced测试几天,观察负载和温度。 - 修改 BIOS 电源策略(如 Dell iDRAC 或 HP iLO)前,务必记录原始值,并确认服务器有冗余电源,防止意外断电。
- 如果使用虚拟机(如 KVM 或 VMware),宿主机电源配置会影响所有虚拟机,需要统一规划。
---
如何验证电源配置已经生效
修改完成后,用以下命令双重确认:
# 查看当前 CPU 调速器
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor | sort -u
# 应该输出 performance 或你设置的模式
# 查看 tuned 激活的 profile
sudo tuned-adm active
# 监控实际频率是否稳定
watch -n 1 'cat /proc/cpuinfo | grep MHz'
如果你切换到 performance 模式,频率应该长时间保持在最高值附近(除非系统过热或限制)。
如果发现频率仍然上下波动,可能是受 intel_pstate 驱动影响,可以添加内核参数 intel_pstate=disable 并重启测试。
最后,建议用 stress 或 sysbench 加压验证性能提升(可选)。
完成所有步骤后,你的服务器电源配置就已经按预期生效了。