Linux系统初始化脚本

Stella981
• 阅读 602
#查看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\n"
  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=0\2/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=0\2/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'\nalias 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'\nalias 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 completed\e[0m\n"
  ;;
replace_yum)
  replace_yum
  echo -e "\e[1;33mYum source replacement is complete\e[0m\n"
  ;;
close_firewalld_selinux)
  close_firewalld_selinux
  echo -e "\e[1;33mFirewalld and selinux have been closed\e[0m\n"
  ;;
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\n"
  ;;
modify_PS1)
  modify_PS1
  echo -e "\e[1;33mPS1 has been modified\e[0m\n"
  ;;
modify_vimrc)
  modify_vimrc
  echo -e "\e[1;33m.vimrc has been set up\e[0m\n"
  ;;
automatic_update_time)
  automatic_update_time
  echo -e "\e[1;33mSet automatic update time to complete\e[0m\n"
  ;;
ssh_adjust)
  ssh_adjust
  echo -e "\e[1;33mssh service has been tuned to complete\e[0m\n"
  ;;
limits_tune)
  limits_tune
  echo -e "\e[1;33mThe maximum number of open files has been set.\e[0m\n"
  ;;
add_alias)
  add_alias
  echo -e "\e[1;33mAlias setting completed\e[0m\n"
  ;;
add_python3)
  add_python3
  echo -e "\e[1;33mPython3 installed completed\e[0m\n" 
  ;;
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 configurations\e[0m"
  exit
  ;;
quit)
  echo -e "\e[1;31mSome profiles need to be reconnected to take effect\e[0m\n"
  exit
  ;;
esac
done
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
1星期前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
6个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这