您的位置:首页 > 运维架构 > Linux

CentOS(5.8/6.4)linux生产环境若干优化实战

2013-12-08 09:37 573 查看
CentOS系统安装之后并不能立即投入生产环境使用,往往需要先经过我们运维人员的优化才行。在此讲解几点关于Linux系统安装后的基础优化操作。CentOS修改时区
echo 'ZONE="Asia/Shanghai"' > /etc/sysconfig/clock
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
注意:本次优化都是基于CentOS(5.8/6.4)。关于5.8和6.4两者优化时的小区别,我会在文中提及的。优化条目:
1、修改ip地址、网关、主机名、DNS等
2、关闭selinux,清空iptables关闭selinux
清空iptables
3、添加普通用户并进行
sudo
授权管理
4、更新yum源及必要软件安装yum安装软件,默认获取rpm包的途径从国外官方源,改成国内的源。国内较快的两个站点:搜狐镜像站点、网易镜像站点法1:自己配置好安装源配置文件,然后上传到linux。法2:使用镜像站点配置好的yum安装源配置文件
接下来执行如下命令,检测yum是否正常
然后使用如下命令将系统更新到最新
接下来就要安装几个必要的软件了
lrzsz是一个上传下载的软件sysstat是用来检测系统性能及效率的工具5、定时自动更新服务器时间
提示:CentOS 6.4的时间同步命令路径不一样6是/usr/sbin/ntpdate5是/sbin/ntpdate扩展:在机器数量少时,以上定时任务同步时间就可以了。如果机器数量大时,可以在网内另外部署一台时间同步服务器NTP Server。此处仅提及,不做部署。时间同步服务器架构图:6、精简开机自启动服务刚装完操作系统可以只保留crond,network,syslog,sshd这四个服务。(Centos6.4为rsyslog)
7、定时自动清理
/var/spool/clientmqueue/
目录垃圾文件,放置inode节点被占满
本优化点,在6.4上可以忽略不需要操作即可!
然后将其加入到crontab定时任务中
8、变更默认的
ssh
服务端口,禁止root用户远程连接
9、锁定关键文件系统
使用chattr命令后,为了安全我们需要将其改名
10、调整文件描述符大小
配置完成后,重新登录即可查看。提示:也可以把ulimit -SHn 65535命令加入到/etc/rc.local,然后每次重启生效
扩展:文件描述符文件描述符在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。但是文件描述符这一概念往往只适用于Unix、Linux这样的操作系统。习惯上,标准输入(standard input)的文件描述符是 0,标准输出(standard output)是 1,标准错误(standard error)是 2。尽管这种习惯并非Unix内核的特性,但是因为一些 shell 和很多应用程序都使用这种习惯,因此,如果内核不遵循这种习惯的话,很多应用程序将不能使用。11、调整字符集,使其支持中文
扩展:什么是字符集?简单的说就是一套文字符号及其编码。常用的字符集有:GBK 定长双字节不是国际标准,支持系统不少UTF-8 非定长 1-4字节广泛支持,MYSQL也使用UTF-812、去除系统及内核版本登录前的屏幕显示
13、内核参数优化说明:本优化适合apache,nginx,squid多种等web应用,特殊的业务也可能需要略作调整。
提示:由于CentOS6.X系统中的模块名不是ip_conntrack,而是nf_conntrack,所以在/etc/sysctl.conf优化时,需要把net.ipv4.netfilter.ip_conntrack_max 这种老的参数,改成net.netfilter.nf_conntrack_max这样才可以。即对防火墙的优化,在5.8上是
在6.4上是
另外,在此优化过程中可能会有报错:1、5.8版本上
这个错误可能是你的防火墙没有开启或者自动处理可载入的模块ip_conntrack没有自动载入,解决办法有二,一是开启防火墙,二是自动处理开载入的模块ip_conntrack
2、6.4版本上
这个错误可能是你的防火墙没有开启或者自动处理可载入的模块ip_conntrack没有自动载入,解决办法有二,一是开启防火墙,二是自动处理开载入的模块ip_conntrack
3、6.4版本上
这个错误是由于自动处理可载入的模块bridge没有自动载入,解决办法是自动处理开载入的模块ip_conntrack
到此,我们Linux系统安装后的基础优化已经操作的差不多了。补充一些优化知识如下:清空防火墙并设置规则,根据需求开启相应端口
[root@localhost~]# iptables -F #清楚防火墙规则[root@localhost~]# iptables -L #查看防火墙规则[root@localhost~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT[root@localhost~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT[root@localhost~]# iptables -A INPUT -p tcp --dport 53 -j ACCEPT[root@localhost~]# iptables -A INPUT -p udp --dport 53 -j ACCEPT[root@localhost~]# iptables -A INPUT -p udp --dport 123 -j ACCEPT[root@localhost~]# iptables -A INPUT -p icmp -j ACCEPT[root@localhost~]# iptables -P INPUT DROP[root@localhost~]# /etc/init.d/iptables save
添加普通用户并进行sudo授权管理
[root@localhost~]# useradd user[root@localhost~]# echo "123456" | passwd --stdin user #设置密码[root@localhost~]# vi /etc/sudoers #或visudo打开,添加user用户所有权限root ALL=(ALL) ALLuser ALL=(ALL) ALL
禁用root远程登录
[root@localhost~]# vi /etc/ssh/sshd_configPermitRootLogin no   #限制root用户远程登录PermitEmptyPasswords no #禁止空密码登录UseDNS no #关闭DNS查询X11Forwarding no   #关闭X11转发,防止额外的信息泄露LoginGraceTime 60  #调整认证时限(默认单位为s),即当用户登录ssh之后,要求输入密码的时间限制,规定时间没有输入则自动断线PrintMotd yes   #取消注释,使得登录后显示一些信息PrintLastLog yes  #取消注释,使得登录后显示一些信息Port xxxx       #修改ssh默认端口22AllowUsers test1 test2 #设置允许登录的ssh用户,也可以设置允许的组 AllowGroups adminTCPKeepAlive yes   #TCPKeepAlive指定系统是否向客户端发送TCP keepalive消息,这种消息可以检测到死连接、连接不当关闭、客户端崩溃等异常,避免僵尸进程产生,推荐开启。编辑/etc/motd文件,添加一些警告信息,如下This computer system is for authorized users only. All activityis logged and regularly checked. Individuals using this systemwithout authority or in excess of their authority are subject tohaving all their services revoked...关闭ssh闲置会话ClientAliveInterval 60ClientAliveCountMax 5就是客户端如果60*5=300s,即5分钟如果没有任何操作,则空闲连接会被强制断开,关闭时提示如下Connection to x.x.x.x closed by remote host.Connection to x.x.x.x closed.关闭不必要的认证如果可以,只允许公钥认证,关闭其它认证方式PasswordAuthentication noChallengeResponseAuthentication no
关闭不必要开机自启动服务for m in `chkconfig | grep 3:on | awk '{print $1}'`;do chkconfig $m off;donefor n in crond network rsyslog sshd iptables;do chkconfig $n on;done可以参考:http://ffcxyq.blog.163.com/blog/static/9958797201392945424339/
先可以查看一下相关服务情况chkconfig | grep 3:启用chkconfig auditd offchkconfig blk-availability offchkconfig ip6tables offchkconfig lvm2-monitor offchkconfig udev-post off
***不必要的系统用户
userdel admuserdel lpuserdel shutdownuserdel haltuserdel uucpuserdel operatoruserdel gamesuserdel gopher
关闭重启ctl-alt-delete组合键
[root@localhost ~]# vi /etc/init/control-alt-delete.conf#exec /sbin/shutdown -r now "Control-Alt-Deletepressed" #注释掉
调整文件描述符大小
[root@localhost ~]# ulimit –n #默认是10241024[root@localhost ~]# echo "ulimit -SHn 102400">> /etc/rc.local #设置开机自动生效
去除系统相关信息
[root@localhost ~]# echo "Welcome to Server" >/etc/issue[root@localhost ~]# echo "Welcome to Server" >/etc/redhat-release
修改history记录
[root@localhost ~]# vi /etc/profile #修改记录1000个HISTCONTROL=ignorespace   ###强制linux不记录敏感历史命令HISTFILESIZE=1000HISTSIZE=1000HISTTIMEFORMAT="%F %T `whoami` "export HISTIMEFORMATexport LANG="zh_CN.UTF-8"
同步系统时间
[root@localhost ~]# cp /usr/share/zoneinfo/Asia/Shanghai/etc/localtime #设置Shanghai时区[root@localhost ~]# ntpdate cn.pool.ntp.org ;hwclock–w #同步时间并写入blos硬件时间[root@localhost ~]# crontab –e #设置任务计划每天零点同步一次0 * * * * /usr/sbin/ntpdate cn.pool.ntp.org ; hwclock -w
内核参数优化
[root@localhost ~]# vi /etc/sysctl.conf #末尾添加如下参数net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.tcp_max_tw_buckets = 10000net.ipv4.tcp_sack = 1net.ipv4.tcp_window_scaling = 1net.ipv4.tcp_rmem = 4096        87380   4194304net.ipv4.tcp_wmem = 4096        16384   4194304net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.core.netdev_max_backlog = 262144net.core.somaxconn = 262144net.ipv4.tcp_max_orphans = 3276800net.ipv4.tcp_max_syn_backlog = 262144net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_fin_timeout = 1net.ipv4.tcp_keepalive_time = 15net.ipv4.ip_local_port_range = 1024    65535net.ipv4.tcp_syncookies = 1 #1是开启SYN Cookies,当出现SYN等待队列溢出时,启用Cookies来处,理,可防范少量SYN攻击,默认是0关闭net.ipv4.tcp_tw_reuse = 1 #1是开启重用,允许讲TIME_AIT sockets重新用于新的TCP连接,默认是0关闭net.ipv4.tcp_tw_recycle = 1 #TCP失败重传次数,默认是15,减少次数可释放内核资源net.ipv4.ip_local_port_range = 4096 65000 #应用程序可使用的端口范围net.ipv4.tcp_max_tw_buckets = 5000 #系统同时保持TIME_WAIT套接字的最大数量,如果超出这个数字,TIME_WATI套接字将立刻被清除并打印警告信息,默认180000net.ipv4.tcp_max_syn_backlog = 4096 #进入SYN宝的最大请求队列,默认是1024net.core.netdev_max_backlog = 10240 #允许送到队列的数据包最大设备队列,默认300net.core.somaxconn = 2048 #listen挂起请求的最大数量,默认128net.core.wmem_default = 8388608 #发送缓存区大小的缺省值net.core.rmem_default = 8388608 #接受套接字缓冲区大小的缺省值(以字节为单位)net.core.rmem_max = 16777216 #最大接收缓冲区大小的最大值net.core.wmem_max = 16777216 #发送缓冲区大小的最大值net.ipv4.tcp_synack_retries = 2 #SYN-ACK握手状态重试次数,默认5net.ipv4.tcp_syn_retries = 2 #向外SYN握手重试次数,默认4net.ipv4.tcp_tw_recycle = 1 #开启TCP连接中TIME_WAIT sockets的快速回收,默认是0关闭net.ipv4.tcp_max_orphans = 3276800 #系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上,如果超出这个数字,孤儿连接将立即复位并打印警告信息net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_mem[0]:低于此值,TCP没有内存压力;net.ipv4.tcp_mem[1]:在此值下,进入内存压力阶段;net.ipv4.tcp_mem[2]:高于此值,TCP拒绝分配socket。内存单位是页,可根据物理内存大小进行调整,如果内存足够大的话,可适当往上调。上述内存单位是页,而不是字节。
至此CentOS 6.5_x64最小化安装系统基本优化调整完毕,需要重启下系统。net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.tcp_max_tw_buckets = 100000net.ipv4.tcp_sack = 1net.ipv4.tcp_window_scaling = 1net.ipv4.tcp_rmem = 4096 87380 4194304net.ipv4.tcp_wmem = 4096 16384 4194304net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.core.netdev_max_backlog = 262144net.core.somaxconn = 262144net.ipv4.tcp_max_orphans = 3276800net.ipv4.tcp_max_syn_backlog = 262144net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_fin_timeout = 1net.ipv4.tcp_keepalive_time = 30net.ipv4.ip_local_port_range = 1024 65530net.ipv4.icmp_echo_ignore_all = 0http://www.jfedu.net/thread-1529-1-1.htmllinux服务器禁pingcat /proc/sys/net/ipv4/icmp_echo_ignore_all 查看icmp_echo_ignore_all的值 0是允许ping 1是禁止ping修改为icmp_echo_ignore_all的值为1 这个只是在内存生效,重启就恢复0永久禁ping[root@localhost ~]# vim /etc/sysctl.conf在配置文件最后加入一行net.ipv4.icmp_echo_ignore_all = 1

删除不必要的帐号

Linux系统中提供一些可能不需要的预置帐号.如果确实不需要这些帐号,就把它们删掉.删除一些不必要的用户:
# userdel adm# userdel lp# userdel sync# userdel shutdown# userdel halt# userdel news# userdel uucp# userdel operator# userdel games //如果不是用X Window服务器,可以删除这个用户# userdel gopher# userdel ftp //如果没有安装FTP服务,可以删除这个用户
删除一些不必要的组:
# groupdel adm# groupdel lp# groupdel news# groupdel uucp# groupdel games# groupdel dip# groupdel pppusers# groupdel popusers# groupdel slipusers
"不允许改变"位可以用来保护文件使其不被意外地删除或重写,也可以防止有些人创建这个文件的符号连接.给口令文件和组文件设置不可改变位,可以用下面的命令:
# chattr +i /etc/password# chattr +i /etc/shadow# chattr +i /etc/group# chattr +i /etc/gshadow
注意:如果将来要在口令或组文件增加或删除用户,就必须先清除这些文件的不可改变位,否则就不能做任何改变.如果没有清除这些文件的不可改变位,安装那些会自动在口令文件和组文件中加入新用户的RPM软件包的时候,在安装过程中就会出现出错的提示.# cat /etc/sysctl.conf# Kernel sysctl configuration file for Red Hat Linux## For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and# sysctl.conf(5) for more details.# Controls IP packet forwardingnet.ipv4.ip_forward = 0net.ipv6.conf.default.disable_ipv6 = 1net.ipv6.conf.all.diable_ipv6 = 1# Controls source route verificationnet.ipv4.conf.default.rp_filter = 1# Do not accept source routingnet.ipv4.conf.default.accept_source_route = 0# Controls the System Request debugging functionality of the kernelkernel.sysrq = 0# Controls whether core dumps will append the PID to the core filename.# Useful for debugging multi-threaded applications.kernel.core_uses_pid = 1# Controls the use of TCP syncookiesnet.ipv4.tcp_syncookies = 1# Disable netfilter on bridges.net.bridge.bridge-nf-call-ip6tables = 0net.bridge.bridge-nf-call-iptables = 0net.bridge.bridge-nf-call-arptables = 0# Controls the default maxmimum size of a mesage queuekernel.msgmnb = 65536# Controls the maximum size of a message, in byteskernel.msgmax = 65536# Controls the maximum shared segment size, in byteskernel.shmmax = 68719476736# Controls the maximum number of shared memory segments, in pageskernel.shmall = 4294967296fs.file-max = 999999net.ipv4.tcp_fin_timeout = 2net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_syncookies = 1net.ipv4.tcp_keepalive_time = 600net.ipv4.ip_local_port_range = 4000 65000net.ipv4.tcp_max_syn_backlog = 16384net.ipv4.tcp_max_tw_buckets = 36000net.ipv4.route.gc_timeout = 100net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_synack_retries = 1net.core.somaxconn = 16384net.core.netdev_max_backlog = 16384net.core.wmem_max = 16777216net.core.rmem_max = 16777216net.ipv4.tcp_rmem = 1024 87380 12482912net.ipv4.tcp_wmem = 1023 87380 21582912net.ipv4.tcp_max_orphans = 16384net.nf_conntrack_max = 25000000net.netfilter.nf_conntrack_max = 25000000net.netfilter.nf_conntrack_tcp_timeout_established = 180net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120http://www.sa-log.com/59.htmlhttp://www.tiejiang.org/1253.html/article/4518978.html/article/4421684.htmlhttp://www.tiejiang.org/1253.html/article/4425520.htmlhttp://nolinux.blog.51cto.com/4824967/1318607http://www.skyshe.cn/783本文出自 “从心开始” 博客,请务必保留此出处http://hao360.blog.51cto.com/5820068/1337957
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: