文章转载至:“http://www.centoscn.com/CentOS/config/2013/1102/2034.html”
CentOS系统安装之后并不能立即投入生产环境使用,往往需要先经过我们运维人员的优化才行。在此讲解几点关于Linux系统安装后的基础优化操作。注意:本次优化都是基于CentOS(5.8/6.4)。
修改ip地址、网关、主机名、DNS等 关闭selinux,清空iptables 添加普通用户并进行sudo授权管理 更新yum源及必要软件安装 定时自动更新服务器时间 精简开机自启动服务 定时自动清理/var/spool/clientmqueue/目录垃圾文件,放置inode节点被占满 变更默认的ssh服务端口,禁止root用户远程连接 锁定关键文件系统 调整文件描述符大小 调整字符集,使其支持中文 去除系统及内核版本登录前的屏幕显示 内核参数优化
[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
[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
[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 #保存防火墙配置信息
[root@c64 ~]# useradd sunsky [root@c64 ~]# echo "123456"|passwd --stdin sunsky&&history –c [root@c64 ~]# visudo 在root ALL=(ALL) ALL此行下,添加如下内容 sunsky ALL=(ALL) ALL
[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
[root@c64 yum.repos.d]# yum clean all #清空yum缓存 [root@c64 yum.repos.d]# yum makecache #建立yum缓存
[root@c64 yum.repos.d]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY* #导入签名KEY到RPM [root@c64 yum.repos.d]# yum upgrade-y #更新系统内核到最新
[root@c64 yum.repos.d]# yum install lrzsz ntpdate sysstat -y
[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
[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
[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
[root@c64 ~]# echo '*/30 * * * * /bin/sh /server/scripts/spool_clean.sh >/dev/null 2>&1'>>/var/spool/cron/root
[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
[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
[root@c64 ~]# /bin/mv /usr/bin/chattr /usr/bin/任意名称
[root@localhost ~]# ulimit –n #查看文件描述符大小 1024 [root@localhost ~]# echo '* - nofile 65535' >> /etc/security/limits.conf
提示:也可以把ulimit -SHn 65535命令加入到/etc/rc.local,然后每次重启生效
[root@c64 ~]# cat >>/etc/rc.local<<EOF #open files ulimit -HSn 65535 #stack size ulimit -s 65535 EOF
扩展:文件描述符
sed-i 's#LANG="en_US.UTF-8"#LANG="zh_CN.GB18030"#'/etc/sysconfig/i18n source/etc/sysconfig/i18n
UTF-8 非定长 1-4字节广泛支持,MYSQL也使用UTF-8
[root@c64 ~]# >/etc/redhat-release [root@c64 ~]# >/etc/issue
[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 #使配置文件生效
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
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
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
modprobe ip_conntrack echo "modprobe ip_conntrack">> /etc/rc.local
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
modprobe nf_conntrack echo "modprobe nf_conntrack">> /etc/rc.local
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
modprobe bridge echo "modprobe bridge">> /etc/rc.local