Home | Mirror | Search

第 1 章 OS 安装与初始化

快速初始化Linux服务器

目录

1. 服务器快速安装与部署
1.1. Kickstart
1.2. 快速初始化
1.3. JDK and Tomcat
1.4. 批量安装
2. IP / Mac Address
2.1. IP Address
2.2. Mac Address
2.3. NIC
2.4. DNS
2.5. bonding
3. limits.conf
4. sysctl
5. profile
6. Crontab
7. NFS
8. 释放内存
9. TCP/IP
9.1. TCP 状态
9.2. TIME_WAIT
9.3. SYN_RECV / SYN_SENT
10. Linux 日常管理

1. 服务器快速安装与部署

1.1. Kickstart

使用 Kickstart 实现无人值守安装,制作一个Linux 安装光盘或ISO文件将ks文件放置到根目录。这样光盘放入光驱后无需人为干预,实现自动安装。

另外还可以采用无盘启动网络安装

anaconda-ks.cfg

# Kickstart file automatically generated by anaconda.

#version=RHEL6
install
cdrom
lang en_US.UTF-8
keyboard us
network --device eth0 --bootproto static --ip 172.16.3.81 --netmask 255.255.255.0 --gateway 172.16.3.254 --nameserver 8.8.8.8 --hostname www.example.com
rootpw  --iscrypted $6$Ze/iw9HmY5LVYHhN$Hil8L/e8r2EwzmpacjW7VnZu1Jx6V9ZE55oAAWNr52qnh82ZL9m9J340mSRQXB5fYI2/ahL09xSlZ2WbdeTe3.
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512 --enablefingerprint
selinux --enforcing
timezone --utc Asia/Harbin
bootloader --location=mbr --driveorder=sda,sdb --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 --drives=sda

#part / --fstype=ext4 --grow --size=200
#part swap --size=64000

#part /www --fstype=ext4 --grow --size=200



repo --name="CentOS"  --baseurl=file:///mnt/source --cost=100

%packages
@core
@server-policy
%end
			

1.2. 快速初始化

后面章节中提供很多脚本,直接复制下面命令,右键粘贴到终端中运用即可。

例如每个服务器都需要安装ntp,您只需要服务下面代码,粘贴到终端即可,它能实现自动安装,修改配置文件与启动服务等等

			
yum install ntp -y
vim /etc/ntp.conf <<VIM > /dev/null 2>&1
:17,17s/^/server 172.16.1.10\r/
:wq
VIM
service ntpd start
chkconfig ntpd on
			
			

你也可以吧这些脚本组合,制作一个你自己的脚本,然后批量执行。例如:

			
$ cat install.sh
#!/bin/bash

if [ ! -f /usr/bin/vim ] ; then
        alias vim='vi'
fi

if [ -z "$( egrep "CentOS|Redhat" /etc/issue)" ]; then
        echo 'Only for Redhat or CentOS'
        exit
fi

echo -ne "
search example.com
nameserver 172.16.3.51
nameserver 172.16.3.52
nameserver 208.67.222.222
nameserver 202.67.220.220
nameserver 8.8.8.8
nameserver 4.4.4.4
" > /etc/resolv.conf

echo -ne "

* soft nofile 65536
* hard nofile 65536
" >>/etc/security/limits.conf

cat >> /etc/sysctl.conf <<EOF

net.ipv4.ip_local_port_range = 1024 65500
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 60
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 4096
EOF

yum update -y
rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
rpm -K http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rpm -i http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm

# redhat 5.6 cp /etc/ntp.conf.original /etc/ntp.conf
yum install ntp -y
vi /etc/ntp.conf <<VIM > /dev/null 2>&1
:22,24s/^/#/
:25,25s/^/\rserver 172.16.3.51\rserver 172.16.3.52\r/
:wq
VIM
service ntpd start
chkconfig ntpd on

yum install net-snmp -y
vi /etc/snmp/snmpd.conf <<VIM > /dev/null 2>&1
:62,62s/systemview/all/
:85,85s/^#//
:wq
VIM
service snmpd start
chkconfig snmpd on

yum install -y nrpe nagios-plugins
vi /etc/nagios/nrpe.cfg <<VIM > /dev/null 2>&1
:%s/allowed_hosts=127.0.0.1/allowed_hosts=172.16.1.2/
:wq
VIM

cat >> /etc/nagios/nrpe.cfg <<EOF

#command[check_http]=/usr/lib64/nagios/plugins/check_http -I 127.0.0.1 -p 80 -u http://www.example.com/index.html
command[check_swap]=/usr/lib64/nagios/plugins/check_swap -w 20% -c 10%
command[check_all_disks]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -e
EOF

chkconfig nrpe on
service nrpe start

cat >> /etc/bashrc <<EOF

export HISTTIMEFORMAT="%Y-%m-%d-%H:%M:%S "
EOF
			
			

将上面脚本放到web server 上,然后使用rsh循环批量执行的

curl -s http://172.16.0.20/shell/install.sh | bash

1.3. JDK and Tomcat

			
#!/bin/bash
rsync -auzv root@172.16.3.23:/srv/* /srv/
ln -s /srv/java /usr/local/java
ln -s /srv/apache-tomcat /usr/local/
mkdir -p /www/logs/tomcat
echo -ne '
export JAVA_HOME=/srv/java
export CATALINA_HOME=/srv/apache-tomcat
export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CATALINA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$CATALINA_HOME/bin:
export JAVA_OPTS="-server -Xms512m -Xmx8192m  -XX:PermSize=64M -XX:MaxPermSize=512m"
' >> /etc/profile
			
			

1.4. 批量安装

检查日期

			
for ip in {23..32} {49,50} {81..92}; do ssh root@172.16.0.$ip date; done
			
			

时间更新

			
for ip in {23..32} {49,50} {81..92}; do ssh root@172.16.0.$ip ntpdate 172.16.0.1; done
			
			

安装NFS Server

			
for ip in {21..32} {41,50} {81..92}; do rsh root@172.16.0.$ip "yum install -y nfs-utils"; done

for host in {21..32} {41,50} {81..92}; do echo 172.16.0.$host && rsh root@172.16.0.$host "exportfs"; done

for ip in {21..32} {41,50} {81..92}; do rsync -auz ~/config/nfs/exports root@172.16.0.$ip:/etc/; rsh root@172.16.0.$ip "service nfs reload"; done
			
			
comments powered by Disqus