- 用戶目錄權限存在問題ls -ld /home/oracle
- su 程序權限存在問題ll /bin/su
- 程序依賴的共享庫問題ldd /bin/su
- SELinux問題grep "SELINUX" /etc/selinux/config
- 系統根分區問題檢查剩餘容量檢查根分區權限
高性能Linux架構實戰 [高俊峯] 筆記摘要
一 Linux服務器安全與運維
1.賬戶和登錄安全
可刪除的默認無用用戶:
adm lp sync shutdown halt news uucp operator games gopher
可刪除的默認無用組:
adm lp uucp games dip pppusers popusers slipusers
關閉不需要的服務:
anacron auditd autofs avahi-daemon avahi-dnsconfd bluetooth cupspeed firstboot gpm haldaemon hidd ip6tables ipsec isdn lpd mcstrans messagebus netfs nfs nfslock nscd pcscd portmap readahead_early restorecond rpcgssd rpcidmapd rstatd sendmail setroubleshoot yppasswdd ypserv.
[chkconfig --level 345 anacron off]
將SSH客戶端生成的密鑰放置服務端 啓用Publickey認證
mkdir /home/test/.ssh
chmod 700 /home/test/.ssh
ssh-keygen -i -f test.pub >> /root/.ssh/authorized_key2
vim /etc/ssh/sshd_config
Protocol 2 #僅使用SSH2認證
PubkeyAuthentication yes #啓用PublicKey認證
AuthorizedKeysFile .ssh/authorized_key2 #PublicKey文件路徑
PasswordAuthentication no #不使用口令認證
/etc/rc.d/init.d/sshd restart
開放普通用戶單文件特權 (使用sudo 訪問無權限的文件 輸入密碼後獲得5分鐘的訪問券)
vim /etc/sudoers
test01 ALL = /bin/more /etc/shadow
#無需密碼的方式
test01 ALL = NOPASSWD: /etc/init.d/nagios restart
#普通用戶提權爲超級用戶並不需要密碼
test01 ALL=(ALL) NOPASSWD: ALL
系統登錄歡迎信息
/etc/issue /etc/issue.net /etc/redhat-release /etc/motd
vim /etc/ssh/sshd_config
Banner /etc/issue.net
禁止Ctrl+Alt+Del 關機
centos5.x
vim /etc/inittab
ca::ctrlaltdel:/sbin/shutdown -t3 -r now #將此行註釋
telinit q
centos6.x
vim /init/control-alt-delete.conf
exec /sbin/shutdown -r now "Control-Alt-Delete pressed" #註釋此行
禁止telnet方式認證登錄
vim /etc/ssh/sshd_config
修改shell歷史記錄長度
vim /etc/bashrc
HISTFILESIZE=10000
HISTSIZE=10000
HISTTIMEFORMAT='%F %T'
export HISTTIMEFORMAT
/etc/profile
#history
USER_IP= `who -u am i 2>/dev/null |awk '{print $NF}'|sed -e 's/[()]//g'`
HISTDIR=/usr/share/.history
if [ -z $USER_IP]
then
USER_IP=`hostname`
fi
if [ ! -d $HISTDIR ]
then
mkdir -p $HISTDIR
chmod 777 $HISTDIR
fi
if [ ! -d $HISTDIR/${LOGNAME} ]
then
mkdir -p $HISTDIR/${LOGNAME}
chmod 300 $HISTDIR/${LOGNAME}
fi
export HISTSIZE=10000
DT=`date +%Y%m%d_%H%M%s`
export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.history.$DY"
export HISTTIMEFORMAT="{%Y.%m.%d %H:%M:%S}"
chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null
-------------------------------------------------------------
文件權限檢查
任何用戶都有寫權限
find / -type f -perm -2 -o -perm -20 |xargs ls -al
find / -type d -perm -2 -o -perm -20 |xargs ls -al
含有s位的程序
find / -type f -perm -4000 -o -perm -2000 -print |xargs ls -al
查詢含有suid和sgid文件
find / -user root -perm -2000 -print -exec md5sum {} \;
find / -user root -perm -4000 -print -exec md5sum {} \;
查詢沒有屬主的文件
find / -nouser -o nogroup
/tmp單獨分區時安全的掛載方式
LABEL=/tmp /tmp ext4 rw,nosuid,noexec,nodev 0 0
/tmp是目錄的安全掛載方式 [啓動loopback文件系統]
dd if=/dev/zero of=/dev/tmpfs bs=1M count=10000
mke2fs -j /dev/tmpfs
cp -av /tmp /tmp.old
mount -o loop,noexec,nosuid,rw /dev/tmpfs /tmp
chmod 1777 /tmp
mv -f /tmp.old/* /tmp
rm -rf /tmp.old
vim /etc/fstab
/dev/tmpfs /tmp ext4 loop,nosuid,noexec,rw 0 0
/dev/shm 安全掛載方式
vim /etc/fstab
tmpfs /dev/shm tmpfs defaults,nosuid,noexec,rw 0 0
文件級rootkit
檢查文件完整性:Tripwire aide
後門檢測工具 chkrootkit
避免chkrootkit依賴的工具被污染提前備份一份完整所需工具至指定目錄
mkdir /usr/share/.commands
cp `which --skip-alias awk cut echo find egrep id head ls netstat ps strings sed uname` /user/share/.commands
chkrootkit -p /usr/share/.commands/
檢測工具2 RKHunter
#./installer.sh --layout default --install
rkhunter -c
rkhunter --check --skip-keypress #靜默檢查
09 3 *** root rkhunter --check --cronjob
pidof
2.網絡安全運維
Ntop:嗅探網絡數據包類型,分析區分數據流量
Ntopng
iperf:網絡性能評估工具
iperf3 -c 192.168.12.168
iperf3 -c 192.168.12.168 -t 20 -i 5
iperf3 -c 192.168.12.168 -i 10 -n 5000000000 #每隔十秒發送5G的數據
iperf3 -c 192.168.12.168 -F test.tar.gz -i 5 -t 20 -f M
iperf3 -c 192.168.12.168 -u -b 100M -f M -i 3
nmap:網絡發現工具
nmap 39.106.125.107
nmap -T4 -A -v 39.106.125.107
nmap -sn -PE -PS22,80 -PU53 www.baidu.com
PE:發送ICMP echo 報文 PS:發送TCP SYN/ACK的形式偵探主機 PU:以UDP形式偵探
nmap -sU -sS -F www.myblogsite.top
nmap -O --osscan-guess 192.168.3.1-254
3.數據安全
DRDB
yum -y install kmod-drbd83 drbd83
modprobe drbd
lsmod|grep -i drbd
modprobe -l|grep -i drbd
dd創建空數據: dd if=/dev/zero of=/mnt/testblack.tmp bs=10M count=20
數據恢復extundelete: Linux常用恢復工具:debugfs R-Liunx ext3grep extundelete等.extundelete與ext3grep比較常用,兩款工具原理較爲相似.前者功能更強大.
通過分析文件系統的日誌,解析出所有文件的indoe信息,從而可以恢復Linux下主流的ext3 ext4文件系統下被誤刪除的文件.而ext3grep僅支持ext3文件系統,此外在恢復速度上前者要更快,是通過掃描inode和數據恢復同時進行,且支持單文件 單目錄 inode恢復 block恢復與完整磁盤恢復.而ext3grep略顯笨拙需要掃描所有inode信息然後開始恢復數據,速度上相對比較慢且不支持目錄 時間恢復等.
前置安裝e2fsprogs與e2fsprogs-libs
1.誤操作刪除需要立即將分區卸載.
2.extundelete /dev/sdc1 --inode 2
3.extundelete /dev/sdc1 --restore-file passwd #單文件恢復
4.extundelete /dev/sdc1 --restore-directory /ganglia-3.4.0 #單目錄恢復
5.extundelete /dev/sdc1 --restore-all #全部文件恢復
6.extundelete --after 1379146740 --restore-all /dev/sdc1 #恢復一小時內文件 當前 date +%s-3600
4.Linux系統運維故障排查思路
Linux系統常見無法啓動
a.文件系統受損 [ext3 ext4文件系統都是具備日誌記錄功能的日誌文件系統,並且可以進行簡單的容錯與糾錯.日誌文件系統並不是將數據實時寫入到磁盤,而是定期批量寫入,但文件系統的所有讀寫操作都會實時記錄到日誌文件中,當系統發生掉電等錯誤導致數據沒有寫入磁盤時可以通過日誌文件的記錄回滾發生故障的讀寫操作,從而保證數據和文件系統的一致性,但是由於實際環境複雜多樣導致文件系統不能每一次都可以成功自我修復,這是就需要運維人員手動介入修復.]
掉電開機提示: /dev/sda6 contains a file system with errors,check forced
/dev/sda6:UNEXPECTED INCONSISTENCY;RUN fsck MANUALLY
(or type Control-D to continue):
give root password for maintenance
輸入root密碼後進入修復模式,使用fsck命令修復.
fsck.ext3 -y /dev/sda6 [無法恢復的數據會存放在文件系統lost+found目錄中] (fsck -A -y)
若忘記root密碼
centos6.x
1.引導參數中增加 init=/bin/bash 載入系統.
2.mount -o remount,rw / #重掛根
3.passwd #修改密碼
4.重啓輸入root密碼繼續修復.
centos7.x
1.引導參數中增加 rd.break console=tty0 載入系統
2.mount remount,rw /sysroot #重掛根
3.chroot /sysroot #切換根
4.passwd
5.touch /.autorelabel
6.exit
7.reboot
b.系統文件配置不當或/etc/fstab文件丟失
開機提示: starting system logger
1.開機使用引導介質進入救援模式 [linux rescue]
2.e2label 可查看每個分區對應的掛載點
3.查找出原系統的跟分區目錄查找出對應分區 如 /dev/sda3
4.創建一個目錄 /rescue_tmp 將原根掛載至此 mkdir /rescue_tmp ;mount /dev/sda3 /rescue_tmp
5.vim /rescue_tmp/etc/fstab
6.恢復完成後重啓系統.
c.Linux內核丟失或崩潰
d.引導程序出錯
e.硬件故障
f.無響應分析
系統硬件問題: SCSI卡 主板 RAID卡 HBA卡 網卡 硬盤等硬件設備導致,通過更換硬件解決
外圍硬件問題: 主要爲網絡問題導致.網絡設備 網絡參數等方面查找
軟件問題: 系統內核bug 應用軟件bug 驅動程序bug等 通過升級內核 修復程序bug 更新驅動程序解決
系統設置問題: 防火牆配置 開機啓動不正確等.
iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
5.常見故障案例
su 切換普通用戶時報錯.
報錯提示: su: warning: cannot change directory to /home/oracle: Permission denied
su: /bin/bash: Permission denied
排查思路:
創建文件提示: Cannot make directory: file:/www/data/html [Read-only file system]
原因比較多,可能是文件系統數據塊出現不一致導致,也可能是磁盤故障導致.可以嘗試fsck修復
1.卸載無法寫入的磁盤.
2.若有佔用則查詢佔用端口 fuser -m /dev/sdb1 查找出佔用進程
3.停止進城後卸載目錄
4.進行修復 fsck -V -a /dev/sdb1
5.重新掛會
操作大量文件時遇到 Argument list too long getconf ARG_MAX 控制傳遞參數
手動分批刪除
find傳遞刪除
shell腳本循環刪除
重新編譯linux內核 include/liunx/binfmts.h文件 #define MAX_ARG_PAGES 32 將32改爲64或128.重新編譯
inode消耗殆盡
dumpe2fs -h /dev/sda3|grep 'Inode count'
文件刪除後空間不釋放: 文件存放分爲數據部分和指針部分,指針部分位於文件系統的meta-data中,將數據清除後指針從meta-data中被清除.將數據對應的指針清除後空間隨之釋放.
進程佔用不斷的寫數據導致
Too many open files
ulimit -n [/etc/security/limits.conf]
www soft nofile 65535
www hard nofile 65535
內核文件大小限制
進程數據塊大小限制
shell進程創建文件大小限制
可加鎖內存大小限制
常駐內存集的大小限制
打開文件句柄數限制
分配堆棧的最大大小限制
CPU佔用時間限制用戶最大可用的進程數
shell進程所能使用的最大虛擬內存限制
No speace left on device:mod_rewrite:could not create rewrite_log_lock Configyration Failed
信號量佔盡. ipcs -s |grep nobody|perl -e 'while (<STDIN> { @a=split(/\s+/); print `ipcrmsem $a[1]`})'
6.輕量級運維利器pssh pdsh mussh
並行ssh運維工具pssh parallel-ssh 支持文件並行複製/遠程並行執行命令/殺死遠程主機進程等
& 需要保證本地主機與管理服務器之間單向信任
& Python版本大於2.4
& 附帶5個主程序
ssh(pssh) 遠程主機並行運行命令
scp(pscp) 複製文件至遠程多臺主機
rsync(prsync) 使用rsync協議將文件從本地同步到遠程多臺主機
nuke(pnuke) 在遠程主機並行killall某進程
slurp(pslurp) 將文件從遠程主機複製到本地
查看user002 003的時間
pssh -H [email protected].com:9529 -P date
pssh -H user003.bestbook.com -i date
批量管理列表 /etc/pssh/hosts
批量下發命令
獲取運行時間
pssh -i -O "StrictHostKeyChecking=no" -h /etc/pssh/hosts "uptime" -O "StrictHostKeyChecking=no" 讓遠程主機自動接受本地hostkey
解壓文件
passh -i -h /etc/pssh/hosts "tar -xzvf test.zip"
連接指定端口
passh -i -x "-l opsuser" "-p 9529" -h /etc/pssh/hosts -o /etc/pssh/info "uptime;name-a"
本地文件推送給批量遠端
pscp -h /etc/pssh/hosts /etc/ssh/ssh_config /tmp
pscp -h /etc/pssh/hosts -r /etc/httpd/conf /tmp
本地拉取遠端文件
pslurp -h /etc/pssh/hosts -L /home/opsuser/test > /home/opsuser/gmond.conf gmond1.conf
pslurp -h /etc/pssh/hosts -r -L /home/opsuser/test > /home/opsuser/gmond gmond1
本地文件同步到遠程主機
prsync -h /etc/pssh/hosts -l opsuser -a -r /etc/httpd /tmp
prsync -h /etc/pssh/hosts -l opsuser -az -r /var/log/httpd /tmp #壓縮傳輸
殺死遠端進程
punke -h /etc/pssh/hosts httpd
分佈式運維工具pdsh parallel distributed shell 適用於大批量服務器配置/部署/文件複製/運維等操作
& 需要保證本地主機與管理服務器之間的單向信任
& 默認rsh方式 支持ssh mrsh qsh mqsh krb4 xcpu 等多種rcmd模塊
& --with-ssh --with-dshgroups --with-machines --with-dshgroups
& 傳輸文件時 本地與遠端都必須安裝pdcp
批量統計信息
pdsh -w ssh:192.168.16.[1-3] "uname -n"
pdsh -w ssh:user00[1-5] -x user003 "uptime"
pdsh -w ssh:user00[10-31],/15/ "uptime"
文件批量調用
/etc/pdsh/machines
pdsh -R ssh -a uptime
pdsh -R ssh -g userhosts uptime
pdsh -R ssh -a -X userhosts uptume
交互模式
pdsh -R ssh -w user001
文件傳輸
pdcp -R ssh -g userhosts /home/opsuser/mysqldb.tar.gz /home/opsuser
pdcp -R ssh -w userhosts -r /home/opsuser/webdata /home/opsuser
多主機ssh封裝mussh MUltihost SSH Wrapper
& 需要保證本地主機與管理服務器之間的單向信任
批量命令:
mussh -h opsuser@user01 opsuser@user02 -c uptime
文件批量命令 /home/opsuser/myhosts
mussh -H myhosts -l opsuser -c uptime
本地腳本遠端執行
mussh -o "port=22" -H myhosts -s /usr/bin/python -C "/home/opsuser/checkos.py"
7.分佈式監控
Ganglia HPC(高性能計算集羣) 採集CPU 內存 硬盤使用率 I/O負載 網絡流量等情況彙總到gmetad守護進程下,使用rrdtool存儲數據.以曲線方式展示
& gmond: ganglia monitoring daemon 守護檢測進程默認監聽8649
& gmetad:ganglia meta daemon 數據匯聚守護進程,將數據存儲至RRD引擎中
& webfrontend:從gmetad中讀取RRD數據通過rrdtool生成圖表
gmetric -n disk_used -v 40 -t int32 -u '% test' -d 50 -s '8.8.8.8:cloud1'
8.高性能Nginx服務器
HTTP/IMAP/POP3/SMTP代理服務器
--with-http_stub_status_module #開啓nginx status功能 監控狀態
--with-http_gzip_static_module #開啓Gzip壓縮功能
select 標準工作模式1
poll 標準工作模式2
kqueque 高效工作模式1
epoll 高效工作模式2
rtsig
/dev/poll
max_client=worker_processes * worker_connections(默認1024) #ulimit -n 65536
sendfile on; tcp_nopush on ; tco_nodelay on;
開啓ngin運行狀態
localtion /NginxStatus {
stub_status on;
access_log logs/NginxStatus.log;
auth_basic "NginxStatus";
auth_basic_user_file ../htpasswd;
}
htpasswd -C htpassswd password
信號處理:
QUIT: 15 處理完當前請求關閉進程
HUP: 重載配置,關閉原由進程開啓新進程,不中斷請求
USR1: Nginx日誌切割
USR2: 平滑升級
WINCH: 從容關閉
rewrite重寫 來自www.taob.com的請求至www.tb.com
service {
service_name www.taob.com;
rewirte ^/(.*)$ http://www.tb.com/$1 permanent; #301 redirect 302
}
基於核心變量$host實現重定向
service {
server_name www.tb.com www.taob.com;
if ($host != 'www.tb.com') {
rewirte ^/(.*)$ http://www.tb.com/$1 permanent;
}
}
禁止訪問特定資源
location ~* \.(txt|doc)$ {
root /data/www/wwwroot;
deny all;
}
Nginx內置變量
$args 與請求行的參數相等
$document_root 當前root命令指定的值
$uri 當前request中的URI
$document_uri 同等$uri
$host 與頭部"Host"行的值一致
$limit_rate 限制連接速率
$request_method 同等於request的method GET或POST
$remote_addr 客戶端IP
$remote_port 客戶端端口
$remote_user 用戶名 ngx_http_auth_basic_module認證
$request_filename 當前請求的文件的路徑名,由root或alias與URI request組合而成
$request_uri 參數完整的初始URI
$query_string 與$args一致
$server_name 請求到達的服務器名
$server_port 請求到達的端口號
location ~ ^/best/ {
rewirte ^/best/(.*)$ /test/$1 break;
proxy_pass http://www.taob.com;
}
location / {
proxy_pass http://127.0.0.1:8080/;
set $query $query_string;
rewrite /dade /wordpress?$query?;
}
可將http://www.tb.com/wp/?p=160的頁面重寫到www.tb.com/wordpress/?p=160
service {
listen 80;
server_name www.tb.cn www.taob.com;
if ($host != 'www.tb.cn') {
rewirte ^/(.*)$ http://www.tb.cn/error.txt bread;
rewirte ^/(.*)$ http://www.tb.cn/$1 permanent;
}
}
nginx 編譯時註釋 auto/cc/gcc 的 CFLAGS="$CFLAGS -g"
--with-cc-opt='-O3'
--with-cpu-opt=CPU [pentinm pentiumpro pentium3 pentium4 athlon opteron adm64 sparc32 sparc64 ppc64] cat /proc/cpuinfo |grep "model name"
TCMalloc 優化Nginx性能
1.安裝libunwind庫
2.安裝google-perftools
3.重新編譯nginx 添加參數 --with-google_perftools_module
4.添加google-perftools線程目錄 0777 權限
5.nginx主配置文件pid處寫入 google_perftools_profiles /tmp/tcmalloc;
6.重啓nginx 驗證
針對nginx的內核優化 /sbin/sysctl -p
net.ipv4.tcp_max_tw_buckets = 6000 #timewait數量 默認180000
net.ipv4.ip_local_port_range = 1024 65000 #允許系統打開的端口範圍
net.ipv4.tcp_tw_recycle = 1 #啓用timewait快速回收
net.ipv4.tcp_tw_reuse = 1 #允許TIME-WAIT sockets
net.ipv4.tcp_syncookies = 1 #SYN等待隊列溢出時啓用cookies處理
net.core.somaxconn = 262144 #tcp併發連接 默認 128
net.core.netdev_max_backlog = 262144 #網口數據包速率比內核處理快時允許等到的隊列大小
net.ipv4.tcp_max_orphans = 262144 #設定最大套接字不被關聯到任何用戶文件句柄上
net.ipv4.tcp_max_syn_backlog = 262144 #記錄尚未收到客戶端確認信息的連接請求最大值
net.ipv4.tcp_synack_retries = 1 #內核放棄連接之前發送SYN+ACK包的數量
net.ipv4.tcp_syn_retres = 1 #內核放棄建立連接前發送SYN包的數量
net.ipv4.tcp_fin_timeout = 1 #保持FIN-WAIT-2狀態的時間 默認60
net.ipv4.tcp_keepalive_time = 30 #keepalive啓動時tcp發送的頻度 默認2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.