日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux常用运维命令【转】

發布時間:2025/3/20 linux 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux常用运维命令【转】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自己的小網站跑在阿里云的ECS上面,偶爾也去分析分析自己網站服務器日志,看看網站的訪問量。看看有沒有黑闊搞破壞!于是收集,整理一些服務器日志分析命令,大家可以試試!

1、查看有多少個IP訪問:

[plain]?view plaincopy
  • awk?'{print?$1}'?log_file|sort|uniq|wc?-l??


  • PS: wc -l 看看有多少行

    2、查看某一個頁面被訪問的次數:

    [plain]?view plaincopy
  • grep?"/index.php"?log_file?|?wc?-l??


  • 3、查看每一個IP訪問了多少個頁面:

    [plain]?view plaincopy
  • awk?'{++S[$1]}?END?{for?(a?in?S)?print?a,S[a]}'?log_file?>?log.txt??
  • ??
  • sort?-n?-t?'?'?-k?2?log.txt?#?配合sort進一步排序??


  • 4、將每個IP訪問的頁面數進行從小到大排序:

    [plain]?view plaincopy
  • awk?'{++S[$1]}?END?{for?(a?in?S)?print?S[a],a}'?log_file?|?sort?-n??


  • 5、查看某一個IP訪問了哪些頁面:

    [plain]?view plaincopy
  • grep?^111.111.111.111?log_file|?awk?'{print?$1,$7}'??


  • 6、去掉搜索引擎統計的頁面:

    [plain]?view plaincopy
  • awk?'{print?$12,$1}'?log_file?|?grep?^\"Mozilla?|?awk?'{print?$2}'?|sort?|?uniq?|?wc?-l??


  • 7、查看2015年8月16日14時這一個小時內有多少IP訪問:

    [plain]?view plaincopy
  • awk?'{print?$4,$1}'?log_file?|?grep?16/Aug/2015:14?|?awk?'{print?$2}'|?sort?|?uniq?|?wc?-l??


  • 8、查看訪問前十個ip地址

    awk '{print $1}' |sort|uniq -c|sort -nr |head -10 access_log uniq -c 相當于分組統計并把統計數放在最前面cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10 cat access.log|awk '{counts[$(11)]+=1}; END {for(url in counts) print countswww.access.log" target=_blank>, url}



    9、訪問次數最多的10個文件或頁面

    cat log_file|awk '{print $11}'|sort|uniq -c|sort -nr | head -10cat log_file|awk '{print $11}'|sort|uniq -c|sort -nr|head -20 awk '{print $1}' log_file |sort -n -r |uniq -c | sort -n -r | head -20 # 訪問量最大的前20個ip



    10、通過子域名訪問次數,依據referer來計算,稍有不準

    [plain]?view plaincopy
  • cat?access.log?|?awk?'{print?$11}'?|?sed?-e?'?s/http:\/\///'?-e?'?s/\/.*//'?|?sort?|?uniq?-c?|?sort?-rn?|?head?-20??


  • 11、列出傳輸大小最大的幾個文件

    [plain]?view plaincopy
  • <a?href=",?url}</pre><br/><br/>9、訪問次數最多的10個文件或頁面<br/><br/><pre?class='brush:bash;'>cat?log_file|awk?'{print?$11}'|sort|uniq?-c|sort?-nr?|?head?-10??
  • ?cat?log_file|awk?'{print?$11}'|sort|uniq?-c|sort?-nr|head?-20???
  • awk?'{print?$1}'?log_file?|sort?-n?-r?|uniq?-c?|?sort?-n?-r?|?head?-20?#?訪問量最大的前20個ip</pre><br/><br/>10、通過子域名訪問次數,依據referer來計算,稍有不準<br/><br/><pre?class='brush:plain;'>cat?access.log?|?awk?'{print?$11}'?|?sed?-e?'?s/http:\/\///'?-e?'?s/\/.*//'?|?sort?|?uniq?-c?|?sort?-rn?|?head?-20</pre><br/><br/>11、列出傳輸大小最大的幾個文件<br/><br/><pre?class='brush:plain;'>cat?<a?href="?http:=""?www.access.log"=""?target="_blank">cat?[url=http://www.access.log]www.access.log</a>?|awk?'($7~/\.php/){print?$10?"?"?$1?"?"?$4?"?"?$7}'|sort?-nr|head?-100??


  • 12、列出輸出大于200000byte(約200kb)的頁面以及對應頁面發生次數

    [plain]?view plaincopy
  • cat?[url=http://www.access.log]www.access.log?|awk?'($10?>?200000?&&?$7~/\.php/){print?$7}'|sort?-n|uniq?-c|sort?-nr|head?-100??


  • 13、如果日志最后一列記錄的是頁面文件傳輸時間,則有列出到客戶端最耗時的頁面

    [plain]?view plaincopy
  • cat?<a?href="http://www.access.log"?target="_blank">www.access.log</a>?|awk?'($7~/\.php/){print?$NF?"?"?$1?"?"?$4?"?"?$7}'|sort?-nr|head?-100??


  • 14、列出最最耗時的頁面(超過60秒的)的以及對應頁面發生次數

    [plain]?view plaincopy
  • cat?<a?href="http://www.access.log"?target="_blank">www.access.log</a>?|awk?'($NF?>?60?&&?$7~/\.php/){print?$7}'|sort?-n|uniq?-c|sort?-nr|head?-100??


  • 15、列出傳輸時間超過 30 秒的文件

    [plain]?view plaincopy
  • cat?<a?href="http://www.access.log"?target="_blank">www.access.log</a>?|awk?'($NF?>?30){print?$7}'|sort?-n|uniq?-c|sort?-nr|head?-20??


  • 16、列出當前服務器每一進程運行的數量,倒序排列

    [plain]?view plaincopy
  • ps?-ef?|?awk?-F?'?'?'{print?$8?"?"?$9}'?|sort?|?uniq?-c?|sort?-nr?|head?-20??


  • 17、查看apache當前并發訪問數

    對比httpd.conf中MaxClients的數字差距多少。

    [plain]?view plaincopy
  • netstat?-an?|?grep?ESTABLISHED?|?wc?-l??


  • 18、可以使用如下參數查看數據

    [plain]?view plaincopy
  • ps?-ef|grep?httpd|wc?-l??


  • 統計httpd進程數,連個請求會啟動一個進程,使用于Apache服務器。

    表示Apache能夠處理1388個并發請求,這個值Apache可根據負載情況自動調整。

    [plain]?view plaincopy
  • netstat?-nat|grep?-i?"80"|wc?-l??


  • netstat -an會打印系統當前網絡鏈接狀態,而grep -i "80"是用來提取與80端口有關的連接的,wc -l進行連接數統計。

    最終返回的數字就是當前所有80端口的請求總數。

    [plain]?view plaincopy
  • netstat?-na|grep?ESTABLISHED|wc?-l??


  • netstat -an會打印系統當前網絡鏈接狀態,而grep ESTABLISHED 提取出已建立連接的信息。 然后wc -l統計。

    最終返回的數字就是當前所有80端口的已建立連接的總數。

    [plain]?view plaincopy
  • netstat?-nat||grep?ESTABLISHED|wc??


  • 可查看所有建立連接的詳細記錄

    19、輸出每個ip的連接數,以及總的各個狀態的連接數

    netstat -n | awk '/^tcp/ {n=split($(NF-1),array,":");if(n<=2)++S[array[(1)]];else++S[array[(4)]];++s[$NF];++N} END {for(a in S){printf("%-20s %s\n", a, S[a]);++I}printf("%-20s %s\n","TOTAL_IP",I);for(a in s) printf("%-20s %s\n",a, s[a]);printf("%-20s %s\n","TOTAL_LINK",N);}'

    20、其他的收集

    分析日志文件下2012-05-04訪問頁面最高的前20個URL并排序

    [plain]?view plaincopy
  • cat?access.log?|grep?'04/May/2012'|?awk?'{print?$11}'|sort|uniq?-c|sort?-nr|head?-20??


  • 查詢受訪問頁面的URL地址中 含有 www.abc.com 網址的 IP 地址

    [plain]?view plaincopy
  • cat?access_log?|?awk?'($11~/\www.abc.com/){print?$1}'|sort|uniq?-c|sort?-nr??
  • 獲取訪問最高的10個IP地址 同時也可以按時間來查詢

    [plain]?view plaincopy
  • cat?linewow-access.log|awk?'{print?$1}'|sort|uniq?-c|sort?-nr|head?-10??
  • 時間段查詢日志時間段的情況

    [plain]?view plaincopy
  • cat?log_file?|?egrep?'15/Aug/2015|16/Aug/2015'?|awk?'{print?$1}'|sort|uniq?-c|sort?-nr|head?-10??
  • 分析2015/8/15到2015/8/16訪問"/index.php?g=Member&m=Public&a=sendValidCode"的IP倒序排列

    [plain]?view plaincopy
  • cat?log_file?|?egrep?'15/Aug/2015|16/Aug/2015'?|?awk?'{if($7?==?"/index.php?g=Member&m=Public&a=sendValidCode")?print?$1,$7}'|sort|uniq?-c|sort?-nr??


  • ($7~/.php/) $7里面包含.php的就輸出,本句的意思是最耗時的一百個PHP頁面

    [plain]?view plaincopy
  • cat?log_file?|awk?'($7~/\.php/){print?$NF?"?"?$1?"?"?$4?"?"?$7}'|sort?-nr|head?-100??
  • 列出最最耗時的頁面(超過60秒的)的以及對應頁面發生次數*

    [plain]?view plaincopy
  • cat?access.log?|awk?'($NF?>?60?&&?$7~/.php/){print?$7}'|sort?-n|uniq?-c|sort?-nr|head?-100??


  • 統計網站流量(G)

    [plain]?view plaincopy
  • cat?access.log?|awk?'{sum+=$10}?END?{print?sum/1024/1024/1024}'??


  • 統計404的連接

    [plain]?view plaincopy
  • awk?'($9?~/404/)'?access.log?|?awk?'{print?$9,$7}'?|?sort??


  • 統計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

    每秒并發:

    watch "awk '{if($9~/200|30|404/)COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}' log_file|sort -k 2 -nr|head -n10"

    帶寬統計

    cat apache.log |awk '{if($7~/GET/) count++}END{print "client_request="count}'?

    cat apache.log |awk '{BYTE+=$11}END{print "client_kbyte_out="BYTE/1024"KB"}'

    找出某天訪問次數最多的10個IP

    cat /tmp/access.log | grep "20/Mar/2011" |awk '{print $3}'|sort |uniq -c|sort -nr|head

    當天ip連接數最高的ip都在干些什么:

    cat access.log | grep "10.0.21.17" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10

    小時單位里ip連接數最多的10個時段

    awk -vFS="[:]" '{gsub("-.*","",$1);num[$2" "$1]++}END{for(i in num)print i,num[i]}' log_file | sort -n -k 3 -r | head -10

    找出訪問次數最多的幾個分鐘

    awk '{print $1}' access.log | grep "20/Mar/2011" |cut -c 14-18|sort|uniq -c|sort -nr|head

    取5分鐘日志

    if [ $DATE_MINUTE != $DATE_END_MINUTE ] ;then #則判斷開始時間戳與結束時間戳是否相等START_LINE=sed -n "/$DATE_MINUTE/=" $APACHE_LOG|head -n1 #如果不相等,則取出開始時間戳的行號,與結束時間戳的行號

    查看tcp的鏈接狀態

    netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}' 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 netstat -ant|awk '/ip:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}' |sort -n netstat -ant|awk '/:80/{split($5,ip,":");++S[ip[1]]}END{for (a in S) print S[a],a}' |sort -rn|head -n 10 awk 'BEGIN{printf ("http_code\tcount_num\n")}{COUNT[$10]++}END{for (a in COUNT) printf a"\t\t"COUNT[a]"\n"}'



    查找請求數前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 -n20



    用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

    查找較多time_wait連接

    netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20

    找查較多的SYN連接

    netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more

    根據端口列進程

    netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1

    查看了連接數和當前的連接數

    netstat -ant | grep $ip:80 | wc -l?

    netstat -ant | grep $ip:80 | grep EST | wc -l?

    查看IP訪問次數

    [plain]?view plaincopy
  • netstat?-nat|grep?":80"|awk?'{print?$5}'?|awk?-F:?'{print?$1}'?|?sort|?uniq?-c|sort?-n??


  • Linux命令分析當前的鏈接狀況

    netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' watch "netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'" # 通過watch可以一直監控



    LAST_ACK 5 關閉一個TCP連接需要從兩個方向上分別進行關閉,雙方都是通過發送FIN來表示單方向數據的關閉,當通信雙方發送了最后一個FIN的時候,發送方此時處于LAST_ACK狀態,當發送方收到對方的確認(Fin的Ack確認)后才真正關閉整個TCP連接;

    SYN_RECV 30 # 表示正在等待處理的請求數;

    ESTABLISHED 1597 # 表示正常數據傳輸狀態;

    FIN_WAIT1 51 # 表示server端主動要求關閉tcp連接;

    FIN_WAIT2 504 # 表示客戶端中斷連接;

    TIME_WAIT 1057 # 表示處理完畢,等待超時結束的請求數;

    ?

    轉自

    Linux運維常用命令 - Lai18.com IT技術文章收藏夾
    http://www.lai18.com/content/24632831.html

    總結

    以上是生活随笔為你收集整理的linux常用运维命令【转】的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。