1. Introduction
As an operation and maintenance person, we often deploy operating systems for various purposes. However, in these tasks, we will find that many tasks are actually repetitive labor, and the content of the operations is similar. Based on this type of work, In this case, we can make the same operation into a script for unified execution, and different things can be entered manually as variables. Can’t the time saved be used to do more meaningful things?Recently, I found a relatively easy-to-use shell source code based on the recommendation of fans. I adapted it based on this and shared it with everyone.
2. Menu
Main menu:
Secondary menu:
# Mainly realizes various optimizations of the system, such as commonly used functions such as modifying character sets, turning off selinux, turning off firewalls, installing commonly used tools, and accelerating SSH login. .
牛逼啊!接私活必备的 N 个开源项目!赶快收藏吧
3. Source code
#!/bin/sh . /etc/rc.d/init.d/functions export 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. 关闭selinux 3. 关闭firewalld 4. 精简开机启动 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 <<eof LANG="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.关闭selinux selinuxset() { 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.关闭firewalld firewalldset() { 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<<eof net.ipv4.tcp_fin_timeout = 2 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_keepalive_time = 600 net.ipv4.ip_local_port_range = 4000 65000 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.route.gc_timeout = 100 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 0 net.core.somaxconn = 16384 net.core.netdev_max_backlog = 16384 net.ipv4.tcp_max_orphans = 16384 net.netfilter.nf_conntrack_max = 25000000 net.netfilter.nf_conntrack_tcp_timeout_established = 180 net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120 net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60 net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120 EOF 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 $*
Save it as init.sh, then grant execution permission and execute it.
chmod +x init.sh && ./init.sh
If it is troublesome to copy and paste back and forth like this, you can also execute it through my one-click command, which can also achieve the above effect:
bash -c "$(curl -L s.aaa.al/init.sh)"
Finally, if you have any functions you want to implement, It can also be modified and implemented based on the original script.
The above is the detailed content of CentOS 7 system optimization script. For more information, please follow other related articles on the PHP Chinese website!

查版本号的命令:1、“cat /etc/issue”或“cat /etc/redhat-release”,可输出centos版本号;2、“cat /proc/version”、“uname -a”或“uname -r”,可输出内核版本号。

centos重启网卡的方法:1、对于centos6的网卡重启命令是“service network restart”;2、对于centos7的网卡重启命令是“systemctl restart network”。

centos php安装opcache的方法:1、执行“yum list php73* | grep opcache”命令;2、通过“yum install php73-php-opcache.x86_64”安装opcache;3、使用“find / -name opcache.so”查找“opcache.so”的位置并将其移动到php的扩展目录即可。

centos离线安装mysql的方法:1、将lib中的所有依赖上传到linux中,并用yum命令进行安装;2、解压MySQL并把文件复制到想要安装的目录;3、修改my.cnf配置文件;4、复制启动脚本到资源目录并修改启动脚本;5、将mysqld服务加入到系统服务里面;6、将mysql客户端配置到环境变量中,并使配置生效即可。

centos7安装不出现界面的解决办法:1、选择“Install CentOS 7”,按“e”进入启动引导界面;2、 将“inst.stage2=hd:LABEL=CentOS\x207\x20x86_64”改为“linux dd”;3、重新进入“Install CentOS 7”,按“e”将“hd:”后的字符替换成“/dev/sdd4”,然后按“Ctrl+x”执行即可。

centos删除php的方法:1、通过“#rpm -qa|grep php”命令查看全部php软件包;2、通过“rpm -e”命令卸载相应的依赖项;3、重新使用“php -v”命令查看版本信息即可。

我们的PC中有一个磁盘驱动器专门用于所有与Windows操作系统相关的安装。该驱动器通常是C驱动器。如果您还在PC的C盘上安装了最新的Windows11操作系统,那么所有系统更新(很可能是您安装的所有软件)都会将其所有文件存储在C盘中。因此,保持此驱动器没有垃圾文件并在C驱动器中拥有足够的存储空间变得非常重要,因为该驱动器拥有的空间越多,您的Windows11操作系统运行起来就越顺畅。但是您可以在磁盘驱动器上增加多少空间以及可以删除多少文件是有限制的。在这种情况下,

方法:1、利用“vim ~/.bashrc”编辑用户目录(~)下的“.bashrc”文件;2、在文件内添加“alias ls="ls --color"”;3、利用“:wq!”命令保存文件内的更改;4、“exit”命令退出终端后重新连接即可。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version
