You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shell-project/os-test/osinit.sh

100 lines
2.8 KiB

#!/usr/bin/bash
# 将color.sh脚本中的内容在此脚本生效,类似于导入操作
source ./color.sh
# 将不确定的因素交给用户自定义
interface=ens33
gateway=2
hostname=database
# 系统信息采集
green "当前用户 $USER"
green "当前时间 `date "+%F %X"`"
echo 3 >/proc/sys/vm/drop_caches
green "当前内存 $(free |awk 'NR==2{print $4/1024}')M"
green "当前核心 $(grep -E '^processor' /proc/cpuinfo|wc -l)"
green "核心品牌 $(grep -E '^model name' /proc/cpuinfo |head -n 1 |awk -F: '{print $2}')"
green "启动时长 $(uptime |awk -F',' '{print $1}')"
# 网络检测
ping -w1 -c1 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
green "网络正常"
else
red "网络异常"
exit 1
fi
# yum源检查
cd /etc/yum.repos.d/ && \
rename .repo .repo.bak *.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y epel-release yum-utils && \
green "yum源初始化安装"
green "开始安装常用软件"
green "即将为您安装 vim wget unzip lrzsz net-tools yum-utils ntpdate htop psacct 这些软件,如果有其他需要,请手动输入,您有10秒的时间"
read -t 10 -p ">>>:" package
package=${package:=";"}
yum install -y vim wget unzip lrzsz net-tools yum-utils ntpdate psacct $package
if [ $? -eq 0 ];then
green "常用软件安装成功"
else
yellow "部分安装可能出现异常"
fi
green "开始进行系统优化"
systemctl stop firewalld && systemctl disable firewalld
systemctl stop postfix && systemctl disable postfix
sed -i 's/^SELINUX.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0 2>/dev/null
green "修改文件打开数、进程打开数"
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65535
* hard nofile 65535
* soft nproc 1024
* hard nproc 1024
EOF
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sed -i 's/#UseDNS.*/UseDNS no/' /etc/ssh/sshd_config
green "开始进行静态ip修改"
ipaddr=$(ip a |grep ${interface} |grep inet|awk '{print $2}'|awk -F'/' '{print $1}')
netmask=$(ip a |grep ${interface} |grep inet|awk '{print $2}'|awk -F'/' '{print $2}')
cat >/etc/sysconfig/network-scripts/ifcfg-${interface} <<EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
IPADDR="$ipaddr"
PREFIX="$netmask"
GATEWAY="${ipaddr%.*}.${gateway}"
DNS1=114.114.114.114
DNS2=8.8.8.8
DEFROUTE="yes"
DEVICE="${interface}"
ONBOOT="yes"
EOF
systemctl restart network
if [ $? -eq 0 ];then
ping -w1 -c1 www.baidu.com &>/dev/null && green "网络配置成功" || red "网络连接异常"
else
red "网卡配置异常"
fi
green "开始进行主机名配置"
hostname=${hostname:='osinit'}
hostnamectl set-hostname $hostname
green "系统初始化完成,即将重启,配置生效,是否现在重启[Y|N]"
read -t 5 result
result=${result:='N'}
if [ $result == 'Y' ];then
reboot
else
yellow "配置需要重启后生效"
fi