Linux系统初始化脚本

1#查看centos的版本号 2CentOS_version=`cut -d" " -f 4 /etc/centos-release | cut -d"." -f 1` 3#改变PS3格式 4PS3="Please enter the option you need to optimize: " 5#网卡名称 6network_name=`ip a|sed -r -n 's/.. (e.*):.*/\1/p'` 7#网卡的个数 8network_card_sum=`echo $network_name | wc -w` 9 10 11######################判断是否root用户###################### 12Judge_root() { 13if [[ `id | sed -r -n "s/uid=([0-9]{1,4}).*/\1/p"` != 0 ]];then 14  echo -e "\e[1;31mYou are not a root user and you are not allowed to set this optimization.\e[0m\n" 15  break 16fi 17} 18 19######################关闭防火墙和selinux###################### 20close_firewalld_selinux() { 21  Judge_root 22  if [ $CentOS_version == 7 ];then 23    systemctl stop firewalld && systemctl disable firewalld &> /dev/null 24    systemctl stop NetworkManager && systemctl disable NetworkManager &> /dev/null 25    sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config 26    setenforce 0 27  elif [ $CentOS_version == 6 ];then 28    service firewalld stop &> /dev/null && chkconfig firewalld off &> /dev/null 29    service NetworkManager stop &> /dev/null && chkconfig NetworkManager off &> /dev/null 30    sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config 31    setenforce 0 32  fi 33} 34 35#######################更换yum源###################### 36replace_yum() { 37  Judge_root 38  if [ $CentOS_version == 7 ];then 39    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null 40    sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo 41    curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null 42    rm -rf /var/cache/yum && yum makecache &> /dev/null 43  elif [ $CentOS_version == 6 ];then 44    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &> /dev/null 45    sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo 46    curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null 47    rm -rf /var/cache/yum && yum makecache &> /dev/null 48  fi 49} 50 51#######################下载基础包###################### 52download_package() { 53  yum install bash-completion ntpdate lrzsz bzip2 vim net-tools -y &> /dev/null 54  source /usr/share/bash-completion/bash_completion 55} 56 57#######################修改网卡名称###################### 58network_card_name() { 59  Judge_root 60  if [[ $network_card_sum != 1 ]];then 61    i=0 62    for card in `echo $network_name`;do 63      mv /etc/sysconfig/network-scripts/ifcfg-$card /etc/sysconfig/network-scripts/ifcfg-eth$i 64      sed -ri -e 's/(NAME=).*/\1eth$i/g' -e 's/(DEVICE=).*/\1eth$i/g' /etc/sysconfig/network-scripts/ifcfg-eth$i 65      let i++ 66    done 67    sed -i -r "/GRUB_CMDLINE_LINUX/s/(.*quiet)(.*)/\1 net.ifnames=0\2/g" /etc/default/grub 68    grub2-mkconfig -o /etc/grub2.cfg &> /dev/null 69  else 70    mv /etc/sysconfig/network-scripts/ifcfg-$network_name /etc/sysconfig/network-scripts/ifcfg-eth0 71    sed -ri -e 's/(NAME=).*/\1eth0/g' -e 's/(DEVICE=).*/\1eth0/g' /etc/sysconfig/network-scripts/ifcfg-eth0 72    sed -i -r "/GRUB_CMDLINE_LINUX/s/(.*quiet)(.*)/\1 net.ifnames=0\2/g" /etc/default/grub 73    grub2-mkconfig -o /etc/grub2.cfg &> /dev/null 74  fi 75} 76 77#######################添加alias###################### 78add_alias() { 79  if [ ! -f /etc/profile.d/env.sh ];then 80    echo -e "alias rm='echo RM is busy, please use MV'\nalias ping='ping -c4'" > /etc/profile.d/env.sh 81  else 82    sed -ri "s/(alias rm.*)/#\1/g" ~/.bashrc 83    sed -i -e "/alias rm/d" -e "/alias ping/d" /etc/profile.d/env.sh 84    echo -e "alias rm='echo RM is busy, please use MV'\nalias ping='ping -c4'" >> /etc/profile.d/env.sh 85  fi 86} 87 88 89#######################修改PS1###################### 90modify_PS1() { 91  Judge_root 92  if [ ! -f /etc/profile.d/env.sh ];then 93    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 94  else 95    sed -i "/PS1/d" /etc/profile.d/env.sh 96    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 97  fi 98} 99 100#######################设置.vimrc###################### 101modify_vimrc() { 102cat > /root/.vimrc <<EOF 103set encoding=utf-8 "字符编码设置 104set syntax=on "语法高亮 105set completeopt=preview,menu "代码补全 106set smartindent "开启新行时使用智能自动缩进 set autoindent自动缩进,等价于set ai 107set number "显示行号,vim临时添号行号可以写成set nu,去除行号可以写成set nonu 108set history=500 "历史记录数 109set ignorecase "搜索忽略大小写 110set ruler "显示标尺 111set showcmd "输入的命令显示出来,看的清楚些 112set tabstop=4 "Tab键的宽度 113set shiftwidth=4 "这个是用于程序中自动缩进所使用的空白长度指示的 114set expandtab "使代码风格尽量保持一致,一般不允许在代码使用TAB符,而以2个空格代之。 115set softtabstop=4 "按下tab键,插入的是空格和tab制表符的混合 116set cursorline "设置光标行线 117function AddshTitle() 118call setline(1,"#!/bin/bash") 119call setline(2,"#*************************************************************************************") 120call setline(3,"#-*- coding:utf-8 -*-") 121call setline(4,"#File : " . expand("%")) 122call setline(5,"#Author : Cloud_zeng") 123call setline(6,"#Date : " . strftime("%F %T")) 124call setline(7,"#*************************************************************************************") 125call setline(8,"") 126endf 127function AddpyTitle() 128call setline(1,"#!/usr/bin/env python") 129call setline(2,"#*************************************************************************************") 130call setline(3,"#-*- coding:utf-8 -*-") 131call setline(4,"#File : " . expand("%")) 132call setline(5,"#Author : Cloud_zeng") 133call setline(6,"#Date : " . strftime("%F %T")) 134call setline(7,"#*************************************************************************************") 135call setline(8,"") 136endf 137autocmd BufNewFile *.sh exec ":call AddshTitle()" 138autocmd BufNewFile *.py exec ":call AddpyTitle()" 139autocmd BufNewFile * normal G 140EOF 141} 142 143#######################修改时区###################### 144automatic_update_time(){ 145  yum install cronie -y &> /dev/null 146  ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 147  hwclock &> /dev/null 148  echo '*/10 * * * * /usr/sbin/ntpdate -u ntp.aliyun.com &> /dev/null' >> /var/spool/cron/root 149  systemctl restart crond 150} 151 152#######################ssh调整###################### 153ssh_adjust(){ 154  Judge_root 155  sed -ri -e 's/^(GSSAPIAuthentication ).*/\1no/' -e 's/#(UseDNS.*)/\1/' /etc/ssh/sshd_config 156  systemctl restart sshd 157} 158 159#######################最大文件打开数###################### 160limits_tune(){ 161Judge_root 162echo ' 163* soft nofile 128000 164* hard nofile 256000 165 166root soft nofile 128000 167root hard nofile 256000 168' >> /etc/security/limits.conf 169} 170 171 172 173#######################安装python3###################### 174add_python3() { 175  Judge_root 176  yum install gcc libffi-devel zlib-devel readline-devel -y &> /dev/null 177  cd /usr/local/src/ 178  wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz &> /dev/null 179  tar xf Python-3.7.4.tar.xz && cd Python-3.7.4 180  mkdir -p /usr/local/python3 181  ./configure --prefix=/usr/local/python3 182  make && make install 183  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

1} 2 3select 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 4 5case $choice in 6download_package) 7  download_package 8  echo -e "\e[1;33mBasic package download completed\e[0m\n" 9  ;; 10replace_yum) 11  replace_yum 12  echo -e "\e[1;33mYum source replacement is complete\e[0m\n" 13  ;; 14close_firewalld_selinux) 15  close_firewalld_selinux 16  echo -e "\e[1;33mFirewalld and selinux have been closed\e[0m\n" 17  ;; 18network_card_name) 19  network_card_name 20  echo -e "\e[1;33mThe NIC name has been modified, \e[1;31mbut it needs to be restarted to take effect.\e[0m\n" 21  ;; 22modify_PS1) 23  modify_PS1 24  echo -e "\e[1;33mPS1 has been modified\e[0m\n" 25  ;; 26modify_vimrc) 27  modify_vimrc 28  echo -e "\e[1;33m.vimrc has been set up\e[0m\n" 29  ;; 30automatic_update_time) 31  automatic_update_time 32  echo -e "\e[1;33mSet automatic update time to complete\e[0m\n" 33  ;; 34ssh_adjust) 35  ssh_adjust 36  echo -e "\e[1;33mssh service has been tuned to complete\e[0m\n" 37  ;; 38limits_tune) 39  limits_tune 40  echo -e "\e[1;33mThe maximum number of open files has been set.\e[0m\n" 41  ;; 42add_alias) 43  add_alias 44  echo -e "\e[1;33mAlias setting completed\e[0m\n" 45  ;; 46add_python3) 47  add_python3 48  echo -e "\e[1;33mPython3 installed completed\e[0m\n" 49  ;; 50all) 51  download_package 52  replace_yum 53  close_firewalld_selinux 54  network_card_name 55  modify_PS1 56  modify_vimrc 57  automatic_update_time 58  ssh_adjust 59  limits_tune 60  add_alias 61  echo -e "\e[1;31mAll scripts are executed, if circumstances permit. Please restart, if not, then reconnect. Take effect some configurations\e[0m" 62  exit 63  ;; 64quit) 65  echo -e "\e[1;31mSome profiles need to be reconnected to take effect\e[0m\n" 66  exit 67  ;; 68esac 69done
点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

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

Linux系统初始化脚本 - HelloWorld