Pxe 实现linux的自动安装实例
挂载光盘
[root@localhost ~]# mkdir /mnt/cdrom
[root@localhost ~]# mount /dev/cdrom /mnt/cdrom
mount: block device /dev/cdrom is write-protected, mounting read-only
安装dhcp服务器 并设置成开机启动
[root@localhost ~]# rpm -ivh /mnt/cdrom/Server/dhcp-3.0.5-21.el5.i386.rpm
Preparing... ########################################### [100%]
package dhcp-3.0.5-21.el5.i386 is already installed
[root@localhost ~]# chkconfig dhcpd on
安装ftp服务器 并设置开机启动
[root@localhost ~]# rpm -ivh /mnt/cdrom/Server/vsftpd-2.0.5-16.el5.i386.rpm
Preparing... ########################################### [100%]
package vsftpd-2.0.5-16.el5.i386 is already installed
[root@localhost ~]# chkconfig vsftpd on
配置dhcp服务器
[root@localhost ~]# vim /etc/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
ddns-update-style interim;
ignore client-updates;
subnet 192.168.1.0 netmask 255.255.255.0 {
# --- default gateway
option routers 192.168.1.254;
option subnet-mask 255.255.255.0;
option domain-name-servers 222.88.88.88;
range dynamic-bootp 192.168.1.2 192.168.1.253;
default-lease-time 21600;
max-lease-time 43200;
# we want the nameserver to appear at a fixed address
host ns {
next-server marvin.redhat.com;
hardware ethernet 12:34:56:78:AB:CD;
fixed-address 207.175.42.254;
}
重启dhcp服务器
[root@localhost ~]# service dhcpd restart
Shutting down dhcpd: [ OK ]
Starting dhcpd: [ OK ]
编辑tftp文件
[root@localhost ~]# vim /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivial file transfer \
# protocol. The tftp protocol is often used to boot diskless \
# workstations, download configuration files to network-aware printers, \
# and to start the installation process for some operating systems.
service tftp
{
socket_type = dgram
protocol = udp
wait = yes单进程 如果为no 则为多进程
user = root
server = /usr/sbin/in.tftpd
server_args = -s /tftpboot
disable = no 唤醒的状态
per_source = 11每个来源最多可以和本台机器建立11个连接
cps = 100 2
flags = IPv4
}
重启超级守候进程
[root@localhost ~]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
查看日志看是否出现错误
[root@localhost ~]# tail -f /var/log/messages
Aug 9 08:36:48 localhost dhcpd: DHCPOFFER on 192.168.1.252 to 00:0c:29:68:fc:29 via eth0
Aug 9 08:36:51 localhost dhcpd: DHCPDISCOVER from 00:0c:29:68:fc:29 via eth0
Aug 9 08:36:51 localhost dhcpd: DHCPOFFER on 192.168.1.252 to 00:0c:29:68:fc:29 via eth0
Aug 9 08:36:58 localhost dhcpd: DHCPDISCOVER from 00:0c:29:68:fc:29 via eth0
Aug 9 08:36:58 localhost dhcpd: DHCPOFFER on 192.168.1.252 to 00:0c:29:68:fc:29 via eth0
Aug 9 08:37:12 localhost dhcpd: DHCPDISCOVER from 00:0c:29:68:fc:29 via eth0
Aug 9 08:37:12 localhost dhcpd: DHCPOFFER on 192.168.1.252 to 00:0c:29:68:fc:29 via eth0
Aug 9 08:38:16 localhost xinetd[2925]: Exiting...
Aug 9 08:38:16 localhost xinetd[4355]: xinetd Version 2.3.14 started with libwrap loadavg labeled-networking options compiled in.
Aug 9 08:38:16 localhost xinetd[4355]: Started working: 1 available service
把启动需要的内核和驱动拷贝到tftp的根目录下
[root@localhost tftpboot]# cd /
[root@localhost /]# cd tftpboot/
[root@localhost tftpboot]# cp /mnt/cdrom/images/pxeboot/initrd.img ./
[root@localhost tftpboot]# cp /mnt/cdrom/images/pxeboot/vmlinuz ./
查看拷贝是否成功
[root@localhost tftpboot]# ll
total 8536
-r--r--r-- 1 root root 6855881 Aug 9 08:52 initrd.img
drwxr-xr-x 4 root root 4096 Aug 8 19:39 linux-install
-r--r--r-- 1 root root 1855956 Aug 9 08:53 vmlinuz
考呗引导文件到当前目录
[root@localhost tftpboot]# cp /usr/lib/syslinux/pxelinux.0 ./
[root@localhost tftpboot]# ll
total 8552
-r--r--r-- 1 root root 6855881 Aug 9 08:52 initrd.img
drwxr-xr-x 4 root root 4096 Aug 8 19:39 linux-install
-rw-r--r-- 1 root root 13148 Aug 9 09:00 pxelinux.0
-r--r--r-- 1 root root 1855956 Aug 9 08:53 vmlinuz
在当前目录建一个文件夹放置配置文件,把所需要的配置文件拷贝到当前目录下并命名为default
[root@localhost tftpboot]# cp /mnt/cdrom/isolinux/isolinux.cfg pxelinux.cfg/default
[root@localhost tftpboot]# ll
total 8556
-r--r--r-- 1 root root 6855881 Aug 9 08:52 initrd.img
drwxr-xr-x 4 root root 4096 Aug 8 19:39 linux-install
-rw-r--r-- 1 root root 13148 Aug 9 09:00 pxelinux.0
drwxr-xr-x 2 root root 4096 Aug 10 09:58 pxelinux.cfg
-r--r--r-- 1 root root 1855956 Aug 9 08:53 vmlinuz
对pxe的配置文件进行修改
[root@localhost tftpboot]# cd pxelinux.cfg
[root@localhost pxelinux.cfg]# ll
total 4
-r-xr-xr-x 1 root root 366 Aug 10 09:58 default
[root@localhost pxelinux.cfg]# vim default
default linux
default linux
prompt 1
timeout 6
display boot.msg
default linux
prompt 1
timeout 6
display boot.msg
F1 boot.msg
F2 options.msg
F3 general.msg
F4 param.msg
F5 rescue.msg
label linux
kernel vmlinuz
append ks=ftp://192.168.1.110/ks.cfg initrd=initrd.img
label text
kernel vmlinuz
append initrd=initrd.img text
label ks
kernel vmlinuz
append cfg initrd=initrd.img
label local
localboot 1
label memtest86
kernel memtest
append -
修改dhcp的配置文件 指明ftp服务器的ip地址和引导文件
[root@localhost pxelinux.cfg]# cd
[root@localhost ~]# vim /etc/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
ddns-update-style interim;
ignore client-updates;
subnet 192.168.1.0 netmask 255.255.255.0 {
# --- default gateway
option routers 192.168.1.254;
option subnet-mask 255.255.255.0;
option domain-name-servers 222.88.88.88;
next-server 192.168.1.110;
filename "pxelinux.0";
range dynamic-bootp 192.168.1.2 192.168.1.253;
default-lease-time 21600;
max-lease-time 43200;
# we want the nameserver to appear at a fixed address
host ns {
next-server marvin.redhat.com;
hardware ethernet 12:34:56:78:AB:CD;
fixed-address 207.175.42.254;
}
}
#
把光盘上的所有文件拷贝到ftp的 /var/ftp文件下
[root@localhost ~]# cd /var/ftp
[root@localhost ftp]# cp -r /mnt/cdrom/. ./
利用脚本为每一个得到 ip地址的主机配置一个主机名 脚本名称为jiaoben.sh
[root@localhost ftp]# vim jiaoben.sh
#!/bin/bash
ADDRESS=`/sbin/ifconfig eth0 |grep -i "inet\>" |awk '{print $2}'`
NUM=${ADDRESS##*.}
#modify hostname
sed -i -e "/HOSTNAME/c HOSTNAME=station$NUM.a.com" /etc/sysconfig/network
#modify ip address
sed -i -e "/BOOTPROTO/c BOOTPROTO=static\nIPADDRESS=192.168.1.$NUM\nNETWORK=255.255.255.0" /etc/sysconfig/network-scripts/ifcfg-eth0
~
配置yum服务器
[root@localhost ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo
[rhel-server]
name=Red Hat Enterprise Linux server
baseurl=file:///mnt/cdrom/Server
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release
[rhel-vt]
name=Red Hat Enterprise Linux vt
baseurl=file:///mnt/cdrom/VT
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release
[rhel-cluster]
name=Red Hat Enterprise Linux cluster
baseurl=file:///mnt/cdrom/Cluster
enabled=1
gpgcheck=1
用yum安装kick软件包
[root@localhost ~]# yum list all |grep kick
This system is not registered with RHN.
RHN support will be disabled.
pykickstart.noarch 0.43.5-1.el5 rhel-server
system-config-kickstart.noarch 2.6.19.8-2.el5 rhel-server
[root@localhost ~]# yum install system-config-kickstart -y
Loaded plugins: rhnplugin, security
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package system-config-kickstart.noarch 0:2.6.19.8-2.el5 set to be updated
--> Processing Dependency: pykickstart for package: system-config-kickstart
--> Running transaction check
---> Package pykickstart.noarch 0:0.43.5-1.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================
Package Arch Version Repository Size
==============================================================
Installing:
system-config-kickstart
noarch 2.6.19.8-2.el5 rhel-server 985 k
Installing for dependencies:
pykickstart noarch 0.43.5-1.el5 rhel-server 128 k
Transaction Summary
==============================================================
Install 2 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 1.1 M
Downloading Packages:
--------------------------------------------------------------
Total 14 MB/s | 1.1 MB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : pykickstart 1/2
Installing : system-config-kickstart 2/2
Installed:
system-config-kickstart.noarch 0:2.6.19.8-2.el5
Dependency Installed:
pykickstart.noarch 0:0.43.5-1.el5
Complete!
启用kick
[root@localhost ~]# system-config-kickstart
查看一下刚刚的设置并加入如下命令
[root@localhost ~]# vim ks.cfg
1 #platform=x86, AMD64, or Intel EM64T
2 # System authorization information
3 auth --useshadow --enablemd5
4 # System bootloader configuration
5 key --skip
6 bootloader --append="quiet" --location=mbr
7 # Clear the Master Boot Record
8 zerombr
9 # Partition clearing information
10 clearpart --all --initlabel
11 # Use text mode install
12 text
13 # Firewall configuration
14 firewall --disabled
15 # Run the Setup Agent on first boot
16 firstboot --disable
17 # System keyboard
18 keyboard us
19 # System language
20 lang en_US
21 # Installation logging level
把ks.cfg拷贝到ftp的根目录下
[root@localhost ~]# cp ks.cfg /var/ftp/
重启ftp和dhcp
现在去开启一台新的机器吧 可以看到你的机器已经自己安装了