#!/bin/bash set -euo pipefail echo "=== 通用 Linux 初始化脚本(阿里云版) ===" # 获取系统信息 DISTRO_ID=$(grep ^ID= /etc/os-release | cut -d= -f2 | tr -d '"') DISTRO_NAME=$(grep ^PRETTY_NAME= /etc/os-release | cut -d= -f2 | tr -d '"') CODENAME=$(lsb_release -cs 2>/dev/null || echo "") echo "检测到系统: $DISTRO_NAME" # 设置 DNS set_dns() { echo "配置 DNS 到 114.114.114.114 和 223.5.5.5..." echo -e "nameserver 114.114.114.114\nnameserver 223.5.5.5" > /etc/resolv.conf || true } # 设置时区和同步时间 set_timezone() { echo "设置时区为 Asia/Shanghai..." 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 } # 设置软件源 set_mirrors() { echo "更换软件源为阿里云镜像..." case "$DISTRO_ID" in ubuntu) cat > /etc/apt/sources.list < /etc/apt/sources.list <