Linux命令技巧

來源:互聯網
上載者:User
Linux命令技巧

1.網站根目錄下尋找是否被放置webshell木馬根據語句判斷是不是PHP木馬指令碼
# find /storage/www/ -name "*.php" | xargs grep -in --color "eval("
# grep -i --include='*.php' -r system\s*\( /storage/www/

2.統計訪問日誌中來自同ip出現的次數分析盜鏈、攻擊、機器人
# cat access.log |awk '{print $1}'| sort | uniq -c |sort -rn

3.分析出現次數最多的ip對網站的具體資料訪問情況
# grep -e IP access.log > filename
# cat filename |awk '{print $8}'|sort|uniq -c|sort -rn

4.訪問次數最多的檔案或頁面,取前20
# cat access.log|awk '{print $11}'|sort|uniq -c|sort -nr|head -20

5.列出傳輸最大的幾個exe檔案(分析下載站的時候常用)
# cat access.log |awk '($7~/\.exe/){print $10 " " $1 " " $4 " " $7}'|sort -nr|head -20

6.列出輸出大於200000byte(約200kb)的exe檔案以及對應檔案發生次數
# cat access.log |awk '($10 > 200000 && $7~/\.exe/){print $7}'|sort -n|uniq -c|sort -nr|head -100

7.如果日誌最後一列記錄的是分頁檔傳輸時間,則有列出到用戶端最耗時的頁面
# cat access.log |awk '($7~/\.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -100

8.列出最最耗時的頁面(超過60秒的)的以及對應頁面發生次數
# cat access.log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100

9.列出傳輸時間超過 30 秒的檔案
# cat access.log |awk '($NF > 30){print $7}'|sort -n|uniq -c|sort -nr|head -20

10.統計網站流量(G)
# cat access.log |awk '{sum+=$10} END {print sum/1024/1024/1024}'

11.統計404的串連
# awk '($9 ~/404/)' access.log | awk '{print $9,$7}' | sort

12. 統計http status.
# cat access.log |awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}'
# cat access.log |awk '{print $9}'|sort|uniq -c|sort -rn

13.尋找掛馬內容進行批量清除
# find /webbase/ -type f -exec grep 'www.800816.com.cn' -l {} \;    
# sed -i "s/body{.*www.800816.com.cn.*}//g" `grep www.800816.com.cn -rl ./`

14.批量轉換GBK為UTF-8檔案編碼
# find default -type d -exec mkdir -p utf/{} \;
# find default -type f -exec iconv -f GBK -t UTF-8 {} -o utf/{} \;

15.find尋找檔案的時候怎麼避開多個檔案目錄
# find /usr/sam \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "*.txt" -print

16.查看tcp的並發請求數及其TCP串連狀態:
# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
# netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
# netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key,"\t",state[key]}'
# netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}'
# netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
# netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c

17.尋找請求數前20的IP(常用於尋找攻來源)
# netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
# netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}' |sort -rn|head -n10

18.查看有多少個活動的php-cgi進程
# netstat -anp | grep php-cgi | grep ^tcp | wc -l

19.尋找較多time_wait串連
# netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20

20.找查較多的SYN串連
# netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more

21.根據連接埠列進程
# netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1

22.抓包用來防止80連接埠被人攻擊時可以分析資料
# tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts

23.用tcpdump嗅探80連接埠的訪問看看誰最高
# tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20

24.查看是哪些蜘蛛在抓取內容。
# /usr/sbin/tcpdump -i eth0 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'bot|crawler|slurp|spider'

25.按域統計流量
# zcat squid_access.log.tar.gz| awk '{print $10,$7}' |awk 'BEGIN{FS="[ /]"}{trfc[$4]+=$1}END{for(domain in trfc){printf "%s\t%d\n",domain,trfc[domain]}}'

26.查看資料庫執行的sql
# /usr/sbin/tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | egrep -i 'SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER|CALL'

27.將匹配Root一行中no替換成yes
# sed -i '/Root/s/no/yes/' /etc/ssh/sshd_config

28.去掉第一列
# awk '{for(i=2;i<=NF;i++) if(i!=NF){printf $i" "}else{print $i} }' list

29.按記憶體從大到小排列
# ps -e -o "%C : %p : %z : %a"|sort -k5 -nr

30.按cpu利用率從大到小排列
# ps -e -o "%C : %p : %z : %a"|sort -nr

31.怎樣知道某個進程在哪個CPU上運行
# ps -eo pid,args,psr

32.清除僵死進程。
# ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9

33.查看硬體製造商
# dmidecode -s system-product-name

34.尋找佔用磁碟IO最多的進程
# wget -c http://linux.web.psi.ch/dist/scientific/5/gfa/all/dstat-0.6.7-1.rf.noarch.rpm
# dstat -M topio -d -M topbio

35.檢查I/O使用率(%util)是否超過100%
# iostat -x 1 2

36.磁碟空間,檢查是否有分區使用率(Use%)過高(比如超過90%) 如發現某個分區空間接近用盡,可以進入該分區的掛載點,用以下命令找出佔用空間最多的檔案或目錄:
# df -h
# du -cks * | sort -rn | head -n 10

37.CPU負載檢查前三個輸出值是否超過了系統邏輯CPU的4倍。
# cat /proc/loadavg

38.CPU的數量
# cat /proc/cpuinfo |grep -c processor

39.檢查網路流量(rxbyt/s, txbyt/s)是否過高
# sar -n DEV

40.每隔1秒顯示一下網路流量
# watch -n 1 "/sbin/ifconfig eth0 | grep bytes"

41.批量覆蓋目錄下的檔案不用確定是否執行
# \cp -rf /svn/wwwroot /wwwroot

42.調試命令
# strace -p pid

43.跟蹤指定進程的PID
# gdb -p pid

44.查看當前進程開啟了多少個檔案控制代碼

lsof -n |awk ‘{print $2}’|sort|uniq -c |sort -nr|more

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.