Master Server Log Analysis with Essential Linux Commands
This guide presents a curated set of practical Linux command-line techniques for analyzing web server logs, including counting unique IPs, identifying popular pages, measuring traffic volume, detecting slow or error responses, and monitoring connection states, enabling administrators to efficiently monitor performance and security.
Running a personal website on Alibaba Cloud ECS, the author shares useful command-line snippets for analyzing Apache access logs.
Basic IP and Page Statistics
awk '{print $1}' log_file|sort|uniq|wc -lCount the number of unique IP addresses. grep "/index.php" log_file | wc -l Count how many times a specific page was accessed.
awk '{++S[$1]} END {for (a in S) print a,S[a]}' log_file > log.txt
sort -n -t ' ' -k 2 log.txtShow how many pages each IP accessed.
awk '{++S[$1]} END {for (a in S) print S[a],a}' log_file | sort -nList IPs sorted by the number of pages they accessed.
grep ^111.111.111.111 log_file| awk '{print $1,$7}'Show which pages a particular IP visited.
awk '{print $12,$1}' log_file | grep ^"Mozilla | awk '{print $2}' |sort | uniq | wc -lExclude search engine crawlers from the count.
awk '{print $4,$1}' log_file | grep 16/Aug/2015:14 | awk '{print $2}'| sort | uniq | wc -lCount unique IPs within a specific hour.
awk '{print $1}' |sort|uniq -c|sort -nr |head -10Show the top 10 IP addresses by request count.
uniq -c groups and places the count at the beginning.
cat access.log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10Another way to list the top 10 IPs.
cat access.log|awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}List the most requested files or pages.
cat access.log | awk '{print $11}' | sed -e ' s/http:\/\///' -e ' s/\/.*//' | sort | uniq -c | sort -rn | head -20Count accesses by subdomain using the referer field.
cat www.access.log |awk '($7~/\.php/){print $10 " " $1 " " $4 " " $7}'|sort -nr|head -100List files with the largest transfer size.
cat www.access.log |awk '($10 > 200000 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100Find pages larger than 200 KB and how often they were requested.
cat www.access.log |awk '($7~/\.php/){print $NF " " $1 " " $4 " " $7}'|sort -nr|head -100Show pages with the longest response time when the last column records transfer time.
cat www.access.log |awk '($NF > 60 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100List pages that took more than 60 seconds.
cat www.access.log |awk '($NF > 30){print $7}'|sort -n|uniq -c|sort -nr|head -20List pages with transfer time over 30 seconds.
ps -ef | awk -F ' ' '{print $8 " " $9}' |sort | uniq -c |sort -nr |head -20Show the number of processes per command, sorted descending. netstat -an | grep ESTABLISHED | wc -l Count current established connections (Apache concurrent requests). netstat -nat|grep -i "80"|wc -l Total number of requests on port 80. netstat -na|grep ESTABLISHED|wc -l Number of established TCP connections.
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
", a, S[a]);++I}printf("%-20s %s
","TOTAL_IP",I);for(a in s) printf("%-20s %s
",a, s[a]);printf("%-20s %s
","TOTAL_LINK",N);}'Display per‑IP connection counts and totals for each TCP state.
LAST_ACK 5 – connection closing; SYN_RECV 30 – pending requests; ESTABLISHED 1597 – normal data transfer; FIN_WAIT1 51 – server closing; FIN_WAIT2 504 – client closing; TIME_WAIT 1057 – waiting after close.
These commands provide a comprehensive toolbox for monitoring traffic, performance, and security of a web server.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
