十八、生產(chǎn)常用命令
1.刪除0字節(jié)文件
find -type f -size 0 -exec rm -rf {} \;
2.查看進程
按內(nèi)存從大到小排列
ps -e -o “%C : %p : %z : %a”|sort -k5 -nr
3.按cpu利用率從大到小排列
ps -e -o “%C : %p : %z : %a”|sort -nr
4.打印說cache里的URL
grep -r -a jpg /data/cache/* | strings | grep “http:” | awk -F’http:’ ‘{print “http:”$2;}’
5.查看http的并發(fā)請求數(shù)及其TCP連接狀態(tài):
netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
6.
sed -i ‘/Root/s/no/yes/’ /etc/ssh/sshd_config sed在這個文里Root的一行,匹配Root一行,將no替換成yes.
7.1.如何殺掉mysql進程:
ps aux|grep mysql|grep -v grep|awk ‘{print $2}’|xargs kill -9
?。◤闹辛私獾絘wk的用途)
pgrep mysql |xargs kill -9
killall -TERM mysqld
kill -9 `cat /usr/local/apache2/logs/httpd.pid`
試試查殺進程PID
8.顯示運行3級別開啟的服務(wù):
ls /etc/rc3.d/S* |cut -c 15-
?。◤闹辛私獾絚ut的用途,截取數(shù)據(jù))
9.如何在編寫SHELL顯示多個信息,用EOF
cat 《《 EOF +--------------------------------------------------------------+ | === Welcome to Tunoff services === | +--------------------------------------------------------------+ EOF
10. for 的巧用(如給mysql建軟鏈接)
cd /usr/local/mysql/bin for i in * do ln /usr/local/mysql/bin/$i /usr/bin/$i done
11. 取IP地址:
ifconfig eth0|sed -n ‘2p’|awk ‘{print $2}’|cut -c 6-30 或者: ifconfig eth0 |grep “inet addr:” |awk ‘{print $2}’|cut -c 6- 或者 ifconfig | grep ‘inet addr:’| grep -v ‘127.0.0.1’ | cut -d: -f2 | awk ‘{ print $1}’ 或者: ifconfig eth0 | sed -n ‘/inet /{s/.*addr://;s/ .*//;p}’
Perl實現(xiàn)獲取IP的方法:
ifconfig -a | perl -ne ‘if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1\n); exit 0; }’
12.內(nèi)存的大小: free -m |grep “Mem” | awk ‘{print $2}’
13. netstat -an -t | grep “:80” | grep ESTABLISHED | awk ‘{printf “%s %s\n”,$5,$6}’ | sort
14.查看Apache的并發(fā)請求數(shù)及其TCP連接狀態(tài): netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
15.因為同事要統(tǒng)計一下服務(wù)器下面所有的jpg的文件的大小,寫了個shell給他來統(tǒng)計。原來用xargs實現(xiàn),但他一次處理一部分,搞的有多個總和。。。。,下面的命令就能解決啦。
find / -name *.jpg -exec wc -c {} \;|awk ‘{print $1}’|awk ‘{a+=$1}END{print a}’
CPU的數(shù)量(多核算多個CPU, cat /proc/cpuinfo |grep -c processor )越多,系統(tǒng)負載越低,每秒能處理的請求數(shù)也越多。
--------------------------------------------------------------------------------------------------------------------
16. CPU負載 # cat /proc/loadavg 檢查前三個輸出值是否超過了系統(tǒng)邏輯CPU的4倍。
18. CPU負載 #mpstat 1 1 檢查%idle是否過低(比如小于5%)
19. 內(nèi)存空間 # free 檢查free值是否過低 也可以用 # cat /proc/meminfo
20. swap空間 # free 檢查swap used值是否過高 如果swap used值過高,進一步檢查swap動作是否頻繁: # vmstat 1 5 觀察si和so值是否較大
21. 磁盤空間 # df -h 檢查是否有分區(qū)使用率(Use%)過高(比如超過90%) 如發(fā)現(xiàn)某個分區(qū)空間接近用盡,可以進入該分區(qū)的掛載點,用以下命令找出占用空間最多的文件或目錄: # du -cks * | sort -rn | head -n 10
22. 磁盤I/O負載 # iostat -x 1 2 檢查I/O使用率(%util)是否超過100%
23. 網(wǎng)絡(luò)負載 # sar -n DEV 檢查網(wǎng)絡(luò)流量(rxbyt/s, txbyt/s)是否過高
24. 網(wǎng)絡(luò)錯誤 # netstat -i 檢查是否有網(wǎng)絡(luò)錯誤(drop fifo colls carrier) 也可以用命令:# cat /proc/net/dev
25. 網(wǎng)絡(luò)連接數(shù)目 # netstat -an | grep -E “^(tcp)” | cut -c 68- | sort | uniq -c | sort -n
26. 進程總數(shù) # ps aux | wc -l 檢查進程個數(shù)是否正常 (比如超過250)
27. 可運行進程數(shù)目 # vmwtat 1 5 列給出的是可運行進程的數(shù)目,檢查其是否超過系統(tǒng)邏輯CPU的4倍
28. 進程 # top -id 1 觀察是否有異常進程出現(xiàn)
29. 網(wǎng)絡(luò)狀態(tài) 檢查DNS, 網(wǎng)關(guān)等是否可以正常連通
ping traceroute nslookup dig
30. 用戶 # who | wc -l 檢查登錄用戶是否過多 (比如超過50個) 也可以用命令:# uptime
31. 系統(tǒng)日志 # cat /var/log/rflogview/*errors
檢查是否有異常錯誤記錄 也可以搜尋一些異常關(guān)鍵字,例如:
# grep -i error /var/log/messages # grep -i fail /var/log/messages
# egrep -i ‘error|warn’ /var/log/messages 查看系統(tǒng)異常 32 核心日志
# dmesg 檢查是否有異常錯誤記錄
33. 系統(tǒng)時間 # date 檢查系統(tǒng)時間是否正確
當前時間: date +“%Y-%m-%d %H:%M:%S”
34. 打開文件數(shù)目 # lsof | wc -l 檢查打開文件總數(shù)是否過多
35. 日志 # logwatch ?print 配置/etc/log.d/logwatch.conf,將 Mailto 設(shè)置為自己的email 地址,啟動mail服務(wù) (sendmail或者postfix),這樣就可以每天收到日志報告了。
缺省logwatch只報告昨天的日志,可以用# logwatch ?print ?range all 獲得所有的日志分析結(jié)果。
可以用# logwatch ?print ?detail high 獲得更具體的日志分析結(jié)果(而不僅僅是出錯日志)。
36.殺掉80端口相關(guān)的進程 lsof -i :80|grep -v “PID”|awk ‘{print “kill -9”,$2}’|sh
37.清除僵死進程。 ps -eal | awk ‘{ if ($2 == “Z”) {print $4}}’ | kill -9
38.tcpdump 抓包 ,用來防止80端口被人攻擊時可以分析數(shù)據(jù)
# tcpdump -c 10000 -i eth0 -n dst port 80 》 /root/pkts
39.然后檢查IP的重復(fù)數(shù) 并從小到大排序 注意 “-t\ +0″ 中間是兩個空格
# less pkts | awk {‘printf $3“\n”’} | cut -d. -f 1-4 | sort | uniq -c | awk {‘printf $1“ ”$2“\n”’} | sort -n -t\ +0
40.查看有多少個活動的php-cgi進程
netstat -anp | grep php-cgi | grep ^tcp | wc -l
41.利用iptables對應(yīng)簡單攻擊
netstat -an | grep -v LISTEN | awk ‘{print $5}’ |grep -v 127.0.0.1|grep -v 本機ip|sed “s/::ffff://g”|awk ‘BEGIN { FS=”:” } { Num[$1]++ } END { for(i in Num) if(Num》8) { print i} }’ |grep ‘[0-9]\{1,3\}\。[0-9]\{1,3\}\。[0-9]\{1,3\}\。[0-9]\{1,3\}’| xargs -i[] iptables -I INPUT -s [] -j DROP
Num》8部分設(shè)定值為閥值,這條句子會自動將netstat -an 中查到的來自同一IP的超過一定量的連接的列入禁止范圍。 基中本機ip改成你的服務(wù)器的ip地址
42. 怎樣知道某個進程在哪個CPU上運行?
# ps -eo pid,args,psr
43. 查看硬件制造商
dmidecode -s system-product-name
44.perl如何編譯成字節(jié)碼,這樣在處理復(fù)雜項目的時候會更快一點?
perlcc -B -o webseek webseek.pl
45. 統(tǒng)計var目錄下文件以M為大小,以列表形式列出來。
find /var -type f | xargs ls -s | sort -rn | awk ‘{size=$1/1024; printf(“%dMb %s\n”, size,$2);}’ | head
查找var目錄下文件大于100M的文件,并統(tǒng)計文件的個數(shù)
find /var -size +100M -type f | tee file_list | wc -l
46. sed 查找并替換內(nèi)容
sed -i “s/varnish/LTCache/g” `grep “Via” -rl /usr/local/src/varnish-2.0.4`
sed -i “s/X-Varnish/X-LTCache/g” `grep “X-Varnish” -rl /usr/local/src/varnish-2.0.4`
47. 查看服務(wù)器制造商
dmidecode -s system-product-name
48. wget 模擬user-agent抓取網(wǎng)頁
wget -m -e robots=off -U “Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6” http://www.example.com/
50. 統(tǒng)計目錄下文件的大?。ò碝打印顯示)
du $1 –max-depth=1 | sort -n|awk ‘{printf “%7.2fM —-》 %s\n”,$1/1024,$2}’|sed ‘s:/.*/\([^/]\{1,\}\)$:\1:g’
51.關(guān)于CND實施幾個相關(guān)的統(tǒng)計
統(tǒng)計一個目錄中的目錄個數(shù)
ls -l | awk ‘/^d/’ | wc -l
統(tǒng)計一個目錄中的文件個數(shù)
ls -l | awk ‘/^-/’ | wc -l
統(tǒng)計一個目錄中的全部文件數(shù)
find 。/ -type f -print | wc -l
統(tǒng)計一個目錄中的全部子目錄數(shù)
find 。/ -type d -print | wc -l
統(tǒng)計某類文件的大小:
find 。/ -name “*.jpg” -exec wc -c {} \;|awk ‘{print $1}’|awk ‘{a+=$1}END{print a}’
53. 查找占用磁盤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
54. 去掉第一列(如行號代碼)
awk ‘{for(i=2;i《=NF;i++) if(i!=NF){printf $i“ ”}else{print $i} }‘ list
55.輸出256中色彩
for i in {0..255}; do echo -e “\e[38;05;${i}m${i}”; done | column -c 80 -s ’ ‘; echo -e “\e[m”
56.查看機器支持內(nèi)存 機器插內(nèi)存情況: dmidecode |grep -P “Maximum\s+Capacity”
機器最大支持內(nèi)存: dmidecode |grep -P “Maximum\s+Capacity”
57.查看PHP-CGI占用的內(nèi)存總數(shù):
total=0; for i in `ps -C php-cgi -o rss=`; do total=$(($total+$i)); done; echo “PHP-CGI Memory usage: $total kb”
1、find用法
[root@template tmp]# find / -type f -name “text.txt”
/tmp/text.txt
/root/text.txt#找到文件后,交給管道刪除[root@template tmp]# find / -type f -name “text.txt” | xargs rm -f
面試題:刪除一個目錄下的所有文件,但保留一個指定文件(保留file10)
[root@template tmp]# touch file{1..10}
[root@template tmp]# ll
總用量 0
-rw-r--r-- 1 root root 0 5月 20 10:15 file1
-rw-r--r-- 1 root root 0 5月 20 10:11 file10
-rw-r--r-- 1 root root 0 5月 20 10:15 file2
-rw-r--r-- 1 root root 0 5月 20 10:15 file3
-rw-r--r-- 1 root root 0 5月 20 10:15 file4
-rw-r--r-- 1 root root 0 5月 20 10:15 file5
-rw-r--r-- 1 root root 0 5月 20 10:15 file6
-rw-r--r-- 1 root root 0 5月 20 10:15 file7
-rw-r--r-- 1 root root 0 5月 20 10:15 file8
-rw-r--r-- 1 root root 0 5月 20 10:15 file9
#方法一:
[root@template tmp]# find /tmp -type f ! -name “file10”|xargs rm -f
方法二:
?。踨oot@template tmp]# find /tmp -type f ! -name “file10” -exec rm -f {} \;#find找到的內(nèi)容,-exec是參數(shù),{}:要查找的目標,一般可以不寫 \ :反斜杠轉(zhuǎn)義字符 !作用就是:取反
2、rm remove 刪除文件或者目錄
-f 強制
-r 刪除目錄
注意:生產(chǎn)場景盡量不要使用rm,如果非要用,一定要先cp備份
?。踨oot@template tmp]# ll
總用量 0
-rw-r--r-- 1 root root 0 5月 20 10:06 text.txt
?。踨oot@template tmp]# rm -f text.txt
?。踨oot@template tmp]# ll
總用量 0
3、echo用法
打印輸出內(nèi)容,配合“》或》》” 可以為文件覆蓋及追加內(nèi)容
“》” 意思是重定向,會清除文件里所有以前數(shù)據(jù)
“》》” 為追加內(nèi)容
4、cat 查看文件內(nèi)容
特殊用法:增加多行內(nèi)容
?。踨oot@template tmp]# cat 》》/tmp/nulige.txt 《《EOF
》 I am studying linux.
》 EOF
?。踨oot@template tmp]# ll
總用量 4
-rw-r--r-- 1 root root 0 5月 20 10:11 file10
-rw-r--r-- 1 root root 21 5月 20 10:33 nulige.txt
[root@template tmp]# cat nulige.txt
I am studying linux.
cat高級用法示例:
1、過濾出除liya的字母
[root@template tmp]# cat text.txt
test
limen
liya
方法一:
?。踨oot@template tmp]# cat text.txt|grep -v “l(fā)iya” text.txt
test
limen
方法二:
?。踨oot@template tmp]# head -2 text.txt
test
limen
6、sed命令
作用:過濾:sed -n ’/過濾的內(nèi)容/處理的命令‘ 文件
-n 取消sed 的默認輸出
-p print打印
-d delete刪除
-i 改變文件內(nèi)容
1、過濾出除liya的字母
[root@template tmp]# cat text.txt
test
limen
liya
方法一:[root@template tmp]# grep -v “l(fā)iya” text.txt
test
limen方法二:
?。踨oot@template tmp]# sed -e /^tmp/d text.txt
test
limen
liya
方法三:
?。踨oot@template tmp]# sed -e /^liya/d text.txt
test
limen
方法四:
[root@template tmp]# sed /liya/d text.txt
test
limen
5、cp 命令
語法:
cp 源文件 目錄文件(cp 的得要參數(shù)apr)
參數(shù):
-a :相當于-pdr
-d :若源文件為鏈接文件(link file),則復(fù)制鏈接文件屬性而非檔案本身
-f :強制,右目錄檔案已經(jīng)存在且無法開啟,則移除后再嘗試
-i: 若目標文件已經(jīng)存在時,覆蓋時會先詢問
-p:連同檔案的屬性一起復(fù)制過去,而非使用默認屬性
-r:遞歸,用于復(fù)制目錄
-u:若目標文件存在,則目標文件比源文件舊時才復(fù)制
提示:如果源文件是多個,那么目標文件在最后,且是目錄
6、date----顯示、修改系統(tǒng)時間
date [options][+format][date]
$ date -s 06/09/2004 修改日期(按月日年格式)
$ date -s 13:56:00 修改時間(按時分秒格式)
$ date -r test 顯示test文件最后一次的修改時間
$ date +’%Y-%m-%d‘ 以yyyy-mm-dd格式顯示日期,其它格式請參考幫助
$ clock -r 查詢BIOS時間
$ clock -w 把修改后的時間寫回BIOS
評論