Centos6.5生產環境最小化優化配置,知足業務需求!node
#centos6.x最小化安裝後,網卡默認不是啓動狀態linux
ifup eth0 // ifconfig eth0 upnginx
/etc/sysconfig/network-scripts/ifcfg-eth0 #網卡網絡配置web
/etc/udev/rules.d/70-persistent-net.rules #網卡命名規則apache
ifconfig 查看IP後SSH終端鏈接windows
service sshd restart && chkconfig sshd oncentos
/etc/ssh/sshd_config #服務端配置文件緩存
是沒有wget工具的,必須先安裝在修改源安全
yum install wgetbash
備份原系統更新源
Mv /etc/yum.repos.d/CentOS-Base.repo
/etc/yum.repos.d/CentOS-Base.repo.backup
進入yum.repos.d目錄
cd /etc/yum.repos.d
#下載網易鏡像源:
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
#或者
#下載搜狐鏡像源:
wget http://mirrors.sohu.com/help/CentOS-Base-sohu.repo
#阿里雲鏡像
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
清空yum緩存
yum clean all
生存緩存
yum makecache
檢測包的數量和狀態
yum repolist
開始更新系統以及內核
yum upgrade
#生產環境都是純內網環境,所以需在內部搭建yum_source
時間同步
yum install ntpdate –y
ntpdate time.windows.com && hwclock -w && hwclock --systohc
#同步內部時鐘服務器
#crontab -e */10 * * * * /usr/sbin/ntpdate 時鐘服務器IP >/dev/null &
提示:CentOS 6.x的時間同步命令路徑不同 6是/usr/sbin/ntpdate 5是/sbin/ntpdate
備份ifcfg-eth0
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圖形管理工具託管 #圖形化 nm-connection-editor
BOOTPROTO=dhcp | static #靜態IP地址獲取狀態DHCP表示自動獲取IP地址
IPADDR=192.168.1.10 #IP
IPV6INIT=no #關閉ipv6
IPV6_AUTOCONF=no
NETMASK=255.255.255.0 #網卡對應的網絡掩碼
GATEWAY=192.168.1.1 #網關地址
檢查網卡配置
cat /etc/sysconfig/network-scripts/ifcfg-eth0
網關配置
vi /etc/sysconfig/network
#表示系統是否使用網絡,通常設置爲yes。若是設爲no,則不能使用網絡
NETWORKING=yes | no
#設置本機的主機名,這裏設置的主機名要和/etc/hosts中設置的主機名對應
HOSTNAME=c65mini.localdomain
#設置本機鏈接的網關的IP地址。例如,網關爲10.0.0.1或者192.168.1.1
GATEWAY=192.168.1.1
修改主機DNS
vi /etc/resolv.conf
; generated by /sbin/dhclient-script
nameserver 8.8.8.8 223.5.5.5
nameserver 4.4.4.4 223.6.6.6
修改HOSTS
vi /etc/hosts
127.0.0.1 lvtao.localdomain
#使用DNS域名服務器來解析名字
order bind hosts
#一臺主機是否存在多個IP
multi on
#若是用逆向解析找出與指定的地址匹配的主機名,對返回的地址進行解析以確認它確實與您查詢的地址相配。爲了防止「騙取」IP地址
nospoof on
sysctl -n kernel.hostname #查看主機名
sysctl –w kernel.hostname mvpbang #修改主機名
重啓網卡生效設置兩種方法
service network restart
或者
/etc/init.d/network restart
在服務器配置徹底成功後各項服務正常後,在開啓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防火牆規則清理了,根據需求定製
#清空iptables規則
iptables -F
#查看iptables規則
iptables -L
#保存規則,注意,雖然清空了,不保存的話,重啓後,又會有規則。
/etc/init.d/iptables save
建立普通用戶 useradd lvtao 修改用戶密碼 passwd mvp
一次性建立用戶設置密碼 echo "123456"| passwd --stdin mvp&&history –c
sudo受權管理sudo配置文件vi /etc/sudoers
#按:set nu 查看行,找到99行
root ALL=(ALL) ALL
#添加
lvtao ALL=(ALL) ALL
#備份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 #局域網通常使用內部的DNS服務
從新載入SSH配置 /etc/init.d/sshd reload 查看端口裏面是否有剛纔修改過的端口號52113
netstat -lnt
或者反查端口是那個進程
lsof -i tcp:52113
centos6.5最小化安裝沒有lsof工具須要 yum install lsof
chattr +i /etc/passwd
chattr +i /etc/inittab
chattr +i /etc/group
chattr +i /etc/shadow
chattr +i /etc/gshadow
chattr +i ~.bash_history
注意: 剛裝完操做系統通常能夠只保留crond,network,syslog,sshd這四個服務
後期根據業務需求制定自啓服務 #(Centos6.x爲rsyslog Cetnos5.x爲syslog) 若是是中文的話。可能會須要LANG=en 或者替換 3:on 成 3:啓用
#關閉所有服務
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:啓用
[bingoku@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:關閉
#查看文件描述符大小
ulimit -n
第一種:#參考的是阿里雲主機默認設置
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配置文件裏面
cat >>/etc/rc.local<<EOF
#open files
ulimit -HSn 65535
#stack size
ulimit -s 65535
EOF
第四種:若是不修改limits配置文件,直接當即生效,但重啓後又恢復以前的默認。 ulimit -SHn 65535
第一種:vi /etc/sysconfig/i18n
若是想用中文提示:LANG=」zh_CN.UTF-8″
若是想用英文提示:LANG=」en_US.UTF-8″
若是臨時切換也能夠 export LANG=zh_CN.UTF-8
第二種:使用sed快速替換
#替換成英文
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
#查看登錄信息
cat /etc/redhat-release cat /etc/issue
#清理登錄信息
echo >/etc/redhat-release
echo >/etc/issue
vi /etc/sysctl.conf
#可用於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可能會報錯
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橋接模塊
modprobe bridge
echo "modprobe bridge">> /etc/rc.local
查看橋接 lsmod|grep bridge
centos5.X可能會報錯 這個錯誤多是你的防火牆沒有開啓或者自動處理可載入的模塊ip_conntrack沒有自動載入,解決辦法有二,一是開啓防火牆,二是自動處理開載入的模塊ip_conntrack
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可能會報錯 這個錯誤多是你的防火牆沒有開啓或者自動處理可載入的模塊ip_conntrack沒有自動載入,解決辦法有二,一是開啓防火牆,二是自動處理開載入的模塊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內核優化的時候發現,若是不開啓ip6tables去優化nf_conntrack模塊去執行上面的解決方法會依舊提示上面的error。因此在優化服務的時候,能夠選擇留下iptables和ip6tables。固然若是不用iptables的話,在內核優化的時候就要去掉對nf_conntrack的設置,在進行/sbin/sysctl -p 是不會有錯誤提示的。
#centos6.5已經不自動安裝sendmail了因此不必走這一步優化
mkdir -p /server/scripts
vi /server/scripts/spool_clean.sh
#!/bin/sh
find/var/spool/clientmqueue/-typef -mtime +30|xargs rm-f
#刪除沒必要要的用戶
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
vi /etc/init/control-alt-delete.conf
#註釋掉
#exec /sbin/shutdown -r now "Control-Alt-Deletepressed"
#設置自動退出終端,防止非法關閉ssh客戶端形成登陸進程過多,能夠設置大一些,單位爲秒
echo "TMOUT=3600">> /etc/profile #1h後自動退出
#歷史命令記錄數量設置爲10條
sed -i "s/HISTSIZE=1000/HISTSIZE=10/" /etc/profile
#當即生效
source /etc/profile