ubuntu和debian的一樣,都是使用debootstrap來構建基本鏡像

  1. 更新系統
apt update && apt upgrade -y
  1. 安裝必備軟體
sudo apt install qemu-user-static binfmt-support debootstrap

備注 如果按照 amd64 和 i386 的鏡像,可以不安裝qemu-user-static

  1. 構建鏡像目錄
mkdir RootFS
sudo debootstrap --arch=[系統架構] --foreign <系統版本名稱> RootFS <系統mirror,如果在debian中構建ubuntu,請鍵入"http://archive.ubuntu.com/ubuntu/",或者使用您最方便的mirror>
cp /usr/bin/qemu-[系統架構]-static RootFS/usr/bin/ ##構建amd64/i386省略
  1. 配置DNS网络
sudo cp /etc/resolv.conf RootFS/etc/resolv.conf
# 复制本机的dns到容器中,否则进入容器无法访问网络
  1. 安装基本文件系统
chroot RootFS
export LANG=C
/debootstrap/debootstrap --second-stage
  1. 安装配置软件包
dpkg-reconfigure locales #可选(强烈建議選擇,配置系统基本语言选择,建议 en_US.UTF-8 UTF-8 )
apt purge netplan.io -y #(可選)
apt update && apt install vim sudo ifupdown net-tools udev iputils-ping wget dosfstools unzip binutils \  
krb5-locales libcbor0.6 libedit2 libfido2-1 libgssapi-krb5-2 libk5crypto3 libkeyutils1 libkrb5-3 \
libkrb5support0 libwrap0 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 \
ncurses-term python3-certifi python3-chardet python3-distro \
python3-idna python3-requests python3-urllib3 nano -y

關於我爲什麽不安裝 openssh-server,因爲我每次構建后openssh-server都不能在容器中自動啓動,所以只能多步驟

  1. 第一次配置 init.d

建立文檔vim /etc/init.d/new_install.sh,並鍵入以下内容

#!/bin/bash
# command content
### BEGIN INIT INFO
# Default-Start:     4
# Default-Stop:      0 1 6
### END INIT INFO
exit 0

存檔退出后

chmod +x /etc/init.d/new_install.sh
update-rc.d new_install.sh defaults
systemctl enable new_install.sh
  1. 清除緩存等内容
rm -rf /var/log/*
rm -rf /var/tmp/*
rm -rf /var/cache/*
rm -rf /var/apt/lists

再退出chrootexit

  1. 第二次構建init.d

編輯文檔vim RootFS/etc/init.d/new_install.sh
内容如下

#!/bin/bash
# command content
### BEGIN INIT INFO
# Default-Start:     3
# Default-Stop:      0 1 6
### END INIT INFO
sleep 20
apt update && apt install openssh-server -y
sed -ri 's/^#?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config;
sed -ri 's/^#?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config;
systemctl enable ssh
systemctl stop ssh
update-rc.d -f new_install.sh remove
rm -rf /etc/init.d/new_install.sh
reboot
exit 0

其中 sleep 時間根據您系統開機時間定,如果用於PVE容器中,建議設置10秒
其他和上篇一樣
同時,也修正了上篇部分内容

标签: none

添加新评论