shell基础优化脚本
#!/bin/bash
#########################################################
#Created Time: Tue Aug 7 01:29:09 2018 #
#version:1.0 by:kingle Mail: kingle122@vip.qq.com #
#基于oldboy书籍优化编写 #
#实现功能:一键系统优化15项脚本,适用于Centos6.x #
#########################################################
#Source function library.
. /etc/init.d/functions
#date
DATE=`date +"%y-%m-%d %H:%M:%S"`
#ip
IPADDR=`grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0|cut -d= -f `
#hostname
HOSTNAME=`hostname -s`
#user
USER=`whoami`
#disk_check
DISK_SDA=`df -h |grep -w "/" |awk '{print $5}'`
#cpu_average_check
cpu_uptime=`cat /proc/loadavg|awk '{print $1,$2,$3}'`
#set LANG
export LANG=zh_CN.UTF-
#Require root to run this script.
uid=`id | cut -d\( -f1 | cut -d= -f2`
if [ $uid -ne ];then
action "Please run this script as root." /bin/false
exit
fi
#"stty erase ^H"
\cp /root/.bash_profile /root/.bash_profile_$(date +%F)
erase=`grep -wx "stty erase ^H" /root/.bash_profile |wc -l`
if [ $erase -lt ];then
echo "stty erase ^H" >>/root/.bash_profile
source /root/.bash_profile
fi
#Config Yum CentOS-Bases.repo and save Yum file
configYum(){
echo "================更新为国内YUM源=================="
cd /etc/yum.repos.d/
\cp CentOS-Base.repo CentOS-Base.repo.$(date +%F)
ping -c mirrors.aliyun.com >/dev/null
if [ $? -eq ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-.repo
else
echo "无法连接网络。"
exit $?
fi
echo "==============保存YUM源文件======================"
sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf
grep keepcache /etc/yum.conf
sleep
action "配置国内YUM完成" /bin/true
echo "================================================="
echo ""
sleep
}
#Charset zh_CN.UTF-8
initI18n(){
echo "================更改为中文字符集================="
\cp /etc/sysconfig/i18n /etc/sysconfig/i18n.$(date +%F)
>/etc/sysconfig/i18n
cat >>/etc/sysconfig/i18n<<EOF
LANG="zh_CN.UTF-8"
#LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
EOF
source /etc/sysconfig/i18n
echo '#cat /etc/sysconfig/i18n'
grep LANG /etc/sysconfig/i18n
action "更改字符集zh_CN.UTF-8完成" /bin/true
echo "================================================="
echo ""
sleep
}
#Close Selinux and Iptables
initFirewall(){
echo "============禁用SELINUX及关闭防火墙=============="
\cp /etc/selinux/config /etc/selinux/config.$(date +%F)
/etc/init.d/iptables stop
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce
/etc/init.d/iptables status
echo '#grep SELINUX=disabled /etc/selinux/config '
grep SELINUX=disabled /etc/selinux/config
echo '#getenforce '
getenforce
action "禁用selinux及关闭防火墙完成" /bin/true
echo "================================================="
echo ""
sleep
}
#Init Auto Startup Service
initService(){
echo "===============精简开机自启动===================="
export LANG="en_US.UTF-8"
for A in `chkconfig --list |grep :on |awk '{print $1}' `;do chkconfig $A off;done
for B in rsyslog network sshd crond sysstat;do chkconfig $B on;done
echo '+--------which services on---------+'
chkconfig --list |grep :on
echo '+----------------------------------+'
export LANG="zh_CN.UTF-8"
action "精简开机自启动完成" /bin/true
echo "================================================="
echo ""
sleep
}
#Removal system and kernel version login before the screen display
initRemoval(){
echo "======去除系统及内核版本登录前的屏幕显示======="
#must use root user run scripts
if
[ $UID -ne ];then
echo This script must use the root user ! ! !
sleep
exit
fi
>/etc/redhat-release
>/etc/issue
action "去除系统及内核版本登录前的屏幕显示" /bin/true
echo "================================================="
echo ""
sleep
}
#Change sshd default port and prohibit user root remote login.
initSsh(){
echo "========修改ssh默认端口禁用root远程登录=========="
\cp /etc/ssh/sshd_config /etc/ssh/sshd_config.$(date +%F)
sed -i 's/#Port 22/Port 52113/g' /etc/ssh/sshd_config
sed -i 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
echo '+-------modify the sshd_config-------+'
echo 'Port 52113'
echo 'PermitEmptyPasswords no'
echo 'PermitRootLogin no'
echo 'UseDNS no'
echo '+------------------------------------+'
/etc/init.d/sshd reload && action "修改ssh默认参数完成" /bin/true || action "修改ssh参数失败" /bin/false
echo "================================================="
echo ""
sleep
}
#time sync
syncSysTime(){
echo "================配置时间同步====================="
\cp /var/spool/cron/root /var/spool/cron/root.$(date +%F) >/dev/null
NTPDATE=`grep ntpdate /var/spool/cron/root >/dev/null |wc -l`
if [ $NTPDATE -eq ];then
echo "#times sync by lee at $(date +%F)" >>/var/spool/cron/root
echo "*/5 * * * * /usr/sbin/ntpdate time.windows.com &>/dev/null" >> /var/spool/cron/root
fi
echo '#crontab -l'
crontab -l
action "配置时间同步完成" /bin/true
echo "================================================="
echo ""
sleep
}
#install tools
initTools(){
echo "#####安装升级系统补装工具及重要工具升级(选择最小化安装minimal)#####"
ping -c mirrors.aliyun.com
sleep
yum install tree nmap sysstat lrzsz dos2unix -y
sleep
rpm -qa tree nmap sysstat lrzsz dos2unix
sleep
yum install openssl openssh bash -y
sleep
action "安装升级系统补装工具及重要工具升级(选择最小化安装minimal)" /bin/true
echo "================================================="
echo ""
sleep
}
#add user and give sudoers
addUser(){
echo "===================新建用户======================"
#add user
while true
do
read -p "请输入新用户名:" name
NAME=`awk -F':' '{print $1}' /etc/passwd|grep -wx $name >/dev/null|wc -l`
if [ ${#name} -eq 0 ];then
echo "用户名不能为空,请重新输入。"
continue
elif [ $NAME -eq ];then
echo "用户名已存在,请重新输入。"
continue
fi
useradd $name
break
done
#create password
while true
do
read -p "为 $name 创建一个密码:" pass1
if [ ${#pass1} -eq 0 ];then
echo "密码不能为空,请重新输入。"
continue
fi
read -p "请再次输入密码:" pass2
if [ "$pass1" != "$pass2" ];then
echo "两次密码输入不相同,请重新输入。"
continue
fi
echo "$pass2" |passwd --stdin $name
break
done
sleep
#add visudo
echo "#####add visudo#####"
\cp /etc/sudoers /etc/sudoers.$(date +%F)
SUDO=`grep -w "$name" /etc/sudoers |wc -l`
if [ $SUDO -eq ];then
echo "$name ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
echo '#tail -1 /etc/sudoers'
grep -w "$name" /etc/sudoers
sleep
fi
action "创建用户$name并将其加入visudo完成" /bin/true
echo "================================================="
echo ""
sleep
}
#Adjust the file descriptor(limits.conf)
initLimits(){
echo "===============加大文件描述符===================="
LIMIT=`grep nofile /etc/security/limits.conf |grep -v "^#"|wc -l`
if [ $LIMIT -eq ];then
\cp /etc/security/limits.conf /etc/security/limits.conf.$(date +%F)
echo '* - nofile 65535'>>/etc/security/limits.conf
fi
echo '#tail -1 /etc/security/limits.conf'
tail - /etc/security/limits.conf
ulimit -HSn
echo '#ulimit -n'
ulimit -n
action "配置文件描述符为65535" /bin/true
echo "================================================="
echo ""
sleep
}
#set ssh
initSsh(){
echo "======禁用GSSAPI来认证,也禁用DNS反向解析,加快SSH登陆速度======="
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
service sshd restart
action "禁用GSSAPI来认证,也禁用DNS反向解析,加快SSH登陆速度" /bin/true
echo "================================================="
echo ""
sleep
}
#set the control-alt-delete to guard against the miSUSE
initRestart(){
sed -i 's#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#' /etc/init/control-alt-delete.conf
action "将ctrl alt delete键进行屏蔽,防止误操作的时候服务器重启" /bin/true
echo "================================================="
echo ""
sleep
}
#Optimizing the system kernel
initSysctl(){
echo "================优化内核参数====================="
SYSCTL=`grep "net.ipv4.tcp" /etc/sysctl.conf |wc -l`
if [ $SYSCTL -lt ];then
\cp /etc/sysctl.conf /etc/sysctl.conf.$(date +%F)
cat >>/etc/sysctl.conf<<EOF
net.ipv4.tcp_fin_timeout =
net.ipv4.tcp_tw_reuse =
net.ipv4.tcp_tw_recycle =
net.ipv4.tcp_syncookies =
net.ipv4.tcp_keepalive_time =
net.ipv4.ip_local_port_range =
net.ipv4.tcp_max_syn_backlog =
net.ipv4.tcp_max_tw_buckets =
net.ipv4.route.gc_timeout =
net.ipv4.tcp_syn_retries =
net.ipv4.tcp_synack_retries =
net.core.somaxconn =
net.core.netdev_max_backlog =
net.ipv4.tcp_max_orphans =
net.netfilter.nf_conntrack_max =
net.netfilter.nf_conntrack_tcp_timeout_established =
net.netfilter.nf_conntrack_tcp_timeout_time_wait =
net.netfilter.nf_conntrack_tcp_timeout_close_wait =
net.netfilter.nf_conntrack_tcp_timeout_fin_wait =
EOF
fi
\cp /etc/rc.local /etc/rc.local.$(date +%F)
modprobe nf_conntrack
echo "modprobe nf_conntrack">> /etc/rc.local
modprobe bridge
echo "modprobe bridge">> /etc/rc.local
sysctl -p
action "内核调优完成" /bin/true
echo "================================================="
echo ""
sleep
}
#setting history and login timeout
initHistory(){
echo "======设置默认历史记录数和连接超时时间======"
echo "TMOUT=300" >>/etc/profile
echo "HISTSIZE=5" >>/etc/profile
echo "HISTFILESIZE=5" >>/etc/profile
tail - /etc/profile
source /etc/profile
action "设置默认历史记录数和连接超时时间" /bin/true
echo "================================================="
echo ""
sleep
}
#chattr file system
initChattr(){
echo "======锁定关键文件系统======"
chattr +i /etc/passwd
chattr +i /etc/inittab
chattr +i /etc/group
chattr +i /etc/shadow
chattr +i /etc/gshadow
/bin/mv /usr/bin/chattr /usr/bin/lock
action "锁定关键文件系统" /bin/true
echo "================================================="
echo ""
sleep
}
del_file(){
echo "======定时清理邮件任务======"
[ -f /server/scripts/ ] || mkdir -p /server/scripts/
echo "find /var/spool/postfix/maildrop/ -type f|xargs rm -f" >/server/scripts/del_file.sh
echo '#this is del mail task by kingle at 2018-8-8' >>/var/spool/cron/root
echo "*/1 * * * * /bin/bash /server/scripts/del_file.sh &>/dev/null" >>/var/spool/cron/root
echo "================================================="
echo ""
sleep
}
hide_info(){
echo "======!!隐藏系统信息!!======"
echo "======此项注意不要自己忘记了那就没救了======"
echo "======不建议使用======"
Version_information=`cat /etc/issue|grep "CentOS"`
>/etc/issue
>/etc/issue.net
if [ `cat /etc/issue|grep cent|wc -l` -eq -a `cat /etc/issue|grep cent|wc -l` -eq ];then
echo "======清除成功====="
else
>/etc/issue
>/etc/issue.net
fi
echo "$Version_information"
echo "=====认准本系统版本======"
sleep
echo "================================================="
}
grub_md5(){
echo "======grub_md5加密======"
echo "======命令行输入:/sbin/grub-md5-crypt 进行交互式加密======"
echo "把密码写入/etc/grub.conf 格式:password --MD5 密码"
echo ""
sleep
}
ban_ping(){
#内网可以ping 其他不能ping 这个由于自己也要ping测试不一定要设置
echo '#内网可以ping 其他不能ping 这个由于自己也要ping测试不一定要设置'
echo 'iptables -t filter -I INPUT -p icmp --icmp-type 8 -i eth0 -s 10.0.0.0/24 -j ACCEPT'
sleep
} #menu2
menu2(){
while true
do
clear
cat <<EOF
----------------------------------------
|****Please Enter Your Choice:[-]****|
----------------------------------------
() 新建一个用户并将其加入visudo
() 配置为国内YUM源镜像和保存YUM源文件
() 配置中文字符集
() 禁用SELINUX及关闭防火墙
() 精简开机自启动
() 去除系统及内核版本登录前的屏幕显示
() 修改ssh默认端口及禁用root远程登录
() 设置时间同步
() 安装系统补装工具(选择最小化安装minimal)
() 加大文件描述符
() 禁用GSSAPI来认证,也禁用DNS反向解析,加快SSH登陆速度
() 将ctrl alt delete键进行屏蔽,防止误操作的时候服务器重启
() 系统内核调优
() 设置默认历史记录数和连接超时时间
() 锁定关键文件系统
() 定时清理邮件任务
() 隐藏系统信息
() grub_md5加密
() ban_ping
() 返回上一级菜单 EOF
read -p "Please enter your Choice[0-15]: " input2
case "$input2" in
)
clear
break
;;
)
addUser
;;
)
configYum
;;
)
initI18n
;;
)
initFirewall
;;
)
initService
;;
)
initRemoval
;;
)
initSsh
;;
)
syncSysTime
;;
)
initTools
;;
)
initLimits
;;
)
initSsh
;;
)
initRestart
;;
)
initSysctl
;;
)
initHistory
;;
)
initChattr
;;
)
del_file
;;
)
hide_info
;;
)
grub_md5
;;
)
ban_ping
;;
*) echo "----------------------------------"
echo "| Warning!!! |"
echo "| Please Enter Right Choice! |"
echo "----------------------------------"
for i in `seq -w - `
do
echo -ne "\b\b$i";
sleep ;
done
clear
esac
done
}
#initTools
#menu
while true
do
clear
echo "========================================"
echo ' Linux Optimization '
echo "========================================"
cat << EOF
|-----------System Infomation-----------
| DATE :$DATE
| HOSTNAME :$HOSTNAME
| USER :$USER
| IP :$IPADDR
| DISK_USED :$DISK_SDA
| CPU_AVERAGE:$cpu_uptime
----------------------------------------
|****Please Enter Your Choice:[-]****|
----------------------------------------
() 一键优化
() 自定义优化
() 退出
EOF
#choice
read -p "Please enter your choice[0-3]: " input1
case "$input1" in
)
addUser
configYum
initI18n
initFirewall
initService
initRemoval
initSsh
syncSysTime
initTools
initLimits
initSsh
initRestart
initSysctl
initHistory
initChattr
;;
)
menu2
;;
)
clear
break
;;
*)
echo "----------------------------------"
echo "| Warning!!! |"
echo "| Please Enter Right Choice! |"
echo "----------------------------------"
for i in `seq -w - `
do
echo -ne "\b\b$i";
sleep ;
done
clear
esac
done
--- 借鉴于各路大神---
----致谢----
shell基础优化脚本的更多相关文章
- shell基础之脚本执行,命令别名以及快捷键等
脚本执行方式 比如我们在/root/下编写了一个脚本,名字为hello.sh.那么怎么调用执行它呢?有两种办法: (1)直接通过bash,如下: bash hello.sh 注:采用bash执行脚本 ...
- shell基础、变量、相关脚本
目录 一.shell基础 书写规范 引号 配置文件 read交互 脚本调式 小节总结 二.变量 变量类型 位置变量 状态变量 替换和删除 变量补充 变量运算 小节总结 三.相关脚本面试题 统计hist ...
- 《UNIX-Shell编程24学时教程》读书笔记Chap1,2 Shell基础,脚本基础
Chap1 Shell基础 知道该使用哪种命令是依赖于经验的.----惟手熟尔. 1.1 什么是命令 其实知道这些名词好像也没什么帮助,嘻嘻 1.2 什么是Shell 不同用户不同的提示符:不同的环境 ...
- Shell基础(一):Shell基础应用、简单Shell脚本的设计、使用Shell变量、变量的扩展应用
一.Shell基础应用 目标: 本案例要求熟悉Linux Shell环境的特点,主要练习以下操作: 1> 切换用户的Shell环境 2> 练习命令历史.命令别名 3 ...
- -Shell 教程 Bash 脚本 基础语法 MD
目录 目录 Shell 简介 Shell 脚本 Shell 环境 第一个shell脚本 Shell 变量 定义变量 使用变量 只读变量 删除变量 Shell 字符串 单引号 双引号 字符串基本操作 S ...
- Linux实战教学笔记17:精简shell基础
第十七节 精简shell基础 标签(空格分隔): Linux实战教学笔记 1,前言 1.1 为什么学习shell编程 Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具, ...
- Linu之linux系统基础优化和基本命令
Linux系统基础优化和基本命令 网络参数设定命令 ifconfig: 查询,设置网卡和ip等参数 ifup,ifdown: 脚本命令,更简单的方式 ip: 符合指令,直接修改上述功能 编辑网卡配置文 ...
- CentOS6.X 系统安装后的基础优化
特别说明:克隆之后的网卡修改 1 编辑eth0的配置文件:vi /etc/sysconfig/network-scripts/ifcfg-eth0, 删除HWADDR地址那一行及UUID的行如下: H ...
- 生产力工具:shell 与 Bash 脚本
生产力工具:shell 与 Bash 脚本 作者:吴甜甜 个人博客网站: wutiantian.github.io 注意:本文只是我个人总结的学习笔记,不适合0基础人士观看. 参考内容: 王顶老师 l ...
随机推荐
- MyEclipse2014配置2.5版本的struts2
原创 配置struts2一般来说需要以下步骤: 将项目所需要的Jar包导入项目webRoot/WEB-INF/lib下(包不追求多,容易导致冲突或者其他问题,需要多少导入多少) 配置struts.xm ...
- centos7 安装pip
首先安装 python3 安装过程1.安装相关依赖 1 sudo yum install openssl-devel -y 2 sudo yum install zlib-devel -y 2.安装s ...
- SQL计算时间差并排除周末
SQL计算时间差并排除周末 CREATE FUNCTION DI_FN_GET_WorkDay (@begin DATETIME , @end DATETIME ) RETURNS int BEGIN ...
- Docker 镜像的制作和使用
镜像 Layer(层) 镜像里的内容是按「层」来组织的,「层」可以复用,一个完整的镜像也可以看做是一个「层」.多个「层」叠加在一起就形成了一个新的镜像,这个镜像也可以作为别的镜像的基础「层」进行更加复 ...
- ST表略解
题面 给定一个长度为\(N\)的数列,和\(M\)次询问,求出每一次询问的区间内数字的最大值. 对于30%的数据,满足: \(1≤N,M≤10\) 对于70%的数据,满足: \(1≤N,M≤10^5\ ...
- linux内存布局------深入理解计算机系统
- 201621123012 《java程序设计》第2周学习总结
1. 本章学习总结 1.学习了java许多了基本语法,string函数,arraylist函数的各种功能方法和调用. 2.了解了除for循环之外的foreach循环 3.学习了各种数值类的包装 4.熟 ...
- React杂篇(1) -- 打包发布注意事项
打包后资源路径 问题:直接打包会出现一下情况: 方式一: package.json文件下加入homepage字段 { "name": "wap-v2", &qu ...
- innerText、innerHTML
innerTest修改的是标签的文本内容,如果修改的字符串具有标签的格式,不会把标签展示到页面中 innerHtml打印标签中所有子标签 以字符串的形式,如果修改的字符串有标签的格式,则直接展示到页面 ...
- luoguP4868 Preprefix sum
https://www.luogu.org/problemnew/show/P4868 线段树上加等差数列,基础区间修改单点查询 等差数列具有可加性,当在同一段区间内时,首项相加公差相加即可 #inc ...