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
2
3
4
5
6
7
8
9
10
11
12
13
修改ip地址、网关、主机名、DNS等
关闭selinux,清空iptables
添加普通用户并进行sudo授权管理
更新yum源及必要软件安装
定时自动更新服务器时间
精简开机自启动服务
定时自动清理/ var /spool/clientmqueue/目录垃圾文件,放置inode节点被占满
变更默认的ssh服务端口,禁止root用户远程连接
锁定关键文件系统
调整文件描述符大小
调整字符集,使其支持中文
去除系统及内核版本登录前的屏幕显示
内核参数优化

1、修改ip地址、网关、主机名、DNS等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0         #网卡名字
BOOTPROTO= static #静态IP地址获取状态 如:DHCP表示自动获取IP地址
IPADDR= 192.168 . 1.113 #IP地址
NETMASK= 255.255 . 255.0 #子网掩码
ONBOOT=yes#引导时是否激活
GATEWAY= 192.168 . 1.1
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO= static
IPADDR= 192.168 . 1.113
NETMASK= 255.255 . 255.0
ONBOOT=yes
GATEWAY= 192.168 . 1.1
[root@localhost ~]# vi /etc/sysconfig/network
HOSTNAME=c64     #修改主机名,重启生效
GATEWAY= 192.168 . 1.1 #修改默认网关,如果上面eth0里面不配置网关的话,默认就使用这里的网关了。
[root@localhost ~]# cat /etc/sysconfig/network
HOSTNAME=c64
GATEWAY= 192.168 . 1.1
我们也可以用  hostnamec64  来临时修改主机名,重新登录生效
修改DNS
[root@localhost ~]# vi /etc/resolv.conf   #修改DNS信息
nameserver 114.114 . 114.114
nameserver 8.8 . 8.8
[root@localhost ~]# cat /etc/resolv.conf  #查看修改后的DNS信息
nameserver 114.114 . 114.114
nameserver 8.8 . 8.8
[root@localhost ~]# service network restart   #重启网卡,生效
重启网卡,也可以用下面的命令
[root@localhost ~]# /etc/init.d/network restart


2、关闭selinux,清空iptables

关闭selinux

1
2
3
4
5
6
[root@c64 ~]# sed –i ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/config   #修改配置文件则永久生效,但是必须要重启系统。
[root@c64 ~]# grep SELINUX=disabled /etc/selinux/config
SELINUX=disabled     #查看更改后的结果
[root@c64 ~]# setenforce 0 #临时生效命令
[root@c64 ~]# getenforce      #查看selinux当前状态
Permissive


清空iptables

1
2
3
4
5
6
7
8
9
[root@c64 ~]# iptables –F     #清理防火墙规则
[root@c64 ~]# iptables –L     #查看防火墙规则
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@c64 ~]#/etc/init.d/iptables save   #保存防火墙配置信息

3、添加普通用户并进行sudo授权管理

1
2
3
4
5
[root@c64 ~]# useradd sunsky
[root@c64 ~]# echo "123456" |passwd --stdin sunsky&&history –c
[root@c64 ~]# visudo
在root    ALL=(ALL)    ALL此行下,添加如下内容
sunsky    ALL=(ALL)    ALL

4、更新yum源及必要软件安装

yum安装软件,默认获取rpm包的途径从国外官方源,改成国内的源。

国内较快的两个站点:搜狐镜像站点、网易镜像站点

法1:自己配置好安装源配置文件,然后上传到linux。

法2:使用镜像站点配置好的yum安装源配置文件

1
2
3
[root@c64 ~]# cd /etc/yum.repos.d/
[root@c64 yum.repos.d]# /bin/mv CentOS-Base.repo CentOS-Base.repo.bak
[root@c64 yum.repos.d]# wget http: //mirrors.163.com/.help/CentOS6-Base-163.repo

接下来执行如下命令,检测yum是否正常

1
2
[root@c64 yum.repos.d]# yum clean all  #清空yum缓存
[root@c64 yum.repos.d]# yum makecache  #建立yum缓存

然后使用如下命令将系统更新到最新

1
2
[root@c64 yum.repos.d]# rpm -- import /etc/pki/rpm-gpg/RPM-GPG-KEY*       #导入签名KEY到RPM
[root@c64 yum.repos.d]# yum  upgrade-y     #更新系统内核到最新

接下来就要安装几个必要的软件了

1
[root@c64 yum.repos.d]# yum install lrzsz ntpdate sysstat -y

lrzsz是一个上传下载的软件

sysstat是用来检测系统性能及效率的工具

5、定时自动更新服务器时间

1
2
[root@c64 ~]# echo '*/5 * * * * /usr/sbin/ntpdate time.windows.com >/dev/null 2 >&1' >>/ var /spool/cron/root
[root@c64 ~]# echo '*/10 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/ var /spool/cron/root

提示:CentOS 6.4的时间同步命令路径不一样

6是/usr/sbin/ntpdate

5是/sbin/ntpdate

扩展:在机器数量少时,以上定时任务同步时间就可以了。如果机器数量大时,可以在网内另外部署一台时间同步服务器NTP Server。此处仅提及,不做部署。

时间同步服务器架构图:

133048286.png

6、精简开机自启动服务

刚装完操作系统可以只保留crondnetworksyslogsshd这四个服务。(Centos6.4rsyslog

1
2
3
4
5
6
7
[root@c64 ~]# for sun in `chkconfig --list|grep 3 :on|awk '{print $1}' `; do chkconfig --level 3 $sun off;done
[root@c64 ~]# for sun in crond rsyslog sshd network; do chkconfig --level 3 $sun on;done
[root@c64 ~]# 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


7、定时自动清理/var/spool/clientmqueue/目录垃圾文件,放置inode节点被占满

本优化点,在6.4上可以忽略不需要操作即可!

1
2
3
4
[root@c64 ~]# mkdir /server/scripts -p
[root@c64 ~]# vi /server/scripts/spool_clean.sh
#!/bin/sh
find/ var /spool/clientmqueue/-typef -mtime + 30 |xargsrm-f

然后将其加入到crontab定时任务中

1
[root@c64 ~]# echo '*/30 * * * * /bin/sh /server/scripts/spool_clean.sh >/dev/null 2>&1' >>/ var /spool/cron/root

8、变更默认的ssh服务端口,禁止root用户远程连接

1
2
3
4
5
6
7
8
9
[root@c64 ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
[root@c64 ~]# vim /etc/ssh/sshd_config
Port 52113 #ssh连接默认的端口
PermitRootLogin no   #root用户***都知道,禁止它远程登录
PermitEmptyPasswords no #禁止空密码登录
UseDNS no            #不使用DNS
[root@c64 ~]# /etc/init.d/sshd reload    #从新加载配置
[root@c64 ~]# netstat -lnt     #查看端口信息
[root@c64 ~]# lsof -i tcp: 52113

9、锁定关键文件系统

1
2
3
4
5
[root@c64 ~]# chattr +i /etc/passwd
[root@c64 ~]# chattr +i /etc/inittab
[root@c64 ~]# chattr +i /etc/group
[root@c64 ~]# chattr +i /etc/shadow
[root@c64 ~]# chattr +i /etc/gshadow

使用chattr命令后,为了安全我们需要将其改名

1
[root@c64 ~]# /bin/mv /usr/bin/chattr /usr/bin/任意名称

10、调整文件描述符大小

1
2
3
[root@localhost ~]# ulimit –n        #查看文件描述符大小
1024
[root@localhost ~]# echo '*  -  nofile  65535' >> /etc/security/limits.conf

配置完成后,重新登录即可查看。

提示:也可以把ulimit -SHn 65535命令加入到/etc/rc.local,然后每次重启生效

1
2
3
4
5
6
[root@c64 ~]# cat >>/etc/rc.local<<EOF
#open files
ulimit -HSn 65535
#stack size
ulimit -s 65535
EOF


扩展:文件描述符

文件描述符在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。但是文件描述符这一概念往往只适用于Unix、Linux这样的操作系统

习惯上,标准输入(standard input)的文件描述符是 0,标准输出(standard output)是 1,标准错误(standard error)是 2。尽管这种习惯并非Unix内核的特性,但是因为一些 shell 和很多应用程序都使用这种习惯,因此,如果内核不遵循这种习惯的话,很多应用程序将不能使用。


11、调整字符集,使其支持中文

1
2
sed-i 's#LANG="en_US.UTF-8"#LANG="zh_CN.GB18030"#' /etc/sysconfig/i18n
source/etc/sysconfig/i18n

扩展:什么是字符集?

简单的说就是一套文字符号及其编码。常用的字符集有:

GBK 定长双字节不是国际标准,支持系统不少

UTF-8 非定长 1-4字节广泛支持,MYSQL也使用UTF-8

12、去除系统及内核版本登录前的屏幕显示

1
2
[root@c64 ~]# >/etc/redhat-release
[root@c64 ~]# >/etc/issue

13、内核参数优化

说明:本优化适合apache,nginx,squid多种等web应用,特殊的业务也可能需要略作调整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@c64 ~]# vi /etc/sysctl.conf
#by sun in 20131001
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.ipv4.ip_conntrack_max = 25000000
net.ipv4.netfilter.ip_conntrack_max = 25000000
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 180
net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait = 120
net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait = 60
net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait = 120
[root@localhost ~]# sysctl –p    #使配置文件生效

提示:由于CentOS6.X系统中的模块名不是ip_conntrack,而是nf_conntrack,所以在/etc/sysctl.conf优化时,需要把net.ipv4.netfilter.ip_conntrack_max 这种老的参数,改成net.netfilter.nf_conntrack_max这样才可以。

即对防火墙的优化,在5.8上是

1
2
3
4
5
6
net.ipv4.ip_conntrack_max = 25000000
net.ipv4.netfilter.ip_conntrack_max = 25000000
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 180
net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait = 120
net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait = 60
net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait = 120

在6.4上是

1
2
3
4
5
6
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

另外,在此优化过程中可能会有报错:

1、5.8版本上

1
2
3
4
5
6
error: "net.ipv4.ip_conntrack_max" is an unknown key
error: "net.ipv4.netfilter.ip_conntrack_max" is an unknown key
error: "net.ipv4.netfilter.ip_conntrack_tcp_timeout_established" is an unknown key
error: "net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait" is an unknown key
error: "net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait" is an unknown key
error: "net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait" is an unknown key

这个错误可能是你的防火墙没有开启或者自动处理可载入的模块ip_conntrack没有自动载入,解决办法有二,一是开启防火墙,二是自动处理开载入的模块ip_conntrack

1
2
modprobe ip_conntrack
echo "modprobe ip_conntrack" >> /etc/rc.local

2、6.4版本上

1
2
3
4
5
6
error: "net.nf_conntrack_max" isan unknown key
error: "net.netfilter.nf_conntrack_max" isan unknown key
error: "net.netfilter.nf_conntrack_tcp_timeout_established" isan unknown key
error: "net.netfilter.nf_conntrack_tcp_timeout_time_wait" isan unknown key
error: "net.netfilter.nf_conntrack_tcp_timeout_close_wait" isan unknown key
error: "net.netfilter.nf_conntrack_tcp_timeout_fin_wait" isan unknown key

这个错误可能是你的防火墙没有开启或者自动处理可载入的模块ip_conntrack没有自动载入,解决办法有二,一是开启防火墙,二是自动处理开载入的模块ip_conntrack

1
2
modprobe nf_conntrack
echo "modprobe nf_conntrack" >> /etc/rc.local

3、6.4版本上

1
2
3
error: "net.bridge.bridge-nf-call-ip6tables" isan unknown key
error: "net.bridge.bridge-nf-call-iptables" isan unknown key
error: "net.bridge.bridge-nf-call-arptables" isan unknown key


这个错误是由于自动处理可载入的模块bridge没有自动载入,解决办法是自动处理开载入的模块ip_conntrack

1
2
modprobe bridge
echo "modprobe bridge" >> /etc/rc.local

到此,我们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) ALL
user ALL=(ALL) ALL

禁用root远程登录

[root@localhost~]# vi /etc/ssh/sshd_config
PermitRootLogin no   #限制root用户远程登录

PermitEmptyPasswords no #禁止空密码登录

UseDNS no #关闭DNS查询

X11Forwarding no   #关闭X11转发,防止额外的信息泄露

LoginGraceTime 60  #调整认证时限(默认单位为s),即当用户登录ssh之后,要求输入密码的时间限制,规定时间没有输入则自动断线

PrintMotd yes   #取消注释,使得登录后显示一些信息

PrintLastLog yes  #取消注释,使得登录后显示一些信息

Port xxxx       #修改ssh默认端口22

AllowUsers test1 test2 #设置允许登录的ssh用户,也可以设置允许的组 AllowGroups admin

TCPKeepAlive yes   #TCPKeepAlive指定系统是否向客户端发送TCP keepalive消息,这种消息可以检测到死连接、连接不当关闭、客户端崩溃等异常,避免僵尸进程产生,推荐开启。

编辑/etc/motd文件,添加一些警告信息,如下
This computer system is for authorized users only. All activity 
is logged and regularly checked. Individuals using this system 
without authority or in excess of their authority are subject to 
having all their services revoked...

关闭ssh闲置会话
ClientAliveInterval 60
ClientAliveCountMax 5
就是客户端如果60*5=300s,即5分钟如果没有任何操作,则空闲连接会被强制断开,关闭时提示如下
Connection to x.x.x.x closed by remote host.
Connection to x.x.x.x closed.

关闭不必要的认证
如果可以,只允许公钥认证,关闭其它认证方式
PasswordAuthentication no
ChallengeResponseAuthentication no


关闭不必要开机自启动服务

for m in `chkconfig | grep 3:on | awk '{print $1}'`;do chkconfig $m off;done

for 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 off
chkconfig blk-availability off
chkconfig ip6tables off
chkconfig lvm2-monitor off
chkconfig udev-post off

***不必要的系统用户

userdel adm
userdel lp
userdel shutdown
userdel halt
userdel uucp
userdel operator
userdel games
userdel 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 #默认是1024
1024
[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=1000
HISTSIZE=1000
HISTTIMEFORMAT="%F %T `whoami` "
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`  
export HISTTIMEFORMAT="[%F %T][`whoami`][${USER_IP}] " 
export HISTIMEFORMAT

export 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 = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 10000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096        87380   4194304
net.ipv4.tcp_wmem = 4096        16384   4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 15
net.ipv4.ip_local_port_range = 1024    65535
net.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套接字将立刻被清除并打印警告信息,默认180000
net.ipv4.tcp_max_syn_backlog = 4096 #进入SYN宝的最大请求队列,默认是1024
net.core.netdev_max_backlog = 10240 #允许送到队列的数据包最大设备队列,默认300
net.core.somaxconn = 2048 #listen挂起请求的最大数量,默认128
net.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握手状态重试次数,默认5
net.ipv4.tcp_syn_retries = 2 #向外SYN握手重试次数,默认4
net.ipv4.tcp_tw_recycle = 1 #开启TCP连接中TIME_WAIT sockets的快速回收,默认是0关闭
net.ipv4.tcp_max_orphans = 3276800 #系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上,如果超出这个数字,孤儿连接将立即复位并打印警告信息
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.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 = 0

net.ipv4.conf.default.rp_filter = 1

net.ipv4.conf.default.accept_source_route = 0

kernel.sysrq = 0

kernel.core_uses_pid = 1

net.ipv4.tcp_syncookies = 1

kernel.msgmnb = 65536

kernel.msgmax = 65536

kernel.shmmax = 68719476736

kernel.shmall = 4294967296

net.ipv4.tcp_max_tw_buckets = 100000

net.ipv4.tcp_sack = 1

net.ipv4.tcp_window_scaling = 1

net.ipv4.tcp_rmem = 4096        87380   4194304

net.ipv4.tcp_wmem = 4096        16384   4194304

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.core.netdev_max_backlog = 262144

net.core.somaxconn = 262144

net.ipv4.tcp_max_orphans = 3276800

net.ipv4.tcp_max_syn_backlog = 262144

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 1

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_fin_timeout = 1

net.ipv4.tcp_keepalive_time = 30

net.ipv4.ip_local_port_range = 1024    65530

net.ipv4.icmp_echo_ignore_all = 0

http://www.jfedu.net/thread-1529-1-1.html 






linux服务器禁ping

cat /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 forwarding

net.ipv4.ip_forward = 0

net.ipv6.conf.default.disable_ipv6 = 1

net.ipv6.conf.all.diable_ipv6 = 1

# Controls source route verification

net.ipv4.conf.default.rp_filter = 1


# Do not accept source routing

net.ipv4.conf.default.accept_source_route = 0


# Controls the System Request debugging functionality of the kernel

kernel.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 syncookies

net.ipv4.tcp_syncookies = 1


# Disable netfilter on bridges.

net.bridge.bridge-nf-call-ip6tables = 0

net.bridge.bridge-nf-call-iptables = 0

net.bridge.bridge-nf-call-arptables = 0


# Controls the default maxmimum size of a mesage queue

kernel.msgmnb = 65536


# Controls the maximum size of a message, in bytes

kernel.msgmax = 65536


# Controls the maximum shared segment size, in bytes

kernel.shmmax = 68719476736


# Controls the maximum number of shared memory segments, in pages

kernel.shmall = 4294967296

fs.file-max = 999999 



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_fin_timeout = 30

net.ipv4.tcp_synack_retries = 1

net.core.somaxconn = 16384

net.core.netdev_max_backlog = 16384

net.core.wmem_max = 16777216

net.core.rmem_max = 16777216

net.ipv4.tcp_rmem = 1024 87380 12482912

net.ipv4.tcp_wmem = 1023 87380 21582912

net.ipv4.tcp_max_orphans = 16384

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://www.sa-log.com/59.html 

http://www.tiejiang.org/1253.html

http://alanwake.blog.51cto.com/6881848/1420807

http://chocolee.blog.51cto.com/8158455/1424587

http://www.tiejiang.org/1253.html

http://fengwan.blog.51cto.com/508652/1430307

http://nolinux.blog.51cto.com/4824967/1318607

http://www.skyshe.cn/783