PXE+TFTP+DHCP+Apache2 实现网络安装 Ubuntu 18.04 LTS

安装软件

# apt-get install tftpd-hpa isc-dhcp-server apache2

配置dhcp服务器

#配置文件:/etc/dhcp/dhcpd.conf

option domain-name "example.org";

default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;

allow booting;
allow bootp;

subnet 192.168.11.0 netmask 255.255.255.0 {
  range 192.168.11.200 192.168.11.250;
  option subnet-mask 255.255.255.0;
  option broadcast-address 192.168.11.255;
  option routers 192.168.11.254;
  option domain-name-servers 202.96.128.166;
  next-server 192.168.11.15;
  filename "pxelinux.0";
  server-name "192.168.11.15";
}

#配置文件:/etc/default/isc-dhcp-server

DHCPDv4_CONF=/etc/dhcp/dhcpd.conf
DHCPDv6_CONF=""

DHCPDv4_PID=/var/run/dhcpd.pid
DHCPDv6_PID=""

INTERFACESv4="eno1"
INTERFACESv6=""

#禁用DHCPDv6:

# cd /etc/dhcp
# mv dhcpd6.conf dhcpd6.conf.bak
# touch dhcpd6.conf

# /etc/init.d/isc-dhcp-server restart

查看tftpd服务器配置

#配置文件:/etc/default/tftpd-hpa

#/etc/default/tftpd-hpa

TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/var/lib/tftpboot"  # 此为tftpd启动镜像目录
TFTP_ADDRESS=":69"
TFTP_OPTIONS="--secure"

配置apache2

#配置文件:/etc/apache2/sites-enabled/000-default.conf 
DocumentRoot /var/www

# /etc/init.d/apache2 restart

准备网络安装文件

  • 准备系统安装文件
# wget -c https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cdimage/releases/18.04.3/release/ubuntu-18.04.3-server-amd64.iso
# mkdir -p /var/www/ubuntu
# mount -t iso9660 -o loop /root/ubuntu-18.04.3-server-amd64.iso /var/www/ubuntu

# vim /etc/fstab  # 开启挂载
/root/ubuntu-18.04.3-server-amd64.iso  /var/www/ubuntu    iso9660  loop           0       2
  • 准备tftpd启动镜像
# cd /var/lib/tftpboot/
# wget -c https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/netboot.tar.gz
# tar zxf netboot.tar.gz

准备自动化安装脚本

# mkdir -p /var/www/pxe

#/var/www/pxe/ubuntu.seed文件内容如下:

#### Contents of the preconfiguration file (for ubuntu server 18.04 lts)
#
### Localization
d-i debian-installer/locale string en_US
d-i localechooser/supported-locales multiselect en_US.UTF-8, zh_CN.UTF-8
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/xkb-keymap select us

### Network configuration
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
d-i netcfg/get_nameservers string 202.96.128.166
d-i netcfg/wireless_wep string

### Mirror settings
d-i mirror/country string manual
d-i mirror/http/hostname string 192.168.11.15
d-i mirror/http/directory string /ubuntu
d-i mirror/http/proxy string

### Account setup
d-i passwd/root-login boolean false
d-i passwd/user-fullname string Test-User
d-i passwd/username string testuser
# Generate command: "mkpasswd -m sha-512"
d-i passwd/user-password-crypted password <md5 hash>
# Root password
d-i passwd/root-password password <root-password>
d-i passwd/root-password-again password <root-password>

### Clock and time zone setup
d-i clock-setup/utc boolean false
d-i time/zone string Asia/Shanghai
d-i clock-setup/ntp boolean false

### Partitioning
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-auto/expert_recipe string \
boot-root :: \
1024 1024 1024 ext3 \
$primary{ } $bootable{ } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext3 } \
mountpoint{ /boot } \
. \
40960 1024 40960 ext4 \
$primary{ } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ / } \
. \
2048 1024 2048 linux-swap \
$primary{ } \
method{ swap } format{ } \
. \
10240 1024 10240 ext4 \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ /home } \
. \
4096 1024 4096 ext4 \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ /tmp } \
. \
40960 1024 -1 ext4 \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ /var } \
.
# -------------------------------------------------------
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

### Apt setup
d-i apt-setup/use_mirror boolean false
d-i apt-setup/services-select multiselect main
d-i debian-installer/allow_unauthenticated boolean true

### Package selection
tasksel tasksel/first multiselect standard
d-i pkgsel/include string openssh-server net-tools wget
d-i pkgsel/upgrade select none
d-i pkgsel/language-packs multiselect en, zh
d-i pkgsel/update-policy select none

### Boot loader installation
d-i grub-installer/only_debian boolean true
d-i grub-installer/bootdev string /dev/sda

### Finishing up the installation
d-i finish-install/keep-consoles boolean true
d-i finish-install/reboot_in_progress note

#### Advanced options
#### Running custom commands during the installation
d-i preseed/late_command string chroot /target sh -c "/usr/bin/wget -c http://192.168.11.15/pxe/postinstall.sh -O /tmp/postinstall.sh && /bin/sh -x /tmp/postinstall.sh"

#/var/www/pxe/postinstall.sh文件内容如下:

#!/bin/sh
#
# Set variables.
#
PXESERVER=192.168.11.15
#
# Get firstboot.sh script.
#
/usr/bin/wget -O /root/firstboot.sh http://${PXESERVER}/pxe/firstboot.sh
chmod +x /root/firstboot.sh
#
# Create a service that will run firstboot.sh script.
#
cat >/etc/init.d/firstboot << EOF
#! /bin/sh
#
### BEGIN INIT INFO
# Provides:        firstboot
# Required-Start:  $networking
# Required-Stop:   $networking
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: A script that runs once
# Description: A script that runs once
### END INIT INFO
cd /root; /usr/bin/nohup sh -x /root/firstboot.sh &
EOF
#
# Install the firstboot service.
#
chmod +x /etc/init.d/firstboot
update-rc.d firstboot defaults
echo "Finished postinstall"
#
# End

#/var/www/pxe/firstboot.sh文件内容如下:

#!/bin/sh
#
# Set variables.
USERNAME=testuser
PXESERVER=192.168.11.15

# This script will run the first time the system boots. Even
# though we've told it to run after networking is enabled,

# Introducing a brief sleep makes things work right all the
# time. The time for DHCP to catch up.
sleep 90

# Install new sources.
cat << EOF >/etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF

# Update system and install some softwares.
apt-get update
apt-get -y upgrade
apt-get -y install python chrony tree net-tools vim

# Configure nopassword login for ssh.
mkdir -p /home/${USERNAME}/.ssh
wget http://${PXESERVER}/pxe/sshkey/${USERNAME}/authorized_keys -O /home/${USERNAME}/.ssh/authorized_keys
wget http://${PXESERVER}/pxe/sshkey/${USERNAME}/id_rsa -O /home/${USERNAME}/.ssh/id_rsa
chmod 0600 /home/${USERNAME}/.ssh/*
chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}

# Configure env.
wget http://${PXESERVER}/pxe/env/bashrc.sh -O /etc/profile.d/bashrc.sh
wget http://${PXESERVER}/pxe/env/vimrc -O /etc/vim/vimrc

# Disable use dns for sshd.
sed -i 's/#UseDNS no/UseDNS no/' /etc/ssh/sshd_config

# Configure NTP.
sed -i '/^pool/d' /etc/chrony/chrony.conf
sed -i '$a\server 192.168.11.15 iburst' /etc/chrony/chrony.conf

# Delete the firstboot service.
update-rc.d firstboot remove
rm /etc/init.d/firstboot /root/firstboot.sh

# Reboot system.
reboot

# End

修改开机引导文件

#/var/lib/tftpboot/pxelinux.cfg/default文件内容如下:

# D-I config version 2.0
# search path for the c32 support libraries (libcom32, libutil etc.)
path ubuntu-installer/amd64/boot-screens/
include ubuntu-installer/amd64/boot-screens/menu.cfg
default ubuntu-installer/amd64/boot-screens/vesamenu.c32
prompt 1
timeout 1  # 启动屏幕超时1秒

#/var/lib/tftpboot/ubuntu-installer/amd64/boot-screens/txt.cfg文件内容如下:

default install
label install
    menu label ^Install
    menu default
    kernel ubuntu-installer/amd64/linux
    append vga=788 initrd=ubuntu-installer/amd64/initrd.gz auto=true priority=critical interface=auto netcfg/dhcp_timeout=30 url=http://192.168.11.15/pxe/ubuntu.seed debian-installer/allow_unauthenticated=true --- quiet    # 只需修改此行
label cli
    menu label ^Command-line install
    kernel ubuntu-installer/amd64/linux
    append tasks=standard pkgsel/language-pack-patterns= pkgsel/install-language-support=false vga=788 initrd=ubuntu-installer/amd64/initrd.gz --- quiet

启动服务器自动化安装

注意:必须先将服务器的启动模式由UEFI修改为BIOS,再选择从PXE启动自动安装系统。戴尔服务器开机按F2进入启动设置,按F12从PXE启动,按F11进入启动顺序管理。

上一篇:新手在ubuntu下用命令安装apache2及卸载


下一篇:如何确保PHP使用TBinaryProtocolAccelerated