制作自动安装Ubuntu20/22-server的iso镜像以及安装过程详解

# 本iso适合 刻录到U盘离线自动安装服务器系统

一、下载 Ubuntu iso

http://releases.ubuntu.com/jammy/ubuntu-22.04.3-live-server-amd64.iso

二、准备user-data 和meta-data文件

#cloud-config
autoinstall:
  version: 1
  source:
    id: ubuntu-server
  locale: en_US.UTF-8
  keyboard:
    layout: us
  timezone: Asia/Shanghai
  network:
    version: 2
    ethernets:
      en:
        match:
          name: "en*"
        set-name: ens32
        dhcp4: false
        dhcp6: false
        addresses:
          - 192.168.168.168/24
        routes:
        - to: default
          via: 192.168.168.254
        nameservers:
          addresses: [8.8.8.8, 114.114.114.114]
  identity:
    username: kcadmin
    hostname: ubuntu-server
    password: $6$yoUjV35x8cdqrfIr$Q8kgMdzrdoV6hyMkxE7iPKdDuGNweRnonQcFYnPi03.7mvHtWmqUlNwZEnxUkiaiTJ.4v.x8zi2WwCpfwIkcI/
  ssh:
    allow-pw: true
    install-server: true
  storage:
    layout:
      name: direct
  updates: security
  codecs:
    install: false
  drivers:
    install: false
  late-commands:
    - echo "Autoinstall completed on $(date)" >> /target/root/install.log
    - curtin in-target --target=/target -- systemctl disable ufw
    - curtin in-target --target=/target -- systemctl stop ufw
    - curtin in-target --target=/target -- bash -c "echo 'root:kc@123456' | chpasswd"
    - curtin in-target --target=/target -- sed -i 's/^#\?PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
    - curtin in-target --target=/target -- sed -i 's/^#\?PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
    - curtin in-target --target=/target -- systemctl restart ssh

特别提示: user-data文件是yaml格式  制表符和空格不能混用

# 主机名: ubuntu-server  用户: kcadmin/kc@123456 root/kc@123456
# 加密密码生成 sha-512 -s 加盐 mkpasswd -m sha-512 kc@123456 -s 'yoUjV35x8cdqrfIr' # 网络配置ip 匹配en开头的网卡名称,并改名成ens32 默认 192.168.168.168/24 网关192.168.168.254 DNS: 8.8.8.8,114.114.114.114 # 分区默认标准分区布局(或者LVM) storage:layout-name:direct(or lvm)
# 关闭防火墙 配置root密码登录 重启ssh服务

 meta-data文件

touch meta-data   #空文件即可

三、准备grub.cgf文件启动菜单文件

set timeout=20
loadfont unicode
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
menuentry "Try or Install Ubuntu Server" {
    set gfxpayload=keep
    linux    /casper/vmlinuz autoinstall ds='nocloud;s=/cdrom/' ---
    initrd   /casper/initrd
}
menuentry "Ubuntu Server with the HWE kernel" {
    set gfxpayload=keep
    linux    /casper/hwe-vmlinuz  ---
    initrd   /casper/hwe-initrd
}

四、准备iso刻录修改工具 UltraISO 9.7.3.3618 (软碟通)把以上3个文件导入到Ubuntu22.04-server.iso中

4.1 user-data 和meta-data文件 放到iso的根/目录

4.2 grub.cfg文件放到iso的目录/boot/grub/目录里

4.3 另存为u220403-autoinstall.iso

五、测试安装

六、系统简单优化

6.1 网卡配置

# 编辑网卡配置我文件
vim  /etc/ntplan/00.xxxxxxxxxxxxxxx。iso
# This is the network config written by 'subiquity'
network:
  version: 2
  ethernets:
    en:
      addresses:
      - 192.168.168.168/24
      dhcp4: false
      dhcp6: false
      match:
        name: "en*"
      routes:
        - to: default
          via: 192.168.168.254
      nameservers:
        addresses: [8.8.8.8, 114.114.114.114]

 6.2 关闭防火墙

systemctl stop ufw  && systemctl disable ufw.service

6.3 阿里apt源

sed -i s#cn.archive.ubuntu.com#mirrors.aliyun.com#g' /etc/apt/sources.list

6.3 docker安装源

apt -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt-key list
apt-key export 0EBFCD88 | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/slack.gpg
apt install docker-ce

 参考链接:

https://ubuntu.com/server/docs/install/autoinstall
https://cloudinit.readthedocs.io/en/latest/index.html

 七、补充 Ubuntu20.04 和22.04略有差别

# ubuntu 22.04 + 的网关
      routes:
        - to: default
          via: 192.168.168.254
# ubuntu 20.04的网关
      gateway4: 192.168.168.254
# ubuntu 22.04 + 的引导菜单 grub.cfg 目录
  /boot/grub/grub.cfg
# ubuntu 20.04的引导菜单 txt.cfg 目录
  /isolinux/txt.cfg
# ubuntu 22.04 + 的grub.cfg的内容区别为
    linux    /casper/vmlinuz autoinstall ds='nocloud;s=/cdrom/' ---
    initrd   /casper/initrd
# ubuntu 20.04 的txt.cfg内容为区别为
    kernel /casper/vmlinuz
    append initrd=/casper/initrd autoinstall ds='nocloud;s=/cdrom' ---

 优化 user-data  limit  sysctl

late-commands:
  - curtin in-target --target=/target -- bash -c "ulimit -n 655360"
  - curtin in-target --target=/target -- bash -c "printf '%s\n' '* hard nproc 655360' '* hard nofile 655360' 'root soft nproc 655360' 'root hard nofile 655360' >> /etc/security/limits.conf"
  - curtin in-target --target=/target -- bash -c "printf '%s\n' 'fs.file-max = 655350' 'net.core.somaxconn = 2048' 'net.core.rmem_max = 819200000' 'vm.overcommit_memory = 1' 'vm.swappiness = 0' 'vm.zone_reclaim_mode = 0' >> /etc/sysctl.conf"
  - curtin in-target --target=/target -- sysctl -p

 

posted on 2023-11-28 18:28  luokeli  阅读(3732)  评论(0)    收藏  举报

导航