你的位置:首页 > 软件开发 > 操作系统 > Centos下初始化系统的shell脚本

Centos下初始化系统的shell脚本

发布时间:2015-10-26 19:00:18
根据参考网上的一些文章,总结出来一个系统初始化的shell脚本#!/bin/bashcat << EOF+---------------------------------------------------------------------------+| Ini ...

根据参考网上的一些文章,总结出来一个系统初始化的shell脚本

#!/bin/bashcat << EOF+---------------------------------------------------------------------------+| Initialize for the CentOS 6_installed.                  |+---------------------------------------------------------------------------+EOFfunction format() {  #sleep 1  #echo -e "\033[42;37m ########### Finished ########### \033[0m\n"	echo -e "\033[32m Install Success!!!\033[0m\n"}########################################################################### Set time 时区/时间同步设置echo "Set time."/bin/cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &> /dev/nullyum -y install ntpdate &> /dev/nullntpdate 0.centos.pool.ntp.org &> /dev/nullhwclock -wformat########################################################################### Create Log 创建该脚本运行记录日志echo "Create log file."DATE1=`date +"%F %H:%M"`LOG=/var/log/sysinitinfo.logecho $DATE1 >> $LOGecho "------------------------------------------" >> $LOGformat############################################################################ Disabled Selinux 禁用Selinuxecho "Disabled SELinux."sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinuxformat############################################################################ Stop iptables 禁用iptablesecho "Stop iptables."service iptables stop &> /dev/nullchkconfig --level 235 iptables offformat############################################################################ Disable ipv6 禁用IPV6echo "Disable ipv6."cat << EOF > /etc/modprobe.confalias net-pf-10 offalias ipv6 offEOFchkconfig --level 2235 ip6tables offformat###########################################################################Set history commands 设置命令历史记录参数echo "Set history commands."sed -i 's/HISTSIZE=1000/HISTSIZE=100/' /etc/profilesed -i "8 s/^/alias vi='vim'/" /root/.bashrcgrep 'HISTFILESIZE' /etc/bashrc &>/dev/null if [ $? -ne 0 ] thencat << EOF >> /etc/bashrcHISTFILESIZE=4000HISTSIZE=4000HISTTIMEFORMAT='%F/%T'EOFfisource /etc/bashrcformat########################################################################### set vimecho "Set Vim."cat << EOF > ~/.vimrcset number set laststatus=2set shiftwidth=4set tabstop=4set noexpandtabset softtabstop=4set cmdheight=3set cursorlineset formatoptions=tcrqn set encoding=utf-8 syntax oncolorscheme desertcolor ron set background=darkset ruler set rulerformat=%20(%2*%<%f%=\ %m%r\ %3l\ %c\ %p%%%)set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}nmap z :x<cr> nmap sp :set paste<cr>iEOFformat########################################################################### Epel 升级epel源echo "Install epel"rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm &> /dev/nullsed -i "s/^#base/base/g" /etc/yum.repos.d/epel.reposed -i "s/^mirr/#mirr/g" /etc/yum.repos.d/epel.repoformat###########################################################################Yum install Development tools 安装开发包组及必备软件echo "Install Development tools(It will be a moment)"yum groupinstall -y "Development tools" &> /dev/nullyum install -y bind-utils lrzsz wget gcc gcc-c++ vim htop openssl &>/dev/nullformat########################################################################### Yum update bash and openssl 升级bash/opensslecho "Update bash and openssl"yum -y update bash openssl &> /dev/nullformat############################################################################ Set ssh 设置ssh登录策略echo "Set sshd."sed -i "s/^#PermitEmptyPasswords/PermitEmptyPasswords/" /etc/ssh/sshd_configsed -i "s/^#LoginGraceTime 2m/LoginGraceTime 6m/" /etc/ssh/sshd_configgrep "UseDNS no" /etc/ssh/sshd_config &>/dev/nullif [ $? -ne 0 ]then	echo "UseDNS no" >> /etc/ssh/sshd_config fiformat############################################################################ Set default init 3 设置系统默认初始化echo "Default init 3."sed -i 's/^id:5:initdefault:/id:3:initdefault:/' /etc/inittabformat###########################################################################echo "Tunoff services."for i in `ls /etc/rc3.d/S*`do  servers=`echo $i|cut -c 15-`	echo $servers	case $servers in crond | irqbalance | microcode_ctl | network | random | postfix | sshd | rsyslog | local | smart | cpuspeed | mysqld | httpd | ntpd | php-fpm | nginx)		echo -e "\033[31m Base services, Skip!\033[0m"   ;;   *)		echo -e "\033[31m change $servers to off\033[0m"		chkconfig --level 235 $servers off		service $servers stop   ;;esacdoneformat############################################################################ Del unnecessary users 删除不必要的用户echo "Del unnecessary users."for USERS in adm lp sync shutdown halt mail news uucp operator games gopherdo  grep $USERS /etc/passwd &>/dev/null  if [ $? -eq 0 ]  then    userdel $USERS &> /dev/null  fidoneformat############################################################################ Del unnecessary groups 删除不必要的用户组echo "Del unnecessary groups."for GRP in adm lp mail news uucp games gopher mailnull floppy dip pppusers popusers slipusers daemondo  grep $GRP /etc/group &> /dev/null  if [ $? -eq 0 ]  then    groupdel $GRP &> /dev/null  fidoneformat############################################################################ Disabled reboot by keys ctlaltdelete 禁用ctlaltdelete重启功能echo "Disabled reboot by keys ctlaltdelete"sed -i 's/^exec/#exec/' /etc/init/control-alt-delete.confformat############################################################################ Set ulimit 设置文件句柄数echo "Set ulimit 1000000"cat << EOF > /etc/security/limits.conf*  soft  nofile 1000000*  hard  nofile 1000000*  soft  nproc 102400*  hard  nproc 102400EOFsed -i 's/102400/1000000/' /etc/security/limits.d/90-nproc.confformat############################################################################ Set login message 设置登录时显示的信息echo "Set login message."echo "This is not a public Server" > /etc/issueecho "This is not a public Server" > /etc/redhat-releaseformat############################################################################ Record SUID and SGID files DATE2=`date +"%F"`echo "Record SUID and SGID files."echo "SUID --- " > /var/log/SuSg_"$DATE2".logfind / -path '/proc' -prune -o -perm -4000 >> /var/log/SuSg_"$DATE2".logecho "------------------------------------------------------ " >> /var/log/SuSg_"$DATE2".logecho "SGID --- " >> /var/log/SuSg_"$DATE2".logfind / -path '/proc' -prune -o -perm -2000 >> /var/log/SuSg_"$DATE2".logformat############################################################################ Disabled crontab send mail 禁用执行任务计划时向root发送邮件echo "Disable crontab send mail."sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab sed -i 's/^mail\.\*/mail\.err/' /etc/rsyslog.confformat############################################################################ Set ntp client 设置时间服务客户端echo "Set ntp client."SED() {  cp -p /etc/ntp.conf /etc/ntp.conf.bak  sed -i '/^server/d' /etc/ntp.conf  sed -i '/^includefile/ i\server 0.centos.pool.ntp.org iburst' /etc/ntp.conf  sed -i '/0.centos.pool.ntp.org/ a\server 1.centos.pool.ntp.org iburst' /etc/ntp.conf  sed -i '/1.centos.pool.ntp.org/ a\server 2.centos.pool.ntp.org iburst' /etc/ntp.conf  sed -i '/2.centos.pool.ntp.org/ a\server 3.centos.pool.ntp.org iburst' /etc/ntp.conf  chkconfig --level 35 ntpd on &> /dev/null}rpm -q ntp &> /dev/nullif [ $? -eq 0 ]then  SEDelse  yum -y install ntp &> /dev/null  SEDfiformat############################################################################ Set sysctl.conf 设置内核参数echo "Set sysctl.conf"#web应用中listen函数的backlog默认会将内核参数的net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认是511,所以必须调整,一般调整为2048cat << EOF > /etc/sysctl.confnet.core.somaxconn = 2048 net.core.wmem_default = 262144net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.ipv4.tcp_rmem = 4096 4096 16777216net.ipv4.tcp_wmem = 4096 4096 16777216net.ipv4.tcp_mem = 786432 2097152 3145728net.ipv4.tcp_max_syn_backlog = 16384net.core.netdev_max_backlog = 20000net.ipv4.tcp_fin_timeout = 15net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_max_orphans = 131072net.ipv4.ip_local_port_range = 1024 65535net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1net.bridge.bridge-nf-call-ip6tables = 0net.bridge.bridge-nf-call-iptables = 0net.bridge.bridge-nf-call-arptables = 0kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 4294967295kernel.shmall = 26843545EOFmodprobe bridge & > /dev/null/sbin/sysctl -p & > /dev/nullformat############################################################################ Set nginx 语法mkdir -p ~/.vim/syntax && cd ~/.vim/syntaxwget http://www.vim.org/scripts/download_script.php?src_id=14376 -O nginx.vim > /dev/nullecho "au BufRead,BufNewFile /etc/nginx/vhosts/*.conf set ft=nginx" > ~/.vim/filetype.vim

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Centos下初始化系统的shell脚本

关键词:Centos

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。