linux系统优化脚本实现

脚本内容在文档的底部,将脚本后缀改为.sh,放到系统(CentOS6.X)里直接执行即可,有不能执行的麻烦告诉我,多谢!

#!/bin/bash

. /etc/init.d/functions

if [ "$UID" -ne 0 ];then
  echo "you should change to root ,then run this script,please enter the root password:"
  su - root
fi

RETVAL=0
DIR="/tmp/result_${SERVER_IP}.txt"

read -p "please enter your server ip:" SERVER_IP

result() {
 [ $RETVAL -eq 0 ] && echo "$1 is ok." >> ${DIR} || echo "$1 is false." >> ${DIR}
}

create_user() {
  id nginx &>/dev/null
  [ $? -ne 0 ] || userdel -r nginx >/dev/null 2>&1
  groupadd -g 501 nginx >/dev/null 2>&1
  useradd -u 501 -r -g nginx -s /sbin/nologin nginx >/dev/null 2>&1
  RETVAL=$?
  result create_user
}

fstab_opt() {
  sed -i -e ‘/\/tmp/s/defaults/defaults,nosuid,noexec,nodev/‘ -e ‘/\/home/s/defaults/defaults,nosuid,nodev/‘ -e ‘/\/var/s/defaults/defaults,nosuid/‘ /etc/fstab
  RETVAL=$?
  result fstab_opt
}

time_opt() {
  sed -i ‘s#^#\##g‘ /etc/sysconfig/clock && sed -i ‘1i ZONE="Asia/Shanghai"‘ /etc/sysconfig/clock && \cp -af /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  RETVAL=$?
  result time_opt
}

ntp_opt() {
  echo "*/5 * * * *  /usr/sbin/ntpdate time.sfbest.bj" >/tmp/ntpdate && crontab /tmp/ntpdate && rm -rf /tmp/ntpdate
  RETVAL=$?
  result ntp_opt
}

passwd1_opt() {
  sed -i ‘/^PASS/ s#^#\##g‘ /etc/login.defs && echo -e "PASS_MAX_DAYS 180\nPASS_MIN_DAYS 1\nPASS_MIN_LEN 8\nPASS_WARN_AGE 7\n" >> /etc/login.defs
  RETVAL=$?
  result passwd1_opt
}

passwd2_opt() {
  sed -ir ‘/pam_cracklib.so/ s#^.*$#password    requisite     pam_cracklib.so try_first_pass retry=3 type= ifok=3 minlen=10 ucredit=-1 lcredit=-3 dvredit=-3 dictpath=/usr/share/cracklib/pw_dict#g‘ /etc/pam.d/system-auth
  RETVAL=$?
  result passwd2_opt
}

passwd3_opt() {
  grep ‘remember‘ /etc/pam.d/system-auth &>/dev/null
  if [ $? -eq 0 ];then
    sed -n ‘/remember/p‘ /etc/pam.d/system-auth >> ${DIR}
  else
    sed -i ‘/password    sufficient    pam_unix.so md5/ s#$# remember=3#g‘ /etc/pam.d/system-auth
  fi
  RETVAL=$?
  result passwd3_opt
}

pamd_sshd_opt() {
  sed -i ‘/#%PAM-1.0/a\auth       required     pam_listfile.so item=user sense=allow file=/etc/ssh/sshusers onerr=succeed‘ /etc/pam.d/sshd
  echo sa > /etc/ssh/sshusers
  sed -i ‘s/\(^wheel.*\)/\1,sa/‘ /etc/group
  sed -i ‘/^#auth.* use_uid$/a auth           required        pam_wheel.so use_uid‘ /etc/pam.d/su
  RETVAL=$?
  result pamd_sshd_opt
}

#pam_tally2_opt() {
 # find /lib* -name "pam_tally2.so" &>/dev/null
  #if [ $? -ne 0 ];then
   # echo "pam_tally2.so is no exsit." >> ${DIR}
  #else
   # grep ‘pam_tally2.so‘ /etc/pam.d/sshd &>/dev/null
    #[ $? -eq 0 ] && sed -n ‘/pam_tally2.so/p‘ /etc/pam.d/sshd >> ${DIR} || sed -i ‘1a auth       required     pam_tally2.so deny=3 unlock_time=300‘ /etc/pam.d/sshd
  #fi
  #RETVAL=$?
  #result pam_tally2_opt
#}

ssh_opt() {
  sed -i ‘s#\#PermitRootLogin yes#PermitRootLogin no#g‘ /etc/ssh/sshd_config && sed -i ‘s#\#Port 22#Port 9880#g‘ /etc/ssh/sshd_config && sed -i ‘s#\#ListenAddress 0.0.0.0#ListenAddress ‘$SERVER_IP‘#g‘ /etc/ssh/sshd_config && sed -i ‘s#\#UseDNS yes#UseDNS no#g‘ /etc/ssh/sshd_config && echo "export TMOUT=300" >> /etc/profile && . /etc/profile
  RETVAL=$?
  result ssh_opt
}

issue_opt() {
  cat /etc/issue >> ${DIR} && >/etc/issue && >/etc/issue.net
  RETVAL=$?
  result issue_opt
}

chattr_file_opt() {
  chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab && mv /usr/bin/chattr /etc/zchattr && echo "chattr moved to /etc/zchattr" >> ${DIR}
  RETVAL=$?
  result chattr_file_opt
}

ctr_opt() {
  sed -i ‘s#exec#\#exec#g‘ /etc/init/control-alt-delete.conf
  RETVAL=$?
  result ctr_opt
}

history_opt() {
  echo -e "export HISTFILESIZE=5\nexport HISTSIZE=5" >> /etc/profile && . /etc/profile
  RETVAL=$?
  result history_opt
}

selinux_iptables_opt() {
  sed -i ‘s#^SELINUX=.*$#SELINUX=disabled#g‘ /etc/selinux/config && setenforce 0 &>/dev/null
  /etc/init.d/iptables stop && chkconfig iptables off
  RETVAL=$?
  result selinux_iptables_opt
}

sysctl_opt() {
  echo -ne "
net.ipv4.tcp_max_syn_backlog = 655350000
net.core.netdev_max_backlog =  327680000
net.core.somaxconn = 327680
net.core.wmem_default = 838860800
net.core.rmem_default = 838860800
net.core.rmem_max = 167772160
net.core.wmem_max = 167772160
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_max_tw_buckets = 180
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 10
net.ipv4.ip_local_port_range = 1024  65535
vm.swappiness = 10
net.nf_conntrack_max = 6553500
net.netfilter.nf_conntrack_max = 6553500
net.netfilter.nf_conntrack_tcp_timeout_established = 300
" >>/etc/sysctl.conf
  sysctl -p &>/dev/null
  RETVAL=$?
  result sysctl_opt
}

fs_file_opt() {
  echo -ne "
* soft nofile 65535
* hard nofile 65535
" >>/etc/security/limits.conf
  echo -ne "
* soft nproc  65535
* hard nproc  65535
" >>/etc/security/limits.d/90-nproc.conf 
  sysctl -p &>/dev/null
  RETVAL=$?
  result fs_file_opt
}

yum_opt() {
  rm -rf /etc/yum.repos.d/*
  cd /etc/yum.repos.d/
  for i in [yum] name=yum ‘baseurl=http://yum.sfbest.bj/centos/$releasever/os/$basearch/‘ enable=1 gpgcheck=0; do echo $i >> yum.repo;done
  RETVAL=$?
  result yum_opt
}

lang_opt() {
  sed -i ‘s#^LANG#\#LANG#g‘ /etc/sysconfig/i18n && sed -i ‘1i LANG="zh_CN.UTF-8"‘ /etc/sysconfig/i18n
  RETVAL=$?
  result lang_opt
}

sys_server_opt() {
  chkconfig --list|awk ‘{print $1}‘|xargs -i chkconfig {} --level 0123456 off
  for a in auditd  crond irqbalance  network psacct rsyslog sshd sysstat; do chkconfig --level 2345 $a on; done
  RETVAL=$?
  result sys_server_opt
}

postfix_opt() {
  [ -d /server/scripts ] || mkdir -p /server/scripts
  echo "tmpwatch -afv 30d /var/spool/postfix/maildrop/" > /server/scripts/delete_mail.sh
  echo "00 00 01 * * /bin/sh /server/scripts/delete_mail.sh &>/dev/null" >> /var/spool/cron/root
  RETVAL=$?
  result postfix_opt
}

other_opt() {
  rm -rf /root/*
  chmod 0700 /usr/bin/passwd

}

main() {
  create_user
  fstab_opt
  time_opt
  ntp_opt
  passwd1_opt
  passwd2_opt
  passwd3_opt
  pamd_sshd_opt
  ssh_opt
  issue_opt
  chattr_file_opt
  ctr_opt
  history_opt
  selinux_iptables_opt
  sysctl_opt
  fs_file_opt
  yum_opt
  lang_opt
  sys_server_opt
  postfix_opt
  other_opt
}

main

时间: 2024-08-24 02:54:17

linux系统优化脚本实现的相关文章

Linux系统优化脚本

#!/bin/bash  #this script is only for CentOS 6 #check the OS platform=`uname -i` if [ $platform != "x86_64" ];then echo "this script is only for 64bit Operating System !" exit 1 fi echo "the platform is ok" version=`lsb_relea

常用Linux系统优化脚本

#!/bin/sh # 服务优化,(sshd.network.crond.syslog.rsyslog)服务保持默认开机启动 ServiceList=$(chkconfig --list | grep '0' | awk '{print $1}' | grep -Ev 'sshd|network|crond|syslog') for Service in $ServiceList do     /etc/init.d/$Service stop     chkconfig --level 012

Linux Bash脚本基本语法知识

写在前面:如果此文有幸被某位朋友看见并发现有错的地方,希望批评指正.如有不明白的地方,愿可一起探讨. 前提:读者能够基本使用一款文本编辑器以及了解文件的权限概念. 准备工作 在/home目录创建script文件用于保存所有的脚本程序: # mkdir /home/script # cd /home/script/ Bash脚本的基本输入输出 第一个bash脚本: # vim ./hello-world.sh 说明: "#!"是一个约定标记,它告诉系统这个脚本需要什么解释器来执行 作为可

Linux Shell脚本攻略(1.10)

1.10 获取.设置日期和延时 很多应用程序需要以不同的格式打印日期.设置日期和时间.根据日期和时间执行某项操作.延时通常用于在程序执行过程中提供一段等待时间(比如1秒).同样的,我们也能够一多种格式打印日期,或者在命令行中设置日期.在类Unix系统中,日期被存储为一个整数,其大小为自世界标准时间起所流逝的秒数.这种计时方式称为纪元时或Unix时间. 1.10.1 获取.设置时间 以下的程序给出了多种用法: #!/bin/bash start=$(date +%s) #获取纪元时间 date #

Linux Shell脚本攻略(1.8)

1.8 使用别名 linux中的别名就相当于windows中的快捷方式,使用别名可以省去用户输入一长串命令序列的麻烦. 1.8.1 创建临时别名(快捷方式) alias new_command='command sequence' #格式说明 alias install='sudo apt-get install' #实例说明 在声明 install='sudo apt-get install'之后,就可以用install代替'sudo apt-get install'了.使用这种方式声明的别名

Linux shell脚本基础学习详细介绍(完整版)一

Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Linux 脚本编写基础◆1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编辑好脚本时,如果要执行该脚本,还必须使其可执行. 要使脚本可执

Linux shell脚本基础学习详细介绍(完整版)二

详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续. Linux shell脚本基础已经被分成好几个部分了,这里对控制流程的内容也就马上讲完了,这是最后一部分关于here document,这里举例稍微有点复杂,我们慢慢来分析这个复杂Linux shell脚本. 6. Here documents 当要将几行文字传递给一个命令时,here docu

Linux shell脚本流程控制

博主搬家至51CTO,初来乍到,请多指教. 此次我们来通过实例解析Linux shell脚本流程控制 Linux shell脚本流程控制可分为三类:顺序执行,条件选择执行,循环执行 顺序执行:简单理解就是逐行执行脚本内容,逐行解读,逐行执行.(此处不做实例解析) 条件选择执行:可以理解为先进行某一条件的判断选择,再决定执行怎样的脚本内容.常见语句if case 条件选择语句:if if语句用法: 单分支 if 判断条件;then 条件为真的分支代码 fi 双分支 if 判断条件; then 条件

Linux shell脚本中shift的用法说明

Linux shell脚本中shift的用法说明 shift命令用于对参数的移动(左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理(常见于Linux中各种程序的启动脚本). 示例1:依次读取输入的参数并打印参数个数: run.sh: #!/bin/bash while [ $# != 0 ];do echo "第一个参数为:$1,参数个数为:$#" shift done 输入如下命令运行:run.sh a b c d e f 结果显示如下: 第一个参数为:a