Linux系统初始化脚本
#查看centos的版本号
CentOS_version=`cut -d" " -f /etc/centos-release | cut -d"." -f `
#改变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"` != ]];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 == ];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
elif [ $CentOS_version == ];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
fi
} #######################更换yum源######################
replace_yum() {
Judge_root
if [ $CentOS_version == ];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 == ];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 != ]];then
i=
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- "字符编码设置
set syntax=on "语法高亮
set completeopt=preview,menu "代码补全
set smartindent "开启新行时使用智能自动缩进 set autoindent自动缩进,等价于set ai
set number "显示行号,vim临时添号行号可以写成set nu,去除行号可以写成set nonu
set history= "历史记录数
set ignorecase "搜索忽略大小写
set ruler "显示标尺
set showcmd "输入的命令显示出来,看的清楚些
set tabstop= "Tab键的宽度
set shiftwidth= "这个是用于程序中自动缩进所使用的空白长度指示的
set expandtab "使代码风格尽量保持一致,一般不允许在代码使用TAB符,而以2个空格代之。
set softtabstop= "按下tab键,插入的是空格和tab制表符的混合
set cursorline "设置光标行线
function AddshTitle()
call setline(,"#!/bin/bash")
call setline(,"#*************************************************************************************")
call setline(,"#-*- coding:utf-8 -*-")
call setline(,"#File : " . expand("%"))
call setline(,"#Author : Cloud_zeng")
call setline(,"#Date : " . strftime("%F %T"))
call setline(,"#*************************************************************************************")
call setline(,"")
endf
function AddpyTitle()
call setline(,"#!/usr/bin/env python")
call setline(,"#*************************************************************************************")
call setline(,"#-*- coding:utf-8 -*-")
call setline(,"#File : " . expand("%"))
call setline(,"#Author : Cloud_zeng")
call setline(,"#Date : " . strftime("%F %T"))
call setline(,"#*************************************************************************************")
call setline(,"")
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
* hard nofile root soft nofile
root hard nofile
' >> /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..tar.xz && cd Python-3.7.
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
Linux系统初始化脚本的更多相关文章
- LINUX 系统初始化脚本
#!/bin/bash ######the system first start configuretion #####for install ####copy right by donglei## ...
- centos7 系统初始化脚本
现在自己的本地虚拟机系统,直接安装的是centos7.2 mini版,安装完成发现好多东西都没有安装,所以写了一个简单的系统初始化脚本,让自己可以省一些力气,哈哈 人懒主要是. 下面贴出写的脚本,脚本 ...
- Linux系统初始化流程
POST-->BIOS(Boot Sequence)-->MBR(bootloader)-->Kernel(initrd)-->/sbin/init(/etc/inittab) ...
- Linux系统初始化过程及运行级别简介
Linux开机过程: 1 开机自检(BIOS):初始化硬件,查找启动介质 2 MBR引导 3 GRUB引导菜单:GRUB程序安装在Bootloader 4 加载内核(kernel) 5 运行INIT进 ...
- shell实现增加删除Linux系统用户脚本(密码为随机)
README shell环境下运行脚本,根据需求选择相应的功能. List \t\t create the userlist 这一步是必须执行的,脚本会识别本地当前目录下的文件 Useradd \t\ ...
- linux系统初始化——文件系统初始化步骤
linux文件系统初始化步骤 System V init启动过程 概括地讲,Linux/Unix系统一般有两种不同的初始化启动方式. 1) BSD system init 2) System V in ...
- Linux系统初始化配置项(centos7)
主机刚安装完系统,会做一些配置上的优化. 修改时区 通过命令将时区设置为亚洲/上海. timedatectl set-timezone Asia/Shanghai #centos7 cp /usr/s ...
- Centos7系统初始化脚本
前言: 因公司业务增加,陆续新增服务器,时不时的来几台,手动地一台台对服务器初始化操作感觉太麻烦. 于是乎,根据初始化需求整合了一个初始化脚本,实现批量脚本初始化操作. 说明: 本脚本根据自身需求编写 ...
- linux系统备份脚本
前言 之前写过<<linux系统简单备份的脚本>>, 最开始一直用着,后来觉得有必要改进下它,不管是从操作方式上还是脚本的工作方式上.之所以这么看重备份,是因为我经历过磁盘损坏 ...
随机推荐
- Elasticsearch常见错误与配置简介
一.常见错误 1.1 root用户启动elasticsearch报错 Elasticsearch为了安全考虑,不让使用root启动,解决方法新建一个用户,用此用户进行相关的操作.如果你用root启动, ...
- vue定义global.js,挂载在vue原型上面使用
首先在src目录下创建global目录,在global目录下创建index.js. export default { install(Vue) { var that = this // 1. 添加全局 ...
- pycharm中文专业版安装使用
激活码 MTW881U3Z5-eyJsaWNlbnNlSWQiOiJNVFc4ODFVM1o1IiwibGljZW5zZWVOYW1lIjoiTnNzIEltIiwiYXNzaWduZWVOYW1lI ...
- 栈与队列基本操作 Java实现
一.顺序栈 //数组实现顺序栈 public class OrderStack { private String[] elem; private int top; OrderStack(int k) ...
- mybatis 基础(一) xml配置
如果文章有误,请各位楼下评论,感谢各位积极修正! 一起学习,成为大佬! mybatis: 1.轻量级 2.高级映射(实体类与数据库表字段的映射) 这样就可以后续开发中去操作实体类而不需要去关注数据库, ...
- c++练习—实现简单的4则运算
#pragma once class Counter { public: void setExp(const char* exp);//设置表达式 void cleanExp(); //清除表达式 v ...
- 3d长方体
html <div class="main"> <div class="a1">1</div> <div class= ...
- Iview 中 获取 Menu 导航菜单 选中的值
期望效果: 原来,我用的是脚本来控制,然后........,再然后,我再去仔细看官方文档的时候,才发现,Menu组件 有那么两个事件,on-select 和 on-open-change ,好气啊,之 ...
- 牛客 40F 珂朵莉的约数 (莫队)
珂朵莉给你一个长为n的序列,有m次查询 每次查询给两个数l,r 设s为区间[l,r]内所有数的乘积 求s的约数个数mod 1000000007 直接莫队暴力维护复杂度是$O(8m\sqrt{m})$. ...
- MySQL存储引擎InnoDB大量数据下的问题
MySQL如果只有MyISAM一个引擎的话,那你们黑真的也有道理,但问题是InnoDB现在已经是MySQL默认的引擎,而且这个引擎综合能力很强,能用好这个引擎其实就已经能解决大多数需要数据库的业务逻辑 ...