Linux日常命令整理
一、檢視磁碟使用情況
[root@jerrik gc]# df -h 檔案系統容量已用可用 已用% 掛載點 /dev/vda140G3.9G34G11% / devtmpfs1.9G01.9G0% /dev tmpfs1.9G716K1.9G1% /dev/shm tmpfs1.9G280K1.9G1% /run tmpfs1.9G01.9G0% /sys/fs/cgroup tmpfs380M0380M0% /run/user/0
二、檢視根目錄資料夾佔用磁碟情況
[root@jerrik /]# du -sh * 0bin 154Mboot 716Kdev 32Metc 19Mhome 0lib 0lib64 16Klost+found 2.9Gusr 495Mvar ...
三、刪除指定目錄下的檔案
find /usr/local/software/gc -name "*.log" -exec rm{} \; find . -name "*.log"|xargs rm -rf {};
四、檢視、建立定時任務(crontab)
建立定時任務:
crontab -e 進入vim 然後鍵入: * * * * * check.sh :wq儲存即可
如果要檢視定時任務是否執行,可以檢視/var/log/cron
中的執行日誌.
檢視定時任務:
[root@jerrik software]# crontab -l * * * * * echo "hello world" * * * * * /usr/local/software/demo.sh [root@jerrik software]#
五、按照更新時間來查詢檔案
#檢視最近2分鐘之內沒有更新的檔案 find . -cmin +2 #查詢檔案更新時間在距現在二天以內的檔案 find ./ -mtime -2 #查詢檔案更新時間距現在二天以上的檔案 find ./ -mtime +2
六、後臺執行命令
nohup [command] >/dev/null 2>&1 &
備註:
【/dev/null】:是要把輸出重定向到空裝置上
【2>&1】: 把標準錯誤2重定向到標準輸出中1中
【最後一個&】: 即後臺執行
七、top檢視系統負載
[root@jerrik software]# top top - 23:27:35 up 130 days,1:04,1 user,load average: 0.00, 0.01, 0.05 Tasks:70 total,1 running,69 sleeping,0 stopped,0 zombie %Cpu(s):0.0/0.00[] KiB Mem :3881920 total,2087416 free,350412 used,1444092 buff/cache KiB Swap:0 total,0 free,0 used.3237956 avail Mem
【大寫P】:按CPU從高到低排序
【大寫M】:按Memory從高到低排序
【數字1】:顯示所有CPU
八、檢視CPU型號
[root@jerrik software]# lscpu Architecture:x86_64 CPU op-mode(s):32-bit, 64-bit Byte Order:Little Endian CPU(s):2 On-line CPU(s) list:0,1 Thread(s) per core:2 Core(s) per socket:1 座:1 NUMA 節點:1 廠商 ID:GenuineIntel CPU 系列:6 型號:85 型號名稱:Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz 步進:4 CPU MHz:2499.996 BogoMIPS:4999.99 超管理器廠商:KVM 虛擬化型別:完全 L1d 快取:32K L1i 快取:32K L2 快取:1024K L3 快取:33792K NUMA 節點0 CPU:0,1
九、檢視物理CPU、邏輯CPU、CPU執行緒
#物理CPU #說明只有一核 cat /proc/cpuinfo | grep "physical id"|sort|uniq physical id: 0 #邏輯CPU #說明單個CPU的邏輯核心數量是1個 cat /proc/cpuinfo | fgrep "cores"|uniq cpu cores: 1 #檢視CPU執行緒數 #系統CPU執行緒數為2 cat /proc/cpuinfo | grep "processor" | wc -l 2
備註:該CPU只有一個物理CPU,且該CPU只有一個邏輯核心,但是對應了2個CPU執行緒,所以該CPU啟用了【超執行緒】技術。
十、dstat命令
dstat [-afv] [options..] [delay [count]] -c:顯示CPU系統佔用,使用者佔用,空閒,等待,中斷,軟體中斷等資訊。 -C:當有多個CPU時候,此引數可按需分別顯示cpu狀態,例:-C 0,1 是顯示cpu0和cpu1的資訊。 -d:顯示磁碟讀寫資料大小。 -D hda,total:include hda and total。 -n:顯示網路狀態。 -N eth1,total:有多塊網絡卡時,指定要顯示的網絡卡。 -l:顯示系統負載情況。 -m:顯示記憶體使用情況。 -g:顯示頁面使用情況。 -p:顯示程序狀態。 -s:顯示交換分割槽使用情況。 -S:類似D/N。 -r:I/O請求情況。 -y:系統狀態。 --ipc:顯示ipc訊息佇列,訊號等資訊。 --socket:用來顯示tcp udp埠狀態。 -a:此為預設選項,等同於-cdngy。 -v:等同於 -pmgdsc -D total。 --output 檔案:此選項也比較有用,可以把狀態資訊以csv的格式重定向到指定的檔案中,以便日後檢視. #3秒鐘監控一次,總共100次 dstat 3 100 [root@jerrik software]# dstat 3 1000 You did not select any stats, using -cdngy by default. ----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system-- usr sys idl wai hiq siq| readwrit| recvsend|inout | intcsw 0099000|46B 3975B|00 |00 | 5821149 0099000|0819B|24B40B|00 | 224448 ... #監控swap,process,sockets,filesystem並顯示監控的時間 [root@iZ23uulau1tZ ~]# dstat -tsp --socket --fs ----system---- ----swap--- ---procs--- ------sockets------ --filesystem- date/time| usedfree|run blk new|tot tcp udp raw frg|filesinodes 26-07 09:23:48|00 |00 0.0|1048500|7046488 26-07 09:23:49|00 |000|1048500|7046488 26-07 09:23:50|00 |000|1048500|7046489 26-07 09:23:51|00 |000|1048500|7046489 26-07 09:23:52|00 |000|1048500|7046489 26-07 09:23:53|00 |000|1048500|7046489
十一、檢視程序啟動時間
ps -p [pid] -o lstart
十一、tcpdump抓包
#抓取所有經過 eth0,目的或源地址是 192.168.29.162 的網路資料 命令:tcpdump -n -i eth0 host 192.168.29.162 # 源地址 命令:tcpdump -i eth1 src host 192.168.29.162 # 目的地址 命令:tcpdump -i eth1 dst host 192.168.29.162 #抓取當前伺服器eth0網絡卡埠8080的網路資料 命令:tcpdump -n -i eth0 port 8080 #抓取目標地址192.168.29.162埠8080的資料 命令:tcpdump -n -i eth0 dst host 192.168.29.162 andport 8080 #抓取mysql執行的sql語句 命令:tcpdump -i eth1 -s 0 -l -w - dst port 3306 | strings #抓取mysql通訊的網路包(cap用wireshark開啟) 命令tcpdump -n -nn -tttt -i eth0 -s 65535 'port 3306' -w 20181005mysql.cap #抓取SMTP 資料 命令:tcpdump -i eth1 'tcp[tcpflags] & tcp-syn != 0 and tcp[tcpflags] & tcp-ack != 0' #抓取HTTP GET資料,"GET "的十六進位制是 47455420 命令:tcpdump -i eth1 'tcp[(tcp[12]>>2):4] = 0x47455420' #抓取SSH返回,"SSH-"的十六進位制是 0x5353482D 命令:tcpdump -i eth1 'tcp[(tcp[12]>>2):4] = 0x5353482D' #實時抓取埠號8080的GET包,然後寫入GET.log 命令:tcpdump -i eth0 '((port 8080) and (tcp[(tcp[12]>>2):4]=0x47455420))' -nnAl -w /tmp/GET.log #抓取指定SYN個數,-c 引數指定抓多少個包。 命令:time tcpdump -nn -i eth0 'tcp[tcpflags] = tcp-syn' -c 10
總結
對一些常用的操作簡單的總結了一下,由於時間的關係,今天就先到這裡。以後會陸續進行補充,加深記憶。