在实际工作中,一台服务器安装完系统后还需要做完很多初始化的工作才能正式交付。包括但不限于:

1、安装常用软件包便于工作,如gcc、cmake等

2、关闭不必开启的服务来节约资源,如关闭IPv6、SELINUX

3、优化系统参数,如修改TIME_WAIT值

为了省去重复性操作,可以把这一系列的操作写成一个通用脚本,脚本内容大致如下(参数均为举例,根据实际需求修改):

001#!/bin/bash
002 
003# get OS verison
004 
005RELEASEVER=$(rpm -q --qf "%{Version}"  $(rpm -q --whatprovides readhat-release) )
006 
007 
008#configure yum
009 
010if [ $RELEASEVER == 6 ];then
011 
012    wget http://mirrors.163.com/.help/CentOS6-Base.repo
013 
014fi
015 
016if [ $RELEASEVER == 7 ];then
017 
018    wget http://mirrors.163.com/.help/CentOS7-Base.repo
019fi
020 
021yum clean all
022 
023yum makecache
024 
025 
026#install base rpm package
027 
028yum -y install vim iftop iotop htop ntpdate
029 
030 
031#update rpm package and kernel
032 
033yum -y update
034 
035 
036#ulimit
037 
038> /etc/security/limits.conf
039 
040cat >> /etc/security/limits.conf <<EOF
041 
042* soft nproc 65535
043 
044* hard nproc 65535  #最大进程数
045 
046* soft nofile 65535
047 
048* hard nofile 65535  #最大文件打开数
049 
050EOF
051 
052 
053#time zone
054 
055[ -f /etc/localtime ] && rm -rf /etc/localtime
056 
057ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
058 
059 
060#update time
061 
062if [ $RELEASEVER == 6 ];then
063 
064    /usr/bin/ntpdate pool.ntp.org
065 
066    grep -q ntpdate /var/spool/cron/root
067 
068    if [ $? -ne 0 ];then
069 
070    
071 
072#iptables
073 
074if [ $RELEASEVER == 6 ];then
075 
076    /sbin/iptables -F
077 
078    service iptables save
079 
080    chkconfig iptables off
081 
082fi
083 
084 
085if [ $RELEASEVER == 7 ];then
086    systemctl disable firewalld
087 
088fi
089 
090 
091#SELINUX
092 
093setenforce 0
094 
095sed -i  's/SELINUX=enabled/SELINUX=disabled/'  /etc/selinux/config
096 
097 
098#DNS
099 
100> /etc/resolv.conf
101 
102cat >> /etc/resolv.conf <<EOF
103 
104nameserver 114.114.114.114
105 
106nameserver 8.8.8.8
107 
108EOF
109 
110 
111#sysctl
112 
113cat >> /etc/sysctl.conf << EOF
114 
115net.ipv4.tcp_tw_reuse=1
116 
117net.ipv4.tcp_recycle=0
118 
119EOF
120 
121sysctl -p

posted on 2019-08-16 16:16  天心人  阅读(622)  评论(0编辑  收藏  举报