红联Linux门户
Linux帮助

自建DHCP TFTP服务 用PXE启动实现无人值守安装CentOS6

发布时间:2015-04-03 10:12:30来源:linux网站作者:mondeolove

1、安装DHCP TFTP

yum -y install dhcp tftp-server tftp


2、配置DHCP文件

cat > /etc/dhcp/dhcpd.conf << EOF

#

option domain-name "dwhd.org";

#设置DHCP的DNS

option domain-name-servers 172.16.0.1,114.114.114.114;

#设置DHCP 分配出去的IP超时时间 单位秒

default-lease-time 86440;

#设置DHCPlease-time时间 单位秒

max-lease-time 100000;

log-facility local7;

#设置DHCP的 IP 和掩码

subnet 192.168.1.0 netmask 255.255.255.0 {

#设置DHCP对外分配IP段

range 192.168.1.50 192.168.1.70;

#设置路由IP

option routers 192.168.1.1;

#下面的4行是根据mac分配指定IP

host CentOS7 {
hardware ethernet 00:0c:29:ba:3c:76;
fixed-address 192.168.1.65;
}

#如果是PXE网卡来获取到了IP 告诉网卡下一步联系的服务器IP 192.168.1.120

next-server 192.168.1.120;

#PXE网卡联系了服务器之后告诉它应该去获取文件pxelinux.0

filename "pxelinux.0";

}

EOF
自建DHCP TFTP服务 用PXE启动实现无人值守安装CentOS6


3、检测dhcp配置和启动dhcp

[root@localhost pxelinux.cfg]# service dhcpd configtest

Syntax: OK

#检测dhcp配置,如果出现Syntax: OK则说明配置OK了

#启动DHCP服务

service dhcpd start


4、设置TFTP开机启动

chkconfig tftp on[/bash]

[bash highlight="9,10"][root@localhost ~]# netstat -unl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address              Foreign Address            State

udp        0      0 0.0.0.0:1005                0.0.0.0:*

udp        0      0 0.0.0.0:111                0.0.0.0:*

udp        0      0 0.0.0.0:631                0.0.0.0:*

udp        0      0 0.0.0.0:776                0.0.0.0:*

udp        0      0 0.0.0.0:48819              0.0.0.0:*

udp        0      0 0.0.0.0:67                  0.0.0.0:*

udp        0      0 0.0.0.0:69                  0.0.0.0:*

udp        0      0 :::1005                    :::*

udp        0      0 :::111                      :::*

udp        0      0 :::42066                    :::*

##看看 67 69端口是否被监听了,如果是的就说明好了

开始为PXE启动做准备,由于我在当前Windows物理机上架设了FTP服务,所以我这里就直接用FTP服务器做源了


5、下载必要的文件到TFTP目录

ftpurl="ftp://lookback:lookback@192.168.1.20/centos/6/x86_64/base/"

tftpdir="/var/lib/tftpboot/"

for I in vmlinuz initrd.img; do wget -O ${tftpdir}$I ${ftpurl}images/pxeboot/$I;done

for II in splash.jpg vesamenu.c32 boot.msg isolinux.cfg; do

[ "$II" == "isolinux.cfg" ] && wget -O ${tftpdir}default ${ftpurl}/isolinux/$II

wget -O ${tftpdir}$II ${ftpurl}/isolinux/$II

done

#安装syslinux

yum -y install syslinux

#复制pxelinux.0到TFTP目录下

cp $(rpm -ql syslinux | grep '\bpxelinux.0') /var/lib/tftpboot/

#最后核对下文件

tree /var/lib/tftpboot/ #结果和下图一样就对了
自建DHCP TFTP服务 用PXE启动实现无人值守安装CentOS6


6、制作kf.cfg文件 用于自动化安装

下面是我自己制作的ks.cfg,可根据自己的需求做改动 root密码是CentOS6,这个文件存放的位置需要在系统安装的时候被获取到,我这里存放在我物理机上的FTP下

#platform=x86, AMD64, or Intel EM64T

#version=DEVEL

# Firewall configuration

firewall --disabled

# Install OS instead of upgrade

install

# Use network installation

url --url="ftp://lookback:lookback@192.168.1.20/centos/6/x86_64/base"

repo --name="CentOS" --baseurl=ftp://lookback:lookback@192.168.1.20/centos/6/x86_64/base

#repo --name="Fedora EPEL" --baseurl=ftp://lookback:lookback@192.168.1.20/fedora-epel/6/x86_64/ --cost=1000

# Root password

rootpw --iscrypted $6$W6.dzk.2

#root密码为centos6

# System authorization information

auth  --useshadow  --passalgo=sha512

# Use graphical install

graphical

firstboot --disable

# System keyboard

keyboard us

# System language

lang en_US

# SELinux configuration

selinux --disabled

# Installation logging level

logging --level=info

# Reboot after installation

reboot

# System timezone

timezone  Asia/Shanghai

# Network information

network  --bootproto=dhcp --device=eth0 --onboot=on

# System bootloader configuration

bootloader --append="crashkernel=auto crashkernel=auto rhgb rhgb quiet quiet" --location=mbr --driveorder="sda"

# Clear the Master Boot Record

zerombr

# Partition clearing information

clearpart --all --initlabel

# Disk partitioning information

part /boot --fstype="ext4" --size=200

part pv.001 --size=61440

volgroup vg0 --pesize=8192 pv.001

logvol  /  --fstype=ext4 --vgname=vg0  --size=20480  --name=root

logvol  /var  --fstype=ext4 --vgname=vg0  --size=20480  --name=var

logvol swap --name=swap --vgname=vg0 --size=2048

logvol /usr --fstype=ext4 --vgname=vg0 --size=10240 --name=usr

%post

echo -e 'TEST Centos6\nhttp://www.000.org\n' >> /etc/issue

sed -i '1,$s@id:[0-9]:initdefault:@id:3:initdefault:@g' /etc/inittab

[ ! -d /root/.ssh ] && mkdir /root/.ssh && chmod og=--- /root/.ssh

#下面是我自己为了方便自己使用对自动安装的系统做了免密码登录

cat >> /root/.ssh/authorized_keys << EOF

ssh-rsa
root@lookback.dwhd.org

EOF

# Enable funcd

sed -i 's@certmaster =.*@certmaster = 172.16.0.1@g' /etc/certmaster/minion.conf

/sbin/chkconfig funcd off

# Set the hostname

ClientName=`ifconfig eth0 | awk '/inet addr:/{print $2}' | awk -F. '{print $NF}'`

sed -i "s@HOSTNAME=.*@HOSTNAME=client$ClientName.dwhd.org@g" /etc/sysconfig/networks

# set puppet agent

sed -i '/\[main\]/a server=server.dwhd.org' /etc/puppet/puppet.conf

/sbin/chkconfig puppet off

# set hosts

echo '172.16.0.1  server.dwhd.org server' >> /etc/hosts

# yum repo

%end

%packages

@base

@basic-desktop

@chinese-support

@client-mgmt-tools

@core

@desktop-platform

@development

@fonts

@general-desktop

@graphical-admin-tools

@legacy-x

@network-file-system-client

@perl-runtime

@remote-desktop-clients

@server-platform-devel

@x11

%end

现在来修改下PXE启动后引导菜单

vi /var/lib/tftpboot/pxelinux.cfg/default

default vesamenu.c32

#prompt 1

timeout 60

display boot.msg

menu background splash.jpg

menu title Welcome to CentOS 6.5! This is test PXE Auto Install

menu color border 0 #ffffffff #00000000

menu color sel 7 #ffffffff #ff000000

menu color title 0 #ffffffff #00000000

menu color tabmsg 0 #ffffffff #00000000

menu color unsel 0 #ffffffff #00000000

menu color hotsel 0 #ff000000 #ffffffff

menu color hotkey 7 #ffffffff #ff000000

menu color scrollbar 0 #ffffffff #00000000

label pxelinux

menu label ^Auto Install or upgrade an existing system

menu default

kernel vmlinuz

append initrd=initrd.img ks=ftp://lookback:lookback@192.168.1.20/centos/6/ks.cfg

label linux

menu label ^Install or upgrade an existing system

kernel vmlinuz

append initrd=initrd.img

label vesa

menu label Install system with ^basic video driver

kernel vmlinuz

append initrd=initrd.img xdriver=vesa nomodeset

label rescue

menu label ^Rescue installed system

kernel vmlinuz

append initrd=initrd.img rescue

label local

menu label Boot from ^local drive

localboot 0xffff

label memtest86

menu label ^Memory test

kernel memtest

append -


自建DHCP TFTP服务 用PXE启动实现无人值守安装CentOS6
自建DHCP TFTP服务 用PXE启动实现无人值守安装CentOS6

自建DHCP TFTP服务 用PXE启动实现无人值守安装CentOS6

自建DHCP TFTP服务 用PXE启动实现无人值守安装CentOS6

自建DHCP TFTP服务 用PXE启动实现无人值守安装CentOS6

注:以上图片上传到红联Linux系统教程频道中。