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系统简单备份的脚本>>, 最开始一直用着,后来觉得有必要改进下它,不管是从操作方式上还是脚本的工作方式上.之所以这么看重备份,是因为我经历过磁盘损坏 ...
随机推荐
- Git学习教程一之安装Git&&&本地仓库建立
一. 安装git 1:在Windows上使用Git,可以从Git官网直接下载安装程序,(网速慢的同学请移步国内镜像),然后按默认选项一直安装即可. 2:安装完成后,在开始菜单里找到“Git”-> ...
- swagger-注解
常用注解 @Api(value = "xxx"):用于类,表示标识这个类是swagger的资源. tags–表示说明,如果有多个值,会生成多个list value–也是说明,可以使 ...
- 第四章 信息收集之nmap
@nmap扫描工具 nmap是使用最广泛的扫描工具,主要的使用范围有,嗅探,扫描,ping. 局域网扫描 nmap扫描的基本命令: 首先在桌面右键选择open in terminal进入命令窗口,输入 ...
- linux 自定义函数
用别人的车子出行,总感觉别扭,那怎么自定义自己的车轮子呢? 通过上面的求两个参数的和例子,我们可以学到定义一个函数基本的步骤, function getSum(){ SUM=$[$n1+$n2] ...
- 深入理解C语言-接口封装设计思想
断层思维 在设计时候,不需要知道实现,只需要知道如何使用 接口设计的设计思路 Sckclient客户端api模型 第一套API (*.h) #ifndef _SCK_CLINT_H_ #define ...
- React中this指向常用的2种修正方式
一.使用bind方法(构造函数内绑定) <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- Sentence Screen Fitting
Given a rows x cols screen and a sentence represented by a list of words, find how many times the gi ...
- Hive远程连接
HIVE的连接模式== 本地连接模式 直接启动hive命令 HIVE的远程连接 这里要启动HIVE的服务 thirft进行编写 hiveserver2 —- > 前台启动 后台启动 前台启动 h ...
- 如何利用swoole搭建一個簡易聊天室
<?php class Chat { const HOST = '0.0.0.0';//ip地址 0.0.0.0代表接受所有ip的访问 const PART = 82;//端口号 private ...
- ThinkPHP composer的安装,及image图像处理类库的加载
以下教程针对windows系统,示例系统使用win7 composer安装 下载composer安装包,点击安装. 出现'composer choose the command-line php' 要 ...