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

目录
相关文章
|
1月前
|
安全 Linux Shell
Linux上执行内存中的脚本和程序
【9月更文挑战第3天】在 Linux 系统中,可以通过多种方式执行内存中的脚本和程序:一是使用 `eval` 命令直接执行内存中的脚本内容;二是利用管道将脚本内容传递给 `bash` 解释器执行;三是将编译好的程序复制到 `/dev/shm` 并执行。这些方法虽便捷,但也需谨慎操作以避免安全风险。
134 6
|
2月前
|
Linux
在Linux中,怎么把脚本添加到系统服务里,即用 service 来调用?
在Linux中,怎么把脚本添加到系统服务里,即用 service 来调用?
|
8天前
|
运维 Java Linux
【运维基础知识】Linux服务器下手写启停Java程序脚本start.sh stop.sh及详细说明
### 启动Java程序脚本 `start.sh` 此脚本用于启动一个Java程序,设置JVM字符集为GBK,最大堆内存为3000M,并将程序的日志输出到`output.log`文件中,同时在后台运行。 ### 停止Java程序脚本 `stop.sh` 此脚本用于停止指定名称的服务(如`QuoteServer`),通过查找并终止该服务的Java进程,输出操作结果以确认是否成功。
14 1
|
1月前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
87 2
|
15天前
|
存储 Shell Linux
【Linux】shell基础,shell脚本
Shell脚本是Linux系统管理和自动化任务的重要工具,掌握其基础及进阶用法能显著提升工作效率。从简单的命令序列到复杂的逻辑控制和功能封装,Shell脚本展现了强大的灵活性和实用性。不断实践和探索,将使您更加熟练地运用Shell脚本解决各种实际问题
12 0
|
1月前
|
Shell Linux 开发工具
linux shell 脚本调试技巧
【9月更文挑战第3天】在Linux中调试shell脚本可采用多种技巧:使用`-x`选项显示每行命令及变量扩展情况;通过`read`或`trap`设置断点;利用`echo`检查变量值,`set`显示所有变量;检查退出状态码 `$?` 进行错误处理;使用`bashdb`等调试工具实现更复杂调试功能。
|
2月前
|
安全 Linux 开发工具
探索Linux操作系统:从命令行到脚本编程
【8月更文挑战第31天】在这篇文章中,我们将一起潜入Linux操作系统的海洋,从最基础的命令行操作开始,逐步深入到编写实用的脚本。无论你是初学者还是有一定经验的开发者,这篇文章都将为你提供新的视角和实用技能。我们将通过实际代码示例,展示如何在日常工作中利用Linux的强大功能来简化任务和提高效率。准备好了吗?让我们一起开启这段旅程,探索Linux的奥秘吧!
|
2月前
|
监控 数据挖掘 Linux
Linux服务器PBS任务队列作业提交脚本的使用方法
【8月更文挑战第21天】在Linux服务器环境中,PBS任务队列能高效管理及调度计算任务。首先需理解基本概念:作业是在服务器上执行的任务;队列则是等待执行任务的列表,具有不同的资源限制与优先级;节点指分配给作业的计算资源。
252 4
|
2月前
|
存储 Linux Shell
在Linux中,如何使用脚本,实现判断 192.168.1.0/24 网络里,当前在线的 IP 有哪些?能ping 通则 认为在线。
在Linux中,如何使用脚本,实现判断 192.168.1.0/24 网络里,当前在线的 IP 有哪些?能ping 通则 认为在线。
|
2月前
|
Java Shell Linux
【Linux入门技巧】新员工必看:用Shell脚本轻松解析应用服务日志
关于如何使用Shell脚本来解析Linux系统中的应用服务日志,提供了脚本实现的详细步骤和技巧,以及一些Shell编程的技能扩展。
47 0
【Linux入门技巧】新员工必看:用Shell脚本轻松解析应用服务日志