dhcp、pxe、cobbler 程序安装

来源:互联网 发布:浪潮昆明云计算产业园 编辑:程序博客网 时间:2024/06/06 04:40

前言:温馨提示,在PXE 或 Cobbler做实验的时候,请将虚拟机的内存设置为1.5G左右

(二)DHCP服务详解

前言:动态主机配置协议,给局域网内的主机分配IP地址,子网掩码,网关,DNS

  • ARP协议
arp: address resolveing protocol (地址解析协议)
实现:IP地址 -> Mac地址
  • RARP协议
rarp: reverse address resolveing protocol (反地址解析协议)
实现:Mac地址 -> IP地址
  • DHCP服务的工作流程(基于广播地址进行)
1)客户端 : 发送DHCP的发现请求
2)服务端 : DHCP服务器收到请求,给客户端分配IP、netmask、gateway等
3)客户端 : 确认使用IP、netmask、gateway
4)服务端 : 收到确认信息
  • DHCP客户端单播请求服务器续租 IP、netmask、gateway(基于广播地址进行)
1)客户端请求
2)服务器响应
3)客户端请求
4)服务器不响应
5)客户端广播请求IP、netmask、gateway
  • 实现DHCP服务的包
1)dhcp
dhcpd:服务端
dhcrelay:中继服务
2)dnsmasq:dhcp 和 dns
  • 安装、配置DHCP服务
1)yum install -y dhcp
2)配置网卡为host-only连接模式,给host-only的vboxnet0,这个虚拟交换机设置一个IP地址和netmask,且禁止使用DHCP服务
给DHCP服务器端一个静态地址,配置一个与vboxnet0虚拟交换机同一个网段的IP地址,如果vboxnet0的IP地址为192.168.23.1,netmask为:255.255.255.0,那么可以给DHCP服务端指定IP192.168.23.2
3)编辑主配置文件/etc/dhcp/dhcpd.conf
# 指定服务器主机名
option domain-name "uplooking.com";
# 指定给客户端分配的DNS服务器主机IP地址
option domain-name-servers 192.168.23.1;
# 指定给DHCP客户端分配的网关地址,可以写在全局,也可以写在地址分配池里面
option routers 192.168.23.1;
# 指定默认的租约期限
default-lease-time 60000;
# 指定最大的租约期限
max-lease-time 720000;
# 配置地址分配池
subnet 192.168.23.0 netmask 255.255.255.0 {
range 192.168.23.100 192.168.23.254;
}
# 指定固定Mac地址的主机分配固定的地址,但是这里的固定IP地址不能使用地址池范围内的地址
host passacaglia {
hardware ethernet 08:00:27:c1:cd:2c;
fixed-address 192.168.23.10;
option routers 192.168.23.20;
}
3)启动服务端
systemctl start dhcpd.service
4)服务端监听于:
67号端口
客户端监听于:
68号端口
5)在客户端使用 dhclient -d 命令将DHCP客户端前台运行,此时客户端会开始获得DHCP分配的动态地址,先discover发现,再request续租。
6)如果DHCP服务端的地址池的范围发生改变,那么客户端会重新discover发现
  • DHCP的其他配置项说明
在地址池中可以使用下面两个指令:
1)filename:指明引导文件名称
2)next-server:指明引导文件所在的服务器主机的IP地址
例如:
filename "pxelinux.0"
next-server 192.168.23.6

(三)PXE技术详解

PXE(preboot execute environment,预引导执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,再用TFTP协议下载一个启动软件包到本机内存中执行,由这个启动软件包完成终端(客户端)基本软件设置,从而引导预先安装在服务器中的终端操作系统。

Alt text

  • 首先对于tftp服务的简要使用说明
1)yum安装:tftp、tftp-server
2)启动tftp
CentOS 6
service xinetd restart
chkconfig tftp on
CentOS 7
systemctl start tftp.socket
systemctl enable tftp.socket
3)服务器默认的站点目录
/var/lib/tftpboot
4)tftp服务端监听于udp的69端口
5)启动tftp服务
systemctl start tftp.socket
6)在/var/lib/tftpboot/里面放置一个文件/etc/inittab
cp /etc/inittab /var/lib/tftpboot
7)使用客户端tftp下载inittab文件
tftp 192.168.23.11
tftp> get inittab
8)退出tftp,查看当前目录是否有这个文件
  • PXE环境准备(红帽7)
外网网卡:192.168.23.10, 内网网卡:192.168.10.2
  • 首先准备服务软件环境(DHCP服务、TFTP服务、httpd服务、syslinux包)(红帽7)
1)安装程序包,其中syslinux包提供 pxelinux.0 这个文件
yum install -y httpd dhcp tftp tftp-server syslinux
2)启动各服务
systemctl start dhcpd
systemctl start tftp.socket
systemctl start httpd
3)编辑DHCP服务配置文件/etc/dhcp/dhcpd.conf
# 指定DHCP区域的名称
option domain-name "uplooking.com";
# 指定默认网关
option routers 192.168.10.10;
# 指定 DNS服务器地址
option domain-name-servers 192.168.10.1;
# 指定DHCP服务的默认租约时长
default-lease-time 43200;
# 指定DHCP服务的最大租约时长
max-lease-time 84544;
# 指定日志文件的路径
log-facility local7;
# 设置DHCP服务的网络地址
subnet 192.168.10.0 netmask 255.255.255.0 {
# 指定连接主机的IP的范围
range 192.168.10.200 192.168.10.253;
# 指定主机无盘装机需要加载的文件
filename "pxelinux.0";
# 指定文件所在的主机IP
next-server 192.168.10.10;
}
4)准备yum仓库,拷贝内核文件,ramdisk文件
1:创建光盘挂载目录
mkdir -pv /var/www/html/centos/7/x86_64
2: 将centos7光盘挂载
mount -r /dev/cdrom /var/www/html/centos/7/x86_64/
3:创建kickstarts文件目录
mkdir /var/www/html/kickstarts
4:创建kickstarts文件
vi /var/www/html/kickstarts/centos7.cfg
# Install OS instead of upgrade
install
# X Window System configuration information
xconfig --startxonboot
# Keyboard layouts
# old format: keyboard us
# new format:
keyboard --vckeymap=cn --xlayouts='cn'
# Root password
rootpw --iscrypted $1$93fLd53F$jqPGF7U7XOe3szIVqMcIl0
# System timezone
timezone Asia/Shanghai
# System language
lang en_US
# Firewall configuration
firewall --disabled
selinux --disabled
# System authorization information
auth --useshadow --passalgo=sha512
url --url="http://192.168.10.10/centos/7/x86_64/"
# Use graphical install 这里可以可以使用graphical
text
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Network information
network --bootproto=dhcp --device=enp0s3
# System bootloader configuration
bootloader --append="crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all
# Disk partitioning information
part swap --fstype="swap" --ondisk=sda --size=500
part /boot --fstype="xfs" --ondisk=sda --size=200
part / --fstype="xfs" --ondisk=sda --size=40259
# Reboot after installation
reboot
%packages
@base
@compat-libraries
@core
%end
5:将pxelinux.0文件拷贝到tpft的根目录下
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
6:拷贝引导内核所需文件,这里有专门为pxe引导的内核和ramdisk文件
cp /var/www/html/centos/7/x86_64/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/
cp /usr/share/syslinux/{chain.c32,menu.c32,memdisk,mboot.c32} /var/lib/tftpboot/
7:创建显示菜单的默认配置文件
cd /var/lib/tftpboot/
mkdir pxelinux.cfg
vi default
8:default文件的内容为
default menu.c32
prompt 15
timeout 60
# 指定一个标题
MENU TITLE yhy PXE
# 指定第一个选项
LABEL linux
MENU LABEL Install CentOS 7 x86_64 manually
KERNEL vmlinuz
APPEND initrd=initrd.img inst.repo=http://192.168.10.10/centos/7/x86_64
# 指定第二个标题
# 这个引导选项是给定了ks文件的路径,并且给定了repodata的路径
LABEL linux auto
MENU LABEL Install CentOS 7 x86_64 automatically
KERNEL vmlinuz
APPEND initrd=initrd.img inst.repo=http://192.168.10.10/centos/7/x86_64 ks=http://192.168.10.10/kickstarts/centos7.cfg
  • 红帽6 PXE安装配置说明(为了让大家方便做实验,这里的路径没有改,直接将红帽6的光盘挂载到/var/www/html/centos/7/x86_64)
所有的安装服务都是一样的,只是拷贝的启动文件不一样,那么只需要将红帽6的光盘挂载到/var/www/html/centos/7/x86_64/目录下即可,在拷贝对应的文件
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cp /var/www/html/centos/7/x86_64/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/
cp /var/www/html/centos/7/x86_64/isolinux/{boot.msg,vesamenu.c32,splash.png} /var/lib/tftpboot
mkdir /var/lib/tftpboot/pxelinux.cfg
cp /var/www/html/centos/7/x86_64/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
  • 红帽6的/var/lib/tftpboot/pxelinux.cfg/default的内容为
default vesamenu.c32
#prompt 15
timeout 30
display boot.msg
menu background splash.jpg
menu title Welcome to yhy PXE
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 linux
menu label ^Install CentOS 6 by YHY
menu default
kernel vmlinuz
append initrd=initrd.img ks=http://192.168.10.10/kickstarts/centos6.cfg
  • 红帽 6中kickstart文件的内容为(为了让大家方便做实验,这里的路径没有改,直接将红帽6的光盘挂载到/var/www/html/centos/7/x86_64)
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use Network installation
url --url="http://192.168.10.10/centos/7/x86_64"
# Root password
rootpw --iscrypted $1$ZuMqItjw$Pne66twowNZBHdgzMLfoy/
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
text
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 rhgb quiet" --location=mbr --driveorder="sda"
# Partition clearing information
clearpart --all
# Disk partitioning information
part /boot --asprimary --fstype="ext4" --size=200
part swap --fstype="swap" --size=500
part / --asprimary --fstype="ext4" --grow --size=6000
reboot
%packages
@base
@compat-libraries
@core
%end

(四)cobbler技术详解

Cobbler是PXE的二次封装,使用Python语言开发, 可以用来快速建立 Linux 网络安装环境,它已将 Linux 网络安装的技术门槛,从大专以上文化水平,成功降低到初中以下,连补鞋匠都能学会。

Alt text

  • cobbler配置过程需要基于PXE环境,cobbler是pxe环境的二次封装(演示环境centos7,centos6请自行完成)
一:安装cobbler、httpd
yum install -y cobbler httpd
二:启动cobbler、httpd
systemctl start cobblerd.service
systemctl start httpd.service
三:创建cobbler的配置环境
cobbler check
四:解决报错信息
1 : The 'server' field in /etc/cobbler/settings must be set to something other than localhost, or kickstarting features will not work. This should be a resolvable hostname or IP for the boot server as reachable by all machines that will use it.
2 : For PXE to be functional, the 'next_server' field in /etc/cobbler/settings must be set to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network.
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
4 : some network boot-loaders are missing from /var/lib/cobbler/loaders, you may run 'cobbler get-loaders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that you have installed a *recent* version of the syslinux package installed and can ignore this message entirely. Files in this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to resolve these requirements.
5 : enable and start rsyncd.service with systemctl
6 : debmirror package is not installed, it will be required to manage debian deployments and repositories
7 : The default password used by the sample templates for newly installed machines (default_password_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one
8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them
五:解决错误( 注意每一个问题解决要查看效果,需要重启cobblerd服务)
1)解决第一个问题
这里需要将/etc/cobbler/settings这个文件中的server这一项的主机地址改为 server: 192.168.10.10
2)解决第二个问题
这里需要将/etc/cobbler/settings这个文件中的next_server这一项的主机地址改为 next_server: 192.168.10.10
3)解决第三个问题
启动rsync, 让其开机自启: systemctl start rsyncd.service,systemctl enable rsyncd.service
4)解决第四个问题
在/etc/xinetd.d/tftp配置文件中修改 disable = no ,让tftp服务开机自启动 , 再重启tftp服务,systemctl start tftp.socket ,systemctl enable tftp.socket
5)解决第五个问题
在/etc/cobbler/settings这个文件中的default_password_crypted: 后面的密码改掉,先创建一个用户,给这个用户设定密码,然后就将这个用户的密码贴在这个选项的后面就是
6)解决第六个问题
yum install -y debmirror
将/etc/debmirror.conf文件的dists和arches那两行注释掉
7)解决第七个问题
运行cobbler命令,下载对于的安装包
cobbler get-loaders
8)解决第八个问题
yum install -y fence-agents
六:解决全部的cobbler配置问题之后,最后再重启cobbler,检查
systemctl restart cobblerd.service
cobbler check
No configuration problems found. All systems go
七:配置好dhcp、tftp之后,开始配置cobbler,(dhcp、tftp、服务的配置与PXE环境一样)
确保dhcp、tftp、httpd服务都已经启动
八:准备创建distro
1)挂载centos 7的光盘镜像到/media/cdrom目录下
mount -r /dev/cdrom /media/cdrom/
2)使用cobbler import 命令生成一个distro
cobbler import --name="CentOS-7_x86_64-1611" --path=/media/cdrom
3)查看生成的cobbler distro
[root@7 ks_mirror]# cobbler distro list
CentOS-7-1611-x86_64
4)查看生成的cobbler profile
[root@7 ks_mirror]# cobbler profile list
CentOS-7-1611-x86_64
5)同步cobbler 或 重启
cobbler sync 或 systemctl restart cobblerd
6)此时cobbler已经简单部署完毕,在内网192.168.10.0网段启动一个虚拟机,选择网卡启动,可以像PXE环境一样,可以直接安装系统了。
九:可以给安装的引导选项改名,就是给profile改名
cobbler profile rename --name=CentOS-7-1611-x86_64 --newname=RedHat-minimal
十:在/var/lib/cobbler/kickstarts/目录下拷贝sample_end.ks文件,命名为self.ks
cd /var/lib/cobbler/kickstarts/
cp sample_end.ks self.ks
修改一下self.ks文件,可以在里面修改自己自定义的设置,比如说:添加安装的包组
十一:基于现有的kickstart文件创建一个profile
cobbler profile add --name=RedHad-self --distro=CentOS-7-1611-x86_64 --kickstart=/var/lib/cobbler/kickstarts/self.ks
十二:查看profile
[root@7 ~]# cobbler profile list
RedHad-minimal-self
RedHad-self
十三:同步一下,每一次修改完成都要做一次同步
cobbler sync
十四:启动一台机器,然后安装F12进入boot sequence选项,按 l 键选择LAN启动
十五:如果想删除一个profile
cobbler profile remove --name=RedHad-minimal-self
cobbler sync
  • 效果展示

Alt text

(五)cobbler-web技术详解

前言:

  • cobbler-web安装配置过程详解
1)安装cobbler-web(测试时候,确保物理网络是在内网中进行,在外网会无法访问的哦,cobbler-web的访问入口必须有dhcpd指定的网络保持一致)
yum install -y cobbler-web
2)首先确保httpd, dhcpd,tftp,rsyncd,cobblerd 启动
netstat -lntup
3)首先将/etc/cobbler/modules.conf 先备份一次
cp /etc/cobbler/modules.conf /etc/cobbler/modules.conf.back
4)modules.conf的认证机制默认使用configfile进行认证,因此不用改
5)在指定的认证配置文件中创建用户(与httpd的用户认证类似)
htdigest -c /etc/cobbler/users.digest Cobbler yhycobbler
输入密码
6)重启cobbler
systemctl restart cobblerd.service
7)使用https://192.168.10.10/cobbler_web登入,进入cobbler-web管理界面,以后就可以使用web管理cobbler了
  • 登入后页面展示

Alt text

Alt text