•搭建无人执守安装服务器服务器常见概念
备注:安装的时候,上面所有的服务安装在一台服务器既可。
原理和概念:
1、 什么是PXE 严格来说,PXE 并不是一种安装方式,而是一种引导的方式。进行 PXE 安装的必要条件是要安装的计算机中包含一个 PXE 支持的网卡(NIC),即网卡中必须要有 PXE Client。PXE (Pre-boot Execution Environment)协议使计算机可以通过网络启动。
协议分为 client 和 server 端,PXE client 在网卡的 ROM 中,当计算机引导时,BIOS 把 PXE client 调入内存执行,由 PXE client 将放置在远端的文件通过网络下载到本地运行。
运行 PXE 协议需要设置 DHCP 服务器 和 TFTP 服务器。DHCP 服务器用来给 PXE client(将要安装系统的主机)分配一个 IP 地址,由于是给 PXE client 分配 IP 地址,所以在配置 DHCP 服务器时需要增加相应的 PXE 设置。
此外,在 PXE client 的 ROM 中,已经存在了 TFTP Client。PXE Client 通过 TFTP 协议到 TFTP Server 上下载所需的文件。
2、 什么是KickStart
KickStart是一种无人职守安装方式。KickStart的工作原理是通过记录典型的安装过程中所需人工干预填写的各种参数,并生成一个名为 ks.cfg的文件;在其后的安装过程中(不只局限于生成KickStart安装文件的机器)当出现要求填写参数的情况时,安装程序会首先去查找 KickStart生成的文件,当找到合适的参数时,就采用找到的参数,当没有找到合适的参数时,才需要安装者手工干预。
这样,如果KickStart文件涵盖了安装过程中出现的所有需要填写的参数时,安装者完全可以只告诉安装程序从何处取ks.cfg文件,然后去忙自己的事情。等安装完毕,安装程序会根据ks.cfg中设置的重启选项来重启系统,并结束安装。
设置BIOS引导顺序为: 硬盘,网络
模式: C/S 模式
安装pxe系统的服务器,可选择图形化界面进行安装
操作系统选择字符安装的需要安装一个xstart在自己的电脑上,主要用于生成ks文件
本次选择CentOs 7 图形化安装,为了方便后续的操作
安装服务
1.安装ftp,以及开启服务,设置为开机自启动。
[root@localhost ~]# yum -y install vsftpd
[root@localhost ~]# systemctl start vsftpd
[root@localhost ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
2.安装TFTP,修改TFTP配置文件及启动服务
[root@localhost ~]# yum -y install tftp tftp-server xinetd
修改配置
[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
user = root
server = /usr/sbin/in.tftpd
server_args = -s /tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
修改配置文件里面的13,14行。改成以下内容:
server_args = -s /tftpboot # 表示服务器默认的目录是 /tftpboot
其中,server_args = -s /tftpboot是tftp服务器运行时的参数。-s /tftpboot表示服务器默认的目录是 /tftpboot,当你执行put a.txt时,文件会被放到服务器的/tftpboot/a.txt,省去你敲put a /tftpboot/的麻烦。你也可以加其它服务器运行参数到这,具体可以执行man tftpd命令查阅。
-c :上传文件时,服务器上没有。就自动创建这个文件。
默认tftp客户端,只能上传tftp服务器已经有的文件。也就是只能传上去并覆盖服务器上的原文件。如果想上传原来目录中没有的文件,需要修改tftp服务器的配置文件并重起服务。需要修改如下:
打开/etc/xinetd.d/tftp文件,在 server_args 增加-c参数,如下所示:
server_args = -s /tftpboot -c
TFTP (Trivial File Transfer Protocol),中译简单文件传输协议或小型文件传输协议. 大家一定记得在2003年8月12日全球爆发冲击波(Worm.Blaster)病毒,这种病毒会监听端口69,模拟出一个TFTP服务器,并启动一个攻 击传播线程,不断地随机生成攻击地址,进行入侵。另外tftp被认为是一种不安全的协议而将其关闭,同时也是防火墙打击的对象,这也是有道理的。tftp 在嵌入式linux还是有用武之地的。需要打开防火墙,允许tftp访问网络。
接着重新启动xinetd服务,然后查看服务端口是否打开。
[root@localhost ~]# systemctl start xinetd
[root@localhost ~]# lsof -i:69 #查看69端口正在被哪个进程使用
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
xinetd 57738 root 5u IPv4 1524620 0t0 UDP *:tftp
[root@localhost ~]# netstat -anltpu |grep 69 #这样也可以
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 6929/master
tcp6 0 0 ::1:25 :::* LISTEN 6929/master
udp 0 0 0.0.0.0:69 0.0.0.0:* 57738/xinetd
3.安装dhcp,修改配置文件及开启服务:
[root@localhost ~]# yum -y install dhcp
配置DHCP服务器:
[root@localhost ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y
给DHCP服务器添加一张VMNET4网卡,ip设置为10.10.1.10(只要是1网段都行)
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.10.100.178 netmask 255.255.255.0 broadcast 10.10.100.255
inet6 fe80::856b:e1f9:8d48:4fe4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e8:11:9d txqueuelen 1000 (Ethernet)
RX packets 36770 bytes 13747465 (13.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7712 bytes 724312 (707.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:e8:11:a7 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# ifconfig ens34 10.10.1.10/24 #给新网卡配置一个IP
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf #修改DHCP配置
subnet 10.10.1.0 netmask 255.255.255.0 { #网段,子网掩码
range 10.10.1.100 10.10.1.200; #地址池,分配的网段
option domain-name-servers 10.10.1.1; #DNS
option domain-name "internal.example.org"; #域名
option routers 10.10.1.1; #网关
option broadcast-address 10.10.1.255; #广播地址
default-lease-time 600; #IP租赁时间
max-lease-time 7200; #IP最大租赁时间,单位是秒
next-server 10.10.1.10; #下一条,网关出口
filename "pxelinux.0";
}
配置完,先不启动DHCP ,等所有的软件安装好,最后来启动。
配置使用PXE启动所需的相关文件
安装软件
[root@localhost ~]# yum -y install system-config-kickstart syslinux
准备tftp需要共享出去的文件
[root@localhost ~]# mount /dev/cdrom /media/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]# mkdir /tftpboot
[root@localhost ~]# mkdir /tftpboot/pxelinux.cfg
[root@localhost ~]# cp /usr/share/syslinux/pxelinux.0 /tftpboot/ #注,只有安装了system-config-kickstart软件包,才会有/usr/share/syslinux/目录及目录中的文件。
[root@localhost ~]# cp /media/images/pxeboot/initrd.img /tftpboot/
[root@localhost ~]# cp /media/images/pxeboot/vmlinuz /tftpboot/
[root@localhost ~]# cp /media/isolinux/isolinux.cfg /tftpboot/pxelinux.cfg/default
[root@localhost ~]# chmod 644 /tftpboot/pxelinux.cfg/default
修改default,指定安装操作系统的方式和ks.cfg文件路径
[root@localhost ~]# vim /tftpboot/pxelinux.cfg/default
default vesamenu.c32
改成
default linux
label linux
menu label ^Install CentOS 7
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet
改成
label linux
menu label ^Install CentOS 7
kernel vmlinuz
append initrd=initrd.img inst.repo=ftp://10.10.1.10/pub inst.ks=ftp://10.10.1.10/ks.cfg
意思是使安装程序通过FTP服务器访问kickstart文件。
以上有关default配置文件的修改就是通过ftp服务器方式来访问kickstart文件。
其中第1行:default linux表示指定默认入口名称;
第2行:prompt 1中prompt用来设置是否等待用户选择,1表示等待用户控制;
第18、23、27、31行label linux表示定义的引导入口,表示系统的不同安装方式,如第18行定义的入口表示图形安装。
配置文件修改完了,要开始自动安装,安装程序必须能访问kickstart文件。
有多种方法可访问kickstart文件,其中最常用的一种方法是通过网络服务器进行,
例如:ftp服务器、WEB服务器或NFS服务器,这种方法非常易于部署,并且也使管理更改变得十分简单。也可以通过USB磁盘、CD-ROM或本地硬盘。如果USB或CD-ROM中的kickstart文件非常便于访问,只需将kickstart文件放置在用来开始安装的引导介质中。而使用DHCP服务器和TFTP及PXE配置起来更为复杂。
使安装程序指向kickstart文件的书写格式如下:
ks=ftp://server/dir/file 如:ks=ftp://ftp服务器IP/ks.cfg
ks=http://server/dir/file 如:ks=http://http服务器IP/ks.cfg
ks=nfs:server:/dir/file 如:ks=nfs:nfs服务器IP:/var/ftp/pub/ks.cfg
ks=hd:device:/dir/file 如:ks=hd:sdb1:/kickstar-files/ks.cfg
ks=cdrom:/dir/file 如:ks=cdrom:/kickstart-files/ks.cfg
以上有关default配置文件的修改就是通过ftp服务器方式来访问kickstart文件。
接下来制作kickstart的无人值守安装文件。
需要将本地的光盘挂载到服务器上,要用本地的yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# mount /dev/cdrom /var/ftp/pub/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo
[root@localhost yum.repos.d]# mkdir bak
[root@localhost yum.repos.d]# mv *.repo bak/
[root@localhost yum.repos.d]# ls
bak
[root@localhost yum.repos.d]# vim centos.repo
[devement]
name=centos7-dvd
baseurl=file:///var/ftp/pub
enabled=1
gpgcheck=0
[root@localhost yum.repos.d]# yum makecache
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
develment | 3.6 kB 00:00:00
(1/4): develment/group_gz | 166 kB 00:00:00
(2/4): develment/filelists_db | 3.2 MB 00:00:00
(3/4): develment/primary_db | 3.1 MB 00:00:00
(4/4): develment/other_db | 1.3 MB 00:00:00
元数据缓存已建立
[root@localhost yum.repos.d]# yum install -y gdm
在图形化界面执行下面的命令
[root@localhost ~]# system-config-kickstart ##执行system-config-kickstart弹出来界面
在字符界面执行会报错,或者在电脑上安装一个xstart来执行这条命令也可以正常弹出该图形配置界面
[root@localhost yum.repos.d]# system-config-kickstart
Could not open display because no X server is running.
Try running 'system-config-kickstart --help' for a list of options.
无人执守安装需要配置的参数
/boot分区大小给300M----500M就可以
swap分区正常给2G也足够了,如果2G够用,那就要考虑增加内存了
根分区,可以把剩下的全部分给根分区
网络这块不用动,默认就可以,这里可以配置静态IP,可以配置DHCP, 默认就是DHCP,所不用修改,直接下一步
验证这块,也不用修改,默认既可
防火墙与selinux先关闭
显示,把图形化安装勾选去掉
选择软件包,出现了一个问题
出现这个问题的几个原因
一,本地光盘没有挂载,重新挂载一下
[root@localhost ~]# mount /dev/cdrom /var/ftp/pub/
mount: /dev/sr0 写保护,将以只读方式挂载
二,防火墙和selinux暂时关闭,
[root@localhost ~]# systemctl disable firewalld #永久停止
[root@localhost ~]# systemctl stop firewalld #停止firewalld服务
[root@localhost ~]# systemctl status firewalld #查看firewalld状态
[root@localhost ~]# sestatus #查看selinux状态
[root@localhost ~]# setenforce 0 #临时关闭
[root@localhost ~]# vim /etc/selinux/config #永久关闭
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost yum.repos.d]# sestatus
SELinux status: disabled
三,本地yum源配置的有问题,检查yum源的配置
[root@localhost yum.repos.d]# cat centos.repo
[development] -------------->此处修改为[development]
name=centos7-dvd
baseurl=file:///var/ftp/pub
enabled=1
gpgcheck=0
修改文件后,卸载命令system-config-kickstart并重新安装
[root@localhost yum.repos.d]# yum remove -y system-config-kickstart
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache
[root@localhost yum.repos.d]# yum install -y system-config-kickstart
安装完之后,重新执行命令
[root@localhost ~]# system-config-kickstart
之前填写的需要重新填写,这里就直接到软件包选择
安装系统选择上面的就可以,其他的根据需求选择
安装系统前执行的脚本
安装系统后执行的脚本
查看保存的文件
[root@localhost ~]# ll
总用量 12
-rw-------. 1 root root 1703 10月 18 11:15 anaconda-ks.cfg #记录手动安装系统的参数
-rw-r--r--. 1 root root 1751 10月 18 11:16 initial-setup-ks.cfg
-rw-r--r-- 1 root root 699 10月 18 14:20 ks.cfg
drwxr-xr-x. 2 root root 6 10月 18 11:16 公共
drwxr-xr-x. 2 root root 6 10月 18 11:16 模板
drwxr-xr-x. 2 root root 6 10月 18 11:16 视频
drwxr-xr-x. 2 root root 6 10月 18 11:16 图片
drwxr-xr-x. 2 root root 6 10月 18 11:16 文档
drwxr-xr-x. 2 root root 6 10月 18 11:16 下载
drwxr-xr-x. 2 root root 6 10月 18 11:16 音乐
drwxr-xr-x. 2 root root 6 10月 18 11:16 桌面
[root@localhost ~]# cat ks.cfg
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$TajW86Qg$7BjfMaQ1A7d3VSmREabFl/
# Use network installation
url --url="ftp://10.10.1.10/pub"
# System language
lang en_US
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Firewall configuration
firewall --disabled
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=300
part swap --fstype="xfs" --size=2048
part / --fstype="xfs" --grow --size=1
%packages
@additional-devel
@base
@compat-libraries
@development
@platform-devel
%end
这个生成的文件下次可以直接拿来用,root密码为toor
[root@localhost pub]# pwd
/var/ftp/pub
[root@localhost pub]# ll
总用量 686
-rw-rw-r-- 1 root root 14 11月 26 2018 CentOS_BuildTag
drwxr-xr-x 3 root root 2048 11月 26 2018 EFI
-rw-rw-r-- 1 root root 227 8月 30 2017 EULA
-rw-rw-r-- 1 root root 18009 12月 10 2015 GPL
drwxr-xr-x 3 root root 2048 11月 26 2018 images
drwxr-xr-x 2 root root 2048 11月 26 2018 isolinux
drwxr-xr-x 2 root root 2048 11月 26 2018 LiveOS
drwxrwxr-x 2 root root 663552 11月 26 2018 Packages
drwxrwxr-x 2 root root 4096 11月 26 2018 repodata
-rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r-- 1 root root 2883 11月 26 2018 TRANS.TBL
[root@localhost pub]# cp /root/ks.cfg /var/ftp/
重启vsftpd服务,启动DHCP
[root@localhost pub]# systemctl restart vsftpd
[root@localhost pub]# systemctl start dhcpd
接下来安装系统
创建一台机器,不选择光盘,有网络启动
注意:需要选择同网段的网卡
这些都会自动安装,不需要人工干预
安装包有点多,安装时间比较长。耐心等待
安装成功,登录,查看ip,第一个获取所以是101
查看存储大小,是否跟当初配置的一样
总磁盘大小200G
/boot 300M
swap 2G
剩下的全部分给根分区
到这里pxe就安装完成
扩展:Cobbler比Kickstart更为简单和智能的自动化安装系统的工具
Cobbler 可以用来快速建立 Linux 网络安装环境,它已将 Linux 网络安装的技术门槛,从大专以上文化水平,成功降低到初中以下,连补鞋匠都能学会。
网络安装服务器套件 Cobbler(补鞋匠)从前,我们一直在做装机民工这份很有前途的职业。自打若干年前 Red Hat 推出了 Kickstart,此后我们顿觉身价倍增。不再需要刻了光盘一台一台地安装 Linux,只要搞定 PXE、DHCP、TFTP,还有那满屏眼花缭乱不知所云的 Kickstart 脚本,我们就可以像哈里波特一样,轻点魔棒,瞬间安装上百台服务器。这一堆花里胡哨的东西可不是一般人都能整明白的,没有大专以上学历,通不过英语四级,根本别想玩转。总而言之,这是一份多么有前途,多么有技术含量的工作啊。很不幸,Red Hat 最新(Cobbler项目最初在2008年左右发布)发布了网络安装服务器套件 Cobbler(补鞋匠),它已将 Linux 网络安装的技术门槛,从大专以上文化水平,成功降低到初中以下,连补鞋匠都能学会。对于我们这些在装机领域浸淫多年,经验丰富,老骥伏枥,志在千里的民工兄弟们来说,不啻为一个晴天霹雳。
http://www.osyunwei.com/archives/7606.html
http://os.51cto.com/art/201109/288604.htm