您好,欢迎访问一九零五行业门户网

CentOS 7 系统优化脚本

一、介绍作为一名运维,经常会部署各种用途的操作系统,但在这些工作中,我们会发现很多工作其实是重复性的劳动,操作的内容也是大同小异,基于这类情况,我们可以把相同的操作做成统一执行的脚本,不同的东西作为变量手动输入。节约下来的时间不就可以做更多有意义的事情吗?
最近在粉丝有推荐下发现一款比较好用的shell源码,也基于此改编了一下,分享给大家。
二、菜单主菜单:
二级菜单:
主要实现系统的各类优化,比如常用的修改字符集、关闭selinux、关闭防火墙、安装常用工具和加快ssh登录等功能。
牛逼啊!接私活必备的 n 个开源项目!赶快收藏吧
三、源码#!/bin/sh. /etc/rc.d/init.d/functionsexport lang=zh_cn.utf-8#一级菜单menu1(){ clear cat <<eof----------------------------------------|**** 欢迎使用cetnos7.9优化脚本 ****||**** 博客地址: aaa.al ****|----------------------------------------1. 一键优化2. 自定义优化3. 退出eof read -p "please enter your choice[1-3]:" num1}#二级菜单menu2(){ clear cat <<eof----------------------------------------|****please enter your choice:[0-13]****|----------------------------------------1. 修改字符集2. 关闭selinux3. 关闭firewalld4. 精简开机启动5. 修改文件描述符6. 安装常用工具及修改yum源7. 优化系统内核8. 加快ssh登录速度9. 禁用ctrl+alt+del重启10.设置时间同步11.history优化12.返回上级菜单13.退出eof read -p "please enter your choice[1-13]:" num2}#1.修改字符集localeset(){ echo "========================修改字符集=========================" cat > /etc/locale.conf <<eoflang="zh_cn.utf-8"#lang="en_us.utf-8"sysfont="latarcyrheb-sun16"eof source /etc/locale.conf echo "#cat /etc/locale.conf" cat /etc/locale.conf action "完成修改字符集" /bin/true echo "===========================================================" sleep 2}#2.关闭selinuxselinuxset() { selinux_status=`grep "selinux=disabled" /etc/sysconfig/selinux | wc -l` echo "========================禁用selinux========================" if [ $selinux_status -eq 0 ];then sed -i "s#selinux=enforcing#selinux=disabled#g" /etc/sysconfig/selinux setenforce 0 echo '#grep selinux=disabled /etc/sysconfig/selinux' grep selinux=disabled /etc/sysconfig/selinux echo '#getenforce' getenforce else echo 'selinux已处于关闭状态' echo '#grep selinux=disabled /etc/sysconfig/selinux' grep selinux=disabled /etc/sysconfig/selinux echo '#getenforce' getenforce fi action "完成禁用selinux" /bin/true echo "===========================================================" sleep 2}#3.关闭firewalldfirewalldset(){ echo "=======================禁用firewalld========================" systemctl stop firewalld.service &> /dev/null echo '#firewall-cmd --state' firewall-cmd --state systemctl disable firewalld.service &> /dev/null echo '#systemctl list-unit-files | grep firewalld' systemctl list-unit-files | grep firewalld action "完成禁用firewalld,生产环境下建议启用!" /bin/true echo "===========================================================" sleep 5}#4.精简开机启动chkset(){ echo "=======================精简开机启动========================" systemctl disable auditd.service systemctl disable postfix.service systemctl disable dbus-org.freedesktop.networkmanager.service echo '#systemctl list-unit-files | grep -e "auditd|postfix|dbus-org\.freedesktop\.networkmanager"' systemctl list-unit-files | grep -e "auditd|postfix|dbus-org\.freedesktop\.networkmanager" action "完成精简开机启动" /bin/true echo "===========================================================" sleep 2}#5.修改文件描述符limitset(){ echo "======================修改文件描述符=======================" echo '* - nofile 65535'>/etc/security/limits.conf ulimit -shn 65535 echo "#cat /etc/security/limits.conf" cat /etc/security/limits.conf echo "#ulimit -sn ; ulimit -hn" ulimit -sn ; ulimit -hn action "完成修改文件描述符" /bin/true echo "===========================================================" sleep 2}#6.安装常用工具及修改yum源yumset(){ echo "=================安装常用工具及修改yum源===================" yum install wget -y &> /dev/null if [ $? -eq 0 ];then cd /etc/yum.repos.d/ \cp centos-base.repo centos-base.repo.$(date +%f) ping -c 1 mirrors.aliyun.com &> /dev/null if [ $? -eq 0 ];then wget -o /etc/yum.repos.d/centos-base.repo http://mirrors.aliyun.com/repo/centos-7.repo &> /dev/null yum clean all &> /dev/null yum makecache &> /dev/null else echo "无法连接网络" exit $? fi else echo "wget安装失败" exit $? fi yum -y install ntpdate lsof net-tools telnet vim lrzsz tree nmap nc sysstat &> /dev/null action "完成安装常用工具及修改yum源" /bin/true echo "===========================================================" sleep 2}#7. 优化系统内核 #另外,搜索公众号技术社区后台回复“壁纸”,获取一份惊喜礼包。kernelset(){ echo "======================优化系统内核=========================" chk_nf=`cat /etc/sysctl.conf | grep conntrack |wc -l` if [ $chk_nf -eq 0 ];then cat >>/etc/sysctl.conf<<eofnet.ipv4.tcp_fin_timeout = 2net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_syncookies = 1net.ipv4.tcp_keepalive_time = 600net.ipv4.ip_local_port_range = 4000 65000net.ipv4.tcp_max_syn_backlog = 16384net.ipv4.tcp_max_tw_buckets = 36000net.ipv4.route.gc_timeout = 100net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_synack_retries = 0net.core.somaxconn = 16384net.core.netdev_max_backlog = 16384net.ipv4.tcp_max_orphans = 16384net.netfilter.nf_conntrack_max = 25000000net.netfilter.nf_conntrack_tcp_timeout_established = 180net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120eof sysctl -p else echo "优化项已存在。" fi action "内核调优完成" /bin/true echo "===========================================================" sleep 2}#8.加快ssh登录速度sshset(){ echo "======================加快ssh登录速度======================" sed -i 's#^gssapiauthentication yes$#gssapiauthentication no#g' /etc/ssh/sshd_config sed -i 's/#usedns yes/usedns no/g' /etc/ssh/sshd_config systemctl restart sshd.service echo "#grep gssapiauthentication /etc/ssh/sshd_config" grep gssapiauthentication /etc/ssh/sshd_config echo "#grep usedns /etc/ssh/sshd_config" grep usedns /etc/ssh/sshd_config action "完成加快ssh登录速度" /bin/true echo "===========================================================" sleep 2}#9. 禁用ctrl+alt+del重启restartset(){ echo "===================禁用ctrl+alt+del重启====================" rm -rf /usr/lib/systemd/system/ctrl-alt-del.target action "完成禁用ctrl+alt+del重启" /bin/true echo "===========================================================" sleep 2}#10. 设置时间同步ntpdateset(){ echo "=======================设置时间同步========================" yum -y install ntpdate &> /dev/null if [ $? -eq 0 ];then /usr/sbin/ntpdate time.windows.com echo "*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null" >> /var/spool/cron/root else echo "ntpdate安装失败" exit $? fi action "完成设置时间同步" /bin/true echo "===========================================================" sleep 2}#11. history优化historyset(){ echo "========================history优化========================" chk_his=`cat /etc/profile | grep histtimeformat |wc -l` if [ $chk_his -eq 0 ];then cat >> /etc/profile <<'eof'#设置history格式export histtimeformat="[%y-%m-%d %h:%m:%s] [`whoami`] [`who am i|awk '{print $nf}'|sed -r 's#[()]##g'`]: "#记录shell执行的每一条命令export prompt_command='\if [ -z "$old_pwd" ];then export old_pwd=$pwd;fi;if [ ! -z "$last_cmd" ] && [ "$(history 1)" != "$last_cmd" ]; then logger -t `whoami`_shell_dir "[$old_pwd]$(history 1)";fi;export last_cmd="$(history 1)";export old_pwd=$pwd;'eof source /etc/profile else echo "优化项已存在。" fi action "完成history优化" /bin/true echo "===========================================================" sleep 2}#控制函数main(){ menu1 case $num1 in 1) localeset selinuxset firewalldset chkset limitset yumset kernelset sshset restartset ntpdateset historyset ;; 2) menu2 case $num2 in 1) localeset ;; 2) selinuxset ;; 3) firewalldset ;; 4) chkset ;; 5) limitset ;; 6) yumset ;; 7) kernelset ;; 8) sshset ;; 9) restartset ;; 10) ntpdateset ;; 11) historyset ;; 12) main ;; 13) exit ;; *) echo 'please select a number from [1-13].' ;; esac ;; 3) exit ;; *) echo 'err:please select a number from [1-3].' sleep 3 main ;; esac}main $*
将其保存为init.sh,然后赋予执行权限后执行即可。
chmod +x init.sh && ./init.sh
如果这样来回地复制粘贴很麻烦,也可以通过我的一键命令执行,同样能达到上面的效果:
bash -c "$(curl -l s.aaa.al/init.sh)"
最后,如果大家有想实现的功能,也可以在原有脚本的基础上进行修改实现。
以上就是centos 7 系统优化脚本的详细内容。
其它类似信息

推荐信息