title: linux 優化
tags: linux,優化,新裝
linux 優化
1 登錄安全
清空/etc/issue,/etc/issue.net,/etc/motd。因爲issue文件是系統的版本號信息,當登錄到系統的時候會進行提示,motd文件在登錄時會顯示裏面的字符串,可能包含敏感字符。
[root@server tmp]# > /etc/motd
[root@server tmp]# >/etc/issue
[root@server tmp]# >/etc/issue.net
2 更改主機名:
臨時更改:hostname maiyat
永久更改: vi /etc/sysconfig/network
主機名和ip做好映射:vi /etc/hosts
DNS地址在/etc/resolv.conf,但是因爲它是全局的,因此優先級比網卡的配置文件低一點。
[root@server tmp]# cat /etc/hosts
#127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 server
192.168.50.1 server
192.168.50.2 lamp02
192.168.50.3 lamp01
[root@server tmp]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=server
[root@server tmp]# hostname server
[root@server tmp]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
search localdomain
nameserver 192.168.211
3 安裝時最小化安裝,及啓動必要的服務
因爲系統安裝最小化安裝,所以以下包請注意安裝:yum install tree telnet dos2unix sysstat lrzsz vim man
以下5項服務建議開機啓動:systat,rsyslog,sshd,crond,network,其餘服務一併關閉,待需要的時候在打開。
[root@server tmp]# for n in `chkconfig --list |grep 3:on|awk -F " " '{print $1}'`; do chkconfig $n off;done && for n in sysstat rsyslog sshd crond network;do chkconfig $n on;done
[root@server tmp]#
[root@server tmp]#
[root@server tmp]# 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
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
或者
[root@server tmp]# for n in `chkconfig --list |grep 3:on|egrep -v "sysstat|rsyslog|sshd|crond|network"|awk -F " " '{print $1}'`;do chkconfig $n off;done
4 最好把/etc/rc.local當作系統開機啓動的檔案管理
因爲相對於chkconfig --list管理的啓動項有很多是系統的,但是rc.local裏的啓動都是用戶自己添加進去的,當一個公司的運維把設備上的服務啓動都追加到rc.local裏,並註釋清楚,這樣當該運維離職後,後面來的人只要看一下rc.local裏就可以清楚的知道該服務器啓動了一什麼服務了。
[root@server tmp]# /etc/init.d/smb start >> /etc/rc.local
[root@server tmp]# /etc/init.d/dhcpd start >> /etc/rc.local
[root@server tmp]# /etc/init.d/rpcbind start >> /etc/rc.local [root@server tmp]# /etc/init.d/nfs start >> /etc/rc.local
5 關閉selinux
[root@server tmp]# cp /etc/selinux/config /etc/selinux/config.org
[root@server tmp]# sed -i 's#SELINUX=enforcing#SELINUX=disable#g' /etc/selinux/config
[root@server tmp]# grep SELINUX=disable /etc/selinux/config
SELINUX=disable
因爲關閉selinux需要重啓系統,因此可以臨時更改如
[root@server tmp]# setenforce 0
[root@server tmp]# getenforce
Permissive
5 重要文件加鎖(防止***黑進系統後提權)
[root@server ~]# chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow
[root@server ~]# chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
[root@server ~]# mv /usr/bin/chattr ~/cha
6 文件描述符(ulimit -n)
當一個進程啓用就會佔用一個文件描述符,一般最大有65535個,而系統默認只有1024個
臨時設置: [root@server ~]# ulimit -SHn 65535
永久設置:
[root@server ~]# echo ' * - nofile 65535 ' >> /etc/security/limits.conf
[root@server ~]# ulimit -n
65535
7 字符集,配置文件在/etc/sysconfig/i18n
LANG="en_US.UTF-8" 或 "LANG=zh_CN.UTF-8"
8 同步時鐘:
yum install ntp -y
ntpdate 1.cn.pool.ntp.org
crontab -e
*/20 * * * * ntpdate 1.cn.pool.ntp.org
crontab -l
其中crontab的配置文件在/var/spool/cron/root
因此可以:
echo '*/20 * * * * ntpdate 1.cn.pool.ntp.org' >>/var/spool/cron/root
9 及時清理sendmail臨郵件存放目錄
find /var/spool/clientmqueue -type f |xargs rm -f
10 設置300秒不使用進入超時
export TMOUT=300
echo "export TMOUT=300" >>/etc/profile
11 平時登錄系統時使用普通用戶
當權限不夠時使用su - 進行切換,平時設置可以配置sudo,其中配置文件爲 /etc/sudoers ,編輯時最好使用visudo
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
ouyan ALL=(ALL) ALL
ouyan ALL=(ALL) NOPASSWD: ALL
test ALL=(ALL) NOPASSWD: /usr/sbin/useradd, /usr/bin/passwd
root ALL=(ALL) ALL
root ALL=(ALL) ALL
[root@server ~]# visudo -c
其中visudo檢查visudo的語法,其中 NOPASSWD: buxu
不需要密碼,/usr/sbin/useradd可以使用的命令,如果是ALL,則可以shiy使用所有命令
12 升級最新補丁
yum update
yum install openssl bash -y
注意yum update是全部升級,一定要新裝系統纔可以執行這個
13 注意/proc裏查看系統信息
[root@server proc]# ls -l meminfo cpuinfo mounts loadavg ioports filesystems devices
-r--r--r--. 1 root root 0 May 18 01:12 cpuinfo
-r--r--r--. 1 root root 0 May 18 01:12 devices
-r--r--r--. 1 root root 0 May 18 01:12 filesystems
-r--r--r--. 1 root root 0 May 18 01:12 ioports 正在使用的端口
-r--r--r--. 1 root root 0 May 18 01:12 loadavg 負載信息
-r--r--r--. 1 root root 0 May 18 01:12 meminfo
lrwxrwxrwx. 1 root root 11 May 18 01:12 mounts -> self/mounts
14 禁ping
不過要小心處理,雖然可以防止ping***,但是自己調試也不方便,一般用防火牆echo " net.ipv4.icmp_eth0_ignore_all=1" >>/etc/sysctl.conf<br/>sysctl -p 配置生效