Linux系统初始化优化脚本

主要针对以下几点进行系统初始化以及优化

1.修改主机名

2.配置网卡设备名为eth0

3.配置静态IP

4.优化Linux系统参数

5.设置阿里云yum源

6.时间同步设置

7.安装常用的运维软件、网络软件

8.创建运维账号

#!/bin/bash
#################################################
### Description: System Initialization Script ###
### Auther: Huang-Bo                          ###
### Email: haunbo@163.com                     ###
### Blog: https://blog.csdn.net/Habo_         ###
### Create Date: 2022-10-18                   ###
#################################################
#############################################################################################
# 颜色函数
#############################################################################################
function COLOR_RED() {
  echo -e "\033[1;31m$1\033[0m"
}

function COLOR_GREEN() {
  echo -e "\033[1;32m$1\033[0m"
}

function COLOR_YELLOW() {
  echo -e "\033[1;33m$1\033[0m"
}
#############################################################################################
# 文本颜色函数
#############################################################################################
function echo_check() {
  echo -e "$1  [\033[32m √ \033[0m]"
}

function log_success() {
  COLOR_GREEN "[SUCCESS] $1"
}

function log_error() {
  COLOR_RED "[ERROR] $1"
}

function log_info() {
  COLOR_YELLOW "[INFO] $1"
}

#############################################################################################
# global variable
#############################################################################################
date_time=$(date +"%Y-%m-%d-%H:%M:%S")
yum_dir="/etc/yum.repos.d/"
aliyun_url="https://mirrors.aliyun.com/"
network_url="/etc/sysconfig/network-scripts/"
#############################################################################################
# 设置主机名
#############################################################################################
host_name () {
        read -p "请修改你的主机名:" hostname
        hostnamectl set-hostname "$hostname"
        log_info "您的主机名为:"$hostname" "
}

#############################################################################################
# 设置网卡设备名称为eth0,并设置IP地址
#############################################################################################
set_ethc() {
    sed -ri.bak-${date_time} '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0 biosdevname=0"@' /etc/default/grub
    grub2-mkconfig -o /boot/grub2/grub.cfg >& /dev/null
}

set_ipc() {
    ETHNAME=`ip addr | sed -nr '/^2/s#^.* ([a-z]+[0-9]+).*#\1#p' | head -n1`
    cp ${network_url}"ifcfg-${ETHNAME}" /tmp/ifcfg-${ETHNAME}.bak-${date_time}
    mv ${network_url}"ifcfg-${ETHNAME}" ${network_url}"ifcfg-eth0"
    read -p "请输入你想配置的IP地址: " IPA
    read -p "请配置此IP地址的网关: "   GATE
    read -p "请配置此服务器的DNS地址:" DNS
    read -p "请配置此服务器的DEVICE名称:" DEVI
    read -p "请配置此服务器的NAME名称:" NM
    cat > /etc/sysconfig/network-scripts/ifcfg-${NM} << EFO
DEVICE=${DEVI}
NAME=${NM}
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR="$IPA"
NETMASK=255.255.255.0
GATEWAY="$GATE"
DNS1=$DNS
EFO
log_success "IP地址修改成功,重新启动服务器后生效!"
}

system_optimization() {
#############################################################################################
# 文件描述符
#############################################################################################
cat >>/etc/security/limits.conf <<EOF
*           soft   nofile       65535
*           hard   nofile       65535
EOF
echo ""
echo ""
#############################################################################################
# 取消ctrl+alt+del
#############################################################################################
mv /usr/lib/systemd/system/ctrl-alt-del.target /usr/lib/systemd/system/ctrl-alt-del.target.bak
echo ""
echo ""
#############################################################################################
# SSH服务优化
#############################################################################################

\cp /etc/ssh/sshd_config /etc/ssh/sshd_config."${date_time}"
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%g' /etc/ssh/sshd_config
service sshd restart
echo ""
echo ""
#############################################################################################
# 优化命令行界面
#############################################################################################
echo 'export PS1="[ \033[01;33m\u\033[0;36m@\033[01;34m\h \033[01;31m\w\033[0m ]\033[0m \n#"' >>/etc/profile
echo ""
echo ""
#############################################################################################
# 优化vim
#############################################################################################
cat >>/root/.vimrc <<EOF
syntax enable
syntax on
set ruler
set number
set cursorline
set cursorcolumn
set hlsearch
set incsearch
set ignorecase
set nocompatible
set wildmenu
set paste
set expandtab
set tabstop=2
set shiftwidth=4
set softtabstop=4
set gcr=a:block-blinkon0
set guioptions-=l
set guioptions-=L
set guioptions-=r
set guioptions-=R
highlight CursorLine   cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE
highlight CursorColumn cterm=NONE ctermbg=black ctermfg=green guibg=NONE guifg=NONE
EOF

#############################################################################################
# 关闭selinux
#############################################################################################
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
echo ""
echo ""
#############################################################################################
# 关闭防火墙
#############################################################################################
systemctl stop firewalld && systemctl disable firewalld
echo ""
echo ""

#############################################################################################
# 关闭不需要的服务
#############################################################################################
systemctl stop postfix.service && systemctl disable postfix.service
a=`service NetworkManager status &> /dev/null`
if [ $? -gt 0 ];then
    log_info "NetworkManager已关闭"
else
    log_error "NetworkManager未关闭,即将关闭NetworkManager"
    systemctl disable NetworkManager && systemctl stop NetworkManager &> /dev/null
    log_success "NetworkManager已关闭"
fi
}

set_repo() {
if [ -f $yum_dir"CentOS-Base.repo"]; then
   mkdir -p ${yum_dir}old && mv ${yum_dir}* ${yum_dir}old/
   if curl -o $yum_dir"CentOS-Base.repo" $aliyun_url"repo/Centos-7.repo" && curl -o ${yum_dir}epel-7.repo $aliyun_url"repo/epel-7.repo";then
      log_success "CentOS-Base.repo、epel-7.repo下载成功"
      yum clean all && yum makecache
   else
      log_error "CentOS-Base.repo、epel-7.repo下载失败请检查!"
   fi
else
    log_info "CentOS-Base.repo non-existent"
    curl -o $yum_dir"CentOS-Base.repo" $aliyun_url"repo/Centos-7.repo" && curl -o ${yum_dir}epel-7.repo $aliyun_url"repo/epel-7.repo"
    yum clean all && yum makecache
fi
}
install_softwares() {
yum install -y nc telnet net-tools wget lsof vim lrzsz jq bash-completion zip unzip epel-release tree ntpdate gcc gcc-c++ make cmake yum-utils
}
time_lock () {
        a=`rpm -qa | grep ntpdate`
        if [ $? -eq 0 ];then
                log_info "ntpdate已经安装"
        else
                log_info "ntpdate没有安装,即将进行安装"
                yum install -y ntpdate &> /dev/null
                log_success "ntpdate安装完成"
        fi
        ntpdate time1.aliyun.com &> /dev/null
                log_success "时间同步完成"
        systemctl start ntpdate && systemctl enable ntpdate &> /dev/null
}

user_add() {
# 创建运维账号,并配置sudo权限
GROUP="/etc/group"
group_admin=$(cat ${GROUP} | grep admin -c)

if [ "$group_admin" -ge 1 ]; then
        echo "the group admin is already  "
else
        groupadd admin
        echo "%admin ALL=(ALL)  NOPASSWD: ALL" >>/etc/sudoers

        useradd ops -g admin
        echo "ops:Ops@1234" | chpasswd
fi
}
reboot_init () {
        reboot
}

action=$1
function usage() {
  echo "$(gettext 'System Initialization Script')"
  echo
  echo "Usage: "
  echo "  ./initial_system.sh [COMMAND] [ARGS...]"
  echo "  ./initial_system.sh --help"
  echo "System Initialization: "
  echo "  1)              $(gettext '修改主机名')"
  echo "  2)              $(gettext '配置网卡设备名')"
  echo "  3)              $(gettext '配置IP')"
  echo "  4)              $(gettext '系统参数优化')"
  echo "  5)              $(gettext '设置阿里yum源')"
  echo "  6)              $(gettext '时间同步设置')"
  echo "  7)              $(gettext '安装常用软件')"
  echo "  8)              $(gettext '创建运维账号')"
  echo "  9)              $(gettext '重启服务器')"
}
main () {
while true
do
read -p "输入你的序号(按Q键退出):" action
if [[ "${action}" == "help" || "${action}" == "h" || "${action}" == "-h" || "${action}" == "--help" ]]; then
   usage
else
    case $action in
    1)
            clear
            host_name
            sleep 2
            ;;
    2)
            clear
            set_ethc
            sleep 2
            ;;
    3)
            clear
            set_ipc
            sleep 2
            ;;
    4)
            clear
            system_optimization
            sleep 2
            ;;
    5)      clear
            set_repo
            sleep 2
            ;;
    6)
            clear
            time_lock
            sleep 2
            ;;
    7)
            clear
            install_softwares
            sleep 2
            ;;
    8)
            clear
            user_add
            sleep 2
            ;;
    9)
            clear
            reboot_init
            sleep 2
            ;;
    Q)
            exit
            ;;
    *)
     log_info "No such command: ${action}"
    esac
fi
done
}
main
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不爱吃肉@

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

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

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

打赏作者

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

抵扣说明:

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

余额充值