Linux系统初始化Shell脚本(if判断)

这是一个用于Centos7系统的初始化脚本,功能包括关闭firewalld和selinux防火墙,将动态网络配置为静态,更换yum仓库至国内源,以及进行时间同步和安装常用软件如vim、wget、unzip。
摘要由CSDN通过智能技术生成

适用系统版本: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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Que_art

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值