制作Hyper-V的Ubuntu虚机模板
硬件准备
主机信息
- CPU:默认创建1核处理器
- 内存:1GB
- 硬盘:50GB 系统盘,使用LVM动态磁盘
- IP地址: 172.31.107.15/16
- 安装镜像:https://releases.ubuntu.com/20.04/ubuntu-20.04.2-live-server-amd64.iso (SHA256SUMS校验值为: d1f2bf834bbe9bb43faf16f9be992a6f3935e65be0edece1dee2aa6eb1767423)
- 软件源为: http://cn.archive.ubuntu.com/ubuntu
创建主机
# 使用管理员权限开启PHPV153003主机的powershell控制台 # 创建虚机,并指定第二代且版本为5.0以及路径 New-VM -Name ubuntutemplate -MemoryStartupBytes 1GB -Generation 2 -Version 5.0 -Path d:\VMs\ -Novhd # 创建硬盘文件夹 mkdir "D:\VMs\ubuntutemplate\Virtual Hard Disks" # 创建系统盘,指定文件路径、大小、Blocksize New-VHd -Path "D:\VMs\ubuntutemplate\Virtual Hard Disks\ubuntutemplate.vhdx" -Dynamic -SizeBytes 50GB -BlockSizeBytes 1MB # 将新建磁盘附加到指定虚机 Add-VMHardDiskDrive -VMName ubuntutemplate -Path "D:\VMs\ubuntutemplate\Virtual Hard Disks\ubuntutemplate.vhdx" -ControllerType SCSI -ControllerNumber 0 # 添加光驱并指定iso文件 Add-VMDvdDrive -VMName ubuntutemplate -Path D:\ISO\ubuntu-20.04.2-live-server-amd64.iso # 默认网卡不指定交换机,在安装过程中断网以免安装过程中的升级时间过长 # 设定网卡的VLAN ID Set-VMNetworkAdapterVlan -VMName ubuntutemplate -VMNetworkAdapterName "Network Adapter" -Access -VlanId 31 # 设定主机关闭安全启动 Set-VMFirmware -VMName ubuntutemplate -EnableSecureBoot off # 开启主机集成服务中的来宾服务 Enable-VMIntegrationService -VMName ubuntutemplate -Name "Guest Service Interface" # 启动主机 Start-VM -VMName ubuntutemplate
系统安装
基本配置
语言: English 键盘:English 网络: 子网:172.31.0.0/16 地址:172.31.107.15 网关: 172.31.153.253 DNS: 172.31.100.4 不使用网络代理,并使用官方源 使用自定义磁盘分区 创建1GB ext4格式 /boot 分区 自动创建512MB EFI引导分区 剩余磁盘创建不含格式分区 创建PV(CL) 创建LV:root 3.5GB 创建LV: swap 3GB 创建默认用户以及密码、主机名(VLNX000000) 安装openssh-server
配置语言
配置键盘布局
配置网络属性
配置网络代理(无)
配置系统升级APT软件源
配置硬盘自动分区
配置根分区使用全部剩余空间
创建用户
安装openssh-server
选择软件包
开始安装
完成安装
系统配置
系统更新
apt update apt upgrade -y
安装工具
# 安装网络工具包 apt install -y curl wget net-tools # 安装监控工具包 apt install -y htop iftop iotop # 安装其他工具包 apt install -y locate unzip tree facter ruby libruby2.7 # 安装加域工具包 apt install -y sssd-ad sssd-tools realmd adcli systemctl enable sssd # 开启域用户登录自动创建home目录 pam-auth-update --enable mkhomedir
停用无用服务
# 关闭snapd服务和apparmo服务 systemctl disable apparmor.service snapd.service systemctl disable snap-core18-1944.mount snapd.apparmor.service snapd.core-fixup.service snapd.seeded.service snapd.socket snap-lxd-19188.mount snap.lxd.activate.service snap.lxd.daemon.unix.socket snap-snapd-10707.mount systemctl disable snapd.autoimport.service lxd-agent.service lxd-agent-9p.service snapd.system-shutdown.service snapd.recovery-chooser-trigger.service snapd.snap-repair.timer snapd.socket # 停止cloud-init服务 systemctl disable cloud-init.service cloud-init-local.service cloud-final.service cloud-config.service # 停止自动升级和信息收集服务 systemctl disable unattended-upgrades.service apt-daily.timer apt-daily-upgrade.timer apport-forward.socket apport-autoreport.path sed -i 's/1/0/g' /etc/default/apport
安装Hyper-V驱动
apt install -y linux-image-virtual linux-tools-virtual linux-cloud-tools-virtual
配置时间
# 使用chrony管理时间 apt install -y chrony systemctl enable chrony --now # 使用北京时间 timedatectl set-timezone Asia/Shanghai # 使用24小时制 echo 'LC_TIME=en_US.UTF-8' >> /etc/default/locale echo 'LC_ALL=en_US.UTF-8' >> /etc/default/locale
修改网络管理工具
# 使用NetworkMangager管理网络 apt install -y network-manager systemctl enable network-manager systemctl disable wpa_supplicant.service # 要将全部yaml文件移除之后再创建新的配置文件 mkdir /etc/netplan/bak mv /etc/netplan/*.yaml /etc/netplan/bak/ # 新建配置文件 cat > /etc/netplan/01-network-manager-all.yaml << EOF # This is the network config written by 'subiquity' network: version: 2 renderer: NetworkManager EOF # 应用配置 netplan apply systemctl restart network-manager # 重新配置IP信息,否则开机无法访问网络 nmcli c del eth0 nmcli c m Wired\ connection\ 1 con-name eth0 nmcli c m eth0 ipv4.addresses 172.31.107.15/24 ipv4.gateway 172.31.153.253 ipv4.dns 172.31.100.4 ipv4.method manual autoconnect yes # 配置生效 nmcli c d eth0 && nmcli c u eth0
修改SSH配置文件
# 修改/etc/ssh/sshd_config # 允许root使用密码登录 PasswordAuthentication yes # 开启认证方式 GSSAPIAuthentication yes
配置防火墙
ufw allow ssh ufw enable ufw reload
配置编辑器
# 配置默认编辑器使用vim # 使用第3项 vim.basic update-alternatives --config editor There are 4 choices for the alternative editor (providing /usr/bin/editor). Selection Path Priority Status ------------------------------------------------------------ 0 /bin/nano 40 auto mode 1 /bin/ed -100 manual mode 2 /bin/nano 40 manual mode * 3 /usr/bin/vim.basic 30 manual mode 4 /usr/bin/vim.tiny 15 manual mode
开启脚本自动补全
# 去掉 /etc/bash.bashrc 第35~41行注释 # enable bash completion in interactive shells if ! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then . /usr/share/bash-completion/bash_completion elif [ -f /etc/bash_completion ]; then . /etc/bash_completion fi fi
修改默认shell为bash
root@vlnx000000:~# dpkg-reconfigure dash # 选择NO Removing 'diversion of /bin/sh to /bin/sh.distrib by dash' Adding 'diversion of /bin/sh to /bin/sh.distrib by bash' Removing 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by dash' Adding 'diversion of /usr/share/man/man1/sh.1.gz to /usr/share/man/man1/sh.distrib.1.gz by bash'
设置开机自启动脚本
# 新建开机运行程序 echo '@reboot root /bin/bash /root/firstboot.sh'>>/etc/crontab
启用ROOT用户并重启
# 设置root用户密码 sudo passwd # 更新索引和库依赖关系 ldconfig updatedb # 重启主机 reboot
清理交付
删除安装用户
deluser fxiaoke rm -rf /home/fxiaoke
清理主机信息
# 清除Machine ID >/etc/machine-id # 清除SSH公钥,在后续部署中会使用`dpkg -reconfigure openssh-server`来重新SSHD的key,否则无法SSH登录 rm -rf /etc/ssh/*key*
扩展磁盘
lvextend -l 100%free /dev/cl/root resize2fs /dev/cl/root
删除缓存
# 清除apt缓存 apt autoremove -y apt clean # 清除访问历史 rm –rf /tmp/* rm –rf /var/tmp/* history -c
关机
# 使用带空格的命令避免存入history sync poweroff
备注
- 新部署的主机需要使用
dpkg-reconfigure openssh-server
来重新SSHD的key,否则是登录不上22端口的; - Ubuntu 20.04 使用swap.img来替代以往的swap分区,导致在虚机部署中实际磁盘空间占用要大于centos至少一倍以上;
- 部署时使用自定义分区可以创建swap分区;
- Hyper-V虚机的默认blocksize是32M,会导致vhdx文件远远大于vmdk,建议按照官方实践指南blocksize使用1M。
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.