Centos7系统初始化脚本

时间:2022-11-05 10:46:37

前言:

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

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

说明:

  本脚本根据自身需求编写而成,集成了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

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

Centos7系统初始化脚本的更多相关文章

  1. centos7 系统初始化脚本

    现在自己的本地虚拟机系统,直接安装的是centos7.2 mini版,安装完成发现好多东西都没有安装,所以写了一个简单的系统初始化脚本,让自己可以省一些力气,哈哈 人懒主要是. 下面贴出写的脚本,脚本 ...

  2. 基于CentOS7系统添加自定义脚本服务及参数说明【转】

    概述 centos6如果要添加自定义脚本服务只需要把脚本放到/etc/init.d然后授权后用chkconfig添加后就可以管理了,那么centos7又是怎么添加自定义脚本服务呢? CentOS7添加 ...

  3. 简单的 centos7&amp&semi;rhel7 系统初始化脚本

    #!/bin/bash #描述: 基于RHEL7&centos7的初始化配置 #读取用户输入的ip read -p "输入你当前Linux的IP地址:" LAST #截取网 ...

  4. centos6、7系统初始化脚本

    #!/bin/bash # #******************************************************************** #encoding -*-utf ...

  5. centos系统初始化脚本

    #!/bin/bash #检测是否为root用户 ];then echo "Must be root can do this." exit fi #检测网络 echo " ...

  6. centos7系统初始化

    echo "# swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间" >> /etc/sysctl.conf echo -e "v ...

  7. LINUX 系统初始化脚本

    #!/bin/bash ######the system first start configuretion #####for install  ####copy right by donglei## ...

  8. Linux系统初始化脚本

    #查看centos的版本号 CentOS_version=`cut -d /etc/centos-release | cut -d` #改变PS3格式 PS3="Please enter t ...

  9. 给公司个别安装好的系统环境处理-相当half系统初始化脚本shell

    #!/bin/bash# Used for other system-environment update! echo -e '\n\033[35m~~请使用root权限运行此脚本~~\033[0m\ ...

随机推荐

  1. HandlerMapping的3种访问形式

    1.根据BeanName访问Controller 2.根据简单URL访问 3.根据Controller类名访问

  2. java中的传值与传引用

    java函数中的传值和传引用问题一直是个比较“邪门”的问题,其实java函数中的参数都是传递值的,所不同的是对于基本数据类型传递的是参数的一份拷贝,对于类类型传递的是该类参数的引用的拷贝,当在函数体中 ...

  3. Windows 2003 Server C盘空间被IIS日志文件消耗殆尽案例

    今天突然收到手头一台数据库服务器的磁盘空间告警邮件,C盘空间只剩下5.41GB大小(当系统磁盘剩余空间小于总大小的10%时,发出告警邮件),如下图所示: 由于还有一些微弱印象:前阵子这台服务器的C盘剩 ...

  4. MAC环境下 nodejs 4&period;5&period;0 卸载

    MAC 环境下,我之前的删除 node 环境如果是用brew 安装的 node.js 4.4 之前的版本.可以网上参考其他方法,我之前的帖子也有之前的删除方法.参考帖子: http://www.cnb ...

  5. Mysql时间戳函数

    1.转换为时间戳 select unix_timestamp('2013-07-15 10-06-07') 如果参数为空,则为当前时间 2.转换为时间 select from_unixtime(tim ...

  6. Windows7部署WordPress傻瓜式教程(IIS7&period;5&plus;MySQL&plus;PHP&plus;WordPress)

    http://www.cnblogs.com/vengen/archive/2010/01/01/WordPressInstall.html

  7. 批量删除ASP&period;NET中的缓存&lpar;cache&rpar;

    IDictionaryEnumerator em = HttpContext.Current.Cache.GetEnumerator(); while (em.MoveNext())        { ...

  8. Python读写文件你真的了解吗?

    内容概述 Python文件操作 针对大文件如何操作 为什么不能修改文件? 你需要知道的基本知识 1. Python文件操作 这一部分内容不是重点,因为很简单网上很多,主要看看文件操作的步骤就可以了. ...

  9. CentOS 7设置yum仅仅下载rpm不安装总结

    前言 参考文章:https://linux.cn/article-7937-1.html 在参考文章的基础上进行总结. 使用download-only插件实现 首先我们可以使用downloadonly ...

  10. Python&&num;160&semi;一键上传下载&amp&semi;一键提交文件到SVN入基线工具

    一键上传下载&一键提交文件到SVN入基线工具   by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用说明 1   注: 根据我司项目规则订制的一套工具,集成以下功能,源码 ...