歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux系統基礎優化

linux系統基礎優化

日期:2017/3/1 12:23:02   编辑:關於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=disabled SELINUX=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運行時的一種狀態標示,這個標示用數字表示。 運行級別的其中狀態: 0 halt,關機狀態 1 single user 單用戶,找回root密碼 2 multiuser without nfs 多用戶沒有NFS網絡文件系統 3 文本模式(Full multiuser mode)*******工作模式 4 unused 5 圖形。桌面、X11 6 reboot 重啟 如何查看linux運行級級別: 實現命令:runlevel 輸出: [root@oldboy ~]# runlevel N(前一次) 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: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 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: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 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: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 四.關閉iptables防火牆 查看防火牆:iptables -L -n 關閉防火牆: 實現命令:/etc/init.d/iptables stop 輸出: [root@oldboy ~]# /etc/init.d/iptables stop iptables:將鏈設置為政策 ACCEPT:filter [確定] iptables:清除防火牆規則: [確定] iptables:正在卸載模塊: [確定] 查看防火牆狀態: 實現命令:/etc/init.d/iptables status 輸出: root@bigboy ~]# /etc/init.d/iptables status [root@bigboy ~]# /etc/init.d/iptables status Table: filter Chain 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 52113 PermitRootLogin no PermitEmptyPasswords no UseDNS no GSSAPIAuthentication 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 anywhere root ALL=(ALL) ALL oldboy ALL= (ALL) NOPASSWD: ALL /bin/ls 用戶 用戶管理的機器 臨時擁有的用戶角色 /bin/ls 注意:內置命令無法用到sudo。 九.linux字符顯示設置: 字符集是一套文字符號及其編碼:GBK 、UTF-8(企業廣泛使用) 調整服務器端字符集:調整字符集路徑(/etc/sysconfig/i18n)記住 [root@bigboy/]# cat /etc/sysconfig/i18n LANG="en_US.UTF-8" 英文字符 SYSFONT="latarcyrheb-sun16" [root@bigboy/]# cat /etc/sysconfig/i18n LANG="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/i18n LANG="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 hardwareclock 2015年10月03日星期六 08時59分12秒 -0.737654 seconds crond :定時任務 每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 $TMOUT 300 十二.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_history visudo su - oldboy su oldboy netstat -an|grep EST su 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/profile export TMOUT=300 export HISTSIZE=5 export HISTFILESIZE=5 [root@oldboy ~]# source /etc/profile 使得文件生效 [root@oldboy ~]# echo $TMOUT 300 [root@oldboy ~]# echo $HISTSIZE 5 十三.調整linux系統文件描述符數量 文件描述符是由無符號整數表示的句柄,進程使用它來標示打開文件。 文件描述符概念: 1、表示形式為整數數字(0-65535) 2、會占用文件描述符(標示打開文件) 查看默認文件描述符 ulimit-n 3、調整文件描述符 [root@bigboy ~]# ulimit -SHn 65535 設置文件描述符數量 [root@bigboy ~]# ulimit -n 65535(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 = 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.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 ---------------------------------------------------------------------- 網絡狀態說明及優化命令和優化細節參考資料請看: http://yangrong.blog.51cto.com/6945369/1321594老男孩教育的優秀學生博文 http://oldboy.blog.51cto.com/2561410/1336488 sysctl -p 使得加載的參數生效 十五.隱藏linux版本號: [root@oldboy ~]# cat /etc/issue CentOS release 6.7 (Final) Kernel \r on an \m [root@oldboy ~]# cat /etc/issue.net CentOS 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 dddd useradd: cannot open /etc/passwd [root@oldboy ~]# rm -f /etc/passwd Do not use rm command. -f /etc/passwd [root@oldboy ~]# \rm -f /etc/passwd rm: 無法刪除"/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,缺點是禁止自己ping echo 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.conf net.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.conf net.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、關閉SElinux 2、關閉防火牆,設定運行級別為3. 3、精簡開機自啟動服務 4、SSH安全控制(提前建立普通用戶) 5、sudo 管理用戶授權 6、調整文件描述符 7、更改合適的字符集 8、鎖定關鍵系統文件 9、禁止顯示內核版本及系統版本信息 10、設置會話的超時時間及歷史記錄數 11、禁止PING 12、優化LINUX內核參數 13、特定漏洞yum/rpm升級 14、清楚多余的系統虛擬賬號 15、服務器時間同步 16、打補丁下載軟件調整為國內的下載地址(調整yum源) 17、定時清理郵件服務臨時目錄垃圾文件 18、為grub菜單加密碼
Copyright © Linux教程網 All Rights Reserved