使用preseed一键安装Ubuntu Server 1604

准备preseed配置文件

d-i debian-installer/locale string en_US.UTF-8
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layoutcode string us
d-i netcfg/choose_interface select auto
d-i netcfg/dhcp_timeout string 60
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
d-i netcfg/wireless_wep string
d-i	base-installer/kernel/override-image string linux-server
d-i mirror/http/mirror select gb.archive.ubuntu.com
d-i mirror/http/proxy string
d-i clock-setup/utc boolean true
d-i localechooser/shortlist/zh_CN select
d-i time/zone string Asia/Chongqing
d-i clock-setup/ntp boolean true
d-i partman-auto/disk string /dev/sda
d-i partman-auto/method string regular
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true
d-i partman-auto/choose_recipe select multi
d-i partman/default_filesystem string ext4
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i passwd/user-fullname string scutech.com
d-i passwd/username string scutech
d-i passwd/user-password password dingjia
d-i passwd/user-password-again password dingjia
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
tasksel tasksel/first multiselect none
d-i pkgsel/include string openssh-server curl
d-i pkgsel/language-packs multiselect en,zh
d-i	pkgsel/install-language-support	boolean false
d-i pkgsel/update-policy select unattended-upgrades
d-i	debian-installer/quiet	boolean false
d-i	debian-installer/splash	boolean false
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i	grub-installer/timeout string 0
d-i debian-installer/add-kernel-opts string vga=normal nomodeset audit=0 intel_idle.max_cstate=0 processor.max_cstate=1 cgroup_enable=memory swapaccount=1
d-i finish-install/reboot_in_progress note
d-i preseed/late_command string in-target wget --output-document=/tmp/post-install.sh http://192.168../post-install.sh; in-target /bin/sh /tmp/post-install.sh

为了节省版面我把空行和注释都删除了,大家如果想知道每一个配置项的涵义可以查看ubuntu的官方文档Contents of the preconfiguration file
几点说明:

  1. 这里的语言,地区,键盘等设置都是适合大陆的情况的;
  2. hostname在preseed里面设置了没有用,这应该是Xenial的bug,不知道Bionic改好了没有;
  3. 最后一项是调用一个shell脚本,这个很实用的功能,但我实际测试时这个不支持tftp(wget和curl都不行),只能用http。

准备post-install程序

#!/bin/bash
HOSTNAME='infosemper'
USERNAME='scutech'
PACKAGES='htop nano sudo python-minimal vim rsync dnsutils less ntp'
apt-get update && apt-get upgrade -y
apt-get -y dist-upgrade
apt-get install $PACKAGES -y
# Add SSH Key for default user
mkdir /home/$USERNAME/.ssh/
cat > /home/$USERNAME/.ssh/authorized_keys <-KEY HERE
EOF
chmod 700 /home/$USERNAME/.ssh
chmod 600 /home/$USERNAME/.ssh/authorized_keys
chown -R $USERNAME:$USERNAME /home/$USERNAME/.ssh
# Add SSH Key for root user
mkdir /root/.ssh/
cat > /root/.ssh/authorized_keys <-KEY HERE
EOF
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
chown -R root:root /root/.ssh

# Edit /etc/ssh/sshd_config
sed -i '/^PermitRootLogin/s/prohibit-password/yes/' /etc/ssh/sshd_config
sed -i -e 's/#PasswordAuthentication/PasswordAuthentication/g' /etc/ssh/sshd_config
hostn=$(cat /etc/hostname)
sudo sed -i "s/$hostn/$HOSTNAME/g" /etc/hosts
sudo sed -i "s/$hostn/$HOSTNAME/g" /etc/hostname
sudo reboot

这个程序只是一个例子,让大家知道你在preseed里可以调用shell的各种命令。

配置tftp服务器

安装包

apt-get install tftpd-hpa

修改tftp的配置文件到如下格式:

cat /etc/default/tftpd-hpa
# /etc/default/tftpd-hpa

TFTP_USERNAME="tftp" 
TFTP_DIRECTORY="/home/scutech/tftp" 
TFTP_ADDRESS=":69" 
TFTP_OPTIONS="--secure --create"

查看69端口占用情况:

# ss -naup|grep :69
UNCONN     0      0            *:69                       *:*                   users:(("in.tftpd",pid=20534,fd=4))
UNCONN     0      0           :::69                      :::*                   users:(("in.tftpd",pid=20534,fd=5))

查看服务状态

# systemctl status tftpd-hpa
● tftpd-hpa.service - LSB: HPA's tftp server
   Loaded: loaded (/etc/init.d/tftpd-hpa; bad; vendor preset: enabled)
   Active: active (running) since Thu 2019-10-10 17:23:33 CST; 2min 25s ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/tftpd-hpa.service
           └─20534 /usr/sbin/in.tftpd --listen --user tftp --address :69 --secure /var/lib/tftpboot

Oct 10 17:23:33 ubuntu1604 systemd[1]: Starting LSB: HPA's tftp server...
Oct 10 17:23:33 ubuntu1604 tftpd-hpa[20491]:  * Starting HPA's tftpd in.tftpd
Oct 10 17:23:33 ubuntu1604 tftpd-hpa[20491]:    ...done.
Oct 10 17:23:33 ubuntu1604 systemd[1]: Started LSB: HPA's tftp server.

创建工作目录,并修改权限

mkdir -p /home/scutech/tftp/d-i/xenial/script
chmod 777 /home/scutech/tftp
chown -R tftp:tftp /home/scutech/tftp
systemctl restart tftpd-hpa

使用tftp进行测试(略)
将preseed.cfg拷贝到/home/scuteh/tftp/d-i/xenial目录下,将post-install程序拷贝到 /home/scuteh/tftp/d-i/xenial/script目录下

安装buntu时加载preseed

启动时修改参数,将“file-/cdrom/preseed/ubuntu-server.seed”改成 “auto=true url=tftp://192.168.??/preseed.cfg hostname=scutech”。
安装过程中自动从tftp服务器上下载preseed配置文件

加载这个preseed配置文件后后面就什么不用做了,等着吧:

完成后自动重新启动,我在做分区时选择的是multi,这样 /home, /usr, /var, /tmp 几个分区是分开的:
使用preseed一键安装Ubuntu Server 1604_第1张图片
这是一个40G硬盘的分区例子,实际工作中分区可能比这要复杂,preseed配置文件中可以保护子文件,可以对每一种分区的不同情况调用不同的子文件,以后有机会我会贴一下例子上来。

你可能感兴趣的:(Linux)