linux系統基礎最佳化

來源:互聯網
上載者:User

linux系統基礎最佳化
1.Linux系統基礎最佳化一、關閉SELinux功能 Selinux是什嗎?安全工具,控制太嚴格,生產環境不用它,使用其他安全手段。 簡介:  SELinux帶給Linux的主要價值是:提供了一個靈活的,可配置的MAC機制。 Security-Enhanced Linux (SELinux)由以下兩部分組成: 1) Kernel SELinux模組(/kernel/security/selinux) 2) 使用者態工具 SELinux是一個安全體繫結構,它通過LSM(Linux Security Modules)架構被整合到Linux Kernel 2.6.x中。它是NSA (United States National Security Agency)和SELinux社區的聯合項目。 SELinux提供了一種靈活的強制存取控制(MAC)系統,且內嵌於Linux Kernel中。SELinux定義了系統中每個【使用者】、【進程】、【應用】和【檔案】的訪問和轉變的許可權,然後它使用一個安全性原則來控制這些實體(使用者、進程、應用和檔案)之間的互動,安全性原則指定如何嚴格或寬鬆地進行檢查。 SELinux對系統使用者(system users)是透明的,只有系統管理員需要考慮在他的伺服器中如何制定嚴格的策略。策略可以根據需要是嚴格的或寬鬆的。 關閉SElinux方式:1)         通過vi /etc/selinux/config 進入設定檔進入修改2)         通過sed 命令操作: 實現命令:sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config輸出:[root@oldboy ~]# cp /etc/selinux/config  /etc/selinux/config.ori  操作前的備份[root@oldboy ~]# sed -i  's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config  直接修改源檔案[root@oldboy ~]# grep SELINUX=disabled /etc/selinux/config                SELINUX=disabled  查看修改後的效果 查看對比修改後的檔案:實現命令:(vimdiff)diff /etc/selinux/config.ori /etc/selinux/config 輸出:7c7< SELINUX=enforcing---> SELINUX=disabledSELINUX=disabled(永久生效)重啟系統---------------[root@bigboy ~]# getenforce(查看命令列是否關閉selinux)Enforcing[root@bigboy ~]# setenforce (設定selinux)usage:  setenforce [ Enforcing | Permissive | 1 | 0 ][root@bigboy ~]# setenforce 0 (命令列關閉selinux)[root@bigboy ~]# getenforce Permissive(臨時生效) 二、運行層級 什麼是運行層級: runlevel linux運行時的一種狀態標示,這個標示用數字表示。運行層級的其中狀態:0halt,關機狀態1single  user 單使用者,找回root密碼2multiuser  without nfs 多使用者沒有NFS網路檔案系統3文字模式(Full multiuser mode)*******工作模式4unused5圖形。案頭、X116reboot  重啟 如何查看linux運行級層級: 實現命令:runlevel 輸出:[root@oldboy ~]# runlevelN(前一次) 3(當前) 更改運行層級:init 三、精簡開機系統啟動 為什麼要設定開機自啟動? 1、節省開機時間、加快啟動速度 2、節省資源開銷 3、減少安全隱患 需要保留的開機自啟動: sshd:遠端連線linux伺服器 rsyslog:是作業系統提供的一種機制,系統的精靈通常會使用rsylog將各種資訊系統記錄檔中 network:啟用關閉各個網路介面 crond:用於周期性地執行系統及使用者配置的任務計劃(周期性的處理一些重複問題的計劃任務服務) Sysstat:是一個軟體包,監測系統效能及效率的一組工具 Sysstat軟體包整合的主要工具為: iostat工具提供CPU使用率及硬碟吞吐效率的資料 mpstat工具提供與單個或多個處理器相關的資料 sar工具負責收集、報告並儲存系統活躍的資訊 如何?? 第一種方法:通過setup來修改 第二種方法:通過ntsysv來修改 第三種方法:通過chkconfig來實現a. [root@bigboy ~]# chkconfig --list|grep 3:on|awk '{print $1}'|grep -Ev "sshd|network|rsyslog|crond|sysstat"|awk '{print "chkconfig " $1 " off"}'|bash[root@bigboy ~]# chkconfig --list|grep 3:oncrond           0:off   1:off   2:on    3:on    4:on    5:on       6:offnetwork         0:off   1:off   2:on    3:on    4:on    5:on       6:offrsyslog         0:off   1:off   2:on    3:on    4:on    5:on       6:offsshd            0:off   1:off   2:on    3:on    4:on    5:on       6:offsysstat         0:off   1:on    2:on    3:on    4:on    5:on       6:off b. [root@bigboy ~]# chkconfig --list|grep 3:on|awk '{print $1}'|grep -Ev "sshd|network|rsyslog|crond|sysstat"|sed -r 's#(.*)#chkconfig \1 off#g'|bash[root@bigboy ~]# chkconfig --list|grep 3:oncrond           0:off   1:off   2:on    3:on    4:on    5:on       6:offnetwork         0:off   1:off   2:on    3:on    4:on    5:on       6:offrsyslog         0:off   1:off   2:on    3:on    4:on    5:on       6:offsshd            0:off   1:off   2:on    3:on    4:on    5:on       6:offsysstat         0:off   1:on    2:on    3:on    4:on    5:on       6:off c. [root@bigboy ~]# for name in `chkconfig --list|grep 3:on|awk '{print $1}'|grep -Ev "sshd|network|rsyslog|crond|sysstat"`;do chkconfig $name off;done [root@bigboy ~]# chkconfig --list|grep 3:on                           crond           0:off   1:off   2:on    3:on    4:on    5:on    6:offnetwork         0:off   1:off   2:on    3:on    4:on    5:on    6:offrsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:offsshd            0:off   1:off   2:on    3:on    4:on    5:on    6:offsysstat         0:off   1:on    2:on    3:on    4:on    5:on    6:off 四.關閉iptables防火牆 查看防火牆:iptables -L -n關閉防火牆:實現命令:/etc/init.d/iptables stop輸出: [root@oldboy ~]# /etc/init.d/iptables stopiptables:將鏈設定為政策 ACCEPT:filter                    [確定]iptables:清除防火牆規則:                                 [確定]iptables:正在卸載模組:                                   [確定]查看防火牆狀態:實現命令:/etc/init.d/iptables status輸出:root@bigboy ~]# /etc/init.d/iptables status [root@bigboy ~]# /etc/init.d/iptables statusTable: filterChain INPUT (policy ACCEPT)num  target     prot opt source               destination         Chain FORWARD (policy ACCEPT)num  target     prot opt source               destination         Chain OUTPUT (policy ACCEPT)num  target     prot opt source               destination   五.linux營運思想最小化原則 原則:多一事不如少一事! 1、安裝linux系統最小化,選包最小化,yum安裝軟體包也要最小化,無用的包不裝。 2、開機自啟動最小化 3、操作命令最小化 例如:用rm -f test.txt而不用rm -fr test.txt 4、登入linux使用者最小化。 平時沒有需求不用root登入,用普通使用者登入即可 5、普通使用者授權許可權最小化, 即只給必須的管理系統的命令。 6、linux系統檔案及目錄的使用權限設定最小化,禁止隨意建立、更改、刪除。理論上限制掉。 六. 更改SSH服務端遠程登入的配置(設定檔:/etc/ssh/sshd_config) 更改方法: 方法一:通過vi進入設定檔進行修改vi /etc/ssh/sshd_config####by oldboy#2011-11-24##Port 52113PermitRootLogin noPermitEmptyPasswords noUseDNS noGSSAPIAuthentication no####by oldboy#2011-11-24## 方法二:通過sed命令實現修改sed -ir '13 i Port 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no'  sshd_config 重啟生效:/etc/init.d/sshd reload【平滑重啟不影響使用者】(restart) Linux下SSH遠端連線服務慢的解決方案請見老男孩的部落格:http://oldboy.blog.51cto.com/2561410/1300964 八、利用sudo管理檔案描述 在visudo進入檔案增加普通使用者的命令路徑使得普通使用者環境下利用sudo進行命令操作。Allow root torun any commands anywhereroot     ALL=(ALL)       ALLoldboy   ALL=           (ALL)   NOPASSWD: ALL  /bin/ls使用者     使用者管理的機器  臨時擁有的使用者角色      /bin/ls注意:內建命令無法用到sudo。 九.linux字元顯示設定: 字元集是一套文字元號及其編碼:GBK 、UTF-8(企業廣泛使用)調整伺服器端字元集:調整字元集路徑(/etc/sysconfig/i18n)記住[root@bigboy/]# cat /etc/sysconfig/i18nLANG="en_US.UTF-8"       英文字元SYSFONT="latarcyrheb-sun16"[root@bigboy/]# cat /etc/sysconfig/i18nLANG="en_US.UTF-8"SYSFONT="latarcyrheb-sun16"[root@bigboy/]#cp/etc/sysconfig/i18n /etc/sysconfig/i18n.oldboy.20151003    修改檔案前的備份[root@bigboy/]#sed-i 's#LANG="en_US.UTF-8"#LANG="zh_CN.UTF-8"#g'/etc/sysconfig/i18n    利用sed替換字元檔案修改為中文字元[root@bigboy/]# cat /etc/sysconfig/i18nLANG="zh_CN.UTF-8"SYSFONT="latarcyrheb-sun16"[root@bigboy/]# echo $LANG   查看修改字元後的效果en_US.UTF-8[root@bigboy/]# source /etc/sysconfig/i18n 使得修改後的檔案生效[root@bigboy/]# echo $LANG     zh_CN.UTF-8 十. 設定linux伺服器時間同步 互連網同步時間[root@bigboy/]/usr/sbin/ntpdate time.nist.govov   互連網同步時間[root@bigboy/]# date -s "2015/10/3 9:34"2015年 10月 03日星期六 09:34:00 CST[root@bigboy/]# ntpdate time.nist.gov   set the date and time viaNTP 3 Oct 09:35:21 ntpdate[28135]: adjust timeserver 132.163.4.103 offset 0.286494 sec[root@bigboy/]# date   date 查看時間  -s 修改時間2015年 10月 03日星期六 09:48:46 CST [root@bigboy /]# hwclock   query and set the hardwareclock2015年10月03日星期六 08時59分12秒  -0.737654 secondscrond :定時任務每5分鐘同步一次[root@bigboy/]# echo "*/5 * * * * /usr/sbin/ntpdate time.nist.gov /dev/null2>&1" >>/var/spool/cron/root [root@bigboy /]# crontab –l  定時生效*/5 * * * */usr/sbin/ntpdate time.nist.gov /dev/null 2>&1 十一、設定逾時 臨時生效[root@bigboy /]# export  TMOUT=300設定逾時時間300S[root@bigboy/]# echo "export TMOUT=300" >>/etc/profile[root@bigboy/]# source /etc/profile  使得設定生效[root@bigboy/]# echo $TMOUT300 十二.history記錄數 臨時生效[root@bigboy /]# export  HISTSIZE=5  定義記錄數5條[root@bigboy /]# history 728  cat ~/.bash_history 729  HISTFILESIZE=5 730  cat ~/.bash_history 731  HISTSIZE=5 732  history[root@bigboy /]#export HISTFILESIZE=5 定義記錄檔案數5條[root@bigboy /]# cat ~/.bash_historyvisudosu - oldboysu oldboynetstat -an|grep ESTsu oldboy 永久生效[root@oldboy ~]# echo 'export TMOUT=300'>>/etc/profile[root@oldboy ~]# echo 'exportHISTSIZE=5' >>/etc/profile[root@oldboy ~]# echo 'exportHISTFILESIZE=5' >>/etc/profile[root@oldboy ~]# tail -3 /etc/profileexport TMOUT=300export HISTSIZE=5export HISTFILESIZE=5[root@oldboy ~]# source /etc/profile  使得檔案生效[root@oldboy ~]# echo $TMOUT   300[root@oldboy ~]# echo $HISTSIZE5 十三.調整linux系統檔案描述符數量 檔案描述符是由不帶正負號的整數表示的控制代碼,進程使用它來標示開啟檔案。 檔案描述符概念: 1、表示形式為整數數字(0-65535) 2、會佔用檔案描述符(標示開啟檔案) 查看預設檔案描述符ulimit-n 3、調整檔案描述符[root@bigboy ~]# ulimit -SHn 65535  設定檔案描述符數量[root@bigboy ~]# ulimit -n65535(32768) [root@bigboy ~]#  echo '*       -    nofile      65535' >>/etc/security/limits.conf    將修改的描述符數量寫入檔案[root@bigboy ~]# tail -1/etc/security/limits.conf                   *        -    nofile     65535 十四.調整核心參數檔案 (/etc/sysctl.conf) vim/etc/sysctl.conf linux核心最佳化參數:--------------------------------------------------------------------net.ipv4.tcp_fin_timeout = 2net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_syncookies = 1net.ipv4.tcp_keepalive_time = 600net.ipv4.ip_local_port_range = 4000    65000net.ipv4.tcp_max_syn_backlog = 16384net.ipv4.tcp_max_tw_buckets = 36000net.ipv4.route.gc_timeout = 100net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_synack_retries = 1net.core.somaxconn = 16384net.core.netdev_max_backlog = 16384net.ipv4.tcp_max_orphans = 16384#以下參數是對iptables防火牆的最佳化,防火牆不開會提示,可以忽略不理。net.nf_conntrack_max = 25000000net.netfilter.nf_conntrack_max =25000000net.netfilter.nf_conntrack_tcp_timeout_established= 180net.netfilter.nf_conntrack_tcp_timeout_time_wait= 120net.netfilter.nf_conntrack_tcp_timeout_close_wait= 60net.netfilter.nf_conntrack_tcp_timeout_fin_wait= 120---------------------------------------------------------------------- 網路狀態說明及最佳化命令和最佳化細節參考資料請看:http://yangrong.blog.51cto.com/6945369/1321594老男孩教育的優秀學生博文http://oldboy.blog.51cto.com/2561410/1336488sysctl -p 使得載入的參數生效 十五.隱藏linux版本號碼: [root@oldboy ~]# cat /etc/issueCentOS release 6.7 (Final)Kernel \r on an \m[root@oldboy ~]# cat /etc/issue.netCentOS release 6.7 (Final)Kernel \r on an \m[root@oldboy ~]# >/etc/issue[root@oldboy ~]# >/etc/issue.net[root@oldboy ~]# cat /etc/issue 十六.鎖定系統檔案 相關的系統檔案:/etc/passwd/etc/shadow /etc/group /etc/gshadow  /etc/inittab [root@oldboy~]# chattr +i /etc/passwd /etc/shadow /etc/group  /etc/gshadow /etc/inittab   +i鎖定系統檔案[root@oldboy ~]# useradd dddduseradd: cannot open /etc/passwd[root@oldboy ~]# rm -f /etc/passwdDo not use rm command. -f /etc/passwd[root@oldboy ~]# \rm -f /etc/passwdrm: 無法刪除"/etc/passwd": 不允許的操作[root@oldboy ~]# chattr -i /etc/passwd/etc/shadow /etc/group  /etc/gshadow/etc/inittab  -i解除系統檔案[root@oldboy ~]# useradd dddd     [root@oldboy ~]# chattr +i /etc/passwd/etc/shadow /etc/group  /etc/gshadow/etc/inittab[root@oldboy ~]# lsattr /etc/passwd  查看系統檔案屬性----i--------e- /etc/passwd[root@oldboy ~]# chattr -i /etc/passwd/etc/shadow /etc/group  /etc/gshadow/etc/inittab[root@oldboy ~]# lsattr /etc/passwd-------------e- /etc/passwd 十七.禁止linux系統被ping 核心中修改禁止ping,缺點是禁止自己pingecho 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all [root@www ~]# echo"net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf[root@www ~]# tail -1 /etc/sysctl.confnet.ipv4.icmp_echo_ignore_all=1[root@www ~]# sysctl -p 生效:[root@www ~]# echo"net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf[root@www ~]# tail -1 /etc/sysctl.confnet.ipv4.icmp_echo_ignore_all=1[root@www ~]# sysctl -p 還原禁ping:echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all十八.定時清理郵件服務臨時目錄垃圾檔案 centos5系列的系統預設安裝Sen時dmail服務,因此郵件臨時存放地點的路徑/var/spool/clientmqueue/. centos6預設情況下沒有安轉Sendmail服務,而是改裝了Posfix服務,因此郵件存放地點的路徑為:/var/spool/postfit/maildrop/以上兩個目錄很容易被垃圾檔案填滿導致系統的inode數量不夠用,從而導致無地方存放檔案 手動清理的方法:find /var/spool/clientmqueue/ -typef|xargs rm -f適合centOS5的sendmail服務find /var/spool/postfix/maildrop/ -typef|xargs rm -f適合Centos6的postfix服務定時清理的方法為:將上述命令寫成指令碼,然後做定時任務,每天晚上0點執行一次(定時任務再說)  小結:如何最佳化linux: 1、關閉SElinux2、關閉防火牆,設定運行層級為3.3、精簡開機自啟動服務4、SSH安全控制(提前建立普通使用者)5、sudo 系統管理使用者授權6、調整檔案描述符7、更改合適的字元集8、鎖定關鍵系統檔案9、禁止顯示核心版本及系統版本資訊10、設定會話的逾時時間及記錄數11、禁止PING12、最佳化LINUX核心參數13、特定漏洞yum/rpm升級14、清楚多餘的系統虛擬帳號15、伺服器時間同步16、打補丁下載軟體調整為國內的(調整yum源)17、定時清理郵件服務臨時目錄垃圾檔案18、為grub菜單加密碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.