U

Ubuntu 24.04 noble 一键切换源脚本( 适配HK VPS|DEB822格式)

Captain Linux 2026-10-23
#!/bin/bash
# Ubuntu 24.04 noble 源切换脚本(DEB822,适配HK VPS,仅deb,无deb-src)by Laitai.top
# 选项:hk(https) / aliyun(http) / au(http) / 还原官方默认 / 仅写入超时配置
# 自动写入apt超时配置 /etc/apt/apt.conf.d/99timeout
# 目标源文件:/etc/apt/sources.list.d/ubuntu.sources
set -e

SOURCE_FILE="/etc/apt/sources.list.d/ubuntu.sources"
TIMEOUT_CONF="/etc/apt/apt.conf.d/99timeout"
BAK_FILE="${SOURCE_FILE}.bak.$(date +%Y%m%d_%H%M%S)"

# 写入apt超时配置(永久生效,所有源共用)
write_timeout_conf() {
    cat > "$TIMEOUT_CONF" <<'EOF'
Acquire::http::Timeout "30";
Acquire::https::Timeout "30";
Acquire::ftp::Timeout "30";
Acquire::Retries "3";
EOF
    echo "✅ 已写入apt超时配置: $TIMEOUT_CONF"
}

clear
echo "============================================="
echo "      Ubuntu 24.04 noble 源切换菜单脚本"
echo "        适配HK VPS|DEB822格式"
echo "============================================="
echo "  1) 香港官方源 hk.archive.ubuntu.com (https,优先推荐)"
echo "  2) 阿里云源 mirrors.aliyun.com (http,规避TLS握手卡死)"
echo "  3) 澳洲官方源 au.archive.ubuntu.com (http,保底)"
echo "  4) 还原原始官方默认源 archive+security.ubuntu.com"
echo "  5) 仅写入Apt超时优化配置,【不切换源、不执行update】"
echo "  0) 退出脚本"
echo "============================================="
read -p "请输入选项 [0-5]:" opt

case $opt in
1)
    NAME="HK官方HTTPS源"
    cat > "$SOURCE_FILE" <<'EOF'
Types: deb
URIs: https://hk.archive.ubuntu.com/ubuntu
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

## Ubuntu security updates. Aside from URIs and Suites,
## this should mirror your choices in the previous section.
Types: deb
URIs: https://hk.archive.ubuntu.com/ubuntu
Suites: noble-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
    ;;
2)
    NAME="阿里云HTTP源"
    cat > "$SOURCE_FILE" <<'EOF'
Types: deb
URIs: http://mirrors.aliyun.com/ubuntu
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

## Ubuntu security updates. Aside from URIs and Suites,
## this should mirror your choices in the previous section.
Types: deb
URIs: http://mirrors.aliyun.com/ubuntu
Suites: noble-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
    ;;
3)
    NAME="澳洲AU HTTP源"
    cat > "$SOURCE_FILE" <<'EOF'
Types: deb
URIs: http://au.archive.ubuntu.com/ubuntu
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

## Ubuntu security updates. Aside from URIs and Suites,
## this should mirror your choices in the previous section.
Types: deb
URIs: http://au.archive.ubuntu.com/ubuntu
Suites: noble-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
    ;;
4)
    NAME="原生官方默认源"
    cat > "$SOURCE_FILE" <<'EOF'
Types: deb
URIs: http://archive.ubuntu.com/ubuntu
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

## Ubuntu security updates. Aside from URIs and Suites,
## this should mirror your choices in the previous section.
Types: deb
URIs: http://security.ubuntu.com/ubuntu
Suites: noble-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
    ;;
5)
    echo "👉 模式:仅写入超时配置,源文件保持不变"
    write_timeout_conf
    echo "✅ 完成,不修改源、不执行apt update"
    exit 0
    ;;
0)
    echo "脚本退出,无修改"
    exit 0
    ;;
*)
    echo "❌ 无效选项,退出"
    exit 1
    ;;
esac

# 换源分支才走备份+update
echo ""
echo "✅ 备份原源文件到:$BAK_FILE"
cp "$SOURCE_FILE" "$BAK_FILE"

write_timeout_conf

echo ""
echo "✅ 已切换为:$NAME"
echo "---------------------------------------------"
echo "正在执行 apt clean && apt update ..."
apt clean
apt update

echo ""
echo "✅ 操作完成!"
echo "连通性测试命令:"
case $opt in
1) echo "curl -v https://hk.archive.ubuntu.com/ubuntu/dists/noble/InRelease" ;;
2) echo "curl -v http://mirrors.aliyun.com/ubuntu/dists/noble/InRelease" ;;
3) echo "curl -v http://au.archive.ubuntu.com/ubuntu/dists/noble/InRelease" ;;
4) echo "curl -v http://archive.ubuntu.com/ubuntu/dists/noble/InRelease" ;;
esac
逻辑区分:
选 1~4:换源 → 备份源文件 → 写入超时配置 → clean + update
选 5:只调用 write_timeout_conf,其余都不动,直接结束。
PREV:被 Fail2ban 误封自己的 IP 解封办法
NEXT:PHP2SHELL - PHP老版本临时加固措施