首页 > 代码库 > 搭建PXE 实现自动装机

搭建PXE 实现自动装机

 PXE自动装机是通过网络来实现的,需要装机的电脑从PXE服务器上获得IP,引导文件,系统安装文件,这些都是在PEX服务器上设置的,而需要装机的电脑只要设置为网络启动就可以了,可在BIOS中设置。想从服务器上获得IP就需要DHCP服务,通信需要TFTP服务,共享安装文件需要NFS、FTP、SAMBA都可以,选一种即可。这样就可以网络引导和安装了,但并不是自动的,想实现自动安装系统,还需要自动安装的配置文件,在里面写上,安装信息,分区大小,安装组件等等。这个配置文件可以通过SYSTEM-CONFIG-KICKSTART来生成ks.cfg文件。

实验拓扑:

                        

-----PXE Server(vmnet1)-------------Client(vmnet1)------

 

实验一:搭建PXE Server

    服务器IP为192.168.10.253,可以给192.168.10.0/24安装 RHEL5.9

    分别给每台客户端分配主机名,格式如下(1-100)

           stationx.tarena.com      192.168.10.x

    安装所需要的软件包存放在/data/iso/rhel5.9

前期准备:

1、设置网络参数

[root@localhost ~]# ifconfig eth0 | grep "inetaddr"

inet addr:192.168.10.253  Bcast:192.168.10.255   Mask:255.255.255.0

[root@localhost ~]# cat /etc/sysconfig/network

NETWORKING=yes

NETWORKING_IPV6=yes

HOSTNAME=server01.example.com

[root@localhost ~]# grep server01 /etc/hosts

192.168.10.253  server01.example.com    server01

2、创建YUM源

[root@localhost ~]# mkdir -p /data/iso/rhel5.9

[root@localhost ~]# cp -rpf /misc/cd/* /data/iso/rhel5.9/

3、配置YUM客户端

[root@localhost ~]# cd /etc/yum.repos.d/

[root@localhostyum.repos.d]# cprhel-debuginfo.repo rhel5.9.repo

[root@localhostyum.repos.d]# cat rhel5.9.repo

[rhel-server]

name=Red Hat Enterprise Linux Server

baseurl=file:///data/iso/rhel5.9/Server

enabled=1

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

 

实验步骤:

1、配置DHCP(给需要安装系统的主机分配ip)

[root@server01 ~]# rpm -q dhcp

packagedhcp is not installed

[root@server01 ~]# yum -y install dhcp

[root@server01 ~]# cat /etc/dhcpd.conf

 

ddns-update-style interim;

default-lease-time 21600;

max-lease-time 43200;

option routers                  192.168.10.254;

option domain-name              "tarena.com";

option domain-name-servers      192.168.10.253;

next-server 192.168.10.253;

filename "pxelinux.0";

subnet 192.168.10.0 netmask 255.255.255.0 {

range dynamic-bootp 192.168.10.1 192.168.10.100;

}

[root@server01 ~]# servicedhcpd restart

[root@server01 ~]# chkconfigdhcpd on

[root@server01 ~]# netstat -tulnp | grep :67

udp        0      0 0.0.0.0:67               0.0.0.0:*                     5219/dhcpd

2、配置DNS

[root@server01 ~]# rpm -q bind bind-chroot caching-nameserver

package bind is not installed

package bind-chroot is not installed

package caching-nameserver is not installed

[root@server01 ~]# yum -y install bind bind-chroot caching- nameserver

[root@server01 ~]# cd /var/named/chroot/etc/

[root@server01 etc]# cp -p named.caching-nameserver.confnamed.conf

[root@server01 etc]# vimnamed.conf

...

 15         listen-on port 53 { 192.168.10.253; };

 16 //      listen-on-v6 port 53 { ::1; };

...

 27         allow-query     { any; };

 28         allow-query-cache { any; };

...

 37         match-clients      { any; };

 38         match-destinations { any; };

[root@server01 etc]# vim named.rfc1912.zones

...

 51 zone "tarena.com" IN {

 52         type master;

 53         file "tarena.com.zone";

54 };

[root@server01 etc]# named-checkconfnamed.conf

[root@server01 etc]# cd /var/named/chroot/var/named/

[root@server01 named]# cp -p named.localtarena.com.zone

[root@server01 named]# cattarena.com.zone

$TTL    86400

@       IN      SOA     localhost. root.localhost.  (

2014061801 ; Serial

                                      28800      ; Refresh

                                      14400      ; Retry

                                      3600000    ; Expire

86400 )    ; Minimum

IN      NS      server01.tarena.com.

server01        IN      A       192.168.10.253

$GENERATE       1-100   station$ IN A  192.168.10.$

[root@server01 named]# named-checkzonetarena.com  tarena.com.zone

zone tarena.com/IN: loaded serial 2014061801

OK

[root@server01 named]# service named restart

[root@server01 named]# chkconfig named on

 

3、配置TFTP

[root@server01 ~]# rpm -q tftp-server

tftp-server-0.49-2

[root@server01 ~]# vim /etc/xinetd.d/tftp

...

 13         server_args             = -s /tftpboot

 14         disable                 = no

...

[root@server01 ~]# servicexinetd restart

[root@server01 ~]# chkconfigxinetd on

[root@server01 ~]# netstat -tulnp | grepxinetd

udp        0      0 0.0.0.0:69                  0.0.0.0:*                 5842/xinetd 

[root@server01 ~]# rpm -ql syslinux | grep pxelinux.0

/usr/share/syslinux/gpxelinux.0

/usr/share/syslinux/pxelinux.0

[root@server01 ~]# mkdir /tftpboot/pxelinux.cfg

[root@server01 ~]# cp /data/iso/rhel5.9/isolinux/isolinux.cfg  /tftpboot/pxelinux.cfg/default

 

[root@server01 ~]# cp /usr/share/syslinux/pxelinux.0 /tftpboot/

[root@server01 ~]# cp /data/iso/rhel5.9/isolinux/vmlinuz /tftpboot/

[root@server01 ~]# cp /data/iso/rhel5.9/isolinux/initrd.img /tftpboot/

4、配置NFS共享

[root@localhost ~]# cat /etc/exports

/data/iso/rhel5.9       *(ro)

[root@localhost ~]# serviceportmap restart

[root@localhost ~]# servicenfs restart

[root@localhost ~]# chkconfigportmap on

[root@localhost ~]# chkconfignfs on

或者配置FTP

[root@server01 ~]# yum -y install vsftpd

[root@server01 ~]# tail -n 1 /etc/vsftpd/vsftpd.conf

anon_root=/data/iso/rhel5.9

[root@server01 ~]# servicevsftpd restart

[root@server01 ~]# chkconfigvsftpd on

在或者配置HTTP

[root@server01 ~]# yum -y install httpd

[root@server01 ~]# grepDocumentRoot /etc/httpd/conf/httpd.conf |  grep -v "^#"

DocumentRoot "/data/iso/rhel5.9"

[root@server01 ~]# grep Indexes /etc/httpd/conf.d/welcome.conf

    Options Indexes

[root@server01 ~]# servicehttpd restart

[root@server01 ~]# chkconfighttpd on

 

实验二:通过Kickstart实现无人值守安装(接着实验一)

[root@localhost ~]# yum -y install system-config-kickstart

操作过程见图片

[root@localhost ~]# yum -y install httpd

[root@localhost ~]# cp /root/ks.cfg /var/www/html/

[root@localhost ~]# servicehttpd restart

[root@localhost ~]# chkconfighttpd on

[root@localhost ~]# vim /var/www/html/ks.cfg

在文件中添加key --skip                      跳过注册

[root@server01 ~]# vim /tftpboot/pxelinux.cfg/default

...

 10 label linux

 11   kernel vmlinuz

 12   append initrd=initrd.imgks=http://192.168.10.253/ks.cfg

 

 

ks=nfs:/192.168.10.253:/ks.cfg