zoukankan      html  css  js  c++  java
  • Linux系统初始化脚本

    #查看centos的版本号 
    CentOS_version=`cut -d" " -f 4 /etc/centos-release | cut -d"." -f 1`
    #改变PS3格式
    PS3="Please enter the option you need to optimize: "
    #网卡名称
    network_name=`ip a|sed -r -n 's/.. (e.*):.*/1/p'`
    #网卡的个数
    network_card_sum=`echo $network_name | wc -w`
    
    
    ######################判断是否root用户######################
    Judge_root() {
    if [[ `id | sed -r -n "s/uid=([0-9]{1,4}).*/1/p"` != 0 ]];then
      echo -e "e[1;31mYou are not a root user and you are not allowed to set this optimization.e[0m
    "
      break
    fi 
    }
    
    ######################关闭防火墙和selinux######################
    close_firewalld_selinux() {
      Judge_root
      if [ $CentOS_version == 7 ];then
          systemctl stop firewalld && systemctl disable firewalld &> /dev/null
          systemctl stop NetworkManager && systemctl disable NetworkManager &> /dev/null
          sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
          setenforce 0
      elif [ $CentOS_version == 6 ];then
          service firewalld stop &> /dev/null && chkconfig firewalld off &> /dev/null
          service NetworkManager stop &> /dev/null && chkconfig NetworkManager off &> /dev/null
          sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
          setenforce 0
      fi 
    }
    
    #######################更换yum源######################
    replace_yum() {
      Judge_root
      if [ $CentOS_version == 7 ];then
          curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
        sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
        curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null
        rm -rf /var/cache/yum && yum makecache &> /dev/null 
      elif [ $CentOS_version == 6 ];then
        curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &> /dev/null
        sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
        curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null
        rm -rf /var/cache/yum && yum makecache &> /dev/null
      fi
    }
    
    #######################下载基础包######################
    download_package() {
      yum install bash-completion ntpdate lrzsz bzip2 vim net-tools -y &> /dev/null
      source /usr/share/bash-completion/bash_completion
    }
    
    #######################修改网卡名称######################
    network_card_name() {
      Judge_root
      if [[ $network_card_sum != 1 ]];then
        i=0
        for card in `echo $network_name`;do
          mv /etc/sysconfig/network-scripts/ifcfg-$card /etc/sysconfig/network-scripts/ifcfg-eth$i
          sed -ri -e 's/(NAME=).*/1eth$i/g' -e 's/(DEVICE=).*/1eth$i/g' /etc/sysconfig/network-scripts/ifcfg-eth$i
          let i++
        done
        sed -i -r "/GRUB_CMDLINE_LINUX/s/(.*quiet)(.*)/1 net.ifnames=02/g" /etc/default/grub
        grub2-mkconfig -o /etc/grub2.cfg &> /dev/null
      else
        mv /etc/sysconfig/network-scripts/ifcfg-$network_name /etc/sysconfig/network-scripts/ifcfg-eth0
        sed -ri -e 's/(NAME=).*/1eth0/g' -e 's/(DEVICE=).*/1eth0/g' /etc/sysconfig/network-scripts/ifcfg-eth0
        sed -i -r "/GRUB_CMDLINE_LINUX/s/(.*quiet)(.*)/1 net.ifnames=02/g" /etc/default/grub
        grub2-mkconfig -o /etc/grub2.cfg &> /dev/null
      fi
    }
    
    #######################添加alias######################
    add_alias() {
      if [ ! -f /etc/profile.d/env.sh ];then
        echo -e "alias rm='echo RM is busy, please use MV'
    alias ping='ping -c4'" > /etc/profile.d/env.sh
      else
        sed -ri "s/(alias rm.*)/#1/g" ~/.bashrc
        sed -i -e "/alias rm/d" -e "/alias ping/d" /etc/profile.d/env.sh
        echo -e "alias rm='echo RM is busy, please use MV'
    alias ping='ping -c4'" >> /etc/profile.d/env.sh
      fi
    }
    
    
    #######################修改PS1######################
    modify_PS1() {
      Judge_root
      if [ ! -f /etc/profile.d/env.sh ];then
        echo 'export PS1="[e[32;1m][[e[33;1m]u[e[31;1m]@[e[33;1m]h [e[36;1m]W[e[32;1m]][e[34;1m]\$ [e[0m]"' > /etc/profile.d/env.sh
      else
        sed -i "/PS1/d" /etc/profile.d/env.sh 
        echo 'export PS1="[e[32;1m][[e[33;1m]u[e[31;1m]@[e[33;1m]h [e[36;1m]W[e[32;1m]][e[34;1m]\$ [e[0m]"' >> /etc/profile.d/env.sh
      fi
    }
    
    #######################设置.vimrc######################
    modify_vimrc() {
    cat > /root/.vimrc <<EOF
    set encoding=utf-8 "字符编码设置
    set syntax=on "语法高亮
    set completeopt=preview,menu "代码补全
    set smartindent "开启新行时使用智能自动缩进 set autoindent自动缩进,等价于set ai
    set number "显示行号,vim临时添号行号可以写成set nu,去除行号可以写成set nonu
    set history=500 "历史记录数
    set ignorecase "搜索忽略大小写
    set ruler "显示标尺
    set showcmd "输入的命令显示出来,看的清楚些
    set tabstop=4 "Tab键的宽度
    set shiftwidth=4 "这个是用于程序中自动缩进所使用的空白长度指示的
    set expandtab "使代码风格尽量保持一致,一般不允许在代码使用TAB符,而以2个空格代之。
    set softtabstop=4 "按下tab键,插入的是空格和tab制表符的混合
    set cursorline "设置光标行线
    function AddshTitle()
    call setline(1,"#!/bin/bash")
    call setline(2,"#*************************************************************************************")
    call setline(3,"#-*- coding:utf-8 -*-")
    call setline(4,"#File : " . expand("%"))
    call setline(5,"#Author : Cloud_zeng")
    call setline(6,"#Date : " . strftime("%F %T"))
    call setline(7,"#*************************************************************************************")
    call setline(8,"")
    endf
    function AddpyTitle()
    call setline(1,"#!/usr/bin/env python")
    call setline(2,"#*************************************************************************************")
    call setline(3,"#-*- coding:utf-8 -*-")
    call setline(4,"#File : " . expand("%"))
    call setline(5,"#Author : Cloud_zeng")
    call setline(6,"#Date : " . strftime("%F %T"))
    call setline(7,"#*************************************************************************************")
    call setline(8,"")
    endf
    autocmd BufNewFile *.sh exec ":call AddshTitle()"
    autocmd BufNewFile *.py exec ":call AddpyTitle()"
    autocmd BufNewFile * normal G
    EOF
    }
    
    #######################修改时区######################
    automatic_update_time(){
      yum install cronie -y &> /dev/null
      ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
      hwclock &> /dev/null
      echo '*/10 * * * * /usr/sbin/ntpdate -u ntp.aliyun.com &> /dev/null' >> /var/spool/cron/root
      systemctl restart crond
    }
    
    #######################ssh调整######################
    ssh_adjust(){
      Judge_root
      sed -ri -e 's/^(GSSAPIAuthentication ).*/1no/' -e 's/#(UseDNS.*)/1/' /etc/ssh/sshd_config
      systemctl restart sshd
    }
    
    #######################最大文件打开数######################
    limits_tune(){
    Judge_root
    echo '
    * soft nofile 128000
    * hard nofile 256000
    
    root soft nofile 128000
    root hard nofile 256000
    ' >> /etc/security/limits.conf
    }
    
     
    
    #######################安装python3######################
    add_python3() {
      Judge_root
      yum install gcc libffi-devel zlib-devel readline-devel -y &> /dev/null
      cd /usr/local/src/
      wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz &> /dev/null
      tar xf Python-3.7.4.tar.xz && cd Python-3.7.4
      mkdir -p /usr/local/python3
      ./configure --prefix=/usr/local/python3
      make && make install
      sed -ri "s@^(^PATH=)(.*)@1/usr/local/python3/bin:2@g" .bash_profile
    

    cat > /usr/local/python3/site-packages/tab.py <<EOF
    import sys,readline,rlcompleter,atexit,os
    readline.parse_and_bind('tab: complete')
    histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
    try:
      readline.read_history_file(histfile)
    except IOError:
      pass
      atexit.register(readline.write_history_file, histfile)
    atexit.register(readline.write_history_file, histfile)
    EOF

      echo 'export PYTHONSTARTUP=/usr/local/python3/site-packages/tab.py' >> .bash_profile 

      source ~/.bash_profile

    }
    
    select choice in download_package replace_yum close_firewalld_selinux add_alias network_card_name modify_PS1 modify_vimrc automatic_update_time ssh_adjust limits_tune add_python3 all quit;do
    
    case $choice in
    download_package)
      download_package
      echo -e "e[1;33mBasic package download completede[0m
    "
      ;;
    replace_yum)
      replace_yum
      echo -e "e[1;33mYum source replacement is completee[0m
    "
      ;;
    close_firewalld_selinux)
      close_firewalld_selinux
      echo -e "e[1;33mFirewalld and selinux have been closede[0m
    "
      ;;
    network_card_name)
      network_card_name
      echo -e "e[1;33mThe NIC name has been modified, e[1;31mbut it needs to be restarted to take effect.e[0m
    "
      ;;
    modify_PS1)
      modify_PS1
      echo -e "e[1;33mPS1 has been modifiede[0m
    "
      ;;
    modify_vimrc)
      modify_vimrc
      echo -e "e[1;33m.vimrc has been set upe[0m
    "
      ;;
    automatic_update_time)
      automatic_update_time
      echo -e "e[1;33mSet automatic update time to completee[0m
    "
      ;;
    ssh_adjust)
      ssh_adjust
      echo -e "e[1;33mssh service has been tuned to completee[0m
    "
      ;;
    limits_tune)
      limits_tune
      echo -e "e[1;33mThe maximum number of open files has been set.e[0m
    "
      ;;
    add_alias)
      add_alias
      echo -e "e[1;33mAlias setting completede[0m
    "
      ;;
    add_python3)
      add_python3
      echo -e "e[1;33mPython3 installed completede[0m
    " 
      ;;
    all)
      download_package
      replace_yum
      close_firewalld_selinux
      network_card_name
      modify_PS1
      modify_vimrc
      automatic_update_time
      ssh_adjust
      limits_tune
      add_alias
      echo -e "e[1;31mAll scripts are executed, if circumstances permit. Please restart, if not, then reconnect. Take effect some configurationse[0m"
      exit
      ;;
    quit)
      echo -e "e[1;31mSome profiles need to be reconnected to take effecte[0m
    "
      exit
      ;;
    esac
    done
  • 相关阅读:
    下一周计划
    strategy模式
    Roc加载模块过程
    博客园开通了
    MO sample中的缓冲冲区的例子很简单的一个例子
    作为一个想成为程序员的人来说
    试试用live writer写博客到博客园
    Tomcat崩溃,无法访问
    The class Form1 can be designed, but is not the first class in the file.
    Exception in thread "Timer0" java.lang.NullPointerException
  • 原文地址:https://www.cnblogs.com/mycloudedu/p/11605721.html
Copyright © 2011-2022 走看看