第一章 DHCP服務安裝
1. 環境檢查
[root@m01 ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[root@m01 ~]# uname -r
2.6.32-642.el6.x86_64
[root@m01 ~]# getenforce
Disabled
[root@m01 ~]# /etc/init.d/iptables stop
[root@m01 ~]# /etc/init.d/iptables status
iptables: Firewall is not running.
[root@m01 ~]# hostname -I
10.0.0.61 172.16.1.61
2. 檢查dhcp服務是否安裝
DHCP(Dynamic Host Configuration Protocol,動態主機配置協議)通常被應用在大型的局域網絡環境中,主要作用是集中的管理、分配IP地址,使網絡環境中的主機動態的獲得IP地址、網關地址、DNS服務器地址等信息,並能夠提升地址的使用率。
rpm -qa |grep dhcp
yum -y install dhcp
2. 配置DHCP動態分配ip地址網段
cat>/etc/dhcp/dhcpd.conf<<EOF
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";
}
EOF
3. 查看DHCP服務端口號
[root@m01 ~]# /etc/init.d/dhcpd restart
Starting dhcpd:
[root@m01 ~]# netstat -tunlp|grep dhcp
udp 0 0 0.0.0.0:67 0.0.0.0:* 2083dhcpd
第二章 TFTP服務安裝
1. 安裝tftp服務(簡單的文件傳輸協議)
[root@m01 ~]# yum -y install tftp-server
2. 讓xinetd管理其啓動
sed -i '14s#yes#no#' /etc/xinetd.d/tftp
4. tftp端口號69
[root@m01 ~]# netstat -tlunp| grep 69
udp 0 0 0.0.0.0:69 0.0.0.0:* 2133/xinetd
第三章 APACHE服務安裝
1. 使用yum下載
[root@m01 ~]# yum install httpd -y
2. 啓動httpd服務 是否有80端口占用kill殺掉
[root@m01 ~]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.16.1.61 for ServerName
[ OK ]
[root@m01 ~]# sed -i "277i ServerName 127.0.0.1:80" /etc/httpd/conf/httpd.conf
[root@m01 ~]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
第四章 掛載鏡像文件
1. 創建系統站點目錄
[root@m01 ~]# mkdir /var/www/html/CentOS-6.8
- 掛載系統文件到站點目錄
[root@m01 ~]# mount /dev/cdrom /var/www/html/CentOS-6.8
[root@CentOS7-111 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 48G 2.5G 46G 6% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sr0 4.1G 4.1G 0 100% /var/www/html/CentOS-6.8
- 登錄查看網頁
http://10.0.0.61/CentOS-6.8/
- 或curl查看
curl http://10.0.0.61/CentOS-6.8/
第五章 配置支持PXE的啓動程序
syslinux是一個功能強大的引導加載程序,而且兼容各種介質。SYSLINUX是一個小型的Linux操作系統,它的目的是簡化首次安裝Linux的時間,並建立修護或其它特殊用途的啓動盤。如果沒有找到pxelinux.0這個文件,可以安裝一下。
- 安裝syslinux文件
[root@m01 ~]# yum -y install syslinux
- 複製文件到/var/lib/tftpboot目錄下面
[root@m01 ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
- 複製一些相關的啓動文件
[root@m01 ~]# cp -a /var/www/html/CentOS-6.8/isolinux/* /var/lib/tftpboot/
- 創建pxelinux.cfg啓動目錄
[root@m01 ~]# mkdir -p /var/lib/tftpboot/pxelinux.cfg
- 複製啓動文件到啓動目錄
[root@m01 ~]# cp /var/www/html/CentOS-6.8/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
[root@m01 ~]# ls /var/lib/tftpboot/
boot.cat grub.conf isolinux.bin memtest pxelinux.cfg TRANS.TBL vmlinuz
boot.msg initrd.img isolinux.cfg **pxelinux.0** splash.jpg vesamenu.c32
第六章 編寫KS文件
/var/lib/tftpboot/pxelinux.cfg/default
[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 -
創建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配置文件。
查看anaconda-ks.cfg
# Kickstart file automatically generated by anaconda.
#version=DEVEL
install
cdrom
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto static --ip 10.0.0.6 --netmask 255.255.255.0 --gateway 10.0.0.2 --noipv6 --nameserver 10.0.0.2 --hostname centos67
rootpw --iscrypted $6$2Frfm3Sc/oWdF2Yb$GTS8VbsaeLzAfT46EYJWezKA7VMOnefDAH6anyb12Hu5K1qu1awlpTwBxTjAzXpV/.l983Irdwxo5Rks0QV1p1
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
timezone --utc Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
#clearpart --none
#part /boot --fstype=ext4 --asprimary --size=200
#part swap --asprimary --size=1024
#part / --fstype=ext4 --grow --asprimary --size=200
repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100
%packages
@base
@compat-libraries
@core
@debugging
@development
@server-policy
@workstation-policy
python-dmidecode
sgpio
device-mapper-persistent-data
systemtap-client
ks.cfg詳解
[root@m01 ks_config]# cat CentOS-6.8-ks.cfg
# Kickstart Configurator for CentOS 6.7 by yao zhang
install
url --url="http://172.16.1.61/CentOS-6.8/"
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
network --bootproto=dhcp --device=eth1 --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 ks_config]# cat optimization.sh
#!/bin/bash
. /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
function Msg(){
if [ $? -eq 0 ];then
action "$1" /bin/true
else
action "$1" /bin/false
fi
}
# Defined IP function
function ConfigIP(){
Suffix=`ifconfig eth1|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"
cat >/etc/sysconfig/network-scripts/ifcfg-eth1 <<-END
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=172.16.1.$Suffix
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth1"
END
Msg "config eth1"
}
# 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 add Ordinary users Functions
function AddUser(){
useradd oldboy &>/dev/null &&\
echo "123456"|passwd --stdin oldboy &>/dev/null &&\
sed -i '98a oldboy ALL=(ALL) NOPASSWD:ALL' /etc/sudoers &&\
visudo -c &>/dev/null
Msg "AddUser oldboy"
}
# 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 SSHD config Functions
function sshd(){
SshdDir=/etc/ssh
[ -f "$SshdDir/sshd_config" ] && /bin/mv $SshdDir/sshd_config{,.ori}
wget -O $SshdDir/sshd_config http://$Ip:$Port/$ConfigDir/sshd_config &>/dev/null &&\
chmod 600 $SshdDir/sshd_config
Msg "sshd config"
}
# 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 hosts file Functions
function hosts(){
HostsDir=/etc
[ -f "$HostsDir/hosts" ] && /bin/mv $HostsDir/hosts{,.ori}
wget -O $HostsDir/hosts http://$Ip:$Port/$ConfigDir/hosts &>/dev/null
Msg "Hosts 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"`
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 ntp1.aliyun.com &>/dev/null' >>/var/spool/cron/root
Msg "Time Synchronization"
}
# Defined main Functions
function main(){
ConfigIP
yum
AddUser
HideVersion
sshd
openfiles
kernel
hosts
boot
Time
}
main