【Linux Shell脚本编程】系统初始化脚本


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

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
012wget http://mirrors.163.com/.help/CentOS6-Base.repo
013
014fi
015
016if[ $RELEASEVER == 7 ];then
017
018wget 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 -yinstallvim 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
066grep-q ntpdate /var/spool/cron/root
067
068if[ $? -ne0 ];then
069
070
071
072#iptables
073
074if[ $RELEASEVER == 6 ];then
075
076/sbin/iptables -F
077
078service iptables save
079
080chkconfig iptables off
081
082fi
083
084
085if[ $RELEASEVER == 7 ];then
086systemctl 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
    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn