**Redhat系主要有兩種Kickstart和Cobbler。
Kickstart是一種無人值守的安裝方式。它的工作原理是在安裝過程中記錄人工干預填寫的各種參數,並生成一個名爲ks.cfg的文件。如果在自動安裝過程中出現要填寫參數的情況,安裝程序首先會去查找ks.cfg文件,如果找到合適的參數,就採用所找到的參數;如果沒有找到合適的參數,便會彈出對話框讓安裝者手工填寫。所以,如果ks.cfg文件涵蓋了安裝過程中所有需要填寫的參數,那麼安裝者完全可以只告訴安裝程序從何處下載ks.cfg文件,然後就去忙自己的事情。等安裝完畢,安裝程序會根據ks.cfg中的設置重啓/關閉系統,並結束安裝。
Cobbler集中和簡化了通過網絡安裝操作系統需要使用到的DHCP、TFTP和DNS服務的配置。Cobbler不僅有一個命令行界面,還提供了一個Web界面,大大降低了使用者的入門水平。Cobbler內置了一個輕量級配置管理系統,但它也支持和其它配置管理系統集成,如Puppet,暫時不支持SaltStack。
簡單的說,Cobbler是對kickstart的封裝,簡化安裝步驟、使用流程,降低使用者的門檻。
實戰-環境
[root@m01 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@m01 ~]# uname -r
2.6.32-573.el6.x86_64
[root@m01 ~]# getenforce
Disabled
[root@m01 ~]# /etc/init.d/iptables stop
[root@m01 ~]# ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4}'
10.0.0.61
注意:
• 虛擬機網卡採用NAT模式,不要使用橋接模式,因爲稍後我們會搭建DHCP服務器,在同一局域網多個DHCP服務會有衝突。
• VMware的NAT模式的dhcp服務也關閉,避免干擾。
DHCP簡介
DHCP(Dynamic Host Configuration Protocol,動態主機配置協議)通常被應用在大型的局域網絡環境中,主要作用是集中的管理、分配IP地址,使網絡環境中的主機動態的獲得IP地址、網關地址、DNS服務器地址等信息,並能夠提升地址的使用率。
1.1 DHCP服務安裝配置
[root@m01 ~]# yum install dhcp –y
[root@m01 ~]# rpm -ql dhcp |grep "dhcpd.conf"
/etc/dhcp/dhcpd.conf # 查看配置文件位置
/usr/share/doc/dhcp-4.1.1/dhcpd-conf-to-ldap
/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
/usr/share/man/man5/dhcpd.conf.5.gz
[root@m01 ~]# vim /etc/dhcp/dhcpd.conf
#DHCP Server Configuration file.
see /usr/share/doc/dhcp*/dhcpd.conf.sample
see 'man 5 dhcpd.conf'
subnet 172.16.1.0 netmask 255.255.255.0 {
range 172.16.1.100 172.16.1.200;
option subnet-mask 255.255.255.0;
default-lease-time 21600;
max-lease-time 43200;
next-server 172.16.1.61;
filename "/pxelinux.0";
}
#註釋
range 10.0.0.100 10.0.0.200; # 可分配的起始IP-結束IP
option subnet-mask 255.255.255.0; # 設定netmask
default-lease-time 21600; # 設置默認的IP租用期限
max-lease-time 43200; # 設置最大的IP租用期限
next-server 10.0.0.7; # 告知客戶端TFTP服務器的ip
filename "/pxelinux.0"; # 告知客戶端從TFTP根目錄下載pxelinux.0文件
[root@m01 ~]# /etc/init.d/dhcpd start
正在啓動 dhcpd: [確定]
[root@m01 ~]# netstat -tunlp|grep dhcp
udp 0 0 0.0.0.0:67 0.0.0.0:* 3358/dhcpd
本來軟件裝完後都要加入開機自啓動,但這個Kickstart系統就不能開機自啓動,而且用完後服務都要關閉,防止未來重啓服務器自動重裝系統了。
• 如果機器數量過多的話,注意dhcp服務器的地址池,不要因爲耗盡IP而導致dhcpd服務器沒有IP地址release的情況。
DHCP指定監聽網卡
說明:此知識點與本文無關,只是作者用過這個功能,記於此。
多網卡默認監聽eth0,指定DHCP監聽eth1網卡
[root@linux-node1 ~]# vim /etc/sysconfig/dhcpd
#Command line options here
DHCPDARGS=eth1 # 指定監聽網卡
[root@linux-node1 ~]# /etc/init.d/dhcpd restart
[root@linux-node1 ~]# tailf /var/log/messages
May 26 14:24:38 Kickstart kernel: e1000: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
May 26 14:29:04 Kickstart dhcpd: Internet Systems Consortium DHCP Server 4.1.1-P1
May 26 14:29:04 Kickstart dhcpd: Copyright 2004-2010 Internet Systems Consortium.
May 26 14:29:04 Kickstart dhcpd: All rights reserved.
May 26 14:29:04 Kickstart dhcpd: For info, please visit https://www.isc.org/software/dhcp/
May 26 14:29:04 Kickstart dhcpd: Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file
May 26 14:29:04 Kickstart dhcpd: Wrote 0 leases to leases file.
May 26 14:29:04 Kickstart dhcpd: Listening on LPF/eth1/00:0c:29:ea:c1:83/10.0.10.0/24
May 26 14:29:04 Kickstart dhcpd: Sending on LPF/eth1/00:0c:29:ea:c1:83/10.0.10.0/24
May 26 14:29:04 Kickstart dhcpd: Sending on Socket/fallback/fallback-net
……
1.2 安裝TFTP服務
TFTP簡介
TFTP(Trivial File Transfer Protocol,簡單文件傳輸協議)是TCP/IP協議族中的一個用來在客戶機與服務器之間進行簡單文件傳輸的協議,提供不復雜、開銷不大的文件傳輸服務。端口號爲69。
TFTP安裝配置
[root@linux-node1 ~]# yum -y install tftp-server
[root@linux-node1 ~]# 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 /var/lib/tftpboot # 指定目錄,保持默認,不用修改
disable = no # 由原來的yes改爲no
per_source = 11
cps = 100 2
flags = IPv4
}
[root@linux-node1 ~]# /etc/init.d/xinetd restart
Stopping xinetd: [FAILED]
Starting xinetd: [ OK ]
[root@linux-node1 ~]# netstat -tunlp|grep 69
udp 0 0 0.0.0.0:69 0.0.0.0:* 1106/xinetd
1.3 配置HTTP服務
可以用Apache或Nginx提供HTTP服務。Python的命令web服務不行,會有報錯。
1. [root@linux-node1 ~]# yum -y install httpd
2. [root@linux-node1 ~]# sed -i "277i ServerName 127.0.0.1:80" /etc/httpd/conf/httpd.conf
3. [root@linux-node1 ~]# /etc/init.d/httpd start
4. [root@linux-node1 ~]# mkdir /var/www/html/CentOS-6.7
5. [root@linux-node1 ~]# mount /dev/cdrom /var/www/html/CentOS-6.7/
6. mount: block device /dev/sr0 is write-protected, mounting read-only
7. [root@linux-node1 ~]# df -h
8. Filesystem Size Used Avail Use% Mounted on
9. /dev/sda3 19G 2.4G 16G 14% /
10. tmpfs 491M 16K 491M 1% /dev/shm
11. /dev/sda1 190M 36M 145M 20% /boot
12. /dev/sr0 3.7G 3.7G 0 100% /var/www/html/CentOS-6.7
13. # 不管怎麼弄,只要把安裝光盤內容能通過web發佈即可。因爲是演示,如果複製鏡像就有點浪費時間。但生產環境就一定要複製了,光盤讀取速度有限。
瀏覽器訪問http://10.0.0.61/CentOS-6.7/ 檢查配置是否正確
1.4 配置支持PXE的啓動程序
1.4.1 5.1 PXE引導配置(bootstrap)
syslinux是一個功能強大的引導加載程序,而且兼容各種介質。SYSLINUX是一個小型的Linux操作系統,它的目的是簡化首次安裝Linux的時間,並建立修護或其它特殊用途的啓動盤。如果沒有找到pxelinux.0這個文件,可以安裝一下。
[root@linux-node1 ~]# yum -y install syslinux
[root@linux-node1 ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
#複製啓動菜單程序文件
[root@linux-node1 ~]# cp -a /var/www/html/CentOS-6.7/isolinux/* /var/lib/tftpboot/
[root@linux-node1 ~]# ls /var/lib/tftpboot/
boot.cat grub.conf isolinux.bin memtest splash.jpg vesamenu.c32
boot.msg initrd.img isolinux.cfg pxelinux.0 TRANS.TBL vmlinuz
#新建一個pxelinux.cfg目錄,存放客戶端的配置文件。
[root@linux-node1 ~]# mkdir -p /var/lib/tftpboot/pxelinux.cfg
[root@linux-node1 ~]# cp /var/www/html/CentOS-6.7/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
1.4.2 5.2 PXE配置文件default解析
配合虛擬機演示講解default文件。新建一個虛擬機,注意內存需要給1G。
接下來的畫面就是我們熟悉的界面,輸入ESC可以進入命令行界面。
[root@linux-node1 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32 # 默認加載一個菜單
#prompt 1 # 開啓會顯示命令行'boot: '提示符。prompt值爲0時則不提示,將會直接啓動'default'參數中指定的內容。
timeout 600 # timeout時間是引導時等待用戶手動選擇的時間,設爲1可直接引導,單位爲1/10秒。
display boot.msg
#菜單背景圖片、標題、顏色。
menu background splash.jpg
menu title Welcome to CentOS 6.7!
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指定在boot:提示符下輸入的關鍵字,比如boot:linux[ENTER],這個會啓動label linux下標記的kernel和initrd.img文件。
label linux # 一個標籤就是前面圖片的一行選項。
menu label ^Install or upgrade an existing system
menu default
kernel vmlinuz # 指定要啓動的內核。同樣要注意路徑,默認是/tftpboot目錄。
append initrd=initrd.img # 指定追加給內核的參數,initrd.img是一個最小的linux系統
label vesa
menu label Install system with ^basic video driver
kernel vmlinuz
append initrd=initrd.img 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 –
網絡安裝這裏就不再敘述,唯一的區別是選擇安裝途徑選擇URL –設置(server 地址)http://172.16.1.61/CentOS6.7
選擇設置了的網卡eth1
禁用IPv6
http://172.16.1.61/CentOS6.7
1.4.3 ks.cfg詳解
官網文檔
CentOS5 : http://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-kickstart2-options.html
CentOS6 : https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Installation_Guide/s1-kickstart2-options.html
官網自帶中文版,選一下語言即可
ks.cfg文件組成大致分爲3段
• 命令段
鍵盤類型,語言,安裝方式等系統的配置,有必選項和可選項,如果缺少某項必選項,安裝時會中斷並提示用戶選擇此項的選項
• 軟件包段
- %packages
- @groupname:指定安裝的包組
- package_name:指定安裝的包
- -package_name:指定不安裝的包
在安裝過程中默認安裝的軟件包,安裝軟件時會自動分析依賴關係。
• 腳本段(可選) - %pre:安裝系統前執行的命令或腳本(由於只依賴於啓動鏡像,支持的命令很少)
- %post:安裝系統後執行的命令或腳本(基本支持所有命令)
關鍵字 含義
install 告知安裝程序,這是一次全新安裝,而不是升級upgrade。
url --url=" " 通過FTP或HTTP從遠程服務器上的安裝樹中安裝。
url --url="http://10.0.0.7/CentOS-6.7/"
url --url ftp://<username>:<password>@<server>/<dir>
nfs 從指定的NFS服務器安裝。
nfs --server=nfsserver.example.com --dir=/tmp/install-tree
text 使用文本模式安裝。
lang 設置在安裝過程中使用的語言以及系統的缺省語言。lang en_US.UTF-8
keyboard 設置系統鍵盤類型。keyboard us
zerombr 清除mbr引導信息。
bootloader 系統引導相關配置。
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
--location=,指定引導記錄被寫入的位置.有效的值如下:mbr(缺省),partition(在包含內核的分區的第一個扇區安裝引導裝載程序)或none(不安裝引導裝載程序)。
--driveorder,指定在BIOS引導順序中居首的驅動器。
--append=,指定內核參數.要指定多個參數,使用空格分隔它們。
network 爲通過網絡的kickstart安裝以及所安裝的系統配置聯網信息。
network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS6
--bootproto=[dhcp/bootp/static]中的一種,缺省值是dhcp。bootp和dhcp被認爲是相同的。
static方法要求在kickstart文件裏輸入所有的網絡信息。
network --bootproto=static --ip=10.0.0.100 --netmask=255.255.255.0 --gateway=10.0.0.2 --nameserver=10.0.0.2
請注意所有配置信息都必須在一行上指定,不能使用反斜線來換行。
--ip=,要安裝的機器的IP地址.
--gateway=,IP地址格式的默認網關.
--netmask=,安裝的系統的子網掩碼.
--hostname=,安裝的系統的主機名.
--onboot=,是否在引導時啓用該設備.
--noipv6=,禁用此設備的IPv6.
--nameserver=,配置dns解析.
timezone 設置系統時區。timezone --utc Asia/Shanghai
authconfig 系統認證信息。authconfig --enableshadow --passalgo=sha512
設置密碼加密方式爲sha512 啓用shadow文件。
rootpw root密碼
clearpart 清空分區。clearpart --all --initlabel
--all 從系統中清除所有分區,--initlable 初始化磁盤標籤
part 磁盤分區。
part /boot --fstype=ext4 --asprimary --size=200
part swap --size=1024
part / --fstype=ext4 --grow --asprimary --size=200
--fstype=,爲分區設置文件系統類型.有效的類型爲ext2,ext3,swap和vfat。
--asprimary,強迫把分區分配爲主分區,否則提示分區失敗。
--size=,以MB爲單位的分區最小值.在此處指定一個整數值,如500.不要在數字後面加MB。
--grow,告訴分區使用所有可用空間(若有),或使用設置的最大值。
firstboot 負責協助配置redhat一些重要的信息。
firstboot --disable
selinux 關閉selinux。selinux --disabled
firewall 關閉防火牆。firewall --disabled
logging 設置日誌級別。logging --level=info
reboot 設定安裝完成後重啓,此選項必須存在,不然kickstart顯示一條消息,並等待用戶按任意鍵後才重新引導,也可以選擇halt關機。
先生成一個密碼備用
[root@linux-node1 ~]# grub-crypt
Password:123456
Retype password:123456
$6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
[root@linux-node1 ~]# mkdir /var/www/html/ks_config
# Kickstart Configurator for CentOS 6.7 by yao zhang
install
url --url="http://172.16.1.61/CentOS-6.7/"
text
lang en_US.UTF-8
keyboard us
lang en_US.UTF-8
keyboard us
# Kickstart Configurator for CentOS 6.7 by yao zhang
install
url --url="http://172.16.1.61/CentOS-6.7/"
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS6
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw --iscrypted $6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
clearpart --all --initlabel
part /boot --fstype=ext4 --asprimary --size=200
part swap --size=1024
part / --fstype=ext4 --grow --asprimary --size=200
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot
%packages
@base
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet
%post
wget -O /tmp/optimization.sh http://172.16.1.61/ks_config/optimization.sh &>/dev/null
/bin/sh /tmp/optimization.sh
%end
開機優化腳本
[root@m01 ~]# vim /var/www/html/ks_config/optimization.sh
#!/bin/bash
##############################################################
# File Name: /var/www/html/ks_config/optimization.sh
# Version: V1.0
# Author: yao zhang
# Organization: www.zyops.com
# Created Time : 2018-04-29 15:23:08
# Description: Linux system initialization
##############################################################
. /etc/init.d/functions
Ip=172.16.1.61
Port=80
ConfigDir=ks_config
# Judge Http server is ok?
PortNum=`nmap $Ip -p $Port 2>/dev/null|grep open|wc -l`
[ $PortNum -lt 1 ] && {
echo "Http server is bad!"
exit 1
}
# Defined result function
action "$1" /bin/true
else
action "$1" /bin/false
fi
}
# Defined IP function
function ConfigIP(){
Suffix=`ifconfig eth0|awk -F "[ .]+" 'NR==2 {print $6}'`
cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<-END
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=10.0.0.$Suffix
PREFIX=24
GATEWAY=10.0.0.2
DNS1=10.0.0.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
END
Msg "config eth0"
}
# Defined Yum source Functions
[ -f "$YumDir/CentOS-Base.repo" ] && cp $YumDir/CentOS-Base.repo{,.ori}
wget -O $YumDir/epel.repo http://$Ip:$Port/$ConfigDir/epel.repo &>/dev/null &&\
Msg "YUM source"
}
# Defined Hide the system version number Functions
function HideVersion(){
[ -f "/etc/issue" ] && >/etc/issue
Msg "Hide issue"
[ -f "/etc/issue.net" ] && > /etc/issue.net
Msg "Hide issue.net"
}
# Defined OPEN FILES Functions
function openfiles(){
[ -f "/etc/security/limits.conf" ] && {
echo '* - nofile 65535' >> /etc/security/limits.conf
Msg "open files"
}
}
# Defined Kernel parameters Functions
function kernel(){
KernelDir=/etc
[ -f "$KernelDir/sysctl.conf" ] && /bin/mv $KernelDir/sysctl.conf{,.ori}
wget -O $KernelDir/sysctl.conf http://$Ip:$Port/$ConfigDir/sysctl.conf &>/dev/null
Msg "Kernel config"
}
# Defined System Startup Services Functions
function boot(){
for oldboy in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|rsyslog|sshd|sysstat"`
do
chkconfig $oldboy off
done
Msg "BOOT config"
}
# Defined Time Synchronization Functions
function Time(){
echo "#time sync by zhangyao at $(date +%F)" >>/var/spool/cron/root
echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null' >>/var/spool/cron/root
Msg "Time Synchronization"
}
# Defined main Functions
function main(){
ConfigIP
yum
HideVersion
openfiles
kernel
boot
Time
}
main
整合編輯default配置文件
#最精簡配置
[root@m01~]#vim /var/lib/tftpboot/pxelinux.cfg/default default ks
default ks
prompt 0
label ks
kernel vmlinuz
append initrd=initrd.img ks=http://172.16.1.61/ks_config/CentOS-6.7-ks.cfg ksdevice=eth1 # 告訴安裝程序ks.cfg文件在哪裏
#ksdevice=eth0代表當客戶端有多塊網卡的時候,要實現自動化需要設置從eth1安裝,不指定的話,安裝的時候系統會讓你選擇,那就不叫全自動化了。
無人值守自動安裝
打開系統電源,出去喝杯水。
下面是安裝過程中截的幾個圖
重啓客戶機-開始自動安裝