本腳本是centos 6系統初始化腳本,參考網上已有腳本,以模塊化的形式呈現,可以按照需要方便注釋掉不需要的模塊。
#!/bin/bash
# Author: joj
# Version 1.0
# Description :本腳本是centos 6系統初始化腳本,參考網上已有腳本,以模塊的形式呈現,可以按照需要方便注釋掉不需要的模塊。
#set env
export PATH=$PATH:/bin:/sbin:/usr/sbin
# Require root to run this script.
if [ "$UID" != "0" ]; then
echo "Please run this script by root."
exit 1
fi
#define cmd var
SERVICE=`which service`
CHKCONFIG=`which chkconfig`
#更換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.repo http://mirrors.aliyun.com/repo/Centos-6.repo
fi
#添加EPEL源
wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
yum clean all && yum list
}
#關閉selinux
function close_selinux(){
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
#grep SELINUX=disabled /etc/selinux/config
setenforce 0 &>/dev/null
#getenforce
}
#關閉防火墻
function close_iptables(){
/etc/init.d/iptables stop
/etc/init.d/iptables stop
chkconfig iptables off
}
#關閉不需要的服務,最小化服務開啟
function least_service(){
chkconfig|awk '{print "chkconfig",$1,"off"}'|bash
chkconfig|egrep "crond|sshd|network|rsyslog|sysstat"|awk '{print "chkconfig",$1,"on"}'|bash
#export LANG=en
#chkconfig --list|grep 3:on
}
#添加新用戶
function adduser(){
if [ `grep -w joj /etc/passwd|wc -l` -lt 1 ]
then
useradd joj
echo 123456|passwd --stdin joj
\cp /etc/sudoers /etc/sudoers.ori
#添加sudo權限
echo "joj? ALL=(ALL) NOPASSWD: ALL " >>/etc/sudoers
tail -1 /etc/sudoers
visudo -c &>/dev/null
fi
}
#添加字符集規則,防止亂碼
function charset(){
/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(){
cron=/var/spool/cron/root
if [ `grep -w "ntpdate" $cron|wc -l` -lt 1? ]
then
echo '#time sync by joj ' >>$cron
echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>$cron
crontab -l
fi
}
#歷史命令記錄設置
function com_line_set(){
if [ `egrep "TMOUT|HISTSIZE|HISTFILESIZE" /etc/profile|wc -l` -ge 3? ]
then
echo 'export TMOUT=300' >>/etc/profile
echo 'export HISTSIZE=5' >>/etc/profile
echo 'export HISTFILESIZE=5' >>/etc/profile
. /etc/profile
fi
}
#設置連接數
function open_file_set(){
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(){
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,禁止root用戶登錄,如果需要可以改掉ssh登錄端口
function init_ssh(){
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%Y-%m-%d_%H-%M-%S"`
#可以修改ssh的默認端口號,需要則修改
#sed -i 's%#Port 22%Port 52113%' /etc/ssh/sshd_config
sed -i 's%#PermitRootLogin yes%PermitRootLogin no%' /etc/ssh/sshd_config
sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%' /etc/ssh/sshd_config
sed -i 's%#UseDNS yes%UseDNS no%' /etc/ssh/sshd_config
/etc/init.d/sshd reload &>/dev/null
}
#郵件使用自定義服務器設置(163或者QQ)
function mail(){
#mail config
if [ `rpm -qa mailx|wc -l` -lt 1 ]
then
yum install mailx -y
fi
cat >>/etc/mail.rc<
#發送郵件服務器設置,建議根據實際情況修改
set from="zhouguanjie2005@163.com"
set smtp=smtp.163.com
set smtp-auth-user=zhouguanjie2005@163.com
set smtp-auth-password=zhou789128
set smtp-auth=login
EOF
}
#軟件安裝及更新設置
function update_linux(){
if [ `rpm -qa lrzsz nmap tree dos2unix nc|wc -l` -le 3 ]
then
yum install lrzsz nmap tree dos2unix nc -y
#安裝gcc基礎庫文件以及sysstat工具
yum -y install gcc gcc-c++ vim-enhanced unzip unrar sysstat
#是否更新系統軟件,建議在初始配置的時候更新
#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
}
main