跳转到内容

Ubuntu 初始化脚本

脚本文件位于 scripts/ 目录下,可直接下载使用

系统初始化

scripts/init-ubuntu.sh

配置 root 登录和密码:

bash
#! /bin/bash

# 判断是否是root用户
if [ "$EUID" -ne 0 ]; then
    echo "请使用root用户执行"
    exit 1
fi

echo "开始修改root登录和root密码为hua?lian8"
sed -i '/^PermitRootLogin/d' /etc/ssh/sshd_config &&
    echo "PermitRootLogin yes" | tee -a /etc/ssh/sshd_config &&
    echo "root:hua?lian8" | chpasswd &&
    systemctl restart ssh

echo "修改完成,请切换root用户登录"

APT 换源

scripts/apt-update-ubuntu24.04.sh

将 Ubuntu 24.04 的 apt 源替换为阿里云镜像:

bash
#! /bin/bash

if [ "$EUID" -ne 0 ]; then
    echo "请使用root用户执行"
    exit
fi

echo "备份apt配置"
cp -a /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak

echo "修改为阿里源"
tee /etc/apt/sources.list.d/ubuntu.sources <<'EOF'
Types: deb
URIs: https://mirrors.aliyun.com/ubuntu/
Suites: noble noble-updates noble-security noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF

echo "更新软件源"
apt update

echo "升级软件包"
apt upgrade -y

echo "升级完成"

离线依赖获取

scripts/get-depend.sh

下载 deb 包及其依赖到本地目录,用于离线安装:

bash
#! /bin/bash
### 前提安装好dpkg-dev
# 1. 将整个目录复制到目标机器
#scp -r /data/ubuntu/archives user@target-server:/data/ubuntu/
# 2. 在目标机器上配置本地源
#cat > /etc/apt/sources.list.d/local.list << EOF
#deb [trusted=yes] file:///data/ubuntu/archives ./
#EOF
# 3. 更新并安装
#apt update
#apt install -y wget tmux vim git curl gcc  # 等等

libs="wget tmux psmisc vim net-tools nfs-kernel-server telnet lvm2 git tar curl gcc keepalived haproxy bash-completion chrony sshpass ipvsadm ipset sysstat conntrack libseccomp2"
output_dir="/data/ubuntu/archives"

function tools_install() {
    if ! command -v dpkg-scanpackages &>/dev/null; then
        apt update
        apt install -y dpkg-dev
    fi
}

function get_depends() {
    apt install -d -y --reinstall $libs
}

function save_depends() {
    mkdir -p $output_dir
    cp -a /var/cache/apt/archives/*.deb $output_dir
    cd "$output_dir"
    dpkg-scanpackages . /dev/null | gzip >Packages.gz
    apt clean
}

function main() {
    tools_install
    get_depends
    save_depends
}

main "$@"

基于 MIT 许可发布