Linux核心命令
- strace
- netstat
- perf
- top
- pidstat
- mpstat
- dstat
- vmstat
- slabtop
- free
- top
- tcpdump
- ip
- nicstat
- dtrace
- ping
- dtrace
- blktrace
- iptop
- iostat
- stap
文本處理類的命令:
-
wc
wc [option] [file]... -l: 統計行數 -c: 統計字節數 -w;統計單詞數
-
tr
tr: 轉換字符或刪除字符 tr '集合1' '集合2' tr -d '字符集合'
-
cut
This is a test line. -d字符:指定分隔符 -f#: 指定要顯示字段 單個數字:一個字段 逗號分隔的多個數字:指定多個離散字段 -:連續字段,如3-5;
-
sort
按字符進行比較 sort [option] file... -f: 忽略字符大小寫; -n: 比較數值大小; -t: 指定分隔符 -k: 指定分隔後進行比較字段 -u: 重複的行,只顯示一次;
-
uniq
移除重複的行 -c:顯示每行重複的次數 -d:僅顯示重複過的行 -u: 僅顯示不曾重複的行
-
工具速查鏈接
http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/index.html
shell習題訓練
- 求2個數之和
- 計算1-100的和
- 將一目錄下所有的文件的擴展名改爲bak
- 編譯當前目錄下的所有.c文件:
- 打印root可以使用可執行文件數,處理結果: root's bins: 2306
- 打印當前sshd的端口和進程id,處理結果: sshd Port&&pid: 22 5412
-
輸出本機創建20000個目錄所用的時間,處理結果:
real 0m3.367s user 0m0.066s sys 0m1.925s
-
打印本機的交換分區大小,處理結果: Swap:1024M
-
文本分析,取出/etc/password中shell出現的次數
第一種方法結果: 4 /bin/bash 1 /bin/sync 1 /sbin/halt 31 /sbin/nologin 1 /sbin/shutdown 第二種方法結果: /bin/sync 1 /bin/bash 1 /sbin/nologin 30 /sbin/halt 1 /sbin/shutdown 1
-
文件整理,employee文件中記錄了工號和姓名,(提示join)
employee.txt: 100 Jason Smith 200 John Doe 300 Sanjay Gupta 400 Ashok Sharma bonus文件中記錄工號和工資 bonus.txt: 100 $5,000 200 $500 300 $3,000 400 $1,250 要求把兩個文件合併並輸出如下,處理結果: 400 ashok sharma $1,250 100 jason smith $5,000 200 john doe $500 300 sanjay gupta $3,000
-
寫一個shell腳本來得到當前的日期,時間,用戶名和當前工作目錄。
- 編寫shell腳本獲取本機的網絡地址。
- 編寫個shell腳本將當前目錄下大於10K的文件轉移到/tmp目錄下
-
編寫一個名爲myfirstshell.sh的腳本,它包括以下內容。
a) 包含一段註釋,列出您的姓名、腳本的名稱和編寫這個腳本的目的。 b) 問候用戶。 c) 顯示日期和時間。 d) 顯示這個月的日曆。 e) 顯示您的機器名。 f) 顯示當前這個操作系統的名稱和版本。 g) 顯示父目錄中的所有文件的列表。 h) 顯示root正在運行的所有進程。 i) 顯示變量TERM、PATH和HOME的值。 j) 顯示磁盤使用情況。 k) 用id命令打印出您的組ID。 m) 跟用戶說“Good bye”
-
文件移動拷貝,有m1.txt m2.txt m3.txt m4.txt,分別創建出對應的目錄,m1 m2 m3 m4 並把文件移動到對應的目錄下
- root用戶今天登陸了多長時間
- 終端輸入一個文件名,判斷是否是設備文件
-
統計IP訪問:要求分析apache訪問日誌,找出訪問頁面數量在前100位的IP數。日誌大小在78M左右。以下是apache的訪問日誌節選
202.101.129.218 - - [26/Mar/2006:23:59:55 +0800] "GET /online/stat_inst.php?pid=d065 HTTP/1.1" 302 20-"-" "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
-
設計一個Shell程序,在/userdata目錄下建立50個目錄,即user1~user50,並設置每個目錄的權限,其中其他用戶的權限爲:讀;文件所有者的權限爲:讀、寫、執行;文件所有者所在組的權限爲:讀、執行。
- 設計一個shell程序,添加一個新組爲class1,然後添加屬於這個組的30個用戶,用戶名的形式爲stdxx,其中xx從01到30,並設置密碼爲對應的stdxx。
- 編寫shell程序,實現自動刪除30個賬號的功能。賬號名爲std01至std30。
- 用戶清理,清除本機除了當前登陸用戶以外的所有用戶
- 設計一個shell程序,在每月第一天備份並壓縮/etc目錄的所有內容,存放在/root/bak目錄裏,且文件名,爲如下形式yymmdd_etc,yy爲年,mm爲月,dd爲日。Shell程序fileback存放在/usr/bin目錄下。
- 對於一個用戶日誌文件,每行記錄了一個用戶查詢串,長度爲1-255字節,共幾千萬行,請排出查詢最多的前100條。 日誌可以自己構造。 (提示:awk sort uniq head)
- 編寫自己的ubuntu環境安裝腳本
- 編寫服務器守護進程管理腳本。
-
查看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
-
查找請求數請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
-
獲得訪問前10位的ip地址
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 counts[url], url}’
-
訪問次數最多的文件或頁面,取前20
cat access.log|awk ‘{print $11}’|sort|uniq -c|sort -nr|head -20
-
列出傳輸最大的幾個exe文件(分析下載站的時候常用)
cat access.log |awk ‘($7~/.exe/){print $10 " " $1 " " $4 " " $7}’|sort -nr|head -20
-
列出輸出大於200000byte(約200kb)的exe文件以及對應文件發生次數
cat access.log |awk ‘($10 > 200000 && $7~/.exe/){print $7}’|sort -n|uniq -c|sort -nr|head -100
-
如果日誌最後一列記錄的是頁面文件傳輸時間,則有列出到客戶端最耗時的頁面
cat access.log |awk ‘($7~/.php/){print $NF " " $1 " " $4 " " $7}’|sort -nr|head -100
-
列出最最耗時的頁面(超過60秒的)的以及對應頁面發生次數
cat access.log |awk ‘($NF > 60 && $7~/.php/){print $7}’|sort -n|uniq -c|sort -nr|head -100
-
列出傳輸時間超過 30 秒的文件
cat access.log |awk ‘($NF > 30){print $7}’|sort -n|uniq -c|sort -nr|head -20
-
統計網站流量(G)
cat access.log |awk ‘{sum+=$10} END {print sum/1024/1024/1024}’
-
統計404的連接
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
-
蜘蛛分析,查看是哪些蜘蛛在抓取內容。
/usr/sbin/tcpdump -i eth0 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'bot|crawler|slurp|spider'
-
創建一個用戶mandriva,其ID號爲2002,基本組爲distro(組ID爲3003),附加組爲linux;
# groupadd linux # groupadd -g 3003 distro # useradd -u 2002 -g distro -G linux mandriva
-
創建一個用戶fedora,其全名爲Fedora Community,默認shell爲tcsh; # useradd -c "Fedora Community" -s /bin/tcsh fedora
-
修改mandriva的ID號爲4004,基本組爲linux,附加組爲distro和fedora;
# usermod -u 4004 -g linux -G distro,fedora mandriva
-
給fedora加密碼,並設定其密碼最短使用期限爲2天,最長爲50天;
# passwd fedora # chage -m 2 -M 50 fedora
-
調試命令
strace -p pid
-
寫一個腳本
1、創建一個組newgroup, id號爲4000; 2、創建一個用戶mageedu1, id號爲3001,附加組爲newgroup; 3、創建目錄/tmp/hellodirxyz 4、複製/etc/fstab至上面的目錄中 5、改變目錄及內部文件的屬主和屬組爲mageedu1; 6、讓目錄及內部文件的其它用戶沒有任何權限; #!/bin/bash # Description: # Version: # Datetime: # Author: myGroup="newgroup1" myUser="mageedu2" myDir="/tmp/hellodirxyz1" myID=3002 groupadd -g 4001 $myGroup useradd -u $myID -G $myGroup $myUser mkdir $myDir cp /etc/fstab $myDir chown -R $myUser:$myUser $myDir chmod -R o= $myDir unset myGroup myUser myID myDir
-
統計/bin、/usr/bin、/sbin和/usr/sbin等各目錄中的文件個數;
# ls /bin | wc -l
-
顯示當前系統上所有用戶的shell,要求,每種shell只顯示一次;
# cut -d: -f7 /etc/passwd | sort -u
-
取出/etc/passwd文件的第7行;
# head -7 /etc/passwd | tail -1
-
顯示第3題中取出的第7行的用戶名;
# head -7 /etc/passwd | tail -1 | cut -d: -f1 # head -7 /etc/passwd | tail -1 | cut -d: -f1 | tr 'a-z' 'A-Z'
-
統計/etc目錄下以P或p開頭的文件個數;
# ls -d /etc/[Pp]* | wc -l
-
寫一個腳本,用for循環實現顯示/etc/init.d/functions、/etc/rc.d/rc.sysinit和/etc/fstab各有多少行;
for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab; do wc -l $fileName done #!/bin/bash for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab; do lineCount=`wc -l $fileName | cut -d' ' -f1` echo "$fileName: $lineCount lines." done #!/bin/bash for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab; do echo "$fileName: `wc -l $fileName | cut -d' ' -f1` lines." done
-
寫一個腳本,將上一題中三個文件的複製到/tmp目錄中;用for循環實現,分別將每個文件的最近一次的修改時間改爲2016年12月15號15點43分;
for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab; do cp $fileName /tmp baseName=`basename $fileName` touch -m -t 201109151327 /tmp/$baseName done
-
寫一個腳本, 顯示/etc/passwd中第3、7和11個用戶的用戶名和ID號;
for lineNo in 3 7 11; do userInfo=`head -n $lineNo /etc/passwd | tail -1 | cut -d: -f1,3` echo -e "User: `echo $userInfo | cut -d: -f1`\nUid: `echo $userInfo |cut -d: -f2`" done
-
顯示/proc/meminfo文件中以大小寫s開頭的行;
# grep "^[sS]" /proc/meminfo # grep -i "^s" /proc/meminfo
-
取出默認shell爲非bash的用戶;
# grep -v "bash$" /etc/passwd | cut -d: -f1
-
取出默認shell爲bash的且其ID號最大的用戶;
# grep "bash$" /etc/passwd | sort -n -t: -k3 | tail -1 | cut -d: -f1
-
顯示/etc/rc.d/rc.sysinit文件中,以#開頭,後面跟至少一個空白字符,而後又有至少一個非空白字符的行;
# grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}" /etc/rc.d/rc.sysinit
-
顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行;
# grep "^[[:space:]]\{1,\}[^[:space:]]\{1,\}" /boot/grub/grub.conf
-
找出/etc/passwd文件中一位數或兩位數;
# grep --color=auto "\<[0-9]\{1,2\}\>" /etc/passwd
-
找出ifconfig命令結果中的1到255之間的整數;
# ifconfig | grep -E --color=auto "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
-
查看當前系統上root用戶的所有信息;
# grep "^root\>" /etc/passwd
-
添加用戶bash和testbash、basher,而後找出當前系統上其用戶名和默認shell相同的用戶;
# grep --color=auto "^\([[:alnum:]]\{1,\}\)\>.*\1$" /etc/passwd
-
找出netstat -tan命令執行的結果中以“LISTEN”或“ESTABLISHED”結尾的行;
- 取出當前系統上所有用戶的shell,要求:每種shell只顯示一次,且按升序顯示;
# cut -d: -f7 /etc/passwd | sort -u
自動化
開機自啓動腳本
如果要添加爲開機啓動執行的腳本文件,可先將腳本複製或者軟連接到/etc/init.d/目錄下,然後用:
update-rc.d xxx defaults NN命令(NN爲啓動順序),
將腳本添加到初始化執行的隊列中去。
注意如果腳本需要用到網絡,則NN需設置一個比較大的數字,如99。
1) 將你的啓動腳本複製到 /etc/init.d目錄下,以下假設你的腳本文件名爲 test。
2) 設置腳本文件的權限
$ sudo chmod 755 /etc/init.d/test
3) 執行如下命令將腳本放到啓動腳本中去:
$ cd /etc/init.d $ sudo update-rc.d test defaults 95