linux系统基础优化


1.Linux系统基础优化
一、关闭SELinux功能
 
Selinux是什么?
安全工具,控制太严格,生产环境不用它,使用其他安全手段。
 
简介: 
 
SELinux带给Linux的主要价值是:提供了一个灵活的,可配置的MAC机制。
 
Security-Enhanced Linux (SELinux)由以下两部分组成:
 
1) Kernel SELinux模块(/kernel/security/selinux)
 
2) 用户态工具
 
SELinux是一个安全体系结构,它通过LSM(Linux Security Modules)框架被集成到Linux Kernel 2.6.x中。它是NSA (United States National Security Agency)和SELinux社区的联合项目。
 
SELinux提供了一种灵活的强制访问控制(MAC)系统,且内嵌于Linux Kernel中。SELinux定义了系统中每个【用户】、【进程】、【应用】和【文件】的访问和转变的权限,然后它使用一个安全策略来控制这些实体(用户、进程、应用和文件)之间的交互,安全策略指定如何严格或宽松地进行检查。
 
SELinux对系统用户(system users)是透明的,只有系统管理员需要考虑在他的服务器中如何制定严格的策略。策略可以根据需要是严格的或宽松的。
 
关闭SElinux方式:
1)         通过vi /etc/selinux/config 进入配置文件进入修改
2)         通过sed 命令操作:
 
实现命令:sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
输出:
[root@oldboy ~]# cp /etc/selinux/config  /etc/selinux/config.ori  操作前的备份
[root@oldboy ~]# sed -i  's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config  直接修改源文件
[root@oldboy ~]# grep SELINUX=disabled /etc/selinux/config                
SELINUX=disabled  查看修改后的效果
 
查看对比修改后的文件:
实现命令:(vimdiff)diff /etc/selinux/config.ori /etc/selinux/config
 
输出:
7c7
< SELINUX=enforcing
---
> SELINUX=disabled
SELINUX=disabled(永久生效)重启系统
---------------
[root@bigboy ~]# getenforce(查看命令行是否关闭selinux)
Enforcing
[root@bigboy ~]# setenforce (设置selinux)
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@bigboy ~]# setenforce 0 (命令行关闭selinux)
[root@bigboy ~]# getenforce 
Permissive(临时生效)
 
二、运行级别
 
什么是运行级别:
 
runlevel linux运行时的一种状态标示,这个标示用数字表示。
运行级别的其中状态:
0
halt,关机状态
1
single  user 单用户,找回root密码
2
multiuser  without nfs 多用户没有NFS网络文件系统
3
文本模式(Full multiuser mode)*******工作模式
4
unused
5
图形。桌面、X11
6
reboot  重启
 
如何查看linux运行级级别:
 
实现命令:runlevel
 
输出:
[root@oldboy ~]# runlevel
N(前一次) 3(当前)
 
更改运行级别:init
 
三、精简开机系统启动
 
为什么要设置开机自启动?
 
1、节省开机时间、加快启动速度
 
2、节省资源开销
 
3、减少安全隐患
 
需要保留的开机自启动:
 
sshd:远程连接linux服务器
 
rsyslog:是操作系统提供的一种机制,系统的守护程序通常会使用rsylog将各种信息系统日志文件中
 
network:激活关闭各个网络接口
 
crond:用于周期性地执行系统及用户配置的任务计划(周期性的处理一些重复问题的计划任务服务)
 
Sysstat:是一个软件包,监测系统性能及效率的一组工具
 
Sysstat软件包集成的主要工具为:
 
iostat工具提供CPU使用率及硬盘吞吐效率的数据
 
mpstat工具提供与单个或多个处理器相关的数据
 
sar工具负责收集、报告并存储系统活跃的信息
 
如何实现?
 
第一种方法:通过setup来修改
 
第二种方法:通过ntsysv来修改
 
第三种方法:通过chkconfig来实现
a. [root@bigboy ~]# chkconfig --list|grep 3:on|awk '{print $1}'|grep -Ev "sshd|network|rsyslog|crond|sysstat"|awk '{print "chkconfig " $1 " off"}'|bash
[root@bigboy ~]# 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
 
b. [root@bigboy ~]# chkconfig --list|grep 3:on|awk '{print $1}'|grep -Ev "sshd|network|rsyslog|crond|sysstat"|sed -r 's#(.*)#chkconfig \1 off#g'|bash
[root@bigboy ~]# 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
 
c. [root@bigboy ~]# for name in `chkconfig --list|grep 3:on|awk '{print $1}'|grep -Ev "sshd|network|rsyslog|crond|sysstat"`;do chkconfig $name off;done 
[root@bigboy ~]# 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
 
四.关闭iptables防火墙
 
查看防火墙:iptables -L -n
关闭防火墙:
实现命令:/etc/init.d/iptables stop
输出:
 [root@oldboy ~]# /etc/init.d/iptables stop
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:清除防火墙规则:                                 [确定]
iptables:正在卸载模块:                                   [确定]
查看防火墙状态:
实现命令:/etc/init.d/iptables status
输出:
root@bigboy ~]# /etc/init.d/iptables status
 [root@bigboy ~]# /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination        
 
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination        
 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination  
 
五.linux运维思想最小化原则
 
原则:多一事不如少一事!
 
1、安装linux系统最小化,选包最小化,yum安装软件包也要最小化,无用的包不装。
 
2、开机自启动最小化
 
3、操作命令最小化
 
例如:用rm -f test.txt而不用rm -fr test.txt
 
4、登录linux用户最小化。
 
平时没有需求不用root登录,用普通用户登录即可
 
5、普通用户授权权限最小化,
 
即只给必须的管理系统的命令。
 
6、linux系统文件及目录的权限设置最小化,禁止随意创建、更改、删除。理论上限制掉。
 
六. 更改SSH服务端远程登录的配置(配置文件:/etc/ssh/sshd_config)
 
更改方法:
 
方法一:通过vi进入配置文件进行修改
vi /etc/ssh/sshd_config
####by oldboy#2011-11-24##
Port 52113
PermitRootLogin no
PermitEmptyPasswords no
UseDNS no
GSSAPIAuthentication no
####by oldboy#2011-11-24##
 
方法二:通过sed命令实现修改
sed -ir '13 i Port 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no'  sshd_config
 
重启生效:/etc/init.d/sshd reload【平滑重启不影响用户】(restart)
 Linux下SSH远程连接服务慢的解决方案请见老男孩的博客:
http://oldboy.blog.51cto.com/2561410/1300964
 
八、利用sudo管理文件描述
 
在visudo进入文件增加普通用户的命令路径使得普通用户环境下利用sudo进行命令操作。
Allow root torun any commands anywhere
root     ALL=(ALL)       ALL
oldboy   ALL=           (ALL)   NOPASSWD: ALL  /bin/ls
用户     用户管理的机器  临时拥有的用户角色      /bin/ls
注意:内置命令无法用到sudo。
 
九.linux字符显示设置:
 
字符集是一套文字符号及其编码:GBK 、UTF-8(企业广泛使用)
调整服务器端字符集:调整字符集路径(/etc/sysconfig/i18n)记住
[root@bigboy/]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"       英文字符
SYSFONT="latarcyrheb-sun16"
[root@bigboy/]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
[root@bigboy/]#cp/etc/sysconfig/i18n /etc/sysconfig/i18n.oldboy.20151003    修改文件前的备份
[root@bigboy/]#sed-i 's#LANG="en_US.UTF-8"#LANG="zh_CN.UTF-8"#g'/etc/sysconfig/i18n    利用sed替换字符文件修改为中文字符
[root@bigboy/]# cat /etc/sysconfig/i18n
LANG="zh_CN.UTF-8"
SYSFONT="latarcyrheb-sun16"
[root@bigboy/]# echo $LANG   查看修改字符后的效果
en_US.UTF-8
[root@bigboy/]# source /etc/sysconfig/i18n 使得修改后的文件生效
[root@bigboy/]# echo $LANG     
zh_CN.UTF-8
 
十. 设置linux服务器时间同步
 
互联网同步时间
[root@bigboy/]/usr/sbin/ntpdate time.nist.govov   互联网同步时间
[root@bigboy/]# date -s "2015/10/3 9:34"
2015年 10月 03日星期六 09:34:00 CST
[root@bigboy/]# ntpdate time.nist.gov   set the date and time viaNTP
 3 Oct 09:35:21 ntpdate[28135]: adjust timeserver 132.163.4.103 offset 0.286494 sec
[root@bigboy/]# date   date 查看时间  -s 修改时间
2015年 10月 03日星期六 09:48:46 CST
 [root@bigboy /]# hwclock   query and set the hardwareclock
2015年10月03日星期六 08时59分12秒  -0.737654 seconds
crond :定时任务
每5分钟同步一次
[root@bigboy/]# echo "*/5 * * * * /usr/sbin/ntpdate time.nist.gov /dev/null2>&1" >>/var/spool/cron/root
 [root@bigboy /]# crontab –l  定时生效
*/5 * * * */usr/sbin/ntpdate time.nist.gov /dev/null 2>&1
 
十一、设置超时
 
临时生效
[root@bigboy /]# export  TMOUT=300设置超时时间300S
[root@bigboy/]# echo "export TMOUT=300" >>/etc/profile
[root@bigboy/]# source /etc/profile  使得设置生效
[root@bigboy/]# echo $TMOUT
300
 
十二.history历史记录数
 
临时生效
[root@bigboy /]# export  HISTSIZE=5  定义历史记录数5条
[root@bigboy /]# history
 728  cat ~/.bash_history
 729  HISTFILESIZE=5
 730  cat ~/.bash_history
 731  HISTSIZE=5
 732  history
[root@bigboy /]#export HISTFILESIZE=5 定义历史记录文件数5条
[root@bigboy /]# cat ~/.bash_history
visudo
su - oldboy
su oldboy
netstat -an|grep EST
su oldboy
 
永久生效
[root@oldboy ~]# echo 'export TMOUT=300'>>/etc/profile
[root@oldboy ~]# echo 'exportHISTSIZE=5' >>/etc/profile
[root@oldboy ~]# echo 'exportHISTFILESIZE=5' >>/etc/profile
[root@oldboy ~]# tail -3 /etc/profile
export TMOUT=300
export HISTSIZE=5
export HISTFILESIZE=5
[root@oldboy ~]# source /etc/profile  使得文件生效
[root@oldboy ~]# echo $TMOUT   
300
[root@oldboy ~]# echo $HISTSIZE
5
 
十三.调整linux系统文件描述符数量
 
文件描述符是由无符号整数表示的句柄,进程使用它来标示打开文件。
 
文件描述符概念:
 
1、表示形式为整数数字(0-65535)
 
2、会占用文件描述符(标示打开文件)
 
查看默认文件描述符
ulimit-n
 
3、调整文件描述符
[root@bigboy ~]# ulimit -SHn 65535  设置文件描述符数量
[root@bigboy ~]# ulimit -n
65535(32768)
 [root@bigboy ~]#  echo '*       -    nofile      65535' >>/etc/security/limits.conf    将修改的描述符数量写入文件
[root@bigboy ~]# tail -1/etc/security/limits.conf                   *        -    nofile     65535
 
十四.调整内核参数文件 (/etc/sysctl.conf)
 
vim/etc/sysctl.conf
 
linux内核优化参数:
--------------------------------------------------------------------
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
----------------------------------------------------------------------
 
网络状态说明及优化命令和优化细节参考资料请看:
http://yangrong.blog.51cto.com/6945369/1321594老男孩教育的优秀学生博文
http://oldboy.blog.51cto.com/2561410/1336488
sysctl -p 使得加载的参数生效
 
十五.隐藏linux版本号:
 
[root@oldboy ~]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
[root@oldboy ~]# cat /etc/issue.net
CentOS release 6.7 (Final)
Kernel \r on an \m
[root@oldboy ~]# >/etc/issue
[root@oldboy ~]# >/etc/issue.net
[root@oldboy ~]# cat /etc/issue
 
十六.锁定系统文件
 
相关的系统文件:/etc/passwd/etc/shadow /etc/group /etc/gshadow  /etc/inittab
 [root@oldboy~]# chattr +i /etc/passwd /etc/shadow /etc/group  /etc/gshadow /etc/inittab   +i锁定系统文件
[root@oldboy ~]# useradd dddd
useradd: cannot open /etc/passwd
[root@oldboy ~]# rm -f /etc/passwd
Do not use rm command. -f /etc/passwd
[root@oldboy ~]# \rm -f /etc/passwd
rm: 无法删除"/etc/passwd": 不允许的操作
[root@oldboy ~]# chattr -i /etc/passwd/etc/shadow /etc/group  /etc/gshadow/etc/inittab  -i解除系统文件
[root@oldboy ~]# useradd dddd     
[root@oldboy ~]# chattr +i /etc/passwd/etc/shadow /etc/group  /etc/gshadow/etc/inittab
[root@oldboy ~]# lsattr /etc/passwd  查看系统文件属性
----i--------e- /etc/passwd
[root@oldboy ~]# chattr -i /etc/passwd/etc/shadow /etc/group  /etc/gshadow/etc/inittab
[root@oldboy ~]# lsattr /etc/passwd
-------------e- /etc/passwd
 
十七.禁止linux系统被ping
 
内核中修改禁止ping,缺点是禁止自己ping
echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all 
[root@www ~]# echo"net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf
[root@www ~]# tail -1 /etc/sysctl.conf
net.ipv4.icmp_echo_ignore_all=1
[root@www ~]# sysctl -p
 
生效:
[root@www ~]# echo"net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf
[root@www ~]# tail -1 /etc/sysctl.conf
net.ipv4.icmp_echo_ignore_all=1
[root@www ~]# sysctl -p
 
还原禁ping:
echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all
十八.定时清理邮件服务临时目录垃圾文件
 
centos5系列的系统默认安装Sen时dmail服务,因此邮件临时存放地点的路径/var/spool/clientmqueue/.
 
centos6默认情况下没有安转Sendmail服务,而是改装了Posfix服务,因此邮件存放地点的路径为:/var/spool/postfit/maildrop/
以上两个目录很容易被垃圾文件填满导致系统的inode数量不够用,从而导致无地方存放文件
 
手动清理的方法:
find /var/spool/clientmqueue/ -typef|xargs rm -f适合centOS5的sendmail服务
find /var/spool/postfix/maildrop/ -typef|xargs rm -f适合Centos6的postfix服务
定时清理的方法为:将上述命令写成脚本,然后做定时任务,每天晚上0点执行一次(定时任务再说)
 
 
小结:如何优化linux:
 
1、关闭SElinux
2、关闭防火墙,设定运行级别为3.
3、精简开机自启动服务
4、SSH安全控制(提前建立普通用户)
5、sudo 管理用户授权
6、调整文件描述符
7、更改合适的字符集
8、锁定关键系统文件
9、禁止显示内核版本及系统版本信息
10、设置会话的超时时间及历史记录数
11、禁止PING
12、优化LINUX内核参数
13、特定漏洞yum/rpm升级
14、清楚多余的系统虚拟账号
15、服务器时间同步
16、打补丁下载软件调整为国内的下载地址(调整yum源)
17、定时清理邮件服务临时目录垃圾文件
18、为grub菜单加密码

相关内容

    暂无相关文章