feat(init): 添加 Linux 系统初始化脚本
- 新增 linux.sh 脚本,用于通用 Linux 系统的初始化 - 添加 README.md 文件,说明脚本功能和使用方法 -脚本功能包括: - 更换软件源为清华大学源 - 设置 DNS 为 114.114.114.114 和 223.5.5.5 - 设置时区为 Asia/Shanghai 并同步时间 - 支持 Debian、Ubuntu、CentOS、Fedora 等主流 Linux 发行版
This commit is contained in:
13
init/README.md
Normal file
13
init/README.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# 初始化脚本
|
||||||
|
|
||||||
|
## Linux
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -sSL https://git.sugarscat.cn/me/script/-/raw/main/init/linux.sh | bash
|
||||||
|
```
|
||||||
|
|
||||||
|
进行的操作有:
|
||||||
|
|
||||||
|
1. 更换软件源为 `清华大学源`;
|
||||||
|
2. 更换DNS为 `114.114.114.114`,`223.5.5.5`;
|
||||||
|
3. 设置时区 `Asia/Shanghai` 和时间同步
|
||||||
117
init/linux.sh
Normal file
117
init/linux.sh
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
echo "=== 通用 Linux 初始化脚本 ==="
|
||||||
|
|
||||||
|
# 确保为 root
|
||||||
|
if [ "$EUID" -ne 0 ]; then
|
||||||
|
echo "请以 root 用户运行此脚本。" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 检测系统信息
|
||||||
|
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
|
||||||
|
set_dns() {
|
||||||
|
echo "设置 DNS..."
|
||||||
|
rm -f /etc/resolv.conf
|
||||||
|
cat > /etc/resolv.conf <<EOF
|
||||||
|
nameserver 114.114.114.114
|
||||||
|
nameserver 223.5.5.5
|
||||||
|
EOF
|
||||||
|
if command -v chattr &>/dev/null; then
|
||||||
|
chattr +i /etc/resolv.conf || echo "警告: 无法锁定 resolv.conf"
|
||||||
|
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() {
|
||||||
|
echo "更换软件源为清华镜像..."
|
||||||
|
|
||||||
|
case "$DISTRO_ID" in
|
||||||
|
debian|ubuntu|raspbian)
|
||||||
|
CODENAME=$(lsb_release -cs 2>/dev/null || echo "stable")
|
||||||
|
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}-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}-security ${CODENAME}-security main contrib non-free non-free-firmware
|
||||||
|
EOF
|
||||||
|
apt update -y
|
||||||
|
;;
|
||||||
|
|
||||||
|
centos|rhel|almalinux|rocky)
|
||||||
|
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
|
||||||
|
fi
|
||||||
|
# shellcheck disable=SC2046
|
||||||
|
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)
|
||||||
|
dnf install -y dnf-plugins-core
|
||||||
|
sed -i 's|^metalink=|#metalink=|g' /etc/yum.repos.d/fedora*.repo
|
||||||
|
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
|
||||||
|
dnf makecache
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
echo "不支持的系统: $DISTRO_NAME"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# 主程序
|
||||||
|
get_os_info
|
||||||
|
|
||||||
|
echo "检测到系统: $DISTRO_NAME $DISTRO_VERSION"
|
||||||
|
|
||||||
|
# 判断使用哪个包管理器
|
||||||
|
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 "=== 初始化完成!系统已优化 ==="
|
||||||
Reference in New Issue
Block a user