在系統優化之前先簡單的說一下優化的原則,要做到最小化:
1、根據需求安裝系統最小化;
2、開啓程序最小化;
3、操作最小化;
4、登錄最小化;
5、權限最小化,就是使用普通用戶登錄。
6、配置參數合理化,不要最大化。
#修改配置文件的時候注意備份。
系統安裝之前包的選擇如下圖(centos爲例):
yum grouplist:
Installed Groups:
Base
Compatibility libraries
Debugging Tools
Development tools
Dial-up Networking Support
Hardware monitoring utilities
Performance Tools
如果忘記選包,安裝後可以按如下方式選包:
yum groupinstall "Compatibility libraries" "Base" "Development tools"
yum groupinstall "Performance Tools" "debugging Tools" "Dial-up Networking Support"
yum groupinstall "Base" "Development tools"
可以通過yum groupinfo 包組查看具體安裝的組件。
簡單優化項如下:
⑴:禁止root遠程登陸,創建賬戶monitor,修改端口22爲52113
#如果防火牆設置規則 ,勿忘記關閉,且修改防火牆配置。
配置文件修改操作步驟:
useradd monitor
passwd monitor nihao123!
mv /etc/ssh/sshd_config /etc/ssh/sshd_config.back
cat /etc/ssh/sshd_config.back |egrep -v "^#|^$" >/etc/ssh/sshd_config
添加配置:
Port 52113 #→ssh連接默認的端口修改
PermitRootLogin no #→root禁止遠程登陸。
PermitEmptyPasswords no #→禁止空密碼登陸
UseDNS no #→不使用DNS
重啓: /etc/init.d/sshd restart
⑵:防火牆設置;
#端口掃描命令: nmap 127.0.0.1 -p 1-65535,防火牆一般爲關閉狀態
1、清空iptables 設置: iptables -F iptables -X iptables -Z
2、配置允許(自己)52113登錄端口進入
iptables -t filter -A INPUT -p tcp --dport 52113 -s 10.XX.XX.XX -j ACCEPT
或開啓信任的iP網段
iptables -A INPUT -s xx.xx.xx.xx -p all -j ACCEPT
3、允許業務服務端口對外訪問(運行http服務無條件通過)
iptables -A INPUT -p tcp --dport (待定) -j ACCEPT
4、設置默認的防火牆禁止和允許規則。
iptable -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
⑶:關閉selinux
修改配置位置: cat -n /etc/selinux/config
修改配置項:7 SELINUX=disabled
命令修改: sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
查看:setenforce 1 — SELinux以強制(enforcing)模式運行
setenforce 0 — SELinux以警告(permissive)模式運行
getenforce查看selinux
備註:如果要徹底禁用SELinux 需要在/etc/sysconfig/selinux中設置參數selinux=0
⑷:關閉不需要的服務;
#開機精簡啓動,一般常用的服務sshd遠程連接;syelog 記錄系統日誌;network 網卡服務;crond 定時任務;。
查看運行級別: runlevel N 3
查看啓動的服務: chkconfig --list | grep 3:啓用 | awk '{print $1}'
需要開啓:crond network yum-updatesd sshd syslog
關閉: for kong in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|sshd|rsyslog"`;do chkconfig $kong off;done
開啓單個服務: chkconfig [--level<等級代號>][系統服務] on/off
範例: chkconfig --level 3 syslog on
結果: chkconfig --list|grep "3:啓用"
crond 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
network 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
sshd 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
syslog 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
⑸:時間同步:
echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root
檢查:crontab -l
⑹:設置文件打開最大數:
#數值適當調整以增加文件打開數量。可以加快服務性能:
文件路徑:/etc/security/limits.conf
查看:ulimit -a
設置:ulimit -n 65535
⑺:修改yum源,根據自己喜好,自由選擇:
文件路徑: 163:http://mirrors.163.com/.help/CentOS6-Base-163.repo
阿里雲:http://mirrors.aliyun.com/repo/Centos-6.repo
路徑:/etc/yum.repos.d
wget /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
⑻:鎖定密碼相關文件:
chattr +i 系統不允許對這個文件進行任何的修
chattr +i /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/inittab
查看:lsattr + 文件名
⑼:修改習慣的中文語言:
sed -i 's#LANG="en_US.UTF-8"#LANG="zh_CN.gb2312"#g' /etc/sysconfig/i18n
執行文件 ,且生效source /etc/sysonfig/i18n
#注意備份
⑽:關閉ctrl+alt+del重啓:
sed -i 's/^ca::ctrlaltdel:*/#ca::ctrlaltdel:*/' /etc/inittab
init q
⑾:修改系統運行級別爲:3(需要重啓生效):
sed -i 's/^id:5:initdefault:/id:3:initdefault:/' /etc/inittab
⑿:設置SSH的TIMEOUT時間限制爲10分鐘:
echo "TMOUT=600" >> /etc/profile
⒀:修改密碼有效期爲:91天,老用戶不生效,需用chage命令修改:
sed -i 's/^PASS_MAX_DAYS.*$/PASS_MAX_DAYS\t91/' /etc/login.defs
⒁:history記錄條目爲50條:
echo "HISTSIZE=50" >> /etc/profile
echo "HISTFILESIZE=50" >> /etc/profile
#histfilesize歷史記錄文件大小,例如10M或者50M
⒂:自動檢測.netrc & .rhosts文件,存在即刪除: FIND1=`find / -name .netrc` FIND2=`find / -name .rhosts` for NETRC in $FIND1 do if [ -f $NETRC ] ; then rm -f $NETRC fi done for RHOSTS in $FIND2 do if [ -f $RHOSTS ] ; then rm -f $RHOSTS fi done
⒃:各主目錄權限應爲750:
chmod 750 /home/*
chmod 700 /home/lost+found
⒄:用戶刪除或鎖定:
# passwd -l 鎖定用戶
# userdel -r 刪除用戶
# usermod -s /sbinlogin 禁止(ssh/telnet)登陸,但可以ftpchmod 755 /sbinlogin
usermod -s /sbinlogin daemon
usermod -s /sbinlogin bin
usermod -s /sbinlogin nobody
usermod -s /sbinlogin sshd
usermod -s /sbinlogin adm
usermod -s /sbinlogin bin
usermod -s /sbinlogin mail
usermod -s /sbinlogin operator
usermod -s /sbinlogin gopher
usermod -s /sbinlogin ftp
usermod -s /sbinlogin nobody
usermod -s /sbinlogin rpm
usermod -s /sbinlogin dbus
usermod -s /sbinlogin avahi
usermod -s /sbinlogin nscd
usermod -s /sbinlogin mailnull
usermod -s /sbinlogin smmsp
usermod -s /sbinlogin vcsa
usermod -s /sbinlogin haldaemon
usermod -s /sbinlogin rpc
usermod -s /sbinlogin rpcuser
usermod -s /sbinlogin nfsnobody
usermod -s /sbinlogin pcap
usermod -s /sbinlogin ntp
usermod -s /sbinlogin distcache
usermod -s /sbinlogin apache
usermod -s /sbinlogin webalizer
usermod -s /sbinlogin squid
usermod -s /sbinlogin named
usermod -s /sbinlogin xfs
usermod -s /sbinlogin gdm
usermod -s /sbinlogin sabayon
usermod -s /sbinlogin ais
usermod -s /sbinlogin pegasus
usermod -s /sbinlogin luci
usermod -s /sbinlogin piranha
usermod -s /sbinlogin ricc
usermod -s /sbinlogin uucp
usermod -s /sbinlogin games
usermod -s /sbinlogin lp
⒅:設置所有用戶umask 爲027:
sed -i 's/^UMASK.*$/UMASK\t027/' /etc/login.defs
⒆:刪除NIS:
i=`rpm -qa yp-tools`
n=`rpm -qa ypbind`
rpm -e $i $n
⒇:密碼策略:密碼有效期90天,最短長度爲8位,密碼重試3次後提示密碼錯誤,最多有3位與原密碼相同,至少有一個大寫字母、一個小寫字母、一個數字、一個字符:
cp -ap /etc/login.defs /etc/login.defs.back
sed -i 's/^PASS_MAX_DAYS.*$/PASS_MAX_DAYS\t90/' /etc/login.defs
sed -i 's/^PASS_MIN_LEN.*$/PASS_MIN_LEN\t8/' /etc/login.defs
cp -ap /etc/pam.d/system-auth /etc/pam.d/system-auth.back
sed -i 's/password.*pam_cracklib.so.*$/password requisite pam_cracklib.so try_first_pass retry=3 difok=3 dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1 minlen=8/g' /etc/pam.d/system-auth
#防火牆設置:
vi /etc/sysctl.conf
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
#超時時間調小
解決辦法:http://linuxboys.blog.51cto.com/9150052/1598234
報錯解決辦法:http://www.lvtao.net/server/centos-server-setup.html
注意系統版本號:
cat /etc/redhat-release
CentOS release 5.9 (Final)