本次安装镜像Ubuntu服务器版:ubuntu-24.04-live-server-amd64.iso
https://cn.ubuntu.com/download/server/step1
我们可以在终端输入命令sudo passwd,然后输入当前用户的密码,按enter回车键,终端会提示我们输入新的密码并确认,此时的密码就是root新密码。
并且系统是没有安装ssh相关服务的,需要手动安装,下述为初始化脚本。
sudo apt install -y openssh-serversudo systemctl status ssh
#!/bin/bash# 使脚本以管理员权限运行
if [ "$(id -u)" -ne "0" ]; thenecho "请使用管理员权限运行此脚本。"exit 1
fiecho "开始初始化脚本..."# 1. 更换镜像源为清华大学镜像
echo "更换镜像源为清华大学镜像..."
cat <<EOL > /etc/apt/sources.list
# 默认注释掉原有源
# deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -cs) main restricted universe multiverse
# deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -cs)-updates main restricted universe multiverse
# deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -cs)-backports main restricted universe multiverse
# deb http://security.ubuntu.com/ubuntu $(lsb_release -cs)-security main restricted universe multiverse# 清华大学镜像源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs) main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs)-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs)-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $(lsb_release -cs)-security main restricted universe multiverse
EOLecho "更新软件包列表..."
apt update# 2. 检查并安装 openssh-server
echo "检查并安装 openssh-server..."
if dpkg -l | grep -q openssh-server; thenecho "openssh-server 已安装。"
elseecho "openssh-server 未安装,正在安装..."apt install -y openssh-server
fi# 启动并启用 SSH 服务
echo "启动并启用 SSH 服务..."
systemctl start ssh
systemctl enable ssh# 重新加载系统服务单元配置
echo "重新加载系统服务单元配置..."
systemctl daemon-reload# 3. 关闭防火墙(ufw)
echo "关闭防火墙(ufw)..."
ufw status | grep -q "Status: inactive"
if [ $? -ne 0 ]; thenufw disableecho "防火墙(ufw)已关闭。"
elseecho "防火墙(ufw)已经关闭。"
fi# 4. 检查并关闭 SELinux
echo "检查 SELinux 状态..."
if command -v sestatus >/dev/null 2>&1; thenSELINUX_STATUS=$(sestatus | grep "SELinux status" | awk '{print $3}')if [ "$SELINUX_STATUS" = "enabled" ]; thenecho "SELinux 已启用,正在禁用 SELinux..."# 修改 SELinux 配置文件sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config# 需要重启系统以使更改生效echo "已将 SELinux 配置为禁用。请重新启动系统以使更改生效。"elseecho "SELinux 已经禁用。"fi
elseecho "SELinux 工具未安装,无法检查 SELinux 状态。"
fi# 5. 时间同步
echo "安装并启动系统时间同步服务..."
apt install -y systemd-timesyncd
systemctl start systemd-timesyncd
systemctl enable systemd-timesyncdecho "初始化脚本完成。"