refactor(init): 重构 Linux 初始化脚本

- 移除冗余的系统信息检测逻辑
- 优化 DNS 配置和时区设置
- 改进软件源配置,支持更多发行版
- 简化主执行逻辑,提高可读性
This commit is contained in:
2025-05-11 13:21:56 +08:00
parent f4e4145c8e
commit 539c304616

View File

@@ -1,117 +1,90 @@
#!/bin/bash #!/bin/bash
set -euo pipefail set -euo pipefail
echo "=== 通用 Linux 初始化脚本 ===" echo "=== 通用 Linux 初始化脚本 ==="
# 确保为 root # 获取系统信息
if [ "$EUID" -ne 0 ]; then DISTRO_ID=$(grep ^ID= /etc/os-release | cut -d= -f2 | tr -d '"')
echo "请以 root 用户运行此脚本。" >&2 DISTRO_NAME=$(grep ^PRETTY_NAME= /etc/os-release | cut -d= -f2 | tr -d '"')
exit 1 CODENAME=$(lsb_release -cs 2>/dev/null || echo "")
fi
# 检测系统信息 echo "检测系统: $DISTRO_NAME"
get_os_info() {
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO_ID=$ID
DISTRO_NAME=$NAME
DISTRO_VERSION=$VERSION_ID
else
echo "无法识别系统类型。" >&2
exit 1
fi
}
# 设置 DNS # 设置 DNS
set_dns() { set_dns() {
echo "置 DNS..." echo "置 DNS 到 114.114.114.114 和 223.5.5.5..."
rm -f /etc/resolv.conf echo -e "nameserver 114.114.114.114\nnameserver 223.5.5.5" > /etc/resolv.conf || true
cat > /etc/resolv.conf <<EOF }
nameserver 114.114.114.114
nameserver 223.5.5.5 # 设置时区和同步时间
EOF set_timezone() {
if command -v chattr &>/dev/null; then echo "设置时区为 Asia/Shanghai..."
chattr +i /etc/resolv.conf || echo "警告: 无法锁定 resolv.conf" ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime || true
if command -v timedatectl >/dev/null 2>&1; then
timedatectl set-timezone Asia/Shanghai || true
timedatectl set-ntp true || true
else
echo "timedatectl 不可用,尝试使用 ntpdate..."
(apt install -y ntpdate || yum install -y ntpdate || dnf install -y ntpdate) >/dev/null 2>&1 || true
ntpdate ntp.aliyun.com || true
fi fi
} }
# 设置时区和时间同步 # 设置软件源
set_timezone_and_time() {
echo "设置时区为 Asia/Shanghai..."
timedatectl set-timezone Asia/Shanghai 2>/dev/null || ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo "安装 ntpdate..."
$INSTALL_CMD ntpdate -y
echo "正在同步时间..."
ntpdate ntp.aliyun.com || echo "时间同步失败,请检查网络连接"
}
# 设置清华源
set_mirrors() { set_mirrors() {
echo "更换软件源为清华镜像..." echo "更换软件源为清华镜像..."
case "$DISTRO_ID" in case "$DISTRO_ID" in
debian|ubuntu|raspbian) ubuntu)
CODENAME=$(lsb_release -cs 2>/dev/null || echo "stable") cat > /etc/apt/sources.list <<EOF
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${CODENAME} main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${CODENAME}-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${CODENAME}-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu ${CODENAME}-security main restricted universe multiverse
EOF
apt update -y || true
;;
debian|raspbian)
cat > /etc/apt/sources.list <<EOF cat > /etc/apt/sources.list <<EOF
deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}/ ${CODENAME} main contrib non-free non-free-firmware deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}/ ${CODENAME} main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}/ ${CODENAME}-updates main contrib non-free non-free-firmware deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}/ ${CODENAME}-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}/ ${CODENAME}-backports main contrib non-free non-free-firmware deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}/ ${CODENAME}-backports main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}-security ${CODENAME}-security main contrib non-free non-free-firmware deb https://mirrors.tuna.tsinghua.edu.cn/${DISTRO_ID}-security ${CODENAME}-security main contrib non-free non-free-firmware
EOF EOF
apt update -y apt update -y || true
;; ;;
centos|rhel|almalinux|rocky) centos|rhel|almalinux|rocky)
echo "配置 RHEL 系列清华镜像..."
if [ -f /etc/yum.repos.d/CentOS-Base.repo ]; then if [ -f /etc/yum.repos.d/CentOS-Base.repo ]; then
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak sed -i.bak -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
/etc/yum.repos.d/CentOS-Base.repo
fi fi
# shellcheck disable=SC2046 yum makecache || true
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tuna.tsinghua.edu.cn/help/centos/?date=$(date +%Y%m%d)
yum clean all && yum makecache
;; ;;
fedora) fedora)
dnf install -y dnf-plugins-core echo "配置 Fedora 清华镜像..."
sed -i 's|^metalink=|#metalink=|g' /etc/yum.repos.d/fedora*.repo sed -i.bak -e 's|^metalink=|#metalink=|g' \
sed -i 's|^#baseurl=http.*|baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/linux/releases/$releasever/Everything/$basearch/os/|g' /etc/yum.repos.d/fedora.repo -e 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora|g' \
dnf makecache /etc/yum.repos.d/fedora*.repo
dnf makecache || true
;; ;;
*) *)
echo "不支持的系统: $DISTRO_NAME" echo "不支持的系统类型:$DISTRO_ID,跳过换源"
exit 1
;; ;;
esac esac
} }
# 主程序 # 主执行函数
get_os_info main() {
set_dns
set_timezone
set_mirrors
echo "✅ 初始化完成。"
}
echo "检测到系统: $DISTRO_NAME $DISTRO_VERSION" main
# 判断使用哪个包管理器
if command -v apt &>/dev/null; then
INSTALL_CMD="apt install -y"
apt update -y
elif command -v dnf &>/dev/null; then
INSTALL_CMD="dnf install -y"
dnf makecache
elif command -v yum &>/dev/null; then
INSTALL_CMD="yum install -y"
yum makecache
else
echo "不支持的包管理器。" >&2
exit 1
fi
# 安装通用工具
$INSTALL_CMD curl wget lsb-release || true
# 执行配置步骤
set_mirrors
set_dns
set_timezone_and_time
echo "=== 初始化完成!系统已优化 ==="