在ubuntu 24 命令行 下,制作无人值守ubuntu-24.04.2-desktop 桌面版安装U盘

发布于:2025-03-11 ⋅ 阅读:(18) ⋅ 点赞:(0)

1、说明

在线教育平台需要的电脑配置较高,如果直接买阿里云的配置,有点小贵,自己搞了一套专门运行,二手便宜,但是没有核显也没有显卡,比较麻烦,想做一个无人值守的ubuntu系统,这次完全在deepseek的指导下进行

二、制作过程 

1、准备工作

# 安装必要工具
sudo apt update && sudo apt install -y wget xorriso isolinux syslinux-utils

# 下载Ubuntu 24.04.2桌面版ISO(替换为实际URL)
wget https://releases.ubuntu.com/24.04.2/ubuntu-24.04.2-desktop-amd64.iso

2. 挂载ISO并提取文件

# 我挂载了u盘
sudo mkdir /mnt/upan            #新建目录
sudo mount /dev/sdb1 /mnt/upan    #挂载磁盘 /mntu
# 创建挂载点和提取目录
sudo mkdir -p /mnt/iso
sudo mkdir ~/custom-iso

# 挂载ISO文件
sudo mount -o loop ubuntu-24.04.2-desktop-amd64.iso /mnt/iso

# 复制文件到临时目录(保留权限)
rsync -av /mnt/iso/ ~/custom-iso/

# 卸载原始ISO
sudo umount /mnt/iso

3. 创建Autoinstall配置文件

# 在 ~/custom-iso 目录下创建 user-data 文件: 
# 本次配置添加了静态ip,ssh,远程桌面、虚拟显示器
# 密码用mkpasswd -m sha-512
#例如 x58@x58:/mntu$ mkpasswd -m sha-512 123456

sudo nano ~/custom-iso/user-data

#cloud-config
autoinstall:
  version: 1
  refresh-installer:
    update: yes
  locale: en_US.UTF-8
  keyboard:
    layout: us
  identity:
    hostname: auto-ubuntu
    username: x58
    password: "$6$THVVReBQdldHeQxy$E2Mn0vZjjAh058Bc60dUTGcj9NvONFlfq5YQzqV9Phq9pOCd1PiD7gp2YhMIjW6JKxtGb7h2sAusD.xCM9hLw1"
  storage:
    layout:
      name: direct
  network:
    version: 2
    ethernets:
      en*:  # 通配符匹配所有以太网接口
        dhcp4: no
        addresses:
          - 192.168.1.123/24
        routes:
          - to: default
            via: 192.168.1.1
        nameservers:
          addresses: [8.8.8.8, 114.114.114.114]
  ssh:
    install-server: yes
    allow-pw: yes
  packages:
    - openssh-server
    - xrdp
    - ufw
    - xserver-xorg-video-dummy  # 虚拟显示器驱动
    - xorgxrdp  # xrdp与Xorg集成
  late-commands:
    # 配置用户sudo权限
    - echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu-nopasswd
    
    # 配置防火墙规则
    - curtin in-target --target /target -- ufw allow 22/tcp
    - curtin in-target --target /target -- ufw allow 3389/tcp
    - curtin in-target --target /target -- ufw --force enable
    
    # 强制使用Xorg(禁用Wayland)
    - sed -i 's/#WaylandEnable=false/WaylandEnable=false/g' /target/etc/gdm3/custom.conf
    
    # 创建虚拟显示器配置文件
    - |
      cat > /target/etc/X11/xorg.conf.d/10-virtual-display.conf <<EOF
      Section "Device"
          Identifier  "DummyDevice"
          Driver      "dummy"
          Option      "ConstantDPI" "true"
          Option      "IgnoreEDID" "true"
          Option      "NoDDC" "true"
      EndSection

      Section "Monitor"
          Identifier  "DummyMonitor"
          HorizSync   31.5-48.5
          VertRefresh 50-70
          Modeline "1920x1080" 148.50 1920 2448 2492 2640 1080 1084 1089 1125 +HSync +VSync
      EndSection

      Section "Screen"
          Identifier  "DummyScreen"
          Device      "DummyDevice"
          Monitor     "DummyMonitor"
          DefaultDepth 24
          SubSection "Display"
              Depth 24
              Modes "1920x1080"
          EndSubSection
      EndSection
      EOF
    
    # 设置xrdp使用虚拟显示器
    - sed -i 's/use_vsock=false/use_vsock=false\ndefault_display=:20/g' /target/etc/xrdp/xrdp.ini
    
    # 确保服务自启
    - curtin in-target --target /target -- systemctl enable xrdp

4. 修改引导配置

# 编辑GRUB配置文件以启用自动安装:
sudo nano ~/custom-iso/boot/grub/grub.cfg

# 在第一个 linux 行末尾添加参数:
linux   /casper/vmlinuz quiet autoinstall ds=nocloud\;s=/cdrom/ ---

5. 重新生成ISO文件

#使用 xorriso 打包新ISO

cd ~/custom-iso

sudo xorriso -as mkisofs \
  -r -V "UBUNTU_2404_AUTO" \
  -o ~/ubuntu_2404_auto.iso \
  --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
  -partition_offset 16 \
  --mbr-force-bootable \
  -append_partition 2 0xEF ~/custom-iso/EFI/boot/bootx64.efi \
  -appended_part_as_gpt \
  -c boot.catalog \
  -b boot/grub/i386-pc/eltorito.img \
  -no-emul-boot -boot-load-size 4 -boot-info-table \
  --grub2-boot-info \
  -eltorito-alt-boot \
  -e '--interval:appended_partition_2:all::' \
  -no-emul-boot \
  -isohybrid-gpt-basdat \
  .

6、验证

# 查看ISO的EFI分区信息
fdisk -l ~/ubuntu_2404_auto.iso
# 应有 "EFI System" 分区标记

修正后的命令将生成支持以下特性的ISO:

  • ✅ 无人值守安装(包含用户名、密码、静态IP)

  • ✅ 自动安装openssh、xrdp、虚拟显示器驱动

  • ✅ 禁用Wayland,强制使用Xorg

  • ✅ 兼容UEFI/BIOS双引导设备

三、写入u盘做启动盘 

sudo dd if=~/ubuntu_2404_auto.iso of=/dev/sdX bs=4M status=progress oflag=sync

x58@x58:~$ sudo dd if=~/ubuntu_2404_auto.iso of=/dev/sdc bs=4M status=progress oflag=sync
6337593344 字节 (6.3 GB, 5.9 GiB) 已复制,635 s,10.0 MB/s
输入了 1511+1 块记录
输出了 1511+1 块记录
6338052096 字节 (6.3 GB, 5.9 GiB) 已复制,636.118 s,10.0 MB/s