硬件准备

模板机位于办公环境宿主机PHPV153003,主机名为ubuntutemplate,ISO文件存储于该主机 D:\ISO 目录下。

  1. 主机信息

    1. CPU:默认创建1核处理器
    2. 内存:1GB
    3. 硬盘:50GB 系统盘,使用LVM动态磁盘
    4. IP地址: 172.31.107.15/16
    5. 安装镜像:https://releases.ubuntu.com/20.04/ubuntu-20.04.2-live-server-amd64.iso (SHA256SUMS校验值为: d1f2bf834bbe9bb43faf16f9be992a6f3935e65be0edece1dee2aa6eb1767423)
    6. 软件源为: http://cn.archive.ubuntu.com/ubuntu
  2. 创建主机

    # 使用管理员权限开启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
    # 添加网卡并指定交换机
    Add-VMNetworkAdapter -VMName ubuntutemplate -Name "Network Adapter" -SwitchName FSOPublicNetwork
    # 设定网卡的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

    系统安装

  3. 配置语言

  4. 配置键盘布局

  5. 配置网络属性

  6. 配置网络代理(无)

  7. 配置系统升级APT软件源

  8. 配置硬盘自动分区

  9. 创建swap分区

  10. 配置根分区使用全部剩余空间

  11. 创建用户

  12. 安装openssh-server

  13. 选择软件包

  14. 开始安装

  15. 完成安装

    系统配置

  16. 系统更新

    apt update
    apt upgrade -y
  17. 安装工具

    # 安装网络工具包
    apt install -y curl wget net-tools
    # 安装监控工具包
    apt install -y htop iftop iotop atop
    # 安装其他工具包
    apt install -y locate unzip tree vim
    # 安装加域工具包
    apt install -y realmd sssd sssd-tools libnss-sss libpam-sss adcli samba-common-bin oddjob oddjob-mkhomedir packagekit sssd-krb5 sssd-krb5-common
    systemctl enable sssd
    # 开启域用户登录自动创建home目录
    pam-auth-update
    
    # 配置默认编辑器使用vim
    update-alternatives --config editor
    # 使用第3项 vim.basic
    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
  18. 卸载无用软件包

    # 删除cloud-init
    apt purge -y cloud-init apparmor
    rm -rf /etc/cloud/
    # 删除snapd
    snap remove lxd
    snap remove core18
    snap remove snapd
    apt purge -y snapd
    rm -rf /root/snap
    # 删除自动升级
    apt purge -y unattended-upgrades
    # 清理无用软件包
    apt autoremove -y
  19. 安装Hyper-V驱动

    apt install -y linux-image-virtual linux-tools-virtual linux-cloud-tools-virtual
  20. 配置时间

    # 使用chrony管理时间
    apt install -y chrony
    systemctl enable chrony --now
    # 使用北京时间
    timedatectl set-timezone Asia/Shanghai
    # 使用24小时制
    echo 'LC_TIME=en_DK.UTF-8' >> /etc/default/locale
  21. 修改网络管理工具

    # 使用NetworkMangager管理网络
    apt install -y network-manager
    systemctl enable network-manager
    # 关闭原有netplan服务的自启动
    systemctl disable systemd-networkd
    
    # 启用Networkmanager
    sed -i "s/false/true/g" /etc/NetworkManager/NetworkManager.conf
    
    # 创建备份
    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 m eth0 ipv4.method manual ipv4.addresses 172.31.107.15/16 ipv4.gateway 172.31.153.253 ipv4.dns 172.31.100.3,172.31.100.4
    
    # 配置生效
    nmcli c d eth0 && nmcli c u eth0
  22. 修改SSH配置文件

    # 修改/etc/ssh/sshd_config
    # 使用严格模式
    LoginGraceTime 2m
    PermitRootLogin yes
    StrictModes yes
    MaxAuthTries 6
    MaxSessions 10
    
    # 允许密码登录
    PasswordAuthentication yes
    
    # 不允许空密码
    PermitEmptyPasswords no
    
    # 关闭认证方式
    GSSAPIAuthentication no
    
    UsePAM yes
    
    # 关闭图形转发
    X11Forwarding no
    
    # 关闭问候报文
    PrintMotd no
    
    # 显示上次登录信息
    PrintLastLog yes
    
    # 保持TCP连接
    TCPKeepAlive yes
    
    # 延迟使用压缩数据模式
    Compression delayed
    
    # 不使用DNS解析
    UseDNS no
    
    # 不显示Banner
    Banner none
  23. 配置防火墙

    ufw allow ssh
    ufw reload
    ufw enable
  24. 删除swap文件

    swapoff -a
    sed -i "s@/swap.img@#/swap.img@g" /etc/fstab
    rm /swap.img
  25. 开启BBR模块

    # 开启BBR
    echo net.core.default_qdisc=fq >> /etc/sysctl.conf
    echo net.ipv4.tcp_congestion_control=bbr >> /etc/sysctl.conf
    sysctl -p
  26. 设置开机自启动脚本

    # 新建开机运行程序
    echo '@reboot root /bin/bash /root/firstboot.sh'>>/etc/crontab
  27. 启用ROOT用户并重启

    # 设置root用户密码
    sudo passwd
    # 更新索引和库依赖关系
    ldconfig
    updatedb
    # 重启主机
    reboot

    清理交付

  28. 删除安装用户

    deluser fxiaoke
    rm -rf /home/fxiaoke
  29. 清理主机信息

    # 清除Machine ID
    >/etc/machine-id
    # 清除SSH公钥,在后续部署中会使用`dpkg -reconfigure openssh-server`来重新SSHD的key,否则无法SSH登录
    rm -rf /etc/ssh/*key*
  30. 删除缓存

    # 清除apt缓存
    apt clean
    # 清除访问历史
    rm –rf /tmp/*
    rm –rf /var/tmp/*
    history -c
  31. 关机

    # 使用PHPV153003主机的Powershell控制台,通过相关命令关闭主机
    Stop-VM -VMName ubuntutemplate -Force

备注

  • 新部署的主机需要使用dpkg-reconfigure openssh-server来重新SSHD的key,否则是登录不上22端口的;
  • Ubuntu 20.04 使用swap.img来替代以往的swap分区,导致在虚机部署中磁盘空间占用要大于centos至少一倍以上;
  • Hyper-V虚机的默认blocksize是32M,会导致vhdx文件远远大于vmdk