适用系统版本:Centos7
脚本引用函数:
firewalld:用于关闭firewalld和selinux防火墙
network:将动态网络修改为静态网络,自定义网段
ware:将国外的yum仓库改为国内的yum仓库
date:进行时间同步
install:安装常用命令vim、wget、unzip
使用过程:
创建脚本文件:
vi a.sh //使用vi文件管理命令打开,文件名随意但必须以.sh结尾
#!/bin/bash
#author:hyy
#version:1.0
#function:Operating system initialization
#Turn off firewall operations.
firewalld(){
#判断Firewalld运行状态
status=`firewall-cmd --state &>/dev/null`
if [ "$status" = "not running" ];then
#判断Selinux运行状态
dis=`cat /etc/selinux/config|awk NR==7|awk -F '=' '{print $2}'`
if [ "${dis}" = "disabled" ];then
echo "The firewall and seliunx are turned off."
else
#临时关闭和永久关闭Selinux
setenforce 0 &>/dev/null
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "The firewall and seliunx shut down successfully."
else
echo "Selinux shutdown failed."
fi
fi
else
#关闭和永久关闭Firewalld
systemctl stop firewalld && systemctl disable firewalld &>/dev/null
if [ $? -eq 0 ];then
#判断Selinux运行状态
dis=`cat /etc/selinux/config|awk NR==7|awk -F '=' '{print $2}'`
if [ "${dis}" = "disabled" ];then
echo "The firewall and seliunx are turned off."
else
#临时关闭和永久关闭Selinux
setenforce 0 &>/dev/null
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "The firewall and seliunx shut down successfully."
else
echo "Selinux shutdown failed."
fi
fi
else
echo "Firewall shutdown failed."
fi
fi
}
firewalld
#The network is configured as static.
#手动输入局域网网关,如果网关不是10.0.0注意修改下面网卡配置文件中的ip和网关
read -p "Please enter the host address of 10.0.0. LAN:" input
network(){
#ping百度域名判断网络是否通畅
ping -c1 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
echo "Network available."
#网络通畅就将以下内容写入网卡配置文件中,注意不同的主机可能网卡名不同
network_path="/etc/sysconfig/network-scripts/ifcfg-ens33"
rm -rf ${network_path}
cat >> ${network_path} <<eof
TYPE="Ethernet"
BOOTPROTO="static"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=10.0.0.$input
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
DNS1=8.8.8.8
eof
#网络配置完成后重启网络
systemctl restart network
if [ $? -eq 0 ];then
#ping网络检查网络配置是否正常
ping -c1 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
echo "Network configuration is available."
else
echo "There is a problem with the network configuration."
fi
else
echo "Failed to restart the network."
fi
else
echo "Network unavailable: Check the network mode and configuration."
fi
}
network
#Replace the yum repository file operation.
ware(){
#删除国外仓库
rm -rf /etc/yum.repos.d/*
if [ $? -eq 0 ];then
#使用阿里提供的仓库,将仓库更换为国内
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null && curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null
ls /etc/yum.repos.d/{CentOS-Base,epel}.repo &>/dev/null
if [ $? -eq 0 ];then
echo "The warehouse is replaced successfully."
else
echo "Warehouse replacement failed."
fi
else
echo "Repository file cleanup failed."
fi
}
ware
#Perform a time synchronization operation.
#定义变量时间
t=`date`
date(){
#判断ntpdate软件是否安装
rpm -qa |grep ntpdate &>/dev/null
if [ $? -eq 0 ];then
#使用阿里公共时间进行时间同步,如果此公共时间不可用可自行百度搜索更换公共时间地址
ntpdate -b ntp1.aliyun.com &>/dev/null
if [ $? -eq 0 ];then
echo "The current is $t."
else
#该公共时间地址第一次同步不成功进行第二次同步一般会成功
ntpdate -b ntp1.aliyun.com &>/dev/null
if [ $? -eq 0 ];then
echo "The current is $t."
else
echo "Time synchronization failed."
fi
fi
else
#安装ntpdate软件进行时间同步
yum -y install ntpdate &>/dev/null
ntpdate -b ntp1.aliyun.com &>/dev/null
if [ $? -eq 0 ];then
echo "The current is $t."
else
ntpdate -b ntp1.aliyun.com &>/dev/null
if [ $? -eq 0 ];then
echo "The current is $t."
else
echo "Time synchronization failed."
fi
fi
fi
}
date
#Install common software
install(){
#安装常用软件vim、wget、unzip,如果有自己常用的软件可以直接在unzip后面添加
yum -y install vim wget unzip &>/dev/null
if [ $? -eq 0 ];then
echo "vim and wget installed successfully."
else
echo "vim and wget installation failed."
fi
}
install
执行脚本文件:
方法一:bash a.sh
方法二:chmod a+x a.sh
./a.sh