本文 centos 6.5 優(yōu)化 的項(xiàng)有18處: #centos6.x最小化安裝后,網(wǎng)卡默認(rèn)不是啟動(dòng)狀態(tài) ifup eth0 2、SSH鏈接 ifconfig 查看IP后SSH終端連接。 yum install wget 備份原系統(tǒng)更新源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 進(jìn)入yum.repos.d目錄 cd /etc/yum.repos.d 下載網(wǎng)易鏡像源或者搜狐鏡像源 #下載網(wǎng)易鏡像源: wget http://mirrors.163.com/.help/CentOS6-Base-163.repo #或者 #下載搜狐鏡像源: wget http://mirrors.sohu.com/help/CentOS-Base-sohu.repo 網(wǎng)易搜狐的源可能有問(wèn)題。 清空yum緩存 yum clean all 生存緩存 yum makecache 開始更新系統(tǒng)以及內(nèi)核 yum upgrade 必備軟件 yum install ntpdate -y 4、系統(tǒng)時(shí)間更新和設(shè)定定時(shí)任務(wù) 第一種:更新時(shí)間并且寫入BOIS ntpdate time.windows.com && hwclock -w && hwclock --systohc 或者:查看時(shí)間服務(wù)器的時(shí)間: # rdate time-b.nist.gov 設(shè)置時(shí)間和時(shí)間服務(wù)器同步: # rdate -s time-b.nist.gov 第二種:更新時(shí)間并且寫入定時(shí)任務(wù) echo '*/30 * * * * ntpdate time.windows.com && hwclock -w && hwclock --systohc >/dev/null 2>&1' >>/var/spool/cron/root 第三種:每間隔5分鐘和10分鐘同步一次時(shí)間 echo '*/5 * * * * /usr/sbin/ntpdate time.windows.com >/dev/null 2 >&1' >>/var/spool/cron/root echo '*/10 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root 提示:CentOS 6.x的時(shí)間同步命令路徑不一樣 6是/usr/sbin/ntpdate 5是/sbin/ntpdate mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #網(wǎng)卡設(shè)備名稱 HWADDR=00:0C:29:D0:C7:B5 #以太網(wǎng)設(shè)備的對(duì)應(yīng)的物理地址 TYPE=Ethernet #網(wǎng)絡(luò)類型為以太網(wǎng)模式 UUID=080a457b-6a53-4a3a-9155-a23c1146c2c6 #通用唯一識(shí)別碼 ONBOOT=yes #是否啟動(dòng)引導(dǎo)的時(shí)候激活YES NM_CONTROLLED=no #設(shè)備eth0是否可以由Network Manager圖形管理工具托管 BOOTPROTO=dhcp #靜態(tài)IP地址獲取狀態(tài) 如:DHCP表示自動(dòng)獲取IP地址 IPADDR=192.168.1.10 #IP IPV6INIT=no IPV6_AUTOCONF=no NETMASK=255.255.255.0 #網(wǎng)卡對(duì)應(yīng)的網(wǎng)絡(luò)掩碼 GATEWAY=192.168.1.1 #網(wǎng)關(guān)地址 檢查網(wǎng)卡配置 cat /etc/sysconfig/network-scripts/ifcfg-eth0 網(wǎng)關(guān)配置 vi /etc/sysconfig/network #表示系統(tǒng)是否使用網(wǎng)絡(luò),一般設(shè)置為yes。如果設(shè)為no,則不能使用網(wǎng)絡(luò),而且很多系統(tǒng)服務(wù)程序?qū)o(wú)法啟動(dòng) NETWORKING=yes #設(shè)置本機(jī)的主機(jī)名,這里設(shè)置的主機(jī)名要和/etc/hosts中設(shè)置的主機(jī)名對(duì)應(yīng) HOSTNAME=c65mini.localdomain #設(shè)置本機(jī)連接的網(wǎng)關(guān)的IP地址。例如,網(wǎng)關(guān)為10.0.0.1或者192.168.1.1 GATEWAY=192.168.1.1 修改主機(jī)DNS vi /etc/resolv.conf ; generated by /sbin/dhclient-script nameserver 8.8.8.8 nameserver 4.4.4.4 修改HOSTS vi /etc/hosts 127.0.0.1 lvtao.localdomain #使用DNS域名服務(wù)器來(lái)解析名字 order bind hosts #一臺(tái)主機(jī)是否存在多個(gè)IP multi on #如果用逆向解析找出與指定的地址匹配的主機(jī)名,對(duì)返回的地址進(jìn)行解析以確認(rèn)它確實(shí)與您查詢的地址相配。為了防止“騙取”IP地址 nospoof on 重啟網(wǎng)卡生效設(shè)置兩種方法 service network restart 或者 /etc/init.d/network restart 6、關(guān)閉selinux,清空iptables 在服務(wù)器配置完全成功后各項(xiàng)服務(wù)正常后,在開啟selinux
修改selinux狀態(tài) 如果修改配置文件則永久生效,但是必須要重啟系統(tǒng)
如果想立即生效(如果想臨時(shí)性的改變) setenforce 0 #清空iptables規(guī)則 iptables -F #查看iptables規(guī)則 iptables -L #保存規(guī)則,注意,雖然清空了,不保存的話,重啟后,又會(huì)有規(guī)則。 /etc/init.d/iptables save 7、創(chuàng)建普通用戶并進(jìn)行sudo授權(quán)管理 創(chuàng)建普通用戶 useradd lvtao 修改用戶密碼 passwd lvtao #按:set nu 查看行,找到99行 root ALL=(ALL) ALL #添加 lvtao ALL=(ALL) ALL 8、修改SSH端口號(hào)和屏蔽root賬號(hào)遠(yuǎn)程登陸 #備份SSH配置 cp /etc/ssh/sshd_config sshd_config_bak #修改SSH安全配置 vi /etc/ssh/sshd_config #SSH鏈接默認(rèn)端口 port 52113 #禁止root賬號(hào)登陸 PermitRootLogin no #禁止空密碼 PermitEmptyPasswords no #不使用DNS UseDNS no 重新載入SSH配置 /etc/init.d/sshd reload 查看端口里面是否有剛才修改過(guò)的端口號(hào)52113 netstat -lnt 或者反查端口是那個(gè)進(jìn)程 lsof -i tcp:52113 centos6.5最小化安裝沒(méi)有l(wèi)sof工具需要 yum install lsof chattr +i /etc/passwd chattr +i /etc/inittab chattr +i /etc/group chattr +i /etc/shadow chattr +i /etc/gshadow 10、精簡(jiǎn)開機(jī)自啟動(dòng)服務(wù) #關(guān)閉全部服務(wù) for sun in `chkconfig --list|grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $sun off;done #或者 for sun in `chkconfig --list|grep 3:啟用|awk '{print $1}'`;do chkconfig --level 3 $sun off;done #開啟需要的服務(wù) for sun in crond rsyslog sshd network;do chkconfig --level 3 $sun on;done #或者需要使用防火墻的話可以開啟iptables和ip6tables for sun in crond rsyslog sshd network iptables ip6tables;do chkconfig --level 3 $sun on;done 查詢下開啟的服務(wù) chkconfig –list | grep 3:on 或者 chkconfig –list|grep 3:啟用 [bingoku@c65mini ~]$ chkconfig --list|grep 3:啟用 crond 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉 ip6tables 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉 iptables 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉 network 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉 rsyslog 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉 sshd 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉 11、調(diào)整文件描述符大小 #查看文件描述符大小 ulimit -n 第一種:#這里參考的是阿里云主機(jī)默認(rèn)設(shè)置。 vi /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535 * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 第二種:echo '* - nofile 65535' >> /etc/security/limits.conf cat >>/etc/rc.local<<EOF #open files ulimit -HSn 65535 #stack size ulimit -s 65535 EOF 第四種:如果不修改limits配置文件,直接立即生效,但重啟后又恢復(fù)之前的默認(rèn)。 ulimit -SHn 65535 #替換成英文 sed -i 's#LANG="zh_CN.*"#LANG="en_US.UTF-8"#' /etc/sysconfig/i18n #替換成中文 sed -i 's#LANG="en_US.*"#LANG="zh_CN.UTF-8"#' /etc/sysconfig/i18n #替換成UTF-8中文 sed -i 's#LANG="zh_CN.*"#LANG="zh_CN.UTF-8"#' /etc/sysconfig/i18n 13、清理登陸的時(shí)候顯示的系統(tǒng)及內(nèi)核版本 #查看登陸信息 cat /etc/redhat-release cat /etc/issue #清理登陸信息 echo >/etc/redhat-release echo >/etc/issue 14、內(nèi)核參數(shù)優(yōu)化 vi /etc/sysctl.conf #可用于apache,nginx,squid多種等web應(yīng)用 net.ipv4.tcp_max_syn_backlog = 65536 net.core.netdev_max_backlog = 32768 net.core.somaxconn = 32768 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_tw_recycle = 1 #net.ipv4.tcp_tw_len = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_max_orphans = 3276800 #net.ipv4.tcp_fin_timeout = 30 #net.ipv4.tcp_keepalive_time = 120 net.ipv4.ip_local_port_range = 1024 65535 #以下參數(shù)是對(duì)centos6.x的iptables防火墻的優(yōu)化,防火墻不開會(huì)有提示,可以忽略不理。 #如果是centos5.X需要吧netfilter.nf_conntrack替換成ipv4.netfilter.ip #centos5.X為net.ipv4.ip_conntrack_max = 25000000 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 立即生效 /sbin/sysctl -p centos6.5可能會(huì)報(bào)錯(cuò) error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key error: "net.bridge.bridge-nf-call-iptables" is an unknown key error: "net.bridge.bridge-nf-call-arptables" is an unknown key 出現(xiàn)這個(gè)的原因是,沒(méi)有自動(dòng)載入bridge橋接模塊 modprobe bridge echo "modprobe bridge">> /etc/rc.local 查看橋接 lsmod|grep bridge 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 centos5.X解決方法: modprobe ip_conntrack echo "modprobe ip_conntrack">> /etc/rc.local centos6.X可能會(huì)報(bào)錯(cuò) 這個(gè)錯(cuò)誤可能是你的防火墻沒(méi)有開啟或者自動(dòng)處理可載入的模塊ip_conntrack沒(méi)有自動(dòng)載入,解決辦法有二,一是開啟防火墻,二是自動(dòng)處理開載入的模塊ip_conntrack 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 centos6.X解決方法: modprobe nf_conntrack echo "modprobe nf_conntrack">> /etc/rc.local 注意:筆者在整理這篇centos6.5內(nèi)核優(yōu)化的時(shí)候發(fā)現(xiàn),如果不開啟ip6tables去優(yōu)化nf_conntrack模塊去執(zhí)行上面的解決方法會(huì)依舊提示上面的error。所以在優(yōu)化服務(wù)的時(shí)候,可以選擇留下iptables和ip6tables。當(dāng)然如果不用iptables的話,在內(nèi)核優(yōu)化的時(shí)候就要去掉對(duì)nf_conntrack的設(shè)置,在進(jìn)行/sbin/sysctl -p 是不會(huì)有錯(cuò)誤提示的。 #centos6.5已經(jīng)不自動(dòng)安裝sendmail了所以沒(méi)必要走這一步優(yōu)化 mkdir -p /server/scripts vi /server/scripts/spool_clean.sh #!/bin/sh find/var/spool/clientmqueue/-typef -mtime +30|xargsrm-f 16、刪除不必要的系統(tǒng)用戶和群組 #刪除不必要的用戶 userdel adm userdel lp userdel sync userdel shutdown userdel halt userdel news userdel uucp userdel operator userdel games userdel gopher userdel ftp #刪除不必要的群組 groupdel adm groupdel lp groupdel news groupdel uucp groupdel games groupdel dip groupdel pppusers 17、關(guān)閉重啟ctl-alt-delete組合鍵 vi /etc/init/control-alt-delete.conf #注釋掉 #exec /sbin/shutdown -r now "Control-Alt-Deletepressed" 18、設(shè)置一些全局變量 #設(shè)置自動(dòng)退出終端,防止非法關(guān)閉ssh客戶端造成登錄進(jìn)程過(guò)多,可以設(shè)置大一些,單位為秒 echo "TMOUT=3600">> /etc/profile #歷史命令記錄數(shù)量設(shè)置為10條 sed -i "s/HISTSIZE=1000/HISTSIZE=10/" /etc/profile #立即生效 source /etc/profile |
|