12.3. 自动安装系统(kickstart+PXE)
- Date:
2018-09
小技巧
前面的准备过程和 手动安装系统(kickstart+PXE) 一样
12.3.1. 系统环境准备
12.3.1.1. 系统版本
1[root@cent6_cobbler_01 ~]# cat /etc/redhat-release
2CentOS release 6.6 (Final)
3[root@cent6_cobbler_01 ~]# uname -r
42.6.32-504.el6.x86_64
5[root@cent6_cobbler_01 ~]# cat /etc/sysconfig/network
6NETWORKING=yes
7HOSTNAME=cent6_cobbler_01
12.3.1.2. 网络时间同步
1[root@cent6_cobbler_01 ~]# date
2Thu Sep 6 21:07:25 CST 2018
3[root@cent6_cobbler_01 ~]# ntpdate pool.ntp.org
428 Sep 00:53:38 ntpdate[1577]: step time server 5.103.139.163 offset 1827966.915121 sec
12.3.1.3. 关闭selinux
注意
如果不关闭selinux也没有配置selinux。则安装以后zabbix会启动失败。会发现zabbix网页可以访问,但是提示zabbix服务没有启动。
- 永久关闭:
下面配置会让selinux的关闭重启系统后还是关闭状态。但是配置不会立即生效。
注意
通过 source /etc/selinux/config 也不能让修改的文件立即生效。所以需要下面的临时关闭的方式结合使用。
1[root@cent6_cobbler_01 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
2[root@cent6_cobbler_01 ~]# grep SELINUX /etc/selinux/config
3# SELINUX= can take one of these three values:
4SELINUX=disabled
5# SELINUXTYPE= can take one of these two values:
6SELINUXTYPE=targeted
- 临时关闭:
下面配置是立即生效,但是系统重启后会失效。
1[root@cent6_cobbler_01 ~]# getenforce
2Enforcing
3[root@cent6_cobbler_01 ~]# setenforce 0
4[root@cent6_cobbler_01 ~]# getenforce
5Permissive
12.3.1.4. 关闭防火墙
注意
防火墙一般都是关闭。如果不不关闭,也可以通过配置规则允许所有使用的端口被访问。
1[root@cent6_cobbler_01 ~]# /etc/init.d/iptables stop
2iptables: Setting chains to policy ACCEPT: filter [ OK ]
3iptables: Flushing firewall rules: [ OK ]
4iptables: Unloading modules: [ OK ]
关闭防火墙开机自启动
1[root@cent6_cobbler_01 ~]# chkconfig iptables off
12.3.1.5. 系统准备命令集合
1ntpdate pool.ntp.org
2sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
3setenforce 0
4/etc/init.d/iptables stop
5chkconfig iptables off
12.3.2. DHCP安装配置
安装dhcp服务:
1[root@cent6_cobbler_01 ~]# yum install dhcp -y
查看DHCP安装目录:
1[root@cent6_cobbler_01 ~]# rpm -ql dhcp
DHCP配置
1[root@cent6_cobbler_01 ~]# cat >>/etc/dhcp/dhcpd.conf<<EOF
2> subnet 192.168.6.0 netmask 255.255.255.0 {
3> range 192.168.6.100 192.168.6.200;
4> option subnet-mask 255.255.255.0;
5> default-lease-time 21600;
6> max-lease-time 43200;
7> next-server 192.168.6.10;
8> filename "/pxelinux.0";
9> }
10> EOF
11[root@cent6_cobbler_01 ~]# cat /etc/dhcp/dhcpd.conf
12#
13# DHCP Server Configuration file.
14# see /usr/share/doc/dhcp*/dhcpd.conf.sample
15# see 'man 5 dhcpd.conf'
16#
17subnet 192.168.6.0 netmask 255.255.255.0 {
18 range 192.168.6.100 192.168.6.200;
19 option subnet-mask 255.255.255.0;
20 default-lease-time 21600;
21 max-lease-time 43200;
22 next-server 192.168.6.10;
23 filename "/pxelinux.0";
24}
检查网卡信息:
1[root@cent6_cobbler_01 ~]# ifconfig
2eth0 Link encap:Ethernet HWaddr 00:0C:29:B3:93:42
3 inet addr:192.168.161.132 Bcast:192.168.161.255 Mask:255.255.255.0
4 inet6 addr: fe80::20c:29ff:feb3:9342/64 Scope:Link
5 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
6 RX packets:1014 errors:0 dropped:0 overruns:0 frame:0
7 TX packets:592 errors:0 dropped:0 overruns:0 carrier:0
8 collisions:0 txqueuelen:1000
9 RX bytes:108635 (106.0 KiB) TX bytes:97793 (95.5 KiB)
10
11eth1 Link encap:Ethernet HWaddr 00:0C:29:B3:93:4C
12 inet addr:192.168.6.10 Bcast:192.168.6.255 Mask:255.255.255.0
13 inet6 addr: fe80::20c:29ff:feb3:934c/64 Scope:Link
14 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
15 RX packets:4 errors:0 dropped:0 overruns:0 frame:0
16 TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
17 collisions:0 txqueuelen:1000
18 RX bytes:316 (316.0 b) TX bytes:916 (916.0 b)
19
20lo Link encap:Local Loopback
21 inet addr:127.0.0.1 Mask:255.0.0.0
22 inet6 addr: ::1/128 Scope:Host
23 UP LOOPBACK RUNNING MTU:65536 Metric:1
24 RX packets:0 errors:0 dropped:0 overruns:0 frame:0
25 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
26 collisions:0 txqueuelen:0
27 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
修改默认网关:
注意
这一步根据实际情况。本例子,因为默认网关192.168.6.1不能访问外网,所以修改了默认网关。
1[root@cent6_cobbler_01 ~]# route
2Kernel IP routing table
3Destination Gateway Genmask Flags Metric Ref Use Iface
4192.168.6.0 * 255.255.255.0 U 0 0 0 eth1
5192.168.161.0 * 255.255.255.0 U 0 0 0 eth0
6link-local * 255.255.0.0 U 1002 0 0 eth0
7link-local * 255.255.0.0 U 1003 0 0 eth1
8default 192.168.6.1 0.0.0.0 UG 0 0 0 eth1
9
10[root@cent6_cobbler_01 ~]# route del default gw 192.168.6.1
11[root@cent6_cobbler_01 ~]# route add default gw 192.168.161.2
启动DHCP
1[root@cent6_cobbler_01 ~]# /etc/init.d/dhcpd start
2Starting dhcpd: [ OK ]
3
4[root@cent6_cobbler_01 ~]# lsof -i :67
5COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
6dhcpd 1866 dhcpd 7u IPv4 14762 0t0 UDP *:bootps
12.3.3. TFTP安装配置
tfpt安装:
1[root@cent6_cobbler_01 ~]# yum install tftp-server -y
配置tftp:
1[root@cent6_cobbler_01 ~]# cat -n /etc/xinetd.d/tftp
2 1 # default: off
3 2 # description: The tftp server serves files using the trivial file transfer \
4 3 # protocol. The tftp protocol is often used to boot diskless \
5 4 # workstations, download configuration files to network-aware printers, \
6 5 # and to start the installation process for some operating systems.
7 6 service tftp
8 7 {
9 8 socket_type = dgram
10 9 protocol = udp
11 10 wait = yes
12 11 user = root
13 12 server = /usr/sbin/in.tftpd
14 13 server_args = -s /var/lib/tftpboot
15 14 disable = yes
16 15 per_source = 11
17 16 cps = 100 2
18 17 flags = IPv4
19 18 }
20
21[root@cent6_cobbler_01 ~]# sed -i '14s/yes/no/' /etc/xinetd.d/tftp
22
23[root@cent6_cobbler_01 ~]# cat -n /etc/xinetd.d/tftp
24 1 # default: off
25 2 # description: The tftp server serves files using the trivial file transfer \
26 3 # protocol. The tftp protocol is often used to boot diskless \
27 4 # workstations, download configuration files to network-aware printers, \
28 5 # and to start the installation process for some operating systems.
29 6 service tftp
30 7 {
31 8 socket_type = dgram
32 9 protocol = udp
33 10 wait = yes
34 11 user = root
35 12 server = /usr/sbin/in.tftpd
36 13 server_args = -s /var/lib/tftpboot
37 14 disable = no
38 15 per_source = 11
39 16 cps = 100 2
40 17 flags = IPv4
41 18 }
启动tftp服务:
1[root@cent6_cobbler_01 ~]# /etc/init.d/xinetd start
2Starting xinetd: [ OK ]
3
4
5[root@cent6_cobbler_01 ~]# ss -tunlp|grep 69
6udp UNCONN 0 0 *:68 *:* users:(("dhclient",3269,6))
7udp UNCONN 0 0 *:69 *:* users:(("xinetd",3449,5))
12.3.4. apache安装配置
安装apache:
1[root@cent6_cobbler_01 ~]# yum -y install httpd
添加ServerName,防止http提示域名和主机名映射的问题:
1[root@cent6_cobbler_01 ~]# sed -i "277i ServerName 127.0.0.1:80" /etc/httpd/conf/httpd.conf
启动apache服务:
1[root@cent6_cobbler_01 ~]# /etc/init.d/httpd start
2Starting httpd: [ OK ]
查看http服务状态:
1[root@cent6_cobbler_01 ~]# lsof -i :80
2COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
3httpd 3553 root 4u IPv6 18461 0t0 TCP *:http (LISTEN)
4httpd 3554 apache 4u IPv6 18461 0t0 TCP *:http (LISTEN)
5httpd 3555 apache 4u IPv6 18461 0t0 TCP *:http (LISTEN)
6httpd 3556 apache 4u IPv6 18461 0t0 TCP *:http (LISTEN)
7httpd 3558 apache 4u IPv6 18461 0t0 TCP *:http (LISTEN)
8httpd 3559 apache 4u IPv6 18461 0t0 TCP *:http (LISTEN)
9httpd 3560 apache 4u IPv6 18461 0t0 TCP *:http (LISTEN)
10httpd 3561 apache 4u IPv6 18461 0t0 TCP *:http (LISTEN)
11httpd 3562 apache 4u IPv6 18461 0t0 TCP *:http (LISTEN)
创建挂载系统的目录,建议用操作系统版本命名。这样方便以后安装其他版本系统:
1[root@cent6_cobbler_01 ~]# mkdir /var/www/html/centos/6.6 -p
挂载并检查挂载情况:
1[root@cent6_cobbler_01 ~]# mount /dev/cdrom /var/www/html/centos/6.6
2mount: block device /dev/sr0 is write-protected, mounting read-only
3[root@cent6_cobbler_01 ~]# ls /var/www/html/centos/6.6/
4CentOS_BuildTag GPL RPM-GPG-KEY-CentOS-6 RPM-GPG-KEY-CentOS-Testing-6 isolinux
5EFI Packages RPM-GPG-KEY-CentOS-Debug-6 TRANS.TBL repodata
6EULA RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-Security-6 images
测试http访问情况:
1[root@cent6_cobbler_01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://192.168.6.10/centos/6.6/
2200
12.3.5. 配置支持PXE的启动程序
安装syslinux
1[root@cent6_cobbler_01 ~]# yum -y install syslinux
syslinux是一个功能强大的引导加载程序,而且兼容各种介质。 SYSLINUX是一个小型的Linux操作系统,它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。
1[root@cent6_cobbler_01 ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
2[root@cent6_cobbler_01 ~]# cp -a /var/www/html/centos/6.6/isolinux/* /var/lib/tftpboot/
3[root@cent6_cobbler_01 ~]# ls /var/lib/tftpboot/
4TRANS.TBL boot.msg initrd.img isolinux.cfg pxelinux.0 vesamenu.c32
5boot.cat grub.conf isolinux.bin memtest splash.jpg vmlinuz
6
7[root@cent6_cobbler_01 ~]# cp /var/www/html/centos/6.6/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
12.3.6. 创建ks.cfg文件
我们一般普通安装系统的时候是一个交互过程。为了减少这个交互过程,kickstart就诞生了。
使用这种kickstart,只需事先定义好一个Kickstart自动应答配置文件ks.cfg(通常存放在安装服务器上),并让安装程序知道该配置文件的位置,在安装过程中安装程序就可以自己从该文件中读取安装配置,这样就避免了在安装过程中多次的人机交互,从而实现无人值守的自动化安装。
- 生成kickstart配置文件的三种方法:
方法1:每安装好一台Centos机器,Centos安装程序都会创建一个kickstart配置文件,记录你的真实安装配置。如果你希望实现和某系统类似的安装,可以基于该系统的kickstart配置文件来生成你自己的kickstart配置文件。(生成的文件名字叫anaconda-ks.cfg位于/root/anaconda-ks.cfg)
方法2:Centos提供了一个图形化的kickstart配置工具。在任何一个安装好的Linux系统上运行该工具,就可以很容易地创建你自己的kickstart配置文件。kickstart配置工具命令为redhat-config-kickstart(RHEL3)或system-config-kickstart(RHEL4,RHEL5).网上有很多用CentOS桌面版生成ks文件的文章,如果有现成的系统就没什么可说。但没有现成的,也没有必要去用桌面版,命令行也很简单。
方法3:阅读kickstart配置文件的手册。用任何一个文本编辑器都可以创建你自己的kickstart配置文件。
[root@cent6_cobbler_01 ~]# ll anaconda-ks.cfg -rw——-. 1 root root 1040 Mar 30 17:41 anaconda-ks.cfg
- 官网文档
- ks.cfg文件组成大致分为3段:
命令段
软件包段
脚本段(可选)
提示
脚本段在生产环境用来做服务器系统安装后的初始优化。
关键字 |
含义 |
install |
告知安装程序,这是一次全新安装,而不是升级upgrade。 |
url –url=” “ |
通过FTP或HTTP从远程服务器上的安装树中安装。 url –url=”http://192.168.6.10/centos/6.6/” url –url ftp://<username>:<password>@<server>/<dir> |
nfs |
|
text |
使用文本模式安装。 |
lang |
设置在安装过程中使用的语言以及系统的缺省语言。lang en_US.UTF-8 |
keyboard |
设置系统键盘类型。keyboard us |
zerombr |
清除mbr引导信息。 |
bootloader |
–location=,指定引导记录被写入的位置. 有效的值如下:mbr(缺省),partition(在包含内核的分区的第一个扇区安装引导装载程序) 或none(不安装引导装载程序)。 –driveorder,指定在BIOS引导顺序中居首的驱动器。 –append=,指定内核参数.要指定多个参数,使用空格分隔它们。 |
network |
为通过网络的kickstart安装以及所安装的系统配置联网信息。
–bootproto=[dhcp/bootp/static]中的一种,缺省值是dhcp。bootp和dhcp被认为是相同的。 static方法要求在kickstart文件里输入所有的网络信息。
请注意所有配置信息都必须在一行上指定,不能使用反斜线来换行。 –ip=,要安装的机器的IP地址. –gateway=,IP地址格式的默认网关. –netmask=,安装的系统的子网掩码. –hostname=,安装的系统的主机名. –onboot=,是否在引导时启用该设备. –noipv6=,禁用此设备的IPv6. –nameserver=,配置dns解析. |
timezone |
设置系统时区。timezone –utc Asia/Shanghai |
authconfig |
设置密码加密方式为sha512 启用shadow文件。 |
rootpw |
root密码 |
clearpart |
–all 从系统中清除所有分区,–initlable 初始化磁盘标签 |
part |
磁盘分区。
–fstype=,为分区设置文件系统类型.有效的类型为ext2,ext3,swap和vfat。 –asprimary,强迫把分区分配为主分区,否则提示分区失败。 –size=,以MB为单位的分区最小值.在此处指定一个整数值,如500.不要在数字后面加MB。 –grow,告诉分区使用所有可用空间(若有),或使用设置的最大值。 负责协助配置redhat一些重要的信息。 |
firstboot |
|
selinux |
关闭selinux。 |
firewall |
关闭防火墙。 |
logging |
设置日志级别。 |
reboot |
设定安装完成后重启,此选项必须存在,不然kickstart显示一条消息, 并等待用户按任意键后才重新引导,也可以选择halt关机。 |
配置root密码
密码123,但是不显示
1[root@cent6_cobbler_01 ~]# grub-crypt
2Password:
3Retype password:
4$6$GafRCAkqcz35Y62c$yqmxZeTgOsMWawSyJ/crWjx9N2zBQBUn1A6295uAhRLJqptzvX5pnU.vct6snauchxB8aUF486ojM6aICqemb0
配置ks文件
1[root@cent6_cobbler_01 ~]# cat >>/var/www/html/centos/ks_config/centos-6.6-ks.cfg<<EOF
2> # Kickstart Configurator for CentOS 6.6 by cent6_cobbler_01
3> install
4> url --url="http://192.168.6.10/centos/6.6/"
5> text
6> lang en_US.UTF-8
7> keyboard us
8> zerombr
9> bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
10> network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS6
11> timezone --utc Asia/Shanghai
12> authconfig --enableshadow --passalgo=sha512
13> rootpw --iscrypted $6$GafRCAkqcz35Y62c$yqmxZeTgOsMWawSyJ/crWjx9N2zBQBUn1A6295uAhRLJqptzvX5pnU.vct6snauchxB8aUF486ojM6aICqemb0
14> clearpart --all --initlabel
15> part /boot --fstype=ext4 --asprimary --size=200
16> part swap --size=1024
17> part / --fstype=ext4 --grow --asprimary --size=200
18> firstboot --disable
19> selinux --disabled
20> firewall --disabled
21> logging --level=info
22> reboot
23> %packages
24> @base
25> @compat-libraries
26> @debugging
27> @development
28> tree
29> nmap
30> sysstat
31> lrzsz
32> dos2unix
33> telnet
34> %post
35> wget -O /tmp/optimization.sh http://192.168.6.10/centos/ks_config/centos6_optimization.sh &>/dev/null
36> /bin/sh /tmp/optimization.sh
37> %end
38> EOF
编辑装机完成后运行的系统优化脚本:
1[root@cent6_cobbler_01 ~]# vi >>/var/www/html/centos/ks_config/centos6_optimization.sh
把 开机优化脚本 内容插入上面的文件中。
整合编辑default配置文件
1[root@cent6_cobbler_01 ~]# cat >>/var/lib/tftpboot/pxelinux.cfg/default<<EOF
2> default ks
3> prompt 0
4>
5> label ks
6> kernel vmlinuz
7> append initrd=initrd.img ks=http://192.168.6.10/centos/ks_config/centos-6.6-ks.cfg ksdevice=eth0
8> EOF
注意
上面文件中的超链接指定的文件需要和前面配置的http文件路径一致。
参数
ksdevice指定默认网卡,如果不指定,在服务器有多网卡时会弹出页面让选择网卡。一般无论有几个网卡都有eth0,所以选择eth0。
12.3.7. 开机优化脚本
1#!/usr/bin/env bash
2PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
3export PATH
4#=================================================================#
5# System Required: CentOS 6+, #
6# Description: optimization CentOS6.X #
7# Author: cent6_cobbler_01 <login_root@163.com> #
8# Thanks: @XXX <XXX> #
9# Intro: #
10#=================================================================#
11
12Interface=eth0
13
14# add log to the log file
15function sh_log(){
16 if [ $? -eq 0 ];then
17 echo "$1 success" >>/tmp/optimization-`date +%F`.log
18 else
19 echo "$1 fail" >>/tmp/optimization-`date +%F`.log
20 fi
21}
22
23# get the network info:ip mask gateway suffix
24function get_networkinfo(){
25 Ip=`/sbin/ifconfig $Interface|awk -F '[ :]' '{if(NR==2) print $13}'`
26 Suffix=`ifconfig $Interface|awk -F "[ .]+" 'NR==2 {print $6}'`
27 Mask=`/sbin/ifconfig $Interface|awk -F '[ :]' '{if(NR==2) print $NF}'`
28 Gateway=`/sbin/route|grep "^default.*$Interface$"|awk '{print $2}'`
29}
30
31# config network use static ip
32function config_network(){
33 get_networkinfo
34cat >/etc/sysconfig/network-scripts/ifcfg-$Interface <<-END
35DEVICE=$Interface
36TYPE=Ethernet
37ONBOOT=yes
38NM_CONTROLLED=yes
39BOOTPROTO=none
40IPADDR=$Ip
41NETMASK=$Mask
42GATEWAY=$Gateway
43DEFROUTE=yes
44IPV4_FAILURE_FATAL=yes
45IPV6INIT=no
46END
47}
48
49# config max limit of open file
50function config_unlimit(){
51 [ -f "/etc/security/limits.conf" ] && {
52 echo '* - nofile 65535' >> /etc/security/limits.conf
53 ulimit -HSn 65535 >/dev/null 2&1
54 }
55}
56
57# config service start when sys start
58function config_base_services(){
59 Services="crond|network|rsyslog|sshd|sysstat|ntpd"
60 /sbin/chkconfig --list|grep "3:on"|grep -vE $Services|awk '{print "chkconfig " $1 " off"}'|/bin/bash >/dev/null 2&1
61}
62
63# config ssh service
64function config_ssh(){
65 File_ssh=/etc/ssh/sshd_config
66 cp $File_ssh $File_ssh.backup
67 sed -i 's%#PermitRootLogin no%PermitRootLogin yes%' $File_ssh >/dev/null 2&1
68 sed -i 's%#UseDNS yes%UseDNS no%' $File_ssh >/dev/null 2&1
69 sed -i 's%GSSAPIAuthentication yes%GSSAPIAuthentication no%' $File_ssh >/dev/null 2&1
70
71}
72
73# config quick time when sys boot
74function config_boot_time(){
75 Bootloader="/boot/grub/grub.conf"
76 /bin/sed -i 's#rhgb quiet##' $Bootloader >/dev/null 2&1
77 /bin/sed -i 's#timeout=5#timeout=1#' $Bootloader >/dev/null 2&1
78}
79
80# config sys time
81function config_systime(){
82 Dateserver='pool.ntp.org'
83 /usr/sbin/ntpdate $Dateserver >/dev/null 2&1
84 echo "#time sysc by myhome at 2018-03-30" >>/var/spool/cron/root
85 echo "*/5 * * * * /usr/sbin/ntpdate $Dateserver >/dev/null 2&1" >>/var/spool/cron/root
86}
87
88# the script's main function
89function main(){
90 config_base_services
91 sh_log "config_base_services:base service config"
92 config_ssh
93 sh_log "config_ssh:config ssh service"
94 config_network
95 sh_log "config_network:config network"
96 config_systime
97 sh_log "config_systime:config system time sync"
98 config_unlimit
99 sh_log "config_unlimit:config max open file"
100 config_boot_time
101 sh_log "config_boot_time:config boot time"
102}
103
104main