前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS 6.X 系统初始化脚本

CentOS 6.X 系统初始化脚本

作者头像
shaonbean
发布2019-05-26 16:11:57
5540
发布2019-05-26 16:11:57
举报
文章被收录于专栏:运维前线运维前线

版权声明:本文为木偶人shaon原创文章,转载请注明原文地址,非常感谢。 https://cloud.tencent.com/developer/article/1435745

代码语言:txt
复制
          #!/bin/bash 

#################################################

--Info

Initialization CentOS 6.x script

#################################################

Changelog

20160601 shaonbean initial creation

#################################################

Auther: hwang@aniu.tv

#################################################

Check if user is root

if $(id -u) != "0" ; then

代码语言:txt
复制
 echo "Error: You must be root to run this script, please use root to initialization OS"
代码语言:txt
复制
 exit 1

fi

echo "+------------------------------------------------------------------------+"

echo "| To initialization the system for security and performance |"

echo "+------------------------------------------------------------------------+"

#check host && network

check_hosts()

{

代码语言:txt
复制
 hostname=`hostname`
代码语言:txt
复制
 if grep -Eqi '^127.0.0.1[[:space:]]\*localhost' /etc/hosts; then
代码语言:txt
复制
     echo "Hosts: ok."
代码语言:txt
复制
 else
代码语言:txt
复制
     echo "127.0.0.1 localhost.localdomain $hostname" >> /etc/hosts
代码语言:txt
复制
 fi
代码语言:txt
复制
 ping -c1 www.aniu.tv
代码语言:txt
复制
 if [ $? -eq 0 ] ; then
代码语言:txt
复制
     echo "DNS...ok"
代码语言:txt
复制
     echo "nameserver 8.8.8.8" >> /etc/resolv.conf
代码语言:txt
复制
 else
代码语言:txt
复制
     echo "DNS...fail"
代码语言:txt
复制
     echo -e "nameserver 8.8.8.8\nnameserver 114.114.114.114" > /etc/resolv.conf
代码语言:txt
复制
 fi

}

#Set time zone synchronization

set_timezone()

{

代码语言:txt
复制
 echo "Setting timezone..."
代码语言:txt
复制
 rm -rf /etc/localtime
代码语言:txt
复制
 ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
代码语言:txt
复制
 #install ntp
代码语言:txt
复制
 echo "[+] Installing ntp..."
代码语言:txt
复制
 yum install ntpdate -y
代码语言:txt
复制
 /usr/sbin/ntpdate pool.ntp.org
代码语言:txt
复制
 echo '\*/5 \* \* \* \* /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1' > /var/spool/cron/root;chmod 600 /var/spool/cron/root
代码语言:txt
复制
 /sbin/service crond restart

}

#update os

update(){

代码语言:txt
复制
 yum -y update 

change yum source

cd /etc/yum.repos.d/

mkdir bak

mv ./*.repo bak

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

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

yum clean all && yum makecache

代码语言:txt
复制
 yum -y install wget vim unzip openssl-devel gcc gcc-c++ sysstat iotop openssh-clients telnet lsof
代码语言:txt
复制
 echo "yum update && yum install common command ......... succeed."

}

selinux()

{

代码语言:txt
复制
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
代码语言:txt
复制
    setenforce 0
代码语言:txt
复制
    echo "disbale selinux ..................succeed."

}

#xen_hwcap_setting()

#{

if -s /etc/ld.so.conf.d/libc6-xen.conf ; then

sed -i 's/hwcap 1 nosegneg/hwcap 0 nosegneg/g' /etc/ld.so.conf.d/libc6-xen.conf

fi

#}

#Modify file open number,define 1024

/etc/security/limits.conf

limits_config()

{

cat >> /etc/security/limits.conf <<EOF

* soft nproc 65535

* hard nproc 65535

* soft nofile 8192

* hard nofile 8192

EOF

#ulimit -n 8192

echo "ulimit -SHn 65535" >> /etc/rc.local

}

ulimit -n 8192

#Shut off system service

stop_server()

{

代码语言:txt
复制
  echo "stop not nessccery services!"
代码语言:txt
复制
  for server in `chkconfig --list |grep 3:on|awk '{ print $1}'`
代码语言:txt
复制
      do
代码语言:txt
复制
        chkconfig --level 3 $server off
代码语言:txt
复制
      done
代码语言:txt
复制
  for server in crond network rsyslog sshd lvm2-monitor sysstat netfs blk-availability udev-post
代码语言:txt
复制
      do
代码语言:txt
复制
        chkconfig --level 3 $server on
代码语言:txt
复制
      done

}

#define sshd

sshd_config(){

代码语言:txt
复制
 #sed -i '/^#Port/s/#Port 22/Port 54077/g' /etc/ssh/sshd\_config
代码语言:txt
复制
 sed -i '/^#UseDNS/s/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd\_config
代码语言:txt
复制
 #sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd\_config
代码语言:txt
复制
 sed -i 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/g' /etc/ssh/sshd\_config
代码语言:txt
复制
 /etc/init.d/sshd restart
代码语言:txt
复制
 echo "set sshd && restat sshd succedd!"

}

iptables

iptables(){

代码语言:txt
复制
#disable iptables
代码语言:txt
复制
/etc/init.d/iptables stop
代码语言:txt
复制
chkconfig --level 3 iptables off
代码语言:txt
复制
#disable ipv6
代码语言:txt
复制
echo "alias net-pf-10 off" >> /etc/modprobe.d/modprobe.conf
代码语言:txt
复制
echo "alias ipv6 off" >> /etc/modprobe.d/modprobe.conf
代码语言:txt
复制
echo "NETWORKING\_IPV6=no" >> /etc/sysconfig/network
代码语言:txt
复制
chkconfig --level 3 ip6tables off
代码语言:txt
复制
/etc/init.d/ip6tables stop
代码语言:txt
复制
echo "iptables is stop && ipv6 is disabled!"

}

other(){

initdefault

sed -i 's/^id:.*$/id:3:initdefault:/' /etc/inittab

/sbin/init q

PS1

#echo 'PS1="[\e[37;40m][[\e[32;40m]\u[\e[37;40m]@\h [\e[35;40m]\W[\e0m]\$ [\e[33;40m]"' >> /etc/profile

echo "TMOUT=7200" >> /etc/profile

Record command

sed -i 's/^HISTSIZE=.*$/HISTSIZE=1000/' /etc/profile

#echo "export PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; });user=\$(whoami); echo \$(date \"+%Y-%m-%d %H:%M:%S\"):\$user:`pwd`/:\$msg ---- \$(who am i); } >> /tmp/`hostname`.`whoami`.history-timestamp'" >> /root/.bash_profile

wrong password five times locked 180s

sed -i '4a auth required pam_tally2.so deny=5 unlock_time=180' /etc/pam.d/system-auth

forbiden ctl-alt-delete

sed -i 's/exec \/sbin\/shutdown -r now \"Control-Alt-Delete pressed"/#exec \/sbin\/shutdown -r now \"Control-Alt-Delete pressed"/g' /etc/init/control-alt-delete.conf

source /etc/profile

}

delete_user()

{

delete no use user

echo "delete not use user"

echo ""

for user in adm lp sync shutdown halt uucp operator gopher

do userdel $user ; done

}

sysctl_add(){

cat >> /etc/sysctl.conf << EOF

appends

net.ipv4.tcp_synack_retries = 0

net.ipv4.tcp_max_syn_backlog = 20480

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_fin_timeout = 10

fs.file-max = 819200

net.core.somaxconn = 65536

net.core.rmem_max = 1024123000

net.core.wmem_max = 16777216

net.core.netdev_max_backlog = 165536

net.ipv4.ip_local_port_range = 10000 65535

EOF

set kernel parameters work

sysctl -p

}

#main function

main(){

代码语言:txt
复制
 check\_hosts
代码语言:txt
复制
 set\_timezone
代码语言:txt
复制
 selinux
代码语言:txt
复制
 update
代码语言:txt
复制
 limits\_config
代码语言:txt
复制
 stop\_server
代码语言:txt
复制
 sshd\_config
代码语言:txt
复制
 iptables
代码语言:txt
复制
 other
代码语言:txt
复制
 delete\_user
代码语言:txt
复制
 sysctl\_add   

}

execute main functions

main

echo "+------------------------------------------------------------------------+"

echo "| To initialization system all completed !!! |"

echo "+------------------------------------------------------------------------+"

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年10月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • --Info
  • Initialization CentOS 6.x script
  • Changelog
  • 20160601 shaonbean initial creation
  • Auther: hwang@aniu.tv
  • Check if user is root
  • change yum source
  • cd /etc/yum.repos.d/
  • mkdir bak
  • mv ./*.repo bak
  • wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
  • wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
  • yum clean all && yum makecache
  • if -s /etc/ld.so.conf.d/libc6-xen.conf ; then
  • sed -i 's/hwcap 1 nosegneg/hwcap 0 nosegneg/g' /etc/ld.so.conf.d/libc6-xen.conf
  • fi
  • /etc/security/limits.conf
  • iptables
  • initdefault
  • PS1
  • Record command
  • wrong password five times locked 180s
  • forbiden ctl-alt-delete
  • delete no use user
  • appends
  • set kernel parameters work
  • execute main functions
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档