Files
ubuntumanager/fix_network_sleep.sh
2025-10-06 19:10:57 +08:00

136 lines
4.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 功能修复Ubuntu Server长时间无网络活动导致网络关闭的问题
# 使用方法sudo bash fix_network_sleep.sh
# 检查是否以root权限运行
if [ "$(id -u)" -ne 0 ]; then
echo "错误请使用root权限运行此脚本sudo bash $0" >&2
exit 1
fi
# 检测网卡名称(获取第一个活动的以太网接口)
get_active_nic() {
# 排除lo和docker等虚拟接口获取第一个物理以太网接口
nic=$(ip -br link show | grep -v 'LOOPBACK' | grep -v 'DOCKER' | grep 'UP' | awk '{print $1}' | head -n 1)
if [ -z "$nic" ]; then
# 如果没有找到活动接口,获取第一个以太网接口
nic=$(ip -br link show | grep -v 'LOOPBACK' | grep -v 'DOCKER' | awk '{print $1}' | head -n 1)
fi
echo "$nic"
}
NIC=$(get_active_nic)
if [ -z "$NIC" ]; then
echo "错误:未检测到网络接口,请检查网络配置"
exit 1
fi
echo "检测到活动网络接口:$NIC"
# 备份相关配置文件
backup_configs() {
echo "正在备份配置文件..."
[ -d /etc/systemd/system/ ] && cp -n /etc/systemd/system/disable-nic-powersave.service /etc/systemd/system/disable-nic-powersave.service.bak 2>/dev/null
}
# 临时禁用网卡节能模式(立即生效)
disable_nic_powersave_temp() {
echo "正在临时禁用网卡节能模式..."
if command -v ethtool &> /dev/null; then
# 禁用EEE节能
sudo ethtool --change "$NIC" eee off 2>/dev/null
# 禁用电源管理
sudo ethtool -s "$NIC" power management off 2>/dev/null
# 启用WOL功能
sudo ethtool -s "$NIC" wol g 2>/dev/null
else
echo "警告未安装ethtool正在安装..."
sudo apt update >/dev/null
sudo apt install -y ethtool >/dev/null
disable_nic_powersave_temp
fi
}
# 永久禁用网卡节能模式(系统启动时生效)
disable_nic_powersave_permanent() {
echo "正在配置永久禁用网卡节能模式..."
# 创建systemd服务
cat << EOF | sudo tee /etc/systemd/system/disable-nic-powersave.service > /dev/null
[Unit]
Description=Disable NIC Power Saving Mode for $NIC
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/ethtool -s $NIC wol g
ExecStart=/usr/sbin/ethtool --change $NIC eee off
ExecStart=/usr/sbin/ethtool --change $NIC power management off
[Install]
WantedBy=multi-user.target
EOF
# 启用并启动服务
sudo systemctl daemon-reload
sudo systemctl enable disable-nic-powersave.service
sudo systemctl start disable-nic-powersave.service
}
# 优化SSH保持连接设置
optimize_ssh_settings() {
echo "正在优化SSH连接设置..."
SSH_CONFIG="/etc/ssh/sshd_config"
# 备份SSH配置
[ -f "$SSH_CONFIG" ] && cp -n "$SSH_CONFIG" "$SSH_CONFIG.bak"
# 设置SSH心跳检测
if ! grep -q "^ClientAliveInterval" "$SSH_CONFIG"; then
echo "ClientAliveInterval 60" >> "$SSH_CONFIG"
else
sed -i 's/^ClientAliveInterval.*/ClientAliveInterval 60/' "$SSH_CONFIG"
fi
if ! grep -q "^ClientAliveCountMax" "$SSH_CONFIG"; then
echo "ClientAliveCountMax 3" >> "$SSH_CONFIG"
else
sed -i 's/^ClientAliveCountMax.*/ClientAliveCountMax 3/' "$SSH_CONFIG"
fi
# 重启SSH服务
sudo systemctl restart sshd
}
# 检查并修复DNS服务问题
fix_dns_service() {
echo "正在检查DNS服务..."
if ! systemctl is-active --quiet systemd-resolved; then
echo "修复DNS服务问题..."
sudo apt update >/dev/null
sudo apt install --reinstall -y systemd-resolved >/dev/null
sudo systemctl enable --now systemd-resolved
fi
}
# 显示当前网卡电源管理状态
show_status() {
echo -e "\n当前网络接口状态"
echo "-------------------------"
ip link show "$NIC" | grep -A 1 "$NIC"
echo -e "\n电源管理状态"
ethtool "$NIC" | grep -iE "power management|energy"
echo "-------------------------"
echo "配置已完成,建议重启系统使所有设置生效"
echo "重启命令sudo reboot"
}
# 主流程执行
backup_configs
disable_nic_powersave_temp
disable_nic_powersave_permanent
optimize_ssh_settings
fix_dns_service
show_status