Linux 系统优化脚本

简介: ##修改yum源,关闭selinux,关闭防火墙,关闭不必要的开机服务,添加用户,设置默认字符集UTF8##时间同步,设置超时时间、文件打开数、历史指令,内核优化,ssh优化,隐藏系统版本号,锁定重要文件#!/bin/bash#set env...

##修改yum源,关闭selinux,关闭防火墙,关闭不必要的开机服务,添加用户,设置默认字符集UTF8

##时间同步,设置超时时间、文件打开数、历史指令,内核优化,ssh优化,隐藏系统版本号,锁定重要文件


#!/bin/bash

#set env

export PATH=$PATH:/bin:/sbin:/usr/sbin

if [ "$UID" != "0" ]

then

echo "please run this script by root."

exit 1

fi

#define cmd var

SERVICE=`which service`

CHKCONFIG=`which chkconfig`

yum install -y wget;

#

修改yum源

function mod_yum(){

if [ -e /etc/yum.repos.d/CentOS-Base.repo ]

 then

  mv /etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.backup&&\

  wget -O/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-6.repo

fi

}

#

关闭selinux

function close_selinux(){

sed -i 's/SELINUX=enforcing/SELINUX=disabled' /etc/selinux/config

setenforce 0 &>/dev/null

}

#

关闭防火墙

function close_iptables(){

/etc/init.d/iptables stop

/etc/init.d/iptables stop

chkconfig iptables off

service firewalld stop

chkconfig firewalld off

}

#

关闭不必要的开机启动服务

function lease_service(){

chkconfig | awk '{print "chkconfig",$1,"off"}'|bash

chkconfig | egrep "crond|sshd|network|rsyslog|sysstat"|awk '{print"chkconfig",$1,"on"}'|bash

}

#

添加用户

function adduser(){

    #4.add hat and sudo

    if [ `grep -w hat /etc/passwd|wc -l`-lt 1 ]

      then

        useradd hat -g root

        echo geeboo|passwd --stdinhat

        \cp /etc/sudoers/etc/sudoers.ori

        echo "hat ALL=(ALL)NOPASSWD: ALL " >>/etc/sudoers

        tail -1 /etc/sudoers

        visudo -c&>/dev/null

    fi

}

#

设置默认字符集为中文

function charset(){

    #5.charset config

    cp /etc/sysconfig/i18n/etc/sysconfig/i18n.ori

    echo'LANG="zh_CN.UTF-8"' >/etc/sysconfig/i18n

    source /etc/sysconfig/i18n

    #echo $LANG

}

#

时间同步

function time_sync(){

    #6.time sync.

    cron=/var/spool/cron/root

    if [ `grep -w "ntpdate"$cron|wc -l` -lt 1  ]

      then

        echo "#time sync">>$cron

        echo "*/5 * * * */usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1">>$cron

        crontab -l

    fi

}

function com_line_set(){

    #7.command set.

    if [ `egrep"TMOUT|HISTSIZE|HISTFILESIZE" /etc/profile|wc -l` -ge 3  ]

      then

        echo "export TMOUT=300">>/etc/profile

        echo "exportHISTSIZE=5" >>/etc/profile

        echo "export HISTFILESIZE=5">>/etc/profile

        . /etc/profile

    fi

}

#

设置打开文件数

function open_file_set(){

    #8.increase open file.

    if [ `grep 65535/etc/security/limits.conf|wc -l` -lt 1 ]

      then

        echo "*               -       nofile          65535 ">>/etc/security/limits.conf

        tail -1/etc/security/limits.conf

    fi

}

function set_kernel(){

    #9.kernel set.

    if [ `grep kernel_flag/etc/sysctl.conf|wc -l` -lt 1 ]

      then

        cat>>/etc/sysctl.conf<

        #kernel_flag

        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 =1

        net.core.somaxconn = 16384

        net.core.netdev_max_backlog =16384

        net.ipv4.tcp_max_orphans =16384

        net.nf_conntrack_max =25000000

        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

    fi

}

#

优化SSH

function init_ssh(){

    cp /etc/ssh/sshd_config/etc/ssh/sshd_config.`date +"%Y-%m-%d_%H-%M-%S"`;

    sed -i "s%#Port 22%Port52113%" /etc/ssh/sshd_config;

    #sed -i "s%#PermitRootLoginyes%PermitRootLogin no%" /etc/ssh/sshd_config;

    sed -i "s%#PermitEmptyPasswordsno%PermitEmptyPasswords no%" /etc/ssh/sshd_config;

    sed -i "s%#UseDNS yes%UseDNSno%" /etc/ssh/sshd_config;

    sed -i "s%GSSAPIAuthenticationyes%GSSAPIAuthentication no%" /etc/ssh/sshd_config;

    sed -i "s%GSSAPIAuthenticationyes%GSSAPIAuthentication no%" /etc/ssh/sshd_config;

   #sed -i "$a\AllowUsers  hat" /etc/ssh/sshd_config;

   service sshd restart&>/dev/null;

   echo "sshd:192.168.10.0/24">> /etc/hosts.allow;

   echo "sshd:ALL" >>/etc/hosts.deny;

   iptables -I INPUT -p tcp --dport 52113-j DROP;

   iptables -I INPUT -p tcp --dport 52113-s 192.168.10.0/24 -j ACCEPT;

   iptables save;

}

function update_linux(){

    #10.upgrade linux.

    if [ `rpm -qa lrzsz nmap treedos2unix nc|wc -l` -le 3 ]

      then

        yum install wget lrzsz nmap treedos2unix nc -y

        #yum update -y

    fi

}

main(){

    mod_yum

    close_selinux

    close_iptables

    least_service

    adduser

    charset

    time_sync

    com_line_set

    open_file_set

    set_kernel

    init_ssh

    update_linux

#

隐藏系统版本号

#> /etc/issue

#> /etc/issue.net

#

锁定关系系统文件

#chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab

#

解锁chattr -i /etc/passwd /etc/shadow /etc/group/etc/gshadow /etc/inittab

#

修改chattr的名字mv /usr/bin/chattr/usr/bin/hat1

#

为grub加密

#/sbin/grub-md5-crypt

,生成密码,然后将密码加入/etc/grub.conf,password --md5 #密码

#

禁止被ping net.ipv4.icmp_echo_ignore_all=1

}

main

目录
相关文章
|
2月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
412 9
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
288 0
Linux系统初始化脚本
|
2月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
298 2
|
4月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
536 1
|
6月前
|
Java Linux
自定义linux脚本用于快速jar包启动、停止、重启
自定义linux脚本用于快速jar包启动、停止、重启
323 29
|
6月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
195 4
|
6月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
397 3
|
7月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
618 25
|
7月前
|
安全 Linux
阿里云linux服务器使用脚本通过安全组屏蔽异常海外访问ip
公网网站可能会遭受黑客攻击导致访问异常,使用此脚本可以屏蔽掉异常IP 恢复访问。也可自行设置定时任务定期检测屏蔽。
597 28
|
安全 Linux Shell
Linux上执行内存中的脚本和程序
【9月更文挑战第3天】在 Linux 系统中,可以通过多种方式执行内存中的脚本和程序:一是使用 `eval` 命令直接执行内存中的脚本内容;二是利用管道将脚本内容传递给 `bash` 解释器执行;三是将编译好的程序复制到 `/dev/shm` 并执行。这些方法虽便捷,但也需谨慎操作以避免安全风险。
402 7
下一篇
oss云网关配置