导航

centos7andcentos6初始化脚本

Posted on 2019-12-24 09:19  狐狸先生~  阅读(216)  评论(0)    收藏  举报
  1 #!/bin/bash
  2 
  3 # get OS verison
  4 
  5 RELEASEVER=$(rpm -q --qf "%{Version}"  $(rpm -q --whatprovides readhat-release) )
  6 
  7 #configure yum
  8 
  9 if [ $RELEASEVER == 6 ];then
 10 
 11     wget http://mirrors.163.com/.help/CentOS6-Base.repo
 12 
 13 fi
 14 
 15 if [ $RELEASEVER == 7 ];then
 16 
 17     wget http://mirrors.163.com/.help/CentOS7-Base.repo
 18 
 19 fi
 20 
 21 yum clean all
 22 
 23 yum makecache
 24 
 25 #install base rpm package
 26 
 27 yum -y install vim iftop iotop htop ntpdate
 28 
 29 #update rpm package and kernel
 30 
 31 yum -y update
 32  
 33 
 34 #ulimit
 35 
 36 > /etc/security/limits.conf
 37 
 38 cat >> /etc/security/limits.conf <<EOF
 39 
 40 * soft nproc 65535
 41 
 42 * hard nproc 65535  #最大进程数
 43 
 44 * soft nofile 65535
 45 
 46 * hard nofile 65535  #最大文件打开数
 47 
 48 EOF
 49 
 50 #time zone
 51 
 52 [ -f /etc/localtime ] && rm -rf /etc/localtime
 53 
 54 ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 55 
 56 #update time
 57 
 58 if [ $RELEASEVER == 6 ];then
 59 
 60     /usr/bin/ntpdate pool.ntp.org
 61 
 62     grep -q ntpdate /var/spool/cron/root
 63 
 64     if [ $? -ne 0 ];then
 65 
 66 #iptables
 67 
 68 if [ $RELEASEVER == 6 ];then
 69 
 70     /sbin/iptables -F
 71 
 72     service iptables save
 73 
 74     chkconfig iptables off
 75 
 76 fi
 77 
 78 if [ $RELEASEVER == 7 ];then
 79 
 80     systemctl disable firewalld
 81 
 82 fi
 83 
 84 
 85 #SELINUX
 86 setenforce 0
 87 sed -i  's/SELINUX=enabled/SELINUX=disabled/'  /etc/selinux/config
 88 
 89 #DNS
 90 > /etc/resolv.conf
 91 cat >> /etc/resolv.conf <<EOF
 92 nameserver 114.114.114.114
 93 nameserver 8.8.8.8
 94 EOF
 95 #sysctl
 96 cat >> /etc/sysctl.conf << EOF
 97 net.ipv4.tcp_tw_reuse=1
 98 net.ipv4.tcp_recycle=0
 99 EOF
100 sysctl -p

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

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

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

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

为了省去重复性操作,可以把这一系列的操作写成一个通用脚本,脚本内容大致如上

PS:

需要注意:直接粘贴复制到linux机器的脚本需要如下操作

安装:

yum install -y  dos2unix

转换

dos2unix **.sh

实际情况中代码中的一些参数须自己更改,如有疑问可私信或私信加QQ