linux基礎優化-轉自www.bravezhang.cn

linux系統調優及安全設置
1.1 關閉SELinux功能
1.1.1 修改配置文件,使關閉SElinux永久生效
[root@brave ~]# sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#g’ /etc/selinux/config

###修改時備份原文件

sed -i.ori.20161208 ‘s#SELINUX=enforcing#SELINUX=disabled#g’ /etc/selinux/config

###修改玩後檢查

[root@brave ~]# grep “SELINUX=disabled” /etc/selinux/config

SELINUX=disabled

1.1.2 臨時關閉SELinux
在生產環境下不能隨便重啓系統,所以一般建議使用此方法關閉

####查看SELinux當前狀態

[root@brave ~]# getenforce

Enforcing

####設置SELinux狀態

[root@brave ~]# setenforce

usage: setenforce [ Enforcing | Permissive | 1 | 0 ]

####0表示Permissive即給出警示提示,但不會阻止操作,相當於disabled ——數字1表示enforcing,即SELinux狀態爲開啓

####臨時將SELinux狀態修改爲Permissive狀態

[root@brave ~]# setenforce 0

####檢查狀態

[root@brave ~]# getenforce

Permissive

1.2 關閉iptables
1.2.1 臨時關閉防火牆
[root@localhost ~]# /etc/init.d/iptables stop

iptables: Setting chains to policy ACCEPT: filter [ OK ]

iptables: Flushing firewall rules: [ OK ]

iptables: Unloading modules: [ OK ]

[root@localhost ~]# /etc/init.d/iptables stop

[root@localhost ~]# /etc/init.d/iptables status

iptables: Firewall is not running.

1.2.2 永久關閉防火牆(但需要重啓服務器)
[root@localhost ~]# chkconfig iptables off

[root@localhost ~]# chkconfig –list|grep ipt

iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off

1.3 精簡開機自啓動服務
[root@localhost ~]# chkconfig –list|egrep -v “crond|sshd|network|rsysstat”|awk ‘{print $1}’|sed -r ‘s#(.*)#chkconfig \1 off#g’

#####關閉不需要開機自啓動的服務

[root@localhost ~]# chkconfig –list|egrep -v “crond|sshd|network|rsyslog|sysstat”|awk ‘{print $1}’|sed -r ‘s#(.*)#chkconfig \1 off#g’|bash

[root@localhost ~]# chkconfig|egrep -v “crond|sshd|network|rsyslog|sysstat”|awk ‘{print “chkconfig”,$1,”off”}’|bash

#####關閉完後進行檢查

[root@localhost ~]# 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

1.4 利用sudo控制用戶對系統命令的使用權限
###新建用戶

[root@localhost ~]# useradd oldboy

####備份

[root@localhost ~]# \cp /etc/sudoers /etc/sudoers20161208.ori

###批量追加

[root@localhost ~]# echo ” oldboy ALL=(ALL) NOPASSWD:ALL” >>/etc/sudoers

[root@localhost ~]# tail -1 /etc/sudoers

oldboy ALL=(ALL) NOPASSWD:ALL

####檢查語法

[root@localhost ~]# visudo -c

/etc/sudoers: parsed OK

1.5 linux中文顯示設置
[root@localhost ~]# cat /etc/sysconfig/i18n

LANG=”en_US.UTF-8″

SYSFONT=”latarcyrheb-sun16″

[root@localhost ~]# \cp /etc/sysconfig/i18n /etc/sysconfig/i18n20161208.ori

[root@localhost ~]# echo ‘LANG=” en_US.UTF-8 “‘ >/etc/sysconfig/i18n

[root@localhost ~]# echo $LANG

en_US.UTF-8

1.6 設置linux服務器時間同步
[root@localhost ~]# echo ‘#time sync by wangyongfu 2016-12-08’ >>/var/spool/cron/root

[root@localhost ~]# echo ‘/5 * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1’ >>/var/spool/cron/root

[root@localhost ~]# crontab -l

#time sync by wangyongfu 2016-12-08

/5 * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1

1.7 歷史記錄數及登錄超時環境變量設置(可不配置)
[root@localhost ~]# echo ‘export TMOUT=300’ >>/etc/profile

[root@localhost ~]# echo ‘export HISTSIZE=5’ >>/etc/profile

[root@localhost ~]# echo ‘export HISTFILESIZE=5’ >>/etc/profile

[root@localhost ~]# tail -3 /etc/profile

export TMOUT=300

export HISTSIZE=5

export HISTFILESIZE=5

[root@localhost ~]# source /etc/profile

1.8 調整linux系統文件描述符數量
1.8.1 第一種調整方法
[root@localhost ~]# echo ‘* – nofile 65535’ >>/etc/security/limits.conf [root@localhost ~]# tail -1 /etc/security/limits.conf

  • – nofile 65535

####配置完成後需要重新登錄纔可以生效

[root@localhost ~]# ulimit -n

1024

1.8.2 第二種調整方法
直接把ulimit -SHn 65535 命令加入到/etc/rc.local,可以設置每次開機啓動時配置生效

cat >>/etc/rc.local<<EOF

#-S use the ‘soft’ resource limit

#-H use the ‘hard’ resource limit

#-n the maximum number of open file desocriptors

ulimit -HSn 65535

#-s the maximum stack size

ulimit -s 65535

1.9 linux服務器內核參數優化
說明:本優化適合apache,nginx,squid多種等web應用,特殊的業務也可能需要略作調整。

[root@c64 ~]# cat >>/etc/sysctl.conf<<EOF

#by sun in 20161208

net.ipv4.tcp_fin_timeout = 2

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_keepalive_time =600

net.ipv4.ip_local_port_range = 4000 65000

net.ipv4.tcp_max_syn_backlog = 16384

net.ipv4.tcp_max_tw_buckets = 36000

net.ipv4.route.gc_timeout = 100

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_synack_retries = 1

net.core.somaxconn = 16384

net.core.netdev_max_backlog = 16384

net.ipv4.tcp_max_orphans = 16384

#以下參數是對iptables防火牆的優化,防火牆不開會有提示,可以忽略不理。

net.ipv4.ip_conntrack_max = 25000000

net.ipv4.netfilter.ip_conntrack_max = 25000000

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

EOF

[root@localhost ~]# sysctl –p #使配置文件生效

1.10 安裝其他小軟件
[root@localhost ~]# yum install lrzsz nmap tree dos2unix nc telnet -y

1.11 ssh連接速度慢優化
[root@localhost ~]# sed -i.bak ‘s@#UseDNS yes@UseDNS no@g;s@^GSSAPIAuthentication yes@GSSAPIAuthentication no@g’ /etc/ssh/sshd_config

[root@localhost ~]# /etc/init.d/sshd reload

Reloading sshd: [ OK ]

1.12 更改yum源
[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

[root@localhost ~]#yum makecache

第2章 Linux 基礎優化與安全重點小結
1)不用root 登錄管理系統,而以普通用戶登錄通過sudo 授權管理。

2)更改默認的遠程連接SSH 服務端口,禁止root 用戶遠程連接,甚至要更改SSH 服務只監聽內網IP。

3)定時自動更新服務器的時間,使其和互聯網時間同步。

4)配置yum 更新源,從國內更新源下載安裝軟件包。

5)關閉SELinux 及iptables(在工作場景中,如果有外部IP 一般要打開iptables,高併發高流量的

服務器可能無法開啓)。

6)調整文件描述符的數量,進程及文件的打開都會消耗文件描述符數量。

7)定時自動清理郵件臨時目錄垃圾文件,防止磁盤的inodes 數被小文件佔滿(注意Centos6 和

Centos5 要清除的目錄不同)。

8)精簡併保留必要的開機自啓動服務(如crond、sshd、network、rsyslog、sysstat)。

9)Linux 內核參數優化/etc/sysctl.conf,執行sysctl -p 生效。

10)更改系統字符集爲“zh_CN.UTF-8”,使其支持中文,防止出現亂碼問題。

11)鎖定關鍵系統文件如/etc/passwd、/etc/shadow、/etc/group、/etc/gshadow、/etc/inittab, 處理以上

內容後把chattr、lsattr 改名爲oldboy,轉移走,這樣就安全多了。

12)清空/etc/issue、/etc/issue.net,去除系統及內核版本登錄前的屏幕顯示。

13)清除多餘的系統虛擬用戶賬號。

14)爲grub 引導菜單加密碼。

15)禁止主機被ping。

16)打補丁並升級有已知漏洞的軟件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章