本文 centos 6.5 優化 的項有18處:
1、centos6.5最小化安裝後啓動網卡
2、ifconfig查詢IP進行SSH鏈接
3、更新系統源並且升級系統
4、系統時間更新和設定定時任
5、修改ip地址、網關、主機名、DNS
6、關閉selinux,清空iptables
7、創建普通用戶並進行sudo授權管理
8、修改SSH端口號和屏蔽root賬號遠程登陸
9、鎖定關鍵文件系統(禁止非授權用戶獲得權限)
10、精簡開機自啓動服務
11、調整系統文件描述符大小
12、設置系統字符集
13、清理登陸的時候顯示的系統及內核版本
14、內核參數優化
15、定時清理/var/spool/clientmqueue
16、刪除不必要的系統用戶和羣組
17、關閉重啓ctl-alt-delete組合鍵
18、設置一些全局變量
1、啓動網卡
Shell
1 2 | #centos6.x最小化安裝後,網卡默認不是啓動狀態 ifup eth0 |
2、SSH鏈接 ifconfig 查看IP後SSH終端連接。
3、更新源 最小化安裝是沒有wget工具的,必須先安裝在修改源)
Shell
1 | yum install wget |
備份原系統更新源
Shell
1 | mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup |
進入yum.repos.d目錄
Shell
1 | cd /etc/yum.repos.d |
下載網易鏡像源或者搜狐鏡像源
Shell
1 2 3 4 5 | #下載網易鏡像源: wget http://mirrors.163.com/.help/CentOS6-Base-163.repo #或者 #下載搜狐鏡像源: wget http://mirrors.sohu.com/help/CentOS-Base-sohu.repo |
網易搜狐的源可能有問題。
參考http://blog.csdn.net/ichsonx/article/details/8518420
Shell
1 2 3 4 5 6 7 8 | 清空yum緩存 yum clean all 生存緩存 yum makecache 開始更新系統以及內核 yum upgrade 必備軟件 yum install lrzsz ntpdate sysstat -y |
使用lrzsz工具常用操作:rz 上傳 sz 下載
4、系統時間更新和設定定時任務 第一種:更新時間並且寫入BOIS
Shell
1 | ntpdate time.windows.com && hwclock -w && hwclock --systohc |
第二種:更新時間並且寫入定時任務
Shell
1 | echo '*/30 * * * * ntpdate time.windows.com && hwclock -w && hwclock --systohc >/dev/null 2>&1' >>/var/spool/cron/root |
第三種:每間隔5分鐘和10分鐘同步一次時間
Shell
1 2 | 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的時間同步命令路徑不一樣 6是/usr/sbin/ntpdate 5是/sbin/ntpdate
5、修改ip地址、網關、主機名、DNS #eth0 網卡設置
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #網卡設備名稱 HWADDR=00:0C:29:D0:C7:B5 #以太網設備的對應的物理地址 TYPE=Ethernet #網絡類型爲以太網模式 UUID=080a457b-6a53-4a3a-9155-a23c1146c2c6 #通用唯一識別碼 ONBOOT=yes #是否啓動引導的時候激活YES NM_CONTROLLED=no #設備eth0是否可以由Network Manager圖形管理工具託管 BOOTPROTO=dhcp #靜態IP地址獲取狀態 如:DHCP表示自動獲取IP地址 IPADDR=192.168.1.10 #IP IPV6INIT=no IPV6_AUTOCONF=no NETMASK=255.255.255.0 #網卡對應的網絡掩碼 GATEWAY=192.168.1.1 #網關地址 |
檢查網卡配置
Shell
1 | cat /etc/sysconfig/network-scripts/ifcfg-eth0 |
網關配置
Shell
1 2 3 4 5 6 7 | vi /etc/sysconfig/network #表示系統是否使用網絡,一般設置爲yes。如果設爲no,則不能使用網絡,而且很多系統服務程序將無法啓動 NETWORKING=yes #設置本機的主機名,這裏設置的主機名要和/etc/hosts中設置的主機名對應 HOSTNAME=c65mini.localdomain #設置本機連接的網關的IP地址。例如,網關爲10.0.0.1或者192.168.1.1 GATEWAY=192.168.1.1 |
修改主機DNS
Shell
1 2 3 4 | vi /etc/resolv.conf ; generated by /sbin/dhclient-script nameserver 8.8.8.8 nameserver 4.4.4.4 |
修改HOSTS
Shell
1 2 3 4 5 6 7 8 | vi /etc/hosts 127.0.0.1 c65mini.localdomain #使用DNS域名服務器來解析名字 order bind hosts #一臺主機是否存在多個IP multi on #如果用逆向解析找出與指定的地址匹配的主機名,對返回的地址進行解析以確認它確實與您查詢的地址相配。爲了防止“騙取”IP地址 nospoof on |
重啓網卡生效設置兩種方法
Shell
1 2 3 | service network restart 或者 /etc/init.d/network restart |
6、關閉selinux,清空iptables 在服務器配置完全成功後各項服務正常後,在開啓selinux
查看selinux狀態
第一種方法:/usr/bin/setstatus -v #如果顯示:SELinux status: enabled 就是開啓狀態
第二種方法:cat /etc/selinux/config #如果顯示:SELINUX=enforcing 則是開啓狀態permissive有提醒的狀態 disabled是關閉
第三種方法:
grep SELINUX=disabled /etc/selinux/config
第四種方法:getenforce
修改selinux狀態 如果修改配置文件則永久生效,但是必須要重啓系統
第一種:vi /etc/selinux/config 修改 SELINUX=disabled
第二種:
sed –i ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/config
如果想立即生效(如果想臨時性的改變) setenforce 0
setenforce 1 設置SELinux 成爲enforcing模式 setenforce 0 設置SELinux 成爲permissive模式 查看狀態 getenforce
iptables防火牆規則清理了,根據需求定製
Shell
1 2 3 4 5 6 | #清空iptables規則 iptables -F #查看iptables規則 iptables -L #保存規則,注意,雖然清空了,不保存的話,重啓後,又會有規則。 /etc/init.d/iptables save |
7、創建普通用戶並進行sudo授權管理
創建普通用戶 useradd bingoku 修改用戶密碼 passwd bingoku
另一種方式:一次性創建用戶和設置密碼 echo "123456"|passwd --stdin bingoku&&history –c
其中bingoku爲你創建的用戶名
sudo授權管理 打開sudo配置文件 visudo
Shell
1 2 3 4 | #按:set nu 查看行,找到99行 root ALL=(ALL) ALL #添加 bingoku ALL=(ALL) ALL |
8、修改SSH端口號和屏蔽root賬號遠程登陸
1 2 3 4 5 6 7 8 9 10 11 12 | #備份SSH配置 cp /etc/ssh/sshd_config sshd_config_bak #修改SSH安全配置 vi /etc/ssh/sshd_config #SSH鏈接默認端口 port 52113 #禁止root賬號登陸 PermitRootLogin no #禁止空密碼 PermitEmptyPasswords no #不使用DNS UseDNS no |
重新載入SSH配置 /etc/init.d/sshd reload
查看端口裏面是否有剛纔修改過的端口號52113
Shell
1 2 | netstat -lnt
|
或者反查端口是那個進程
Shell
1 2 | lsof -i tcp:52113
|
centos6.5最小化安裝沒有lsof工具需要 yum install lsof
9、鎖定關鍵文件系統(禁止非授權用戶獲得權限)
Shell
1 2 3 4 5 | chattr +i /etc/passwd chattr +i /etc/inittab chattr +i /etc/group chattr +i /etc/shadow chattr +i /etc/gshadow |
10、精簡開機自啓動服務
注意: 剛裝完操作系統一般可以只保留crond,network,syslog,sshd這四個服務。 後期根據業務需求制定自啓服務 #(Centos6.x爲rsyslog Cetnos5.x爲syslog) 如果是中文的話。可能會需要LANG=en 或者替換 3:on 成 3:啓用
Shell
1 2 3 4 5 6 7 8 9 10 11 | #關閉全部服務 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
#開啓需要的服務 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 |
查詢下開啓的服務 chkconfig –list | grep 3:on 或者 chkconfig –list|grep 3:啓用
Shell
1 2 3 4 5 6 7 | [root@c65mini ~]$ chkconfig --list|grep 3:啓用 crond 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉 ip6tables 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉 iptables 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉 network 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉 rsyslog 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉 sshd 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉 |
11、調整文件描述符大小
1 2 | #查看文件描述符大小 ulimit -n |
第一種:#這裏參考的是阿里雲主機默認設置。
Shell
1 2 3 4 5 6 7 | 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
第三種:把ulimit -SHn 65535命令加入到/etc/rc.local,然後每次重啓生效 追加命令到rc.local配置文件裏面
Shell
1 2 3 4 5 6 | cat >>/etc/rc.local<<EOF #open files ulimit -HSn 65535 #stack size ulimit -s 65535 EOF |
第四種:如果不修改limits配置文件,直接立即生效,但重啓後又恢復之前的默認。 ulimit -SHn 65535
12、設置系統字符集
第一種:vi /etc/sysconfig/i18n
如果想用中文提示:LANG=”zh_CN.UTF-8″ 如果想用英文提示:LANG=”en_US.UTF-8″ 如果臨時切換也可以 LANG=zh_CN.UTF-8
第二種:使用sed快速替換
1 2 3 4 5 6 | #替換成英文 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、清理登陸的時候顯示的系統及內核版本
Shell
1 2 3 4 5 | #查看登陸信息 cat /etc/redhat-release cat /etc/issue #清理登陸信息 echo >/etc/redhat-release echo >/etc/issue |
14、內核參數優化 vi /etc/sysctl.conf
Shell
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 32 33 34 | #可用於apache,nginx,squid多種等web應用 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
#以下參數是對centos6.x的iptables防火牆的優化,防火牆不開會有提示,可以忽略不理。 #如果是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可能會報錯
1 2 3 | 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 |
出現這個的原因是,沒有自動載入bridge橋接模塊
Shell
1 2 | modprobe bridge echo "modprobe bridge">> /etc/rc.local |
查看橋接 lsmod|grep bridge
centos5.X可能會報錯 這個錯誤可能是你的防火牆沒有開啓或者自動處理可載入的模塊ip_conntrack沒有自動載入,解決辦法有二,一是開啓防火牆,二是自動處理開載入的模塊ip_conntrack
Shell
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 |
centos5.X解決方法:
Shell
1 2 | modprobe ip_conntrack echo "modprobe ip_conntrack">> /etc/rc.local |
centos6.X可能會報錯 這個錯誤可能是你的防火牆沒有開啓或者自動處理可載入的模塊ip_conntrack沒有自動載入,解決辦法有二,一是開啓防火牆,二是自動處理開載入的模塊ip_conntrack
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 |
centos6.X解決方法:
Shell
1 2 | modprobe nf_conntrack echo "modprobe nf_conntrack">> /etc/rc.local |
注意:筆者在整理這篇centos6.5內核優化的時候發現,如果不開啓ip6tables去優化nf_conntrack模塊去執行上面的解決方法會依舊提示上面的error。所以在優化服務的時候,可以選擇留下iptables和ip6tables。當然如果不用iptables的話,在內核優化的時候就要去掉對nf_conntrack的設置,在進行/sbin/sysctl -p 是不會有錯誤提示的。
15、如果安裝sendmail必須定時自動清理/var/spool/clientmqueue/下文件防止inode節點被佔滿
Shell
1 2 3 4 5 | #centos6.5已經不自動安裝sendmail了所以沒必要走這一步優化 mkdir -p /server/scripts vi /server/scripts/spool_clean.sh #!/bin/sh find/var/spool/clientmqueue/-typef -mtime +30|xargsrm-f |
16、刪除不必要的系統用戶和羣組
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #刪除不必要的用戶 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、關閉重啓ctl-alt-delete組合鍵
Shell
1 2 3 | vi /etc/init/control-alt-delete.conf #註釋掉 #exec /sbin/shutdown -r now "Control-Alt-Deletepressed" |
18、設置一些全局變量
Shell
1 2 3 4 5 6 | #設置自動退出終端,防止非法關閉ssh客戶端造成登錄進程過多,可以設置大一些,單位爲秒 echo "TMOUT=3600">> /etc/profile #歷史命令記錄數量設置爲10條 sed -i "s/HISTSIZE=1000/HISTSIZE=10/" /etc/profile #立即生效 source /etc/profile |