
1 您需要了解
- 示例 Ubuntu Server 版本:
ubuntu-24.04.2-live-server-amd64 - Ubuntu Server 官方地址:
https://ubuntu.com/server - 操作过程中请勿重启虚拟机;操作完毕关闭虚拟机,之后请勿开启
2 系统安装
2.1 配置参考

2.2 选择语言


2.3 安装类型

2.4 网络配置
默认选择 DHCP,后续制作模板,无需静态配置

2.5 代理配置(默认留空)

2.6 镜像源地址
改成阿里 https://mirrors.aliyun.com/ubuntu 或国内其它的,速度快

2.7 分区配置
如果没有特殊需求,直接默认第一个,我想自己分区,选择第二个,/boot 引导分区 1G,swap 分区 8G,其他全部给 / 根分区









2.8 用户信息配置

2.9 升级跳过

2.10 安装 OpenSSH

2.11 安装等待并重启


注意:reboot 之后,会提示卸载光驱,直接回车即可


3 系统配置
3.1 配置root用户
cloudbaby@cloudbaby:~$ sudo su -
[sudo] password for cloudbaby:
root@cloudbaby:~# echo "root:rootroot" | sudo chpasswd
root@cloudbaby:~# vi /etc/ssh/sshd_config
......
Port 22
PermitRootLogin yes
......
root@cloudbaby:~# systemctl restart ssh

3.2 改源装包(更新)
在 Ubuntu 24.04 里,APT 的源配置从以前的 /etc/apt/sources.list,迁移到了 /etc/apt/sources.list.d/ubuntu.sources 文件。
- 旧方式如下
cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
cat > /etc/apt/sources.list << EOF
deb https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
# deb https://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
EOF
apt-get update
apt install -y vim net-tools lrzsz wget tree lsof tcpdump screen sysstat unzip
- 新方式(.sources YAML 配置文件)
sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
sudo mv /etc/apt/sources.list /etc/apt/sources.list.disabled
sudo vi /etc/apt/sources.list.d/ubuntu.sources
#将内容贴进去
# Ubuntu 24.04 Aliyun Mirror (Noble Numbat)
Types: deb deb-src
URIs: https://mirrors.aliyun.com/ubuntu/
Suites: noble noble-security noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
#更新
sudo apt update
3.3 关墙
root@cloudbaby:~# ufw disable
Firewall stopped and disabled on system startup
root@cloudbaby:~# ufw status
Status: inactive
3.4 删除 SSH 密钥
注意:ubuntu 和 centos 有些不同,后者在删除密钥后,重启节点会自动生成新的密钥,但 ubuntu 不会,这就导致后期通过模板克隆出来的新虚拟机开机后也无法自动生成密钥,每次手工处理有些麻烦,可通过预配置 systemd 来生成密钥
root@cloudbaby:~# rm -f /etc/ssh/ssh_host_*
3.5 自定义 systemd
创建 systemd 服务,为了克隆后的新机器在首次启动时可以自动创建 SSH 密钥
sudo tee /etc/systemd/system/regenerate-ssh-host-keys.service > /dev/null <<EOF
[Unit]
Description=Regenerate SSH host keys if missing
Before=ssh.service
[Service]
Type=oneshot
ExecStart=/usr/bin/ssh-keygen -A
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
# 启动服务
root@cloudbaby:~# systemctl enable regenerate-ssh-host-keys.service
Created symlink /etc/systemd/system/multi-user.target.wants/regenerate-ssh-host-keys.service → /etc/systemd/system/regenerate-ssh-host-keys.service.
3.6 清理 Machine-ID
root@cloudbaby:~# truncate -s 0 /etc/machine-id
root@cloudbaby:~# rm -f /var/lib/dbus/machine-id
3.7 重置主机名并关机
root@cloudbaby:~# hostnamectl set-hostname ""
root@cloudbaby:~# rm -f /etc/hostname
root@cloudbaby:~# init 0
3.8 附录(网络配置示例)
- DHCP默认配置示例
root@cloudbaby:~# cat /etc/netplan/50-cloud-init.yaml
network:
version: 2
ethernets:
ens33:
dhcp4: true
- 静态网络配置示例
network:
version: 2
ethernets:
ens33:
dhcp4: false
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
# 应用修改
sudo netplan apply
- [END]
















