[三木]十分钟教程之CenotOS5.x 6.x服务器初始化安全设置+用SSH Key远程登录linux服务器(带自动设置shell脚本)

[三木]十分钟系列教程快速导读
第一篇:[三木]十分钟教程之本地安装搭建Ubuntu12.04+Apache+PHP+MySQL+phpMyAdmin+Git+Gitweb(带LAMP全自动安装shell脚本)

第二篇:[三木]十分钟教程之服务器安装搭建CentOS6.x+Nginx+PHP+MariaDB(MySQL)+phpMyAdmin+Git+Gitweb(带LAMP全自动安装shell脚本)

第三篇:PHP环境搭建:Windows 7下安装配置PHP(VC6)+Apache+Mysql+phpMyAdmin环境教程(带自启动bat脚本)

第四篇:[三木]十分钟教程之本地安装搭建Ubuntu12.04+Java7+ZendStudio9.03汉化破解(带全自动安装shell脚本)

第五篇:[三木]十分钟教程之CenotOS6.x字符(最简化)安装+网卡设置+更新yum源(带自动设置网卡和更新yum源shell脚本)

第六篇:[三木]十分钟教程之CenotOS5.x 6.x服务器初始化安全设置+用SSH Key远程登录linux服务器(带自动设置shell脚本)

第七篇:[三木]十分钟教程之CentOS6.x安装搭建Apache+MariaDB(MySQL)+PHP+SSL+phpMyAdmin+Git+Gitweb(带LAMP服务器全自动安装shell脚本)

第八篇:VisualSVN Server + Tortoise SVN windows平台快速搭建SVN服务器(带SVN Hooks(钩子)自动更新测试网站)使用教程

文章介绍CentOS6.x 系统安装好后的初始化配置,主要为系统 便捷设置,安全设置以及系统优化
本文安装系统为CentOS-6.3-x86_64-minimal,即CentOS6.3(64位)的最小安装包,和桌面环境安装有些小区别,不过本质是一样的.

安全守则

1. 废除系统所有默认的帐号和密码。
2. 在用户合法性得到验证前不要显示公司题头、在线帮助以及其它信息。
3. 废除“黑客”可以攻击系统的网络服务。
4. 使用6到8位的字母数字式密码。
5. 限制用户尝试登录到系统的次数。
6. 记录违反安全性的情况并对安全记录进行复查。
7. 对于重要信息,上网传输前要先进行加密。
8. 重视专家提出的建议,安装他们推荐的系统“补丁”。
9. 限制不需密码即可访问的主机文件。
10.修改网络配置文件,以便将来自外部的TCP连接限制到最少数量的端口。不允许诸如tftp,sunrpc,printer,rlogin或rexec之类的协议。
11.用upas代替sendmail。sendmail有太多已知漏洞,很难修补完全。
12.去掉对操作并非至关重要又极少使用的程序。
13.使用chmod将所有系统目录变更为711模式。这样,攻击者们将无法看到它们当中有什么东西,而用户仍可执行。
14.只要可能,就将磁盘安装为只读模式。其实,仅有少数目录需读写状态。
15.将系统软件升级为最新版本。老版本可能已被研究并被成功攻击,最新版本一般包括了这些问题的补救。

CentOS 已经发布了6.3版了,现在已经可以用在生产环境了,新版本的内核也能更好的利用硬件。
写了一个脚本,用来初始化系统环境,现在分享出来,你可以根据自己的需要,对脚本进行相应的修改。

说明:脚本中变量publick_Key的内容,请参考这两篇文章自行创建和替换。
ssh证书登录(实例详解):http://www.cnblogs.com/ggjucheng/archive/2012/08/19/2646346.html
SecureCRT创建公钥图文教程:http://blog.lihaixin.name/2011/02/securecrt-create-public-key.html

以下脚本是针对CentOS6.x 64位系统,5.x系统的设置类似,做相应的修改即可。

脚本的内容如下:

#!/bin/bash

# -------------------------------------------------------------------------------
# Filename:    system_init_shell.sh
# Revision:    1.3
# Date:        2012/09/16
# Author:      三木
# Email:       linmaogan#gmail.com
# Website:     www.3mu.me
# Description: CentOS6.3系统新安装后的初始设置
# Notes:       需要切换到root运行,版本针对64位系统,操作系统为CentOS6.3
#              先设置网络临时可以访问,通过FTP上传本文件,然后再执行批处理
# -------------------------------------------------------------------------------
# Copyright:   2012 (c) 三木
# License:     GPL
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Nagios);
#
# Credits go to Ethan Galstad for coding Nagios
# If any changes are made to this script, please mail me a copy of the changes
# -------------------------------------------------------------------------------
#Version 1.0
#2012-09-06 三木 初始版本建立
#Version 1.1
#2012-09-09 三木 增加系统版本检测
#2012-09-09 三木 增加时间设置
#2012-09-09 三木 重写大部份代码
#Version 1.2
#2012-09-10 三木 增加设置系统时间功能
#2012-09-10 三木 增加运行脚本前的root用户判断
#Version 1.3
#2012-09-16 三木 修复系统版本判断的bug
#2012-09-16 三木 所有设置项目增加是否已经设置的判断
#2012-09-16 三木 增加禁止Ping和内核优化等设置
# -------------------------------------------------------------------------------

#检查是否是root用户######################################################################
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, please use root to run"
exit 1
fi
#系统版本检测############################################################################
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=`more /etc/redhat-release |awk '{print substr($3,1,1)}'`
if [ $version != 6 ];then
echo "this script is only for CentOS 6 !"
exit 1
fi

cat << EOF
+---------------------------------------+
|   your system is CentOS 6 x86_64      |
|        start optimizing.......        |
+---------------------------------------+
EOF

#变量设置###############################################################################
#default users who are administrators
ADMIN_USER="test1 test2 test3"
#set the default password for administrators,it will be changed when user first login
DEFAULT_PASSWD='123456'
#管理员电子邮箱
email=test1@gmail.com #yourname@yourserver.com
#SRV_TEMP keep result from chkconfig command
SRV_TEMP="/tmp/chkconfig_list.tmp"
#定义所要删除的用户
USER_DEL="adm lp sync shutdown halt mail news uucp operator games gopher ftp"
#定义所要删除的用户组
GROUP_DEL="adm lp mail news uucp games dip pppusers popusers slipusers"
#定义所要保留的服务,其它服务将被停止,可以根据实际服务器应用更改
SRV_ON="crond irqbalance microcode_ctl network sshd syslog"     #add services to this VARIABLES if you want to start them  when linux start
# the libs files will be used later
INSTALL_LIBS="gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel  openldap-clients libxslt-devel libevent-devel libtool-ltdl bison libtool vim-enhanced"
# the softs will be used later
INSTALL_SOFTS="wget sed tar unzip lrzsz sudo"
DONE="\e[0;32m\033[1mdone\e[m"
#远程连接公钥Publick Key内容
publick_Key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDaDrVJt/mO/eIW1LbH+l4ZqI51g+lrcaHABOGplr/X70U4u3cdHs0fFcwWH9KTqqF4pOVeUWrh4B5A/oJOF4vg8+oKuKL7yJQ/GGmk0ccab9PuOn4U+ZONE9sQub0MEcu0u2uU0OjBYA6CkBOL22Wo+QUwBkpjTSfwXQCruJBHYxD8TtX6QJxBaTRUEN0IjyQjWZhnKsOtzKi5yZ0HGC5jY2OvjU+w49nv9dpZUq5mJjCYhgX38RiKjTzXzDXoZhJfCR9ibnS6KwGqSVCCl4hVpfJPu257jxy6IayMRfKAV3zpmTuKT5QXUGXZ440XwWBPqoRq73kMo1Mb7jNjo+DF'
#先设置网络临时可以访问#################################################################
#ifconfig eth0 192.168.1.122 netmask 255.255.255.0
#route add default gw 192.168.1.1 #添加默认路由
#vi /etc/resolv.conf #配置DNS
#将nameserver 192.168.1.1加入文本
#设置网络##############################################################################
#修改IP地址
#sed -i 's/dhcp/static/g
#s/no/yes/g'  /etc/sysconfig/network-scripts/ifcfg-eth0
#echo 'IPADDR=192.168.1.101
#NETMASK=255.255.255.0
#IPV6INIT="no"
#IPV6_AUTOCONF="no"' >> /etc/sysconfig/network-scripts/ifcfg-eth0
#修改网关
#echo 'GATEWAY=192.168.1.1' >> /etc/sysconfig/network
#修改DNS
#sed -i 's/nameserver/#nameserver/g'  /etc/resolv.conf
#echo 'nameserver 192.168.1.1' >> /etc/resolv.conf
#重启服务
#service network restart

#安装常用类库##########################################################################
#可以根据需要打开此项,因为我后面的安装都是通过yum安装,所以不需打开此项
#yum -y install ${INSTALL_LIBS} 1>/dev/null
#echo -e "Install the usual libs ${DONE}."
#安装wget等常用工具####################################################################
#yum -y install ${INSTALL_SOFTS} 1>/dev/null 此种方式将看不到任何输出,如果网速太慢还以为脚本卡死了
yum -y install ${INSTALL_SOFTS}
sleep 10
echo “install ${INSTALL_SOFTS} ...”
echo -e "Install the usual softs ${DONE}."
#更新yum源#############################################################################
#将默认源换为什么163源
if cat /etc/yum.repos.d/CentOS-Base.repo |awk -F: '{print $1}'|grep '163.com'  2>&1 >/dev/null
then
echo -e "163 Yum has been \e[0;32m\033[1madded\e[m."
else
mv /etc/yum.repos.d/CentOS-Base.repo{,.bak}
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo -O /etc/yum.repos.d/CentOS-Base.repo
echo -e "Install 163 source ${DONE}."
fi
#增加第三方资源库#######################################################################
#配置yum第三方资源库  主要作用是官方没有的包 会去第三方查找相关的包
#增加epel源
if [ ! -e /etc/yum.repos.d/epel.repo ]
then
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm 1>/dev/null
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
echo -e "Install EPEL source ${DONE}."
fi
#增加RPMforge源
if [ ! -e /etc/yum.repos.d/rpmforge.repo ]
then
rpm -Uvh http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm 1>/dev/null
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
echo -e "Install EPEL source ${DONE}."
fi
#root邮件的发送########################################################################
if cat /etc/aliases |awk -F: '{print $2}'|grep $email  2>&1 >/dev/null
then
echo -e "Email has been \e[0;32m\033[1madded\e[m."
else
echo "root: $email" >> /etc/aliases
#重建aliasesdb,让刚刚设定的内容生效
newaliases
#发送测试邮件给root
echo test | mail root
echo -e "Set mail ${DONE}."
fi
#建立一般用户##########################################################################
#add default administrators
for ADMIN in ${ADMIN_USER}
do
if cat /etc/passwd |awk -F: '{print $1}'|grep ${ADMIN}  2>&1 >/dev/null
then
echo -e "User ${ADMIN} has been \e[0;32m\033[1madded\e[m."
else
#add new user, change password when user login
useradd ${ADMIN}
echo "${DEFAULT_PASSWD}" | passwd --stdin ${ADMIN} 2>&1 >/dev/null
usermod -L ${ADMIN}
chage -d 0 ${ADMIN}
usermod -U ${ADMIN}
echo -e "Add User \e[0;32m\033[1m${ADMIN}\e[m done."

#add user to sudoers file
echo "${ADMIN}    ALL=(ALL)       ALL" >> /etc/sudoers
fi
done
#将一般用户 $name 加在管理员组wheel组中,使用此方式切换到root用户不安全,已在上方使用sudo代替
#usermod -G wheel $name
#sed -i '/pam_wheel.so use_uid$/s/#//' /etc/pam.d/su
#echo "SU_WHEEL_ONLY yes" >> /etc/login.defs
#关闭不需要的服务######################################################################
#需要保留的服务crond , irqbalance , microcode_ctl ,network , sshd ,syslog
#关闭服务器上非必须的系统服务项,并不适用于所有服务器,比如如果是文件服务器则NFS相关服务则不能关闭
# close all services and set necessary services on
chkconfig  --list | awk '{print $1}' > ${SRV_TEMP}

# close all services
while read SERVICE
do
chkconfig --level 345 ${SERVICE} off 1>/dev/null

done < ${SRV_TEMP}

#open necessary services
for SRVS in ${SRV_ON}
do
if [ -e /etc/init.d/${SRVS} ]
then
chkconfig --level 345 ${SRVS} on 1>/dev/null
else
echo -e "Service ${SRVS} is \e[0;31m\033[1mnot exits\e[m."
fi

done

#check if the server is vmware virtual machine
chkconfig  --list|grep vmware* 2>&1 /dev/null
GREP_STATUS=$?
if [[ ${GREP_STATUS} == 0 ]]
then
chkconfig --level 345 vmware on 1>/dev/null
chkconfig --level 345 vmware-USBArbitrator on 1>/dev/null
chkconfig --level 345 vmware-workstation-server on 1>/dev/null
chkconfig --level 345 vmware-wsx-server on 1>/dev/null
chkconfig --level 345 vmamqpd on 1>/dev/null
fi

echo -e "Stop unnecessary services ${DONE}."
#关闭SELINUX###########################################################################
if cat /etc/selinux/config |awk -F: '{print $1}'|grep 'SELINUX=disabled'  2>&1 >/dev/null
then
echo -e "SELinux has been \e[0;32m\033[1madded\e[m."
else
sed -i 's/SELINUX=enforcing/#SELINUX=enforcing/g
s/SELINUXTYPE=targeted/#SELINUXTYPE=targeted/g
$a\SELINUX=disabled'  /etc/selinux/config

echo -e "Set SELinux disabled ${DONE}."
fi

#shutdown -r now   #重启系统
#set LANG en_US.UTF-8##################################################################
if cat /etc/sysconfig/i18n |awk -F: '{print $1}'|grep 'en_US.UTF-8'  2>&1 >/dev/null
then
echo -e "Lang has been \e[0;32m\033[1madded\e[m."
else
sed -i s/LANG=.*$/LANG=\"en_US.UTF-8\"/  /etc/sysconfig/i18n
echo -e "Set LANG en_US.UTF-8 ${DONE}."
fi

#系统时间设置##########################################################################
#Set timezone
if [ ! -L /etc/localtime ]
then
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

yum install -y ntp
ntpdate -d 0.asia.pool.ntp.org
date
echo -e "Time ntpdate set ${DONE}."
fi

#将ctrl alt delete键进行屏蔽###########################################################
#禁止使用Ctrl+Alt+Del快捷键,防止误操作的时候服务器重启
if cat /etc/init/control-alt-delete.conf |awk -F: '{print $1}'|grep '#exec /sbin/shutdown'  2>&1 >/dev/null
then
echo -e "Ctrl+Alt+Del has been \e[0;32m\033[1madded\e[m."
else
sed -i 's#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#' /etc/init/control-alt-delete.conf
echo -e "Set ctrl alt delete disabled ${DONE}."
fi

#禁用IPv6,提升网络性能################################################################
if [ ! -e /etc/modprobe.d/ipv6.conf ]
then
cat > /etc/modprobe.d/ipv6.conf << EOFI
alias net-pf-10 off
options ipv6 disable=1
EOFI

echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network
echo -e "Set IPv6 disabled ${DONE}."
fi

#配置远程登陆工具openssh###############################################################
if cat /etc/ssh/sshd_config |awk -F: '{print $1}'|grep 'UseDNS no'  2>&1 >/dev/null
then
echo -e "ssh has been \e[0;32m\033[1madded\e[m."
else
#禁止root用户通过ssh远程登陆,本脚本执行完成后再单独运行此命令,防止操作过程中突然中断远程连接
#sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
#大家结合自己的情况设置,我这里设置能3个用户同时通过ssh登陆
sed -i 's/#MaxStartups 10/MaxStartups 5/' /etc/ssh/sshd_config
#它的作用禁止用户通过密码的方式进行远程登陆保存并退出
#sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
#禁用GSSAPI来认证,也禁用DNS反向解析,加快SSH登陆速度
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
#service sshd restart 或者 /etc/init.d/sshd restart(ssh服务器重启)
echo -e "set ssh ${DONE}."
fi

#配置用户远程登陆的方式################################################################
for ADMIN in ${ADMIN_USER}
do
if [ ! -e /home/${ADMIN} ]
then
mkdir -p /home/${ADMIN}/.ssh     #建立$name用户公钥存放目录
#建立公钥文件,并复制附件中建立公钥教程中产生公钥的文本保存到公钥文件中
echo $publick_Key > /home/${ADMIN}/.ssh/authorized_keys

chown ${ADMIN}.${ADMIN} -R /home/${ADMIN}/    #更改$name用户目录的权限,使得test用户拥有访问.ssh目录和目录下文件的权限
chmod 700 /home/${ADMIN}/.ssh         #.ssh目录只准$name用户有读写执行的权限
chmod 600 /home/${ADMIN}/.ssh/authorized_keys  #authorized_keys文件只准$name用户具有读写权限

echo -e "User ${ADMIN} publick_Key Add \e[0;32m\033[1m${ADMIN}\e[m done."
fi
done
#/etc/init.d/ssh restart
#用户管理##############################################################################
#删除系统默认创建用户,常用服务器中基本不使用的一些帐号,但是这些帐号常被黑客利用和攻击服务器
if [ ! -e /etc/passwd.bak ]
then
#修改之前先备份
cp  /etc/passwd  /etc/passwd.bak
for i in $USER_DEL ;do
userdel $i ;done
echo -e "Set userdel ${DONE}."
fi

#删除系统默认创建组
if [ ! -e /etc/group.bak  ]
then
#修改之前先备份
cp /etc/group   /etc/group.bak
for i in $GROUP_DEL ;do
groupdel $i ;done
echo -e "Set groupdel ${DONE}."
fi

#增加自动注销帐号功能####################################################################
#root用户拥有最高权限,能够在服务器上做任何事情,当我们登陆过后,忘记注销了,将被别人利用,产生重大的安全隐患。应该让系统拥有自动注销功能。
#修改history命令记录
if cat /etc/profile |awk -F: '{print $1}'|grep 'TMOUT=300'  2>&1 >/dev/null
then
echo -e "TMOUT has been \e[0;32m\033[1madded\e[m."
else
sed -i 's/HISTSIZE=1000/HISTSIZE=1000 TMOUT=300/
s/HISTSIZE=1000/HISTSIZE=50/' /etc/profile
echo -e "Set TMOUT ${DONE}."
fi

#存放用户名密码的系统文件加锁############################################################
#这里使用chattr是改变文件属性,若不通过chattr -i 文件名,命令解锁的话,即使root用户也无法修改它,查看文件是否加锁方法:lsattr 文件名
#文件加上不可更改属性,从而防止非授权用户获得权限
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/gshadow
chattr +i /etc/group
chattr +i /etc/services    #给系统服务端口列表文件加锁,防止未经许可的删除或添加服务
#lsattr  /etc/passwd   /etc/shadow  /etc/group  /etc/gshadow   /etc/services   #显示文件的属性
#注意:执行以上权限修改之后,就无法添加删除用户了。
#如果再要添加删除用户,需要先取消上面的设置,等用户添加删除完成之后,再执行上面的操作
#chattr -i /etc/passwd     #取消权限锁定设置
#chattr -i /etc/shadow
#chattr -i /etc/group
#chattr -i /etc/gshadow
#chattr -i /etc/services   #取消系统服务端口列表文件加锁
#现在可以进行添加删除用户了,操作完之后再锁定目录文件
echo -e "Set chattr ${DONE}."
#安装Denyhosts防止暴力破解FTP/SSH等服务,自动封IP#########################################
#解封直接清除掉/etc/hosts.deny文件中对应的IP即可"echo '' > /etc/hosts.deny"
if [ ! -e /etc/hosts.deny  ]
then
yum -y install denyhosts
service denyhosts start
chkconfig --levels 345 denyhosts on
echo -e "Set Denyhosts ${DONE}."
fi

#更改系统的一些重要文件的权限#############################################################
chmod 700 -R /etc/init.d/  #修改init目录文件执行权限,禁止非root用户执行/etc/init.d/下的系统命令
chmod 600 /etc/grub.conf   #修改系统引导文件的权限
chattr +i /etc/grub.conf   #对系统引导文件加锁
chattr +i /etc/service     #对系统服务端口列表文件加锁
chmod a-s /usr/bin/chage   #修改部分系统文件的SUID和SGID的权限
chmod a-s /usr/bin/gpasswd
chmod a-s /usr/bin/wall
chmod a-s /usr/bin/chfn
chmod a-s /usr/bin/chsh
chmod a-s /usr/bin/newgrp
chmod a-s /usr/bin/write
chmod a-s /usr/sbin/usernetctl
chmod a-s /bin/traceroute
chmod a-s /bin/mount
chmod a-s /bin/umount
chmod a-s /bin/ping
chmod a-s /sbin/netreport

chattr +a .bash_history           #避免删除.bash_history或者重定向到/dev/null
chattr +i .bash_history
chmod 700 /usr/bin                #恢复  chmod 555 /usr/bin
chmod 700 /bin/ping               #恢复  chmod 4755 /bin/ping
chmod 700 /usr/bin/vim               #恢复  chmod 755 /usr/bin/vim
chmod 700 /bin/netstat            #恢复  chmod 755 /bin/netstat
chmod 700 /usr/bin/tail           #恢复  chmod 755 /usr/bin/tail
chmod 700 /usr/bin/less           #恢复  chmod 755 /usr/bin/less
chmod 700 /usr/bin/head           #恢复  chmod 755 /usr/bin/head
chmod 700 /bin/cat                #恢复  chmod 755 /bin/cat
chmod 700 /bin/uname              #恢复  chmod 755 /bin/uname
chmod 500 /bin/ps                 #恢复  chmod 755 /bin/ps
echo -e "Set SUID AND SGID ${DONE}."
#关闭多余的虚拟控制台#####################################################################
#我们知道从控制台切换到 X 窗口,一般采用 Alt-F7 ,为什么呢?因为系统默认定义了 6 个虚拟控制台,
#所以 X 就成了第7个。实际上,很多人一般不会需要这么多虚拟控制台的,修改/etc/inittab ,注释掉那些你不需要的。
#系统如果是最小化安装,没有这部份内容,这段代码可以不用执行
if [ ! -e /etc/inittab.bak  ]
then
cp /etc/inittab  /etc/inittab.bak
sed -i 's/2:2345/#2:2345/
s/3:2345/#3:2345/
s/4:2345/#4:2345/
s/5:2345/#5:2345/
s/6:2345/#6:2345/' /etc/inittab
fi

#1:2345:respawn:/sbin/mingetty tty1 只保留第一个
#删除MySQL历史记录########################################################################
#用户登陆数据库后执行的SQL命令也会被MySQL记录在用户目录的.mysql_history文件里。
#如果数据库用户用SQL语句修改了数据库密码,也会因.mysql_history文件而泄漏。
#所以我们在shell登陆及备份的时候不要在-p后直接加密码,而是在提示后再输入数据库密码。
#另外这两个文件我们也应该不让它记录我们的操作,以防万一。
cd
if [ ! -e .bash_history.bak  ]
then
cd
cp .bash_history  .bash_history.bak  #备份
cp .mysql_history .mysql_history.bak
rm .bash_history .mysql_history
ln -s /dev/null .bash_history
ln -s /dev/null .mysql_history
fi

#隐藏服务器系统信息#######################################################################
#在缺省情况下,当你登陆到linux系统,它会告诉你该linux发行版的名称、版本、内核版本、服务器的名称。
#为了不让这些默认的信息泄露出来,我们要进行下面的操作,让它只显示一个"login:"提示符。
#删除/etc/issue和/etc/issue.net这两个文件,或者把这2个文件改名,效果是一样的。
if [ ! -e /etc/issue.bak ]
then
mv /etc/issue /etc/issue.bak
mv /etc/issue.net /etc/issue.net.bak
fi

#优化Linux内核参数########################################################################
if [ ! -e /etc/sysctl.conf.bak ]
then
cp /etc/sysctl.conf  /etc/sysctl.conf.bak
echo 'net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 10024  65535' >> /etc/sysctl.conf  #(表示用于向外连接的端口范围。缺省情况下很小:32768到61000  #注意:这里不要将最低值设的太低,否则可能会占用掉正常的端口! )
/sbin/sysctl -p   #使配置立即生效
fi

#CentOS 系统优化##########################################################################
if [ ! -e /etc/profile.bak2 ]
then
cp /etc/profile /etc/profile.bak2
echo 'ulimit -c unlimited
ulimit -s unlimited
ulimit -SHn 65535' >> /etc/profile
source  /etc/profile    #使配置立即生效
ulimit -a    #显示当前的各种用户进程限制
fi

#服务器禁止ping###########################################################################
if [ ! -e /etc/rc.d/rc.local.bak ]
then
cp /etc/rc.d/rc.local /etc/rc.d/rc.local.bak
echo 'echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all' >> /etc/rc.d/rc.local
#参数0表示允许   1表示禁止
fi

#对您的系统上所有的用户设置资源限制可以防止DoS类型攻击####################################
#如最大进程数,内存数量等。例如,对所有用户的限制象下面这样:
#下面的代码示例中,所有用户每个会话都限制在 10 MB,并允许同时有四个登录。第三行禁用了每个人的内核转储。第四行除去了用户 bin 的所有限制。
if [ ! -e  /etc/security/limits.conf.bak ]
then
cp /etc/security/limits.conf /etc/security/limits.conf.bak
echo '* hard rss 10000
* hard maxlogins 4
* hard core 0
bin -' >> /etc/security/limits.conf
#激活这些限制
echo 'session required /lib/security/pam_limits.so' >> /etc/pam.d/login
fi

#使用yum update更新系统时不升级内核,只更新软件包#########################################
#由于系统与硬件的兼容性问题,有可能升级内核后导致服务器不能正常启动,这是非常可怕的,
#没有特别的需要,建议不要随意升级内核。
if [ ! -e  /etc/yum.conf.bak ]
then
cp /etc/yum.conf    /etc/yum.conf.bak
echo 'exclude=kernel*' >> /etc/yum.conf
fi

#系统升级#################################################################################
#防止被不小心更改了,或被黑客利用了,导致系统崩溃等问题
/etc/init.d/yum-updatesd stop          #停止自动升级工具
yum -y remove yum-updatesd             #卸载yum-updatesd工具
yum -y install yum-fastestmirror       #安装另外一个自动升级工具,这个工具保证从就近服务器下载安装包

yum clean all
yum makecache                            #将服务器上的软件包信息缓存到本地,以提高搜索安装软件的速度
yum -y update                          #系统升级
#reboot                                #从新启动系统
echo -e "Set update ${DONE}."
#查看系统信息#############################################################################
#输出正在运行的服务
ps aux | wc -l
#输出运行的服务
netstat -na --ip

cat << EOF
+-------------------------------------------------+
|               optimizer is done                 |
|   it's recommond to restart this server !       |
+-------------------------------------------------+
EOF

#init done,and reboot system
echo -e "Do you want to \e[0;31m\033[1mreboot\e[m system now? [Y/N]:\t "
read REPLY
case $REPLY in
Y|y)
echo "The system will reboot now ..."
shutdown -r now
;;
N|n)
echo "You must reboot later..."
source /etc/profile
;;
*)
echo "You must input [Y/N]."
source /etc/profile
;;
esac

预设开启及建议开启 的服务列表

服务名称

建议

说明

acpid

停用

电源进阶设定

anacron

需求

执行在 crontab 设 定的时间没有执行到的工作

apmd

停用

侦测系统的电池电量

atd

需求

at排程服务

auditd

停用

稽核系统(配合SELinux)

autofs

停用

自动挂载文件系统与外围装置 (如 光盘、USB)

avahi-daemon

停用

当区网没有 DNS 服务时会试着寻找提供 zeroconf 协议的主机

bluetooth

停用

Bluetooth 蓝 芽

cpuspeed

停用

控制 CPU 速度主要用来省电

crond

需求

cron排程服务

cups

停用

使系统支持打印机

firstboot

停用

安装完成后第一次启动时的服务

gpm

停用

文本模式下可使用鼠标

haldaemon

停用

援 plug and play 装置

hidd

停用

支持蓝芽装置,例如鼠标,键盘

ip6tables

停用

IPv6防火墙

iptables

启用

IPv4防火墙

irqbalance

需求

多个 CPU 环境当系统中断请求时进行负载平衡的程序

kudzu

需求

开机会都自动执行硬件侦测

lvm2-monitor

需求

支持 Logical Volume Manager 逻辑扇区

mcstrans

停用

配合 SELinux 服务

mdmonitor

停用

支持 RAID 相关程序

messagebus

停用

支持 plug and play 装置

microcode_ctl

停用

支持 Intel IA32 CPU 特色,所以 AMD CPU 不需要

netfs

停用

Network File System Mounter 网络挂载系统 (如 NFS、Samba、NCP)

network

启用

网络服务

nfslock

停用

支持 NFS Server 数据一致性功能

pcscd

停用

支援 Smart Card

portmap

停用

支援 NFS Service

rawdevices

停用

可连接raw device装置

readahead_early

启用

启动时预先将相关档案读到内存内来加快启动速度

restorecond

停用

支持 SELinux 监控档案状态

rpcgssd

停用

支援 NFSv4 Service

rpcidmapd

停用

支援 NFSv4 Service

sendmail

启用

SMTP发信协议服务

smartd

需求

支持检查硬盘状态 (Hardware RAID 要改配置文件)

sshd

启用

支持SSH Service远程连入

syslog

启用

系统记录文件服务

yum-updatesd

停用

YUM自动更新服务

参考资料:

CentOS系统安全配置指南:http://www.ithov.com/linux/119268_2.shtml

CentOS系统初始化脚本:http://waydee.blog.51cto.com/4677242/840271

linux常见服务介绍:http://gehailong.blog.51cto.com/765312/268432

发表评论?

2 条评论。

回复给 匿名 ¬
取消回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据