Centos7之Kickstart无人值守安装

Centos7之Kickstart无人值守安装

概述

红帽Kickstart安装方法主要用于(但不仅限于)红帽企业Linux操作系统自动执行无人值守的操作系统安装和配置。

kickstart安装遵循以下四个步骤:

  1. 机器通过CD / DVD,USB设备启动或通过网络使用PXE和网络协议,动态主机配置协议和简单文件传输协议。
  2. Kickstart文件是从引导媒体或网络(最常见)下载。
  3. Anaconda安装自动启动,并读取Kickstart文件安装源的位置。安装源可以驻留在引导媒体或网络。
  4. 访问安装源后,安装程序会尝试自动安装。如果缺少任何所需信息Kickstart文件,或者配置文件不正确,安装程序可能会提示用户的其他信息。

建立本地YUM源

下载DVD镜像

wget -c http://mirrors.163.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1511.iso

构建本地YUM源(没有连网情况下)

mount -t iso9660 -o loop CentOS-7-x86_64-DVD-1511.iso /mnt/

mv /etc/yum.repos.d/* /home/

~]# cat /etc/yum.repos.d/local.repo
[CentOS-Source]
name=CentOS DVD
baseurl=file:///mnt
enabled=1
gpgcheck=0

yum clean all
yum list

1.Kickstart原理

1).Kickstart三种核心服务:DHCP/TFTP/HTTP服务

2).PXE介绍:PXE是一个微型的操作系统,存于网卡上,全名是预启动执行环境

3).PXE工作过程:首先,PXE客户端上有一个网卡支持PXE程序,安装系统时它会向整个局域网里面发送一个UDP广播请求(只有DHCP服务会进行回应),然后找到DHCP服务(分配IP)进行沟通,DHCP提供信息(告诉PXE客户端tftp服务器在哪)接着会向TFTP服务请求下载启动文件,紧接着TFTP响应并传输文件到PXE客户端,此时PXE已有启动文件,开始启动操作系统,安装Centos操作系统,找到web服务请求下载自动应答文件(ks.cfg),最后在客户端安装操作系统

 

2.系统环境准备

[root@linux-node01 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@linux-node01 ~]# uname -r
3.10.0-327.el7.x86_64
[root@linux-node01 ~]# getenforce 
Disabled
[root@linux-node01 ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
 Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
 Active: inactive (dead)

May 04 07:06:33 test systemd[1]: Starting firewalld - dynamic firewall daemon...
May 04 07:06:40 test systemd[1]: Started firewalld - dynamic firewall daemon.
May 04 07:10:10 linux-node01.com systemd[1]: Stopping firewalld - dynamic firewall daemon...
May 04 07:10:12 linux-node01.com systemd[1]: Stopped firewalld - dynamic firewall daemon.
[root@linux-node01 ~]# ifconfig eth0|awk -F "[ :]+" 'NR==2 {print $3}'
192.168.56.32
[root@linux-node01 ~]# hostname
linux-node01.com

注意: 虚拟机网卡采用NAT模式,不要使用桥接模式,因为稍后我们会搭建DHCP服务器,在同一局域网多个DHCP服务会有冲突。 VMware的NAT模式的dhcp服务也关闭,避免干扰。

 

3.安装DHCP服务

1).DHCP介绍 DHCP:动态分配IP地址

2).DHCP服务安装部署

[root@linux-node01 ~]# yum install -y dhcp
[root@linux-node01 ~]# rpm -ql dhcp|grep "dhcpd.conf"
/etc/dhcp/dhcpd.conf           ##配置文件的位置
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
/usr/share/doc/dhcp-4.2.5/ldap/dhcpd-conf-to-ldap
/usr/share/man/man5/dhcpd.conf.5.gz
[root@linux-node01 ~]# vim /etc/dhcp/dhcpd.conf 
subnet 192.168.56.0 netmask 255.255.255.0 {
 range 192.168.56.100 192.168.56.200;
 option subnet-mask 255.255.255.0;
 default-lease-time 21600;
 max-lease-time 43200;
 next-server 192.168.56.32;
 filename "/pxelinux.0";
}
###解释说明###
range 192.168.56.100 192.168.56.200;         # 可分配的起始IP-结束IP
option subnet-mask 255.255.255.0;            # 设定netmask
default-lease-time 21600;                    # 设置默认的IP租用期限
max-lease-time 43200;                        # 设置最大的IP租用期限
next-server 192.168.56.32;                   # 告知客户端TFTP服务器的ip
filename "/pxelinux.0";                      # 告知客户端从TFTP根目录下载pxelinux.0文件

##启动dhcp服务
[root@linux-node01 ~]# systemctl start dhcpd.service 
##查看日志
[root@linux-node01 ~]# tailf /var/log/messages
May 4 08:44:57 linux-node01 systemd: Starting DHCPv4 Server Daemon...
May 4 08:44:57 linux-node01 dhcpd: Internet Systems Consortium DHCP Server 4.2.5
May 4 08:44:57 linux-node01 dhcpd: Copyright 2004-2013 Internet Systems Consortium.
May 4 08:44:57 linux-node01 dhcpd: All rights reserved.
May 4 08:44:57 linux-node01 dhcpd: For info, please visit https://www.isc.org/software/dhcp/
May 4 08:44:57 linux-node01 dhcpd: Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file
May 4 08:44:57 linux-node01 dhcpd: Wrote 0 leases to leases file.
May 4 08:44:57 linux-node01 dhcpd: Listening on LPF/eth0/00:0c:29:c9:3c:37/192.168.56.0/24
May 4 08:44:57 linux-node01 dhcpd: Sending on LPF/eth0/00:0c:29:c9:3c:37/192.168.56.0/24
May 4 08:44:57 linux-node01 dhcpd: Sending on Socket/fallback/fallback-net
May 4 08:44:57 linux-node01 systemd: Started DHCPv4 Server Daemon.

4.配置TFTP和HTTP服务

1).TFTP简介

TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,端口号为69。

2).TFTP安装部署

[root@linux-node01 ~]# yum -y install tftp-server
[root@linux-node01 ~]# vim /etc/xinetd.d/tftp

 

[root@linux-node01 ~]# sed -i '14s#yes#no#' /etc/xinetd.d/tftp
[root@linux-node01 ~]# systemctl restart tftp.service
[root@linux-node01 ~]# netstat -tunlp|grep 69
udp6         0        0 :::69                   :::*                       1/systemd

3).配置HTTP服务

可以用Apache或Nginx提供HTTP服务。

[root@linux-node01 ~]# sed -i "96i ServerName 127.0.0.1:80" /etc/httpd/conf/httpd.conf
[root@linux-node01 ~]# systemctl start httpd.service
[root@linux-node01 ~]# mkdir /var/www/html/CentOS-7.2   ##创建镜像站点目录

注意:

CD/DVD的设备状态一定要是已连接的状态;

 

[root@linux-node01 ~]# mount /dev/cdrom /var/www/html/CentOS-7.2/ ##镜像挂载到站点目录
mount: /dev/sr0 is write-protected, mounting read-only
[root@linux-node01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 28G 1.5G 27G 6% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 913M 0 913M 0% /dev/shm
tmpfs 913M 8.6M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda1 497M 125M 373M 26% /boot
tmpfs 183M 0 183M 0% /run/user/0
tmpfs 913M 0 913M 0% /tmp
/dev/sr0 4.1G 4.1G 0 100% /var/www/html/CentOS-7.2

浏览器访问http://192.168.56.32/CentOS-7.2/检验配置是否正确。

 

5. 配置支持PXE的启动程序

1).PXE引导配置(bootstrap)

syslinux是一个功能强大的引导加载程序,而且兼容各种介质。syslinux是一个小型的Linux操作系统它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。如果没有找到pxelinux.0这个文件,可以安装一下syslinux这个包。

[root@linux-node01 ~]# yum -y install syslinux
[root@linux-node01 ~]# ll /usr/share/syslinux/pxelinux.0
-rw-r--r-- 1 root root 26764 Nov 6 2016 /usr/share/syslinux/pxelinux.0
[root@linux-node01 ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/   ##/var/lib/tftpboot/是TFTP的根目录
# 复制启动菜单程序文件
[root@linux-node01 ~]# cp -a /var/www/html/CentOS-7.2/isolinux/* /var/lib/tftpboot/
[root@linux-node01 ~]# ls /var/lib/tftpboot/
boot.cat boot.msg grub.conf initrd.img isolinux.bin isolinux.cfg memtest pxelinux.0 splash.png TRANS.TBL upgrade.img vesamenu.c32 vmlinuz
# 新建一个pxelinux.cfg目录,存放客户端的配置文件。
[root@linux-node01 ~]# mkdir -p /var/lib/tftpboot/pxelinux.cfg
[root@linux-node01 ~]# cp /var/www/html/CentOS-7.2/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

2).PXE配置文件default解析

[root@linux-node01 ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32     # 默认加载一个菜单
timeout 600              # timeout时间是引导时等待用户手动选择的时间,设为1可直接引导,单位为1/10秒

display boot.msg         # 菜单背景图片、标题、颜色。

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.

menu tabmsg Press Tab for full configuration options on menu items.

menu separator # insert an empty line
menu separator # insert an empty line

# label指定在boot:提示符下输入的关键字,比如boot:linux[ENTER],这个会启动label linux下标记的kernel和initrd.img文件。
label linux  # 一个标签就是前面图片的一行选项。
 menu label ^Install CentOS 7
 kernel vmlinuz   # 指定要启动的内核。同样要注意路径,默认是/tftpboot目录。
 append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet # 指定追加给内核的参数,initrd.img是一个最小的linux系统

label check
 menu label Test this ^media & install CentOS 7
 menu default
 kernel vmlinuz
 append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
 menu title Troubleshooting

label vesa
 menu indent count 5
 menu label Install CentOS 7 in ^basic graphics mode
 text help
 Try this option out if you're having trouble installing
 CentOS 7.
 endtext
 kernel vmlinuz
 append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet

label rescue
 menu indent count 5
 menu label ^Rescue a CentOS system
 text help
 If the system will not boot, this lets you access files
 and edit config files to try to get it booting again.
 endtext
 kernel vmlinuz
 append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet

label memtest
 menu label Run a ^memory test
 text help
 If your system is having issues, a problem with your
 system's memory may be the cause. Use this utility to
 see if the memory is working correctly.
 endtext
 kernel memtest

menu separator # insert an empty line

label local
 menu label Boot from ^local drive
 localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
 menu label Return to ^main menu
 menu exit

menu end

6. 创建ks.cfg文件

1).使用kickstart,只需事先定义好一个Kickstart自动应答配置文件ks.cfg(通常存放在安装服务器上),并让安装程序知道该配置文件的位置,在安装过程中安装程序就可以自己从该文件中读取安装配置,这样就避免了在安装过程中多次的人机交互,从而实现无人值守的自动化安装。

2).生成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配置文件。

3).查看anaconda-ks.cfg

[root@linux-node01 ~]# cat anaconda-ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network --bootproto=static --device=eno16777736 --gateway=192.168.56.2 --ip=192.168.56.32 --nameserver=192.168.56.2 --netmask=255.255.255.0 --ipv6=auto --activate
network --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$X6zKsreK87qSW/fe$XrmDrp8LBHu8aduj5f3rQ1ekayb1.CUj0/6.DCbvp7kbbs5TMMJ1i0SWOsk2FtChH.I0gJBRY/t6qUodIaDqq/
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
user --name=xiejc --password=$6$xWQy6rKDmtbO5rDj$FnZ565jTGhhz0C.eVCaSWN05/.NxQi5a11hQIgrDYUvtafJ5ZJy4jUWIOOff5ved15lyV71qjQuGN.bvIVOLH0 --iscrypted --gecos="xiejc"
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
autopart --type=lvm
# Partition clearing information
clearpart --all --initlabel --drives=sda

%packages
@^minimal
@compat-libraries
@core
@debugging
@development
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

4). ks.cfg详解

ks.cfg文件组成大致分为3段

命令段

键盘类型,语言,安装方式等系统的配置,有必选项和可选项,如果缺少某项必选项,安装时会中断并提示用户选择此项的选项

软件包段

%packages
@groupname:指定安装的包组
package_name:指定安装的包
-package_name:指定不安装的包

在安装过程中默认安装的软件包,安装软件时会自动分析依赖关系。

脚本段(可选)

%pre:安装系统前执行的命令或脚本(由于只依赖于启动镜像,支持的命令很少)
%post:安装系统后执行的命令或脚本(基本支持所有命令)
关键字 含义
install 告知安装程序,这是一次全新安装,而不是升级upgrade
url --url=" " 通过FTPHTTP从远程服务器上的安装树中安装。
url --url="http://192.168.56.32/CentOS-7.2/"
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]中的一种,缺省值是dhcpbootpdhcp被认为是相同的。
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,swapvfat
--asprimary,强迫把分区分配为主分区,否则提示分区失败。
--size=,以MB为单位的分区最小值.在此处指定一个整数值,如500.不要在数字后面加MB
--grow,告诉分区使用所有可用空间(若有),或使用设置的最大值。
firstboot 负责协助配置redhat一些重要的信息。
firstboot --disable
selinux 关闭selinuxselinux --disabled
firewall 关闭防火墙。firewall --disabled
logging 设置日志级别。logging --level=info
reboot 设定安装完成后重启,此选项必须存在,不然kickstart显示一条消息,并等待用户按任意键后才重新引导,也可以选择halt关机。

7.编写ks文件

使用kickstart生成的ks文件(yum install system-config-kickstart),或者使用/root/目录下自动生成文件anaconda-ks.cfg,做相应修改后就能用了

# 先生成一个密码备用
[root@linux-node01 ~]# perl -e 'print crypt("123456",q($1$password)),"\n"'
[root@linux-node01 ~]# mkdir /var/www/html/ks_config
[root@linux-node01 ~]# cat /var/www/html/ks_config/CentOS-7.2-ks.cfg
# Kickstart Configurator for CentOS 7.2 by John
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use network installation
url --url="http://192.168.56.32/CentOS-7.2/"
# Use graphical install #图形化安装
#graphical
# Use text mode install #使用文本模式安装
text
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information #虚拟机下网络接口是以eno开头的,物理服务器可以修改为eth*或em* #第二块网卡能接收到DHCP广播
network --bootproto=dhcp --device=eth0 --onboot=off --ipv6=auto
network --bootproto=dhcp --device=eth1 --ipv6=auto --activate
network --bootproto=dhcp --device=eth2 --onboot=off --ipv6=auto
network --bootproto=dhcp --device=eth3 --onboot=off --ipv6=auto
network --hostname=localhost.localdomain

# Reboot after installation #安装后重启
reboot
# Root password #安装后的root密码是redhat
rootpw --iscrypted $6$k21QAGt47YZeKcBh$62rXVmxIQBu9je81k0EcJ7xiTqushjZUAGSzOmy1UUObD6OHpZ67oN1XKl2TYZkBZzBz1vOetfFjdIvBplHmA0
# System services
services --disabled="chronyd"
# System timezone
timezone --utc Asia/Shanghai
# System bootloader configuration #使用内核参数net.ifnames=0,装完后网卡会是eth* #系统引导安装到sda
bootloader --append="net.ifnames=0 crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information #使用--all会删掉所有sda数据,用于初次安装或者不保留数据 #使用--list指定用于安装系统的分区,用于想保留其他分区数据的重装系统
clearpart --all --initlabel --drives=sda
#clearpart --initlabel --list=sda2,sda1

# Disk partitioning information #创建LVM分区
#part pv.260 --fstype="lvmpv" --ondisk=sda --size=308008
#part /boot --fstype="xfs" --ondisk=sda --size=953
#volgroup centos --pesize=4096 pv.260
#logvol swap --fstype="swap" --size=8000 --name=swap --vgname=centos
#logvol / --fstype="xfs" --size=300000 --name=root --vgname=centos

# Disk partitioning information #创建标准分区 #--grow --size=1 会使用剩余所有磁盘空间
part /boot --fstype="xfs" --ondisk=sda --size=100
part swap --fstype="swap" --ondisk=sda --size=800
part / --fstype="xfs" --ondisk=sda --size=3000
part / --fstype="xfs" --grow --ondisk=sda --size=1


%packages
@^minimal
@core
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

 

1). 整合编辑default配置文件

[root@linux-node01 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
default ks
prompt 0

label ks
 kernel vmlinuz
 append initrd=initrd.img ks=http://192.168.56.32/ks_config/CentOS-7.2-ks.cfg # 告诉安装程序ks.cfg文件在哪里

# append initrd=initrd.img ks=http://192.168.56.32/ks_config/CentOS-7.2-ks.cfg ksdevice=eth0
# ksdevice=eth0代表当客户端有多块网卡的时候,要实现自动化需要设置从eth1安装,不指定的话,安装的时候系统会让你选择,那就不叫全自动化了。

重启服务

systemctl restart httpd dhcpd tftp

2).开机优化脚本

[root@linux-node1 ~]# 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-05-05 23:46:08
# Description: Linux system initialization
##############################################################

. /etc/init.d/functions

Ip=192.168.56.32
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
function Msg(){
        if [ $? -eq 0 ];then
          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=192.168.56.$Suffix
        PREFIX=24
        GATEWAY=192.168.56.2
        DNS1=192.168.56.2
        DEFROUTE=yes
        IPV4_FAILURE_FATAL=yes
        IPV6INIT=no
        NAME="System eth0"
        END
        Msg "config eth0"
}

# Defined Yum source Functions
function yum(){
        YumDir=/etc/yum.repos.d
        [ -f "$YumDir/CentOS-Base.repo" ] && cp $YumDir/CentOS-Base.repo{,.ori} 
        wget -O $YumDir/CentOS-Base.repo http://$Ip:$Port/$ConfigDir/CentOS-Base.repo &>/dev/null &&\
        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
# rz上传CentOS-Base.repo、epel.repo、sysctl.conf

出现如下界面安装成功:

网卡配置文件

[root@localhost network-scripts]# vi ifcfg-eth0 
TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
DNS1="192.168.56.2"
IPADDR="192.168.56.100"
PREFIX="24"
GATEWAY="192.168.56.2"

 

 

 

 

0
如无特殊说明,文章均为本站原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!