前言:

  因公司业务增加,陆续新增服务器,时不时的来几台,手动地一台台对服务器初始化操作感觉太麻烦。

  于是乎,根据初始化需求整合了一个初始化脚本,实现批量脚本初始化操作。

说明:

  本脚本根据自身需求编写而成,集成了Centos7服务器的基本初始化步骤。

  其中包含如下基础优化内容:

  1)SELinux关闭;

  2)Firewalld关闭;

  3)Bash环境修改;

  4)Openfile系统最大打开文件数配置;

  5)系统内核参数优化配置;

  6)Hostname主机名修改;

  7)History历史记录配置;

  8)个性化配置等。

注意:

  A)脚本执行完后将自动重启服务器;

  B)执行脚本前应在/etc/hosts中配置好对应的解析,如 10.10.10.10 kazihuo 内容添加到hosts文件中,执行完脚本后,服务器10.10.10.10将自动将Hostname主机名配置成 “kazihuo” ;

  C)确保存在 /tmp/sysctl.conf 文件,即将已配置好的Kernel内核优化参数文件放置 /tmp 目录下,执行完脚本后,其优化参数将自动配置到服务器中;如无优化文件,即在最后的函数中注释137行 Kernel 即可;

内容:

  脚本内容如下:

[root@kazihuo ~]# cat init.sh

 #!/bin/bash
#====================================================
# Author: kazihuo
# Blog: https://www.cnblogs.com/kazihuo
# Create Date: --
# Description: It works for system initalization.
#==================================================== #State:Plese confirm the files of /etc/hosts and /tmp/sysctl.conf before using the script [ -f /etc/init.d/functions ] && source /etc/init.d/functions # Defined result function
function Msg(){
if [ $? -eq ];then
action "$1" /bin/true
else
action "$1" /bin/false
fi
} # Defined close selinux function
function Selinux(){
[ -f /etc/selinux/config ] && {
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce
Msg "Close selinux"
}
} # Defined close firewalld function
function Firewalld(){
systemctl stop firewalld.service
systemctl disable firewalld.service >/dev/null >&
Msg "Close firewalld"
} # Defined bashrc function
function Bashrc(){
sed -i 's/\\h \\W/\\h \\w/g' /etc/bashrc
Msg "Bashrc"
} # Defined open files function for Centos6.
function Openfile6(){
if [ `egrep "^\*" /etc/security/limits.conf|wc -l` -eq ];then
echo '* - nofile 65535' >> /etc/security/limits.conf
ulimit -SHn
Msg "Open files"
fi
} # Defined open files function for Centos7.
function Openfile7(){
if [ `egrep "^De" /etc/systemd/system.conf|wc -l` -eq ];then
echo 'DefaultLimitCORE=infinity' >> /etc/systemd/system.conf
echo 'DefaultLimitNOFILE=100000' >> /etc/systemd/system.conf
echo 'DefaultLimitNPROC=100000' >> /etc/systemd/system.conf
ulimit -SHn
Msg "Open files"
fi
} # Defined kernel paramters function
function Kernel(){
if [ -f /tmp/sysctl.conf ];then
/usr/bin/\cp /etc/sysctl.conf /etc/sysctl.conf.$RANDOM
/usr/bin/\cp /tmp/sysctl.conf /etc/
sysctl -p >/dev/null >&
Msg "kernel paramters"
else
echo "/tmp/sysctl.conf is not exist"
fi
} # Defined hostname function
function Hostname(){
ip=`/usr/sbin/ip addr|grep brd|awk 'NR==3{print $2}'|awk -F "/" '{print $1}'`
name=`grep -w "$ip" /etc/hosts|awk '{print $2}'`
if [ -z $name ];then
sleep
else
echo $name > /etc/hostname
hostnamectl set-hostname $name
Msg "Hostname"
fi
} # Defined device function
function Device(){
/usr/sbin/ip addr|grep eth0 >/dev/null
RETVAL=$?
if [ $RETVAL -ne ];then
/usr/bin/mv /etc/sysconfig/network-scripts/ifcfg-e* /etc/sysconfig/network-scripts/ifcfg-eth0 >/dev/null >&
sed -i 's/quiet/quiet net.ifnames=0 biosdevname=0/g' /etc/default/grub
sed -i 's/^DEVICE/#DEVICE/g' /etc/sysconfig/network-scripts/ifcfg-e*
sed -i '1i DEVICE=eth0' /etc/sysconfig/network-scripts/ifcfg-e*
/usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg >/dev/null >&
Msg "Device--[WARNING]effecting after reboot~~~"
else
echo "the name of eths is exist"
fi
} # History collect
function History(){
cat >>/etc/profile.d/history.sh <<EOF
#history
USER=\`whoami\`
USER_IP=\`who -u am i >/dev/null|egrep -o "([0-9]{1,3}\\.){3}[0-9]{1,3}"\`
if [ "\$USER_IP" = "" ]; then
USER_IP=\`hostname\`
fi
if [ ! -d /var/log/history ]; then
mkdir /var/log/history
chmod /var/log/history
fi
if [ ! -d /var/log/history/\${LOGNAME} ]; then
mkdir /var/log/history/\${LOGNAME}
chmod /var/log/history/\${LOGNAME}
fi
export HISTSIZE=
DT=\`date +"%Y%m%d_%H:%M:%S"\`
export HISTFILE="/var/log/history/\${LOGNAME}/\${USER}@\${USER_IP}_\$DT"
chmod /var/log/history/\${LOGNAME}/*history* 2>/dev/null
EOF
Msg "History collect"
} # Defined the hobby.
function Hobby(){
mkdir -p /{luomurui,luomurui-bak}/{scr,pkg,test,info}
} # Defined wait function
function Wait(){
echo ""
echo -n -e "\033[31mTHE SYSTEM IS REBOOTING\033[0m"
for ((i=0;i<3;i++))
do
echo -n "~~ "
sleep 1
done
echo
} # Defined main function
function main(){
Selinux
Firewalld
Bashrc
#Openfile6
Openfile7
Kernel
Hostname
#Device
History
Hobby
Wait
reboot
}
main

  若有其他需求,可以其为基底进行个性化修改!

最新文章

  1. SQL 高效分页查询
  2. [VS2013]如何闪开安装VS2013必须要有安装IE10的限制
  3. linux ls和 ll 命令
  4. SimpleAdapter类使用方法
  5. [转]判断程序是否运行在 Windows x64 系统下
  6. 底部菜单栏(一) TabHost实现
  7. windows下安装php真正的多线程扩展pthreads教程
  8. 《Linear Algebra and Its Applications》-chaper4-向量空间-子空间、零空间、列空间
  9. mysql密码忘记如何处理
  10. word2vec 入门基础(一)
  11. FZU 2243 Daxia like uber
  12. Java线程安全性中的对象发布和逸出
  13. jQuery UI 入门之实用实例分享
  14. 201521123078 《Java程序设计》第11周学习总结
  15. CentOS6.8通过yum安装MySQL5.7
  16. Maven引入自定义jar包
  17. Tomb Raider
  18. c#批量更新list对象sql
  19. python应用:生成简单二维码
  20. 《DSP using MATLAB》Problem 2.17

热门文章

  1. [转帖]老狼:你知道哪些关于 Windows 10 的骚操作?
  2. Angular @的作用
  3. IntersectionObserver简介
  4. k8s 1.9 安装
  5. 【转】CNN卷积神经网络_ GoogLeNet 之 Inception(V1-V4)
  6. BZOJ2325[ZJOI2011]道馆之战——树链剖分+线段树
  7. BZOJ4012 HNOI2015开店(树链剖分+主席树)
  8. (转)二分图匹配匈牙利算法与KM算法
  9. day7 笔记
  10. 25 行 Python 代码实现人脸识别——OpenCV 技术教程