linux版本:centos6.10
1.关闭SELinux功能
- 查看当前状态
[root@template6 ~]# getenforce
Enforcing //开启状态- 修改配置文件,使永久关闭
sed -i ‘s/SELINUX=enforcing/SELINUX=disable/’ /etc/selinux/config //sed替换
grep SELINUX=disable /etc/selinux/config //检查是否修改成功- 临时关闭
setenforce 0
2.关闭iptables
/etc/init.d/iptables stop
/etc/init.d/iptables stop
chkconfig iptables off
3.精简开机自启动服务
必要开机启动服务
- sshd 远程连接
- rsyslog 日志相关软件
- network 网络
- crond 定时任务
- sysstat 监控工具
执行:
chkconfig|egrep -v “crond|sshd|network|rsyslog|sysstat”|awk ‘{print “chkconfig”,$1,“off”}’|bash
检查:
chkconfig --list|grep 3:on
可能系统不存在sysstat服务,需要安装:yum install sysstat
4.添加管理员账号
- 添加用户
useradd silly- 检查过是否添加
id silly
- 添加密码
法1: passwd silly 回车 -->输入密码
法2: echo “123456”|passwd --stdin silly- sudo提权
cp /etc/sudoers /etc/sudoers.ori
echo “silly ALL=(ALL) ALL” >>/etc/sudoers
tail -1 /etc/sudoers
visudo -c
5.中文字符集
备份:
cp /etc/sysconfig/i18n /etc/sysconfig/i18n.ori
修改:
echo ‘LANG=“zh_CN.UTF-8”’ >/etc/sysconfig/i18n
生效:
source /etc/sysconfig/i18n
检查:
echo $LANG
6.时间同步
设置:
echo ‘#time sync by silly at 2016-09-11’ >> /var/spool/cron/root
echo ‘*/5*****/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1’ >>/var/spool/cron/root检查:
crontab -l
7.命令行安全
设置闲置账号超时时间
echo ‘export TMOUT=300’ >>/etc/profile
设置linux命令行历史记录数
echo ‘export HISTSIZE=10’ >>/etc/profile
echo ‘export HISTFILESIZE=10’ >>/etc/profile
检查:
tail -3 /etc/profile
生效:
. /etc/profile 或 source /etc/profile
8.加大文件描述符
修改:
echo '* - nofile 65535 '>>/etc/security/limits.conf
检查:
tail -1 /etc/security/limits.conf
ulimit -n
9.内核优化
cat >>/etc/sysctl.conf<<EOF
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 = 18
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 = 12
EOF
配置生效:
sysctl -p
10.设置yum源
备份:
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.ori
获取:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
生效:
yum clean all
yum makecache
11.定时清理clientmquenue目录
编辑脚本文件:
mkdir /server/scripts -p
touch /server/scripts/del.sh
echo "find /var/spool/clientmqueue/ -type f|xargs rm -f " >/server/scripts/del.sh
检查:
cat /server/scripts/del.sh
加入定时任务(每周六操作一次):
echo ‘#del clientmqueue by silly’ >> /var/spool/cron/root
echo '00 00 * * 6 /bin/sh /server/scripts/del.sh >/dev/null 2>&1 ’ >>/var/spool/cron/root
检查:
crontab -l
12.锁定关键系统文件
chattr +i /etc/passwd
chattr +i /etc/inittab
chattr +i /etc/group
chattr +i /etc/shadow
chattr +i /etc/gshadow
13.隐藏版本信息
> /etc/issue
14.设定运行级别
grep 3:initdefault /etc/inittab
runlevel
init 3
15.基础软件安装
yum install lrzsz nmap tree dos2unix nc -y
16.软件更新
yum -y update -->升级所有包,改变软件设置和系统设置,系统版本内核都升级
yum -y upgrade -->升级所有包,不改变软件设置和系统设置,系统版本升级,内核不改变