centos虚拟机初始化脚本

功能

  1. 修改主机名为node.
  2. 网卡开机启动并设置ip为静态18.8
  3. 更新yum源为国内yum源,并安装vim lrzsz wget man ntpdate软件。
  4. 关闭iptables selinux,设置grub等待时间2秒,只开启crond network rsyslog sshd lvm2-monitor blk-availability udev-post服务。
  5. 服务器时间同步并加大文件描述符为65535.
  6. 最小化硬件,并关闭软盘。

脚本

#!/bin/bash
#
# hostname
if [ `hostname` != 'node' ]; then
    hostname node
    sed -i 's/HOSTNAME=.*/HOSTNAME=node/g' /etc/sysconfig/network
else 
   echo "hostname is node"
fi
# network
NET_CONF="/etc/sysconfig/network-scripts/ifcfg-eth0"
ifconfig eth0 | grep "UP BROADCAST RUNNING MULTICAST" &> /dev/null
if [ $? != 0 ]; then
    sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/g' $NET_CONF
    sed -i 's/ONBOOT=no/ONBOOT=yes/g' $NET_CONF
    sed -i '/NM_CONTROLLED=yes/d' $NET_CONF
    sed -i '/BOOTPROTO=static/a IPADDR=192.168.18.8
NETMASK=255.255.255.0
GATEWAY=192.168.18.2
DNS1=192.168.18.2' $NET_CONF
    service network restart
else 
    echo "network is up"
fi
# yum source is mirrors.aliyun.com
cat /etc/yum.repos.d/CentOS-Base.repo | grep mirrors.aliyun.com &> /dev/null
if [ $? != 0 ]; then
    curl http://mirrors.aliyun.com/repo/Centos-6.repo > /etc/yum.repos.d/CentOS-Base.repo
    curl http://mirrors.aliyun.com/repo/epel-6.repo > /etc/yum.repos.d/epel.repo
    yum clean all && yum makecache
else
    echo "yum source is mirrors.aliyun.com"
fi
# yum -y install vim lrzsz wget man(不需要判断,yum会自动跳过已经安装的程序)
yum -y install vim lrzsz wget man ntpdate
# iptables and selinux 
service iptables status &> /dev/null
if [ $? = 0 ]; then
    service iptables stop
else
    echo "iptables is down"
fi
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/timeout=5/timeout=2/g' /boot/grub/grub.conf 
for i in `chkconfig | grep 3:on | awk '{print $1}'` ; do chkconfig $i off ; done
for i in crond network rsyslog sshd lvm2-monitor blk-availability udev-post; do
    chkconfig $i on
done
if [ `ulimit -n` -lt 65535 ]; then
    echo "*            -         nofile          65535" >> /etc/security/limits.conf
fi
原文地址:https://www.cnblogs.com/cishi/p/6777920.html