一、关闭SELinux功能
selinux功能太严苛,还是关闭了吧
法一:修改配置文件,永久生效
[root@web01 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@web01 ~]# grep SELINUX=disabled /etc/selinux/config
SELINUX=disabled
法二:临时关闭SELinux
[root@web01 ~]# getenforce
Enforcing
[root@web01 ~]# setenforce 0
[root@web01 ~]# getenforce
Permissive
二、精简系统开机自启动程序
系统运行过程中,很多无用的软件在后台启动,这些服务浪费了资源,而且也带来了安全隐患,因此要关闭。
需要保留的服务主要有
- network:系统启动时,若想连接网络,必须要开启这个服务;
- ssh:远程连接linux服务器是需要用到这个服务程序,所以必须要开启;
- rsyslog:日志相关软件,排错很重要;
- crontab:计划任务,生产场经常用到的软件;
- sysstat:sysstat是一个软件包,包含监测系统性能及小路的一组工具,对于收集系统性能很有帮助;
法一:完全关闭所有服务,再开启需要保留的
[root@web01 ~]# LANG=en [root@web01 ~]# for n in $(chkconfig --list|grep 3:on|awk '{print $1}');do chkconfig --level 3 $n off;done [root@web01 ~]# chkconfig --list|grep 3:on ####没有结果,表示全部关闭了#### [root@web01 ~]# for n in crond network rsyslog sshd sysstat;do chkconfig --level 3 $n on;done [root@web01 ~]# chkconfig --list|grep 3:on crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
法二:直接将所有启动的服务打印出来,去除需要开启的服务,其他服务全部关闭
[root@web02 ~]# for n in $(chkconfig --list|grep 3:on|awk '{print $1}'|egrep -v "crond|network|sshd|rsyslog|sysstat");do chkconfig $n off;done [root@web02 ~]# chkconfig --list|grep 3:on crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
三、更改ssh服务端远程登陆的配置
ssh服务是知名服务,如果使用默认配置,容易被人暴力破解,在配置文件底部添加如下内容。
[root@web02 ~]# vim /etc/ssh/sshd_config _______________split________________ Port 55525 #修改默认端口 PermitRootLogin no #禁止root远程登陆 PermitEmptyPasswords no #禁止密码为空的用户远程登陆 UseDNS no #禁用域名反向解析,启用会导致ssh连接慢的问题 GSSAPIAuthentication no #解决linux之间使用ssh远程连接慢的问题
ListenAddress 192.168.127.0:55525 #只监听内网ssh连接
______________split_________________
四、调整系统的字符集设置
防止中文乱码,调整字符集,支持中文
[root@web01 ~]# echo $LANG en_US.UTF-8 [root@web01 ~]# cat /etc/sysconfig/i18n LANG="en_US.UTF-8" SYSFONT="latarcyrheb-sun16" [root@web01 ~]# cp /etc/sysconfig/i18n /etc/sysconfig/i18n.ori [root@web01 ~]#echo ‘LANG=”zh_CN.UTF-8”’>/etc/sysconfig/i18n [root@web01 ~]#source /etc/sysconfig/i18n #使其生效 [root@web01 ~]#echo $LANG zh_CN.UTF-8
[root@web01 ~]# LANG=en #临时修改英文字符集
五、服务器时间同步
[root@web01 ~]# ntpdate time.nist.gov 28 May 17:01:38 ntpdate[4149]: step time server 132.163.97.3 offset 87738.066880 sec [root@web01 ~]# echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root [root@web01 ~]# crontab -l */5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
六、设置登陆超时及历史命令记录数
[root@web01 ~]# echo 'export TMOUT=300' >>/etc/profile [root@web01 ~]# echo 'export HISTSIZE=5' >>/etc/profile [root@web01 ~]# echo 'export HISTFILESIZE=5' >>/etc/profile [root@web01 ~]# tail -3 /etc/profile export TMOUT=300 #连接系统超过300秒,没有操作,自动退出系统 export HISTSIZE=5 #命令行的历史记录数量变量 export HISTFILESIZE=5 #历史记录文件的命令行数量(~/。bash_history)
[root@web01 ~]# source /etc/profile
七、调整linux系统文件描述符数量
文件描述符是由无符号整数表示的句柄,进程使用它来标识打开的文件。文件描述符与包括相关信息(如文件的打开模式、文件的位置类型、文件的初始类型等)的文件对象相关联,这些信息被称为文件的上下文。
对于内核而言,所有打开的文件都是通过文件描述符引用的,当打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。当读写一个文件时,使用open或creat返回的文件描述符标识该文件,并将其作为参数传递给read或write。
[root@web01 ~]# ulimit -n 1024 #默认1024 [root@web01 ~]# echo '* - nofile 65535' >>/etc/security/limits.conf [root@web01 ~]# tail -1 /etc/security/limits.conf * - nofile 65535 [root@web01 ~]# ulimit -n 65535 #调整为65535
八、linux服务器内核参数优化
[root@web01 ~]# 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
#########以下为iptables防火墙优化,如果防火墙未开启,会提示报错,可以忽略################ 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
[root@web01 maildrop]# sysctl -p #手动生效
九、定时清理邮件服务器临时目录垃圾文件
centos默认安装postfix(5为sendmail),因此邮件临时存放地/var/spool/postfix/maildrop/(5为/var/spool/clientmqueue/),需要经常清理,不然容易被垃圾文件填满,导致系统的inode数量不够用。
[root@web01 maildrop]# find /var/spool/postfix/defer/ -type f|xargs rm -f #可以放在脚本里,每天定时任务运行
十、隐藏linux版本信息
linux在本地登陆时,会显示系统的版本和内核
[root@web01 ~]# cat /etc/issue CentOS release 6.5 (Final) Kernel \r on an \m [root@web01 ~]# >/etc/issue [root@web01 ~]# cat /etc/issue
十一、锁定关键系统文件,防止被提权篡改
[root@web01 maildrop]# chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab #上锁
[root@web01 maildrop]# chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab #解锁
十二、升级具有典型漏洞的软件版本
如:openssl openssh bash
[root@web01 maildrop]# rpm -qa openssl openssh bash openssl-1.0.1e-57.el6.x86_64 bash-4.1.2-15.el6_4.x86_64 openssh-5.3p1-94.el6.x86_64 [root@web01 maildrop]# yum install openssl openssh bash -y [root@web01 maildrop]# rpm -qa openssl openssh bash openssl-1.0.1e-57.el6.x86_64 openssh-5.3p1-123.el6_9.x86_64 bash-4.1.2-48.el6.x86_64
十三、配置yum更新源
[root@web01 maildrop]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup [root@web01 maildrop]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
本文非原创,摘自老男孩书籍,整理于此,权为记忆。