使用kickstart批量安装Linux

Posted: 2009年4月26日星期日
作为一名SA,在工作当中会经常安装Linux操作系统,对多台服务器配置同样的环境。如果一个个去安装,一台台去配置,那是一件吃力不讨好的事情。现只要你有一台安装好的Linux,要安装的服务器能支持pxe网络启动安装,所有的服务器能组建在一个局域网,几十台服务器不到半个小时不仅安装好系统同时也完成基本的配置。在Redhat系列操作系统中有个叫Kickstart软件,它的作用是的按照事先设计好的方式自动安装操作系统。最常见的方式是网络安装,也可以采用CDROM或硬盘安装。本文主要介绍Kickstart的网络安装。其中kickstart网络安装方式支持nfs,ftp,http的方式进行安装。本文中实例是通过nfs的方式安装centos,同样适合于安装redhat,fedora等操作系统。
本文的实施方案的拓扑图如下所示
                 ┌────────┐
                 │  Boot Server   │
                 │(DHCP TFTP NFS)│
                 └────┬───┘
                          │
                ┌────┴───┐
                │      Switch   │
                └────┬───┘
                         │
          ┌───────┴───────┐
          │                            │
  ┌───┴───┐              ┌───┴───┐
  │  Client(PxE)│              │  Client(PxE)│
  └───────┘              └───────┘实施步骤
1.配置启动服务器
2.配置安装方案
3.启动待安装服务器,进行系统安装

配置启动服务器
启动服务器的目的是帮助待安装机器上启动Red Hat Linux安装程序。启动服务器上需要搭建一个DHCP 服务器和一个TFTP 服务器,一个nfs用来传输网络镜像文件。前者是为了给待安装机器分配IP地址,后者则是提供了一个让待安装机器下载启动镜像的途径。
首先配置DHCP服务
1) 安装DHCP服务器包(RPM包名:dhcpd)。
2)配置服务器IP地址
3) 编辑DHCP服务器配置文件 /etc/dhcp.conf。

ddns-update-style interim;
ignore client-updates;
allow booting;
allow bootp;

subnet 192.168.0.0 netmask 255.255.255.0 {
    option routers            192.168.0.1;
    option subnet-mask        255.255.255.0;
    option domain-name-servers    192.168.0.1;
    range dynamic-bootp 192.168.0.128 192.168.0.254;
    default-lease-time 21600;
    max-lease-time 43200;
    #deny unknown-clients;

    #group pxe {
        next-server 192.168.0.4;
        filename "/centos/pxelinux.0";
        #host client { hardware ethernet 00:00:00:00:00:00; fixed-address 192.168.0.100; }
     }     
}

注:如需给指定的服务器进行安装的话,就去掉上面的注释。
    简单说一下文中的注释,deny unknown-clients;拒绝给为授权机器分配IP地址
    next-server 指定启动服务器地址,filename指定启动文件地址。
    host client为mac地址为00:00:00:00:00:00的client服务器指定IP地址为:192.168.0.100.

4) 给启动服务器配置一个IP,这个IP必须在DHCP服务器定义的子网内。
5) 启动DHCP服务。
service dhcpd start

配置TFTP服务器(如没有安装xinetd软件包,先安装这个软件包)

1) 安装TFTP服务器包(RPM包名:tftpd)。
2) 编辑TFTP 服务器的配置文件 /etc/xinetd.d/tftp。配置文件如下:

service tftp
{
    socket_type     = dgram
    protocol        = udp
    wait            = yes
    user            = root
    server          = /usr/sbin/in.tftpd
    server_args     = -s /tftpboot
    disable         = no
}

这里选定了/tftpboot为TFTP 服务器的根目录位置。
3)启动TFTP服务
service xinetd start
4)建立存放Linux安装程序的内核/根文件系统文件的目录
mkdir /tftpboot/centos
复制Centos光盘的/isolinux目录下Linux安装程序内核/根文件系统文件initrd.img 和vmlinuz到
/tftpboot/centos
5)将启动镜像文件pxelinux.0复制到TFTP服务器根目录。
启动镜像pxelinux.0可以在syslinux安装包里获得。安装好syslinux安装包后,将pxelinux.0复制到/tftpboot/centos
cp /usr/lib/syslinux/pxelinux.0 /tftpboot/centos
启动镜像pxelinux.0文件在执行过程中,会读取配置文件以确定它应该载入什么Linux内核文件来运行。所有的配置文件都放在启动服务器的/tftpboot/centos/pxelinux.cfg/目录下。
建立启动配置文件存放文件夹mkdir /tftpboot/centos/pxelinux.cfg
6)建立启动配置文件default

default centos5
label centos5
kernel vmlinuz
append load initrd=initrd.img ksdevice=eth0 ks=nfs:192.168.0.4:/home/centos/ks.cfg devfs=nomount


配置nfs
配置nfs共享目录,nfs的配置文件为/etc/exports
/home/centos *(ro,no_root_squash)
启动nfs相关服务
service portmap start
service nfs start
确认共享目录成功了
showmount -e localhost
若得到结果包含/home/centos * ,说明成功配置好nfs

配置安装方案
配置文件为/home/centos/ks.cfg

text
install
repo --name=epel --baseurl=http://download.fedora.redhat.com/pub/epel/5/x86_64/
nfs --server=192.168.0.4 --dir=/home/centos/
keyboard us
lang en_US
key --skip
xconfig --defaultdesktop=GNOME --depth=8 --resolution=640x480
network --bootproto=dhcp --device=eth0 --onboot=on
#network --bootproto=dhcp --device=eth1 --onboot=on
rootpw --iscrypted $1$Sl6MDA1O$Z2HRda3FpCmaSRqrmD.gD0
user --name test --homedir /home/test --iscrypted --password=$1$IXb255iK$wZq/pCb2o70P1F8pXnI6P/ --shell=/bin/bash --groups=test,wheel
firewall --disabled
auth --useshadow --enablemd5
selinux --disabled
timezone --isUtc Asia/Shanghai
bootloader --location=mbr
logging --level=info

clearpart --all --drives=sda
part /boot --bytes-per-inode=4096 --fstype="ext3" --size=150
part swap --bytes-per-inode=4096 --fstype="swap" --size=2048
part pv.01 --size=1 --grow
volgroup lvm pv.01
logvol / --vgname=lvm --size=20480 --name=root
logvol /var --vgname=lvm --size=20480 --name=var
logvol /tmp --vgname=lvm --size=20480 --name=tmp
logvol /data --vgname=lvm --size=1 --grow --name=data
reboot

%packages
@editors
@development-libs
@base
@development-tools
-firstboot-tui
ntp
net-snmp-utils
net-snmp-libs
net-snmp
openssl-devel
nrpe
nagios-plugins
nagios-plugins-all

%post --log=/mnt/sysimage/root/anaconda-post.log --erroronfail
/bin/cat << EOF >/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
HWADDR=`ifconfig eth0 |grep HWaddr |awk '{print $5}'`
ONBOOT=yes
NETMASK=`ifconfig eth0|sed -e 's/^.*Mask:\([^ ]*\)$/\1/p' -e d`
IPADDR=`ifconfig eth0|sed -e 's/^.*inet addr:\([^ ]*\).*$/\1/p' -e d`
GATEWAY=`netstat -rn|grep eth0|awk '{print $2}' | grep -v 0.0.0.0`
TYPE=Ethernet
EOF
echo "nameserver 192.168.0.1" > /etc/resolv.conf

#disable ipv6
sed -i '/NETWORKING_IPV6=/s/yes/no/' /etc/sysconfig/network
echo "alias net-pf-10 off" >> /etc/modprobe.conf
echo "alias ipv6 off" >> /etc/modprobe.conf
/sbin/chkconfig --level 35 ip6tables off

### Setting to ssh
/usr/bin/patch /etc/ssh/sshd_config << EOF
13c13
< #Port 22
---
> Port 30000
39a40
> PermitRootLogin no
60c61
< PasswordAuthentication yes
---
> PasswordAuthentication no
110c111
< #UseDNS yes
---
> UseDNS no
EOF
/sbin/restorecon /etc/ssh/sshd_config

/bin/mkdir ~test/.ssh
/bin/chmod 700 ~test/.ssh
/bin/cat << EOF > ~test/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA3NhtQhIrjeJy5WIlohdTn9SooaTsxpSiBljhx189b08DyC3xALH6R87RvX7+6jwJF/TqzwhZ495HTDfl7en98Dp97Xi8tYibPm9E3QBil9PGG+952flgVxTZZvS+D/rJ/7vuIp8iAiO9u3JUsZWP7X0dIaNhrnVfL31juk8I/EippzzMkjzUeRg62UizCxVD7AynwnqAsPdr6kcv9MGAZOvg7lDMv2Orbgsl0nakwtt+jmtQPGziLW+nQFVlHXTekmiJ2bDfURlRDWaegOftD+qGN3pIjO1TFCxgZccagYAVg1Jm+mFHIMIlmwpTcmBXO9DjjAhj6BXVbBNArqU5lQ== test@test.com
EOF
/bin/chmod 644 ~test/.ssh/authorized_keys
/bin/chown -R test:test ~test/.ssh/

### disable not wheel group user use su chang to root
sed -i '6s/^#auth/auth/g' /etc/pam.d/su

### Setting SNMP
/bin/cat << EOF > /etc/snmp/snmpd.conf
com2sec localhost localhost COMMUNITY_STRING
com2sec localnet 192.168.0.0/24 COMMUNITY_STRING

group test v1 localhost
group test v2c localhost
group test v1 localnet
group test v2c localnet

view all included .1 80

access test "" any noauth exact all none none
access test "" any noauth exact all all none

disk / 100000
EOF
/sbin/restorecon /etc/snmp/snmpd.conf

### disable CTRL-ALT-DELETE
/usr/bin/patch /etc/inittab << EOF
32c32,33
< ca::ctrlaltdel:/sbin/shutdown -t3 -r now
---
> #ca::ctrlaltdel:/sbin/shutdown -t3 -r now
> ca::ctrlaltdel:ca::ctrlaltdel:/usr/bin/logger 'CTRL-ALT-DELETE trap is disabled'
EOF
/sbin/restorecon /etc/inittab

### vim
/bin/sed -i "8 s/^/alias vi='vim'/" /root/.bashrc
/bin/echo 'syntax on' > /root/.vimrc

### ntp time set
/bin/echo '15 3 * * * /usr/sbin/ntpdate 192.168.0.4 > /dev/null 2>&1' >> /var/spool/cron/root

### Setting nagios monitor
/bin/cat << EOF > /etc/nagios/nrpe.cfg
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666
#If you want monitor to oracle please change nrpe_uset and nrpe_group to oracle user an oracle group
nrpe_user=nrpe
nrpe_group=nrpe
allowed_hosts=192.168.0.4
dont_blame_nrpe=0
debug=0
command_timeout=60
connection_timeout=300
command[check_ping]=/usr/lib64/nagios/plugins/check_ping -H 172.16.10.13 -w 500,10% -c 1000,25%
command[check_load]=/usr/lib64/nagios/plugins/check_load -w 9.0,7.0,6.0 -c 10.0,8.0,7.0
command[check_disk_root]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /
command[check_disk_var]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /var
command[check_disk_tmp]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /tmp
command[check_swap]=/usr/lib64/nagios/plugins/check_swap.pl -w 50% -c 10%
command[check_zombie_procs]=/usr/lib64/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib64/nagios/plugins/check_procs -w 150 -c 200 -s RSZDT
command[check_users]=/usr/lib64/nagios/plugins/check_users -w 3 -c 5
command[check_ssh]=/usr/lib64/nagios/plugins/check_ssh -H 127.0.0.1 -p 65508
command[check_ntp]=/usr/lib64/nagios/plugins/check_ntp -H 172.16.10.13 -w 1 -c 2
#Check Oracle Start
#command[check_oracle_tns]=/usr/lib64/nagios/plugins/check_oracle --tns oracle_tns_name
#command[check_oracle_db]=/usr/lib64/nagios/plugins/check_oracle --db oracle_sid
#command[check_oracle_login]=/usr/lib64/nagios/plugins/check_oracle --login oracle_tns_name
#command[check_oracle_cache]=/usr/lib64/nagios/plugins/check_oracle --cache oracle_tns_name DB_username DB_passwd 80 90
#command[check_oracle_tablespace]=/usr/lib64/nagios/plugins/check_oracle --tablespace oracle_tns_name DB_username DB_passwd tab 90 80
#Check Oracle End
#command[check_http]=/usr/lib64/nagios/plugins/check_http -H 127.0.0.1
#command[check_dig]=/usr/lib64/nagios/plugins/check_dig -H 127.0.0.1 -l zhliji2.blogspot.com
#command[check_smtp]=/usr/lib64/nagios/plugins/check_smtp -H 127.0.0.1
#command[check_pop]=/usr/lib64/nagios/plugins/check_pop -H 127.0.0.1
#command[check_imap]=/usr/lib64/nagios/plugins/check_imap -H 127.0.0.1 -p 143
#command[check_clamd]=/usr/lib64/nagios/plugins/check_clamd -H /tmp/clamd.socket
#command[check_amavisd]=/usr/lib64/nagios/plugins/check_tcp -H 127.0.0.1 -p 10024
#command[check_spamd]=/usr/lib64/nagios/plugins/check_tcp -H 127.0.0.1 -p 783
#command[check_mysql]=/usr/lib64/nagios/plugins/check_mysql -H 127.0.0.1 -u root -p mysql root's password
EOF
/sbin/chkconfig nrpe on

###Setting IPtables
cat << EOF > /etc/sysconfig/iptables
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1:140]
:ping - [0:0]
-A INPUT -p icmp -m icmp --icmp-type 8 -m state --state NEW -j ping
-A INPUT -i lo -j ACCEPT
-A INPUT -s 192.168.0.0/255.255.255.255.0 -i eth0 -j ACCEPT
-A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 0 -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 3 -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 3/4 -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 4 -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 11 -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 12 -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 14 -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 16 -j ACCEPT
-A INPUT -i eth0 -p icmp -m icmp --icmp-type 18 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 30000 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -j DROP
-A INPUT -i eth0 -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
-A INPUT -i eth0 -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,ACK,URG -j DROP
-A INPUT -i eth0 -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
-A INPUT -i eth0 -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP
-A INPUT -i eth0 -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
-A ping -p icmp -m icmp --icmp-type 8 -m limit --limit 1/sec -j RETURN
-A ping -p icmp -j REJECT --reject-with icmp-port-unreachable
COMMIT
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT
EOF

###disable serverices
for i in `ls /etc/rc3.d/S*`
do
CURSRV=`echo $i|cut -c 15-`
case $CURSRV in
crond | irqbalance | microcode_ctl | network | random | iptables |sshd | syslog | snmpd | nagios | nrpe )
;;
*)
chkconfig --level 235 $CURSRV off
;;
esac
done

说明:上面的脚本比较的长,它不仅完成了系统安装,还完成了一些共通的初始化配置。
下面简单介绍一下里面的几个比较重要的参数
repo --name=epel --baseurl=http://download.fedora.redhat.com/pub/epel/5/x86_64/
为方便安装时能配置好nagios监控我在安装时添加了一个额外的安装源epel,进行安装安装nrpe。如果你没有网络可以不设定这个选项。
rootpw --iscrypted $1$Sl6MDA1O$Z2HRda3FpCmaSRqrmD.gD0
设定root的秘密为:123456
user --name test --homedir /home/test --iscrypted --password=$1$IXb255iK$wZq/pCb2o70P1F8pXnI6P/ --shell=/bin/bash --groups=test,wheel
建立一个普通用户,用户名为test,组为test和wheel,密码为123456。
clearpart --all --drives=sda
清除第一块磁盘上的所有数据,它下面的参数是分区参数
分区采用普通分区加lvm两者方式混合分区
其中/boot分区为150M,swap分区为2G
根分区,var,tmp分区分别为20G,剩余的磁盘空间全部给data分区

%packages部分为要安装的软件
其中@开头的部分为软件组,-开头的为不要安装的软件包,什么都不加就代表要安装的单独软件包

%post --log=/mnt/sysimage/root/anaconda-post.log --erroronfail
这一部分是安装软件后的配置,所有配置失败的部分都进行log记录,log文件存放在/root/anaconda-post.log
/bin/cat << EOF >/etc/sysconfig/network-scripts/ifcfg-eth0
这一部分为设置它的网卡,由于最初设置的为dhcp方式分配的方式,现实中我们使用的服务器一般为固定IP
通过这几个脚本,自动把dhcp设定的IP地址转为固定IP地址

### Setting to ssh
这一部分为设定ssh,为安全起见,通过脚本更改ssh的默认端口为30000,禁止root用户远程登录,同时设定登录方式为采用密钥的方式,最后一部分是为test用户设定一个密钥,这个密钥我们平时可以通过puttygen的工具,生成的密钥,把公钥的内容粘贴这就行了。

### disable not wheel group user use su chang to root
禁止非wheel组的用户通过su命令切换到root用户

### Setting SNMP
这一部分时建立设定snmp的配置文件,我们通常会用监控软件对服务器进行监控,比如说,
通过cacti等类似的软件对服务器进行流量监控,就需要snmp服务。为方便我在这也加上了

### disable CTRL-ALT-DELETE
这一部分是禁止按下CTRL-ALT-DELETE组合键后服务器重启

### vim
这一部分是方便用vi的一些参数

### ntp time set
这一部分设定服务器的时间同步,每天3:15分跟时间服务器192.168.0.4进行时间同步

### Setting nagios monitor
这一部分时设定服务器监控,脚本比较的全,包括大部分常见的应用服务。

###Setting IPtables
这一部分是设定防火墙,只对外开放ssh端口,同时加了一些网络工具参数

### disable serverices
去掉不必要的服务,只保留必须的服务。

把centos的镜像文件放置到启动服务器的/home/centos目录下,把待安装服务器设定从网卡启动,就可以完成安装。

0 评论: