Java 開發(fā)常用的 Linux 命令知識(shí) (qq.com)
基本操作
Linux關(guān)機(jī),重啟
重啟
shutdown -r now`
查看系統(tǒng),CPU信息
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 查看系統(tǒng)內(nèi)核信息
uname -a
查看系統(tǒng)內(nèi)核版本
cat /proc/version
查看當(dāng)前用戶環(huán)境變量
env
cat /proc/cpuinfo
查看有幾個(gè)邏輯cpu, 包括cpu型號(hào)
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
查看有幾顆cpu,每顆分別是幾核
cat /proc/cpuinfo | grep physical | uniq -c
查看當(dāng)前CPU運(yùn)行在32bit還是64bit模式下, 如果是運(yùn)行在32bit下也不代表CPU不支持64bit
getconf LONG_BIT
結(jié)果大于0, 說明支持64bit計(jì)算. lm指long mode, 支持lm則是64bit
cat /proc/cpuinfo | grep flags | grep ' lm ' | wc -l` </pre>
建立軟連接
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">ln -s /usr/local/jdk1.8/ jdk
</pre>
rpm相關(guān)
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"># 查看是否通過rpm安裝了該軟件 rpm -qa | grep 軟件名
</pre>
sshkey
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 創(chuàng)建sshkey
ssh-keygen -t rsa -C your_email@example.com
id_rsa.pub 的內(nèi)容拷貝到要控制的服務(wù)器的 home/username/.ssh/authorized_keys 中,如果沒有則新建(.ssh權(quán)限為700, authorized_keys權(quán)限為600)` </pre>
命令重命名
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"># 在各個(gè)用戶的.bash_profile中添加重命名配置 alias ll='ls -alF'
</pre>
同步服務(wù)器時(shí)間
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">sudo ntpdate -u ntp.api.bz
</pre>
后臺(tái)運(yùn)行命令
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 后臺(tái)運(yùn)行,并且有nohup.out輸出
nohup xxx &
后臺(tái)運(yùn)行, 不輸出任何日志
nohup xxx > /dev/null &
后臺(tái)運(yùn)行, 并將錯(cuò)誤信息做標(biāo)準(zhǔn)輸出到日志中
nohup xxx >out.log 2>&1 &` </pre>
強(qiáng)制活動(dòng)用戶退出
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"># 命令來完成強(qiáng)制活動(dòng)用戶退出.其中TTY表示終端名稱 pkill -kill -t [TTY]
</pre>
查看命令路徑
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">which <命令>
</pre>
查看進(jìn)程所有打開最大fd數(shù)
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">ulimit -n
</pre>
配置dns
vim /etc/resolv.conf
nslookup,查看域名路由表
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">nslookup google.com
</pre>
last, 最近登錄信息列表
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"># 最近登錄的5個(gè)賬號(hào) last -n 5
</pre>
設(shè)置固定ip
搜索公縱號(hào):MarkerHub喂窟,關(guān)注回復(fù)[ **vue **]獲取前后端入門教程司恳!
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">ifconfig em1 192.168.5.177 netmask 255.255.255.0
</pre>
查看進(jìn)程內(nèi)加載的環(huán)境變量
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"># 也可以去 cd /proc 目錄下, 查看進(jìn)程內(nèi)存中加載的東西 ps eww -p XXXXX(進(jìn)程號(hào))
</pre>
查看進(jìn)程樹找到服務(wù)器進(jìn)程
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">ps auwxf
</pre>
查看進(jìn)程啟動(dòng)路徑
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`cd /proc/xxx(進(jìn)程號(hào))
ls -all
cwd對(duì)應(yīng)的是啟動(dòng)路徑` </pre>
添加用戶, 配置sudo權(quán)限
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 新增用戶
useradd 用戶名
passwd 用戶名
增加sudo權(quán)限
vim /etc/sudoers
修改文件里面的
root ALL=(ALL) ALL
用戶名 ALL=(ALL) ALL` </pre>
強(qiáng)制關(guān)閉進(jìn)程名包含xxx的所有進(jìn)程
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">ps aux|grep xxx | grep -v grep | awk '{print $2}' | xargs kill -9
</pre>
磁盤,文件,目錄相關(guān)操作
vim操作
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`#normal模式下 g表示全局, x表示查找的內(nèi)容, y表示替換后的內(nèi)容
:%s/x/y/g
normal模式下
0 # 光標(biāo)移到行首(數(shù)字0)
$ # 光標(biāo)移至行尾
shift + g # 跳到文件最后
gg # 跳到文件頭
顯示行號(hào)
:set nu
去除行號(hào)
:set nonu
檢索
/xxx(檢索內(nèi)容) # 從頭檢索, 按n查找下一個(gè)
?xxx(檢索內(nèi)容) # 從尾部檢索` </pre>
打開只讀文件,修改后需要保存時(shí)(不用切換用戶即可保存的方式)
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"># 在normal模式下 :w !sudo tee %
</pre>
查看磁盤, 文件目錄基本信息
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 查看磁盤掛載情況
mount
查看磁盤分區(qū)信息
df
查看目錄及子目錄大小
du -H -h
查看當(dāng)前目錄下各個(gè)文件, 文件夾占了多少空間, 不會(huì)遞歸
du -sh *` </pre>
wc命令
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 查看文件里有多少行
wc -l filename
看文件里有多少個(gè)word
wc -w filename
文件里最長的那一行是多少個(gè)字
wc -L filename
統(tǒng)計(jì)字節(jié)數(shù)
wc -c` </pre>
常用壓縮, 解壓縮命令
壓縮命令
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`tar czvf xxx.tar 壓縮目錄
zip -r xxx.zip 壓縮目錄` </pre>
解壓縮命令
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`tar zxvf xxx.tar
解壓到指定文件夾
tar zxvf xxx.tar -C /xxx/yyy/
unzip xxx.zip` </pre>
變更文件所屬用戶, 用戶組
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">chown eagleye.eagleye xxx.log
</pre>
cp, scp, mkdir
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`#復(fù)制
cp xxx.log
復(fù)制并強(qiáng)制覆蓋同名文件
cp -f xxx.log
復(fù)制文件夾
cp -r xxx(源文件夾) yyy(目標(biāo)文件夾)
遠(yuǎn)程復(fù)制
scp -P ssh端口 username@10.10.10.101:/home/username/xxx /home/xxx
級(jí)聯(lián)創(chuàng)建目錄
mkdir -p /xxx/yyy/zzz
批量創(chuàng)建文件夾, 會(huì)在test,main下都創(chuàng)建java, resources文件夾
mkdir -p src/{test,main}/{java,resources}` </pre>
比較兩個(gè)文件
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">diff -u 1.txt 2.txt
</pre>
日志輸出的字節(jié)數(shù),可以用作性能測試
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"># 如果做性能測試, 可以每執(zhí)行一次, 往日志里面輸出 “.” , 這樣日志中的字節(jié)數(shù)就是實(shí)際的性能測試運(yùn)行的次數(shù), 還可以看見實(shí)時(shí)速率. tail -f xxx.log | pv -bt
</pre>
查看, 去除特殊字符
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 查看特殊字符
cat -v xxx.sh
去除特殊字符
sed -i 's/^M//g’ env.sh 去除文件的特殊字符, 比如^M: 需要這樣輸入: ctrl+v+enter` </pre>
處理因系統(tǒng)原因引起的文件中特殊字符的問題
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 可以轉(zhuǎn)換為該系統(tǒng)下的文件格式
cat file.sh > file.sh_bak
先將file.sh中文件內(nèi)容復(fù)制下來然后運(yùn)行, 然后粘貼內(nèi)容, 最后ctrl + d 保存退出
cat > file1.sh
在vim中通過如下設(shè)置文件編碼和文件格式
:set fileencodings=utf-8 入撒,然后 w (存盤)一下即可轉(zhuǎn)化為 utf8 格式,
:set fileformat=unix
在mac下使用dos2unix進(jìn)行文件格式化
find . -name "*.sh" | xargs dos2unix` </pre>
tee, 重定向的同時(shí)輸出到屏幕
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">awk ‘{print $0}’ xxx.log | tee test.log
</pre>
檢索相關(guān)
grep
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 反向匹配, 查找不包含xxx的內(nèi)容
grep -v xxx
排除所有空行
grep -v '^/pre>
返回結(jié)果 2,則說明第二行是空行
grep -n “^$” 111.txt
查詢以abc開頭的行
grep -n “^abc” 111.txt
同時(shí)列出該詞語出現(xiàn)在文章的第幾行
grep 'xxx' -n xxx.log
計(jì)算一下該字串出現(xiàn)的次數(shù)
grep 'xxx' -c xxx.log
比對(duì)的時(shí)候萝招,不計(jì)較大小寫的不同
grep 'xxx' -i xxx.log` </pre>
awk
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 以':' 為分隔符,如果第五域有user則輸出該行
awk -F ':' '{if (0}' /etc/passwd
統(tǒng)計(jì)單個(gè)文件中某個(gè)字符(串)(中文無效)出現(xiàn)的次數(shù)
awk -v RS='character' 'END {print --NR}' xxx.txt` </pre>
find檢索命令
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 在目錄下找后綴是.mysql的文件
find /home/eagleye -name '*.mysql' -print
會(huì)從 /usr 目錄開始往下找,找最近3天之內(nèi)存取過的文件。
find /usr -atime 3 –print
會(huì)從 /usr 目錄開始往下找柬赐,找最近5天之內(nèi)修改過的文件绰姻。
find /usr -ctime 5 –print
會(huì)從 /doc 目錄開始往下找枉侧,找jacky 的、文件名開頭是 j的文件狂芋。
find /doc -user jacky -name 'j*' –print
會(huì)從 /doc 目錄開始往下找榨馁,找尋文件名是 ja 開頭或者 ma開頭的文件。
find /doc ( -name 'ja' -o- -name 'ma' ) –print
會(huì)從 /doc 目錄開始往下找帜矾,找到凡是文件名結(jié)尾為 bak的文件翼虫,把它刪除掉。-exec 選項(xiàng)是執(zhí)行的意思屡萤,rm 是刪除命令珍剑,{ } 表示文件名,“;”是規(guī)定的命令結(jié)尾死陆。
find /doc -name '*bak' -exec rm {} ;` </pre>
網(wǎng)絡(luò)相關(guān)
查看什么進(jìn)程使用了該端口
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">lsof -i:port
</pre>
獲取本機(jī)ip地址
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"
</pre>
iptables
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 查看iptables狀態(tài)
service iptables status
要封停一個(gè)ip
iptables -I INPUT -s ... -j DROP
要解封一個(gè)IP招拙,使用下面這條命令:
iptables -D INPUT -s ... -j DROP
備注: 參數(shù)-I是表示Insert(添加),-D表示Delete(刪除)措译。后面跟的是規(guī)則别凤,INPUT表示入站,...表示要封停的IP领虹,DROP表示放棄連接规哪。
開啟9090端口的訪問
/sbin/iptables -I INPUT -p tcp --dport 9090 -j ACCEPT
防火墻開啟、關(guān)閉掠械、重啟
/etc/init.d/iptables status
/etc/init.d/iptables start
/etc/init.d/iptables stop
/etc/init.d/iptables restart` </pre>
nc命令, tcp調(diào)試?yán)?/h2>
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`#給某一個(gè)endpoint發(fā)送TCP請(qǐng)求,就將data的內(nèi)容發(fā)送到對(duì)端
nc 192.168.0.11 8000 < data.txt
nc可以當(dāng)做服務(wù)器由缆,監(jiān)聽某個(gè)端口號(hào),把某一次請(qǐng)求的內(nèi)容存儲(chǔ)到received_data里
nc -l 8000 > received_data
上邊只監(jiān)聽一次,如果多次可以加上-k參數(shù)
nc -lk 8000` </pre>
tcpdump
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"># dump出本機(jī)12301端口的tcp包 tcpdump -i em1 tcp port 12301 -s 1500 -w abc.pcap
</pre>
跟蹤網(wǎng)絡(luò)路由路徑
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# traceroute默認(rèn)使用udp方式, 如果是-I則改成icmp方式
traceroute -I www.163.com
從ttl第3跳跟蹤
traceroute -M 3 www.163.com
加上端口跟蹤
traceroute -p 8080 192.168.10.11` </pre>
ss
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 顯示本地打開的所有端口
ss -l
顯示每個(gè)進(jìn)程具體打開的socket
ss -pl
顯示所有tcp socket
ss -t -a
顯示所有的UDP Socekt
ss -u -a
顯示所有已建立的SMTP連接
ss -o state established '( dport = :smtp or sport = :smtp )'
顯示所有已建立的HTTP連接
ss -o state established '( dport = :http or sport = :http )'
找出所有連接X服務(wù)器的進(jìn)程
ss -x src /tmp/.X11-unix/*
列出當(dāng)前socket統(tǒng)計(jì)信息
ss -s
解釋:netstat是遍歷/proc下面每個(gè)PID目錄猾蒂,ss直接讀/proc/net下面的統(tǒng)計(jì)信息均唉。所以ss執(zhí)行的時(shí)候消耗資源以及消耗的時(shí)間都比netstat少很多` </pre>
netstat
# 輸出每個(gè)ip的連接數(shù),以及總的各個(gè)狀態(tài)的連接數(shù)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);}'# 統(tǒng)計(jì)所有連接狀態(tài), # CLOSED:無連接是活動(dòng)的或正在進(jìn)行# LISTEN:服務(wù)器在等待進(jìn)入呼叫# SYN_RECV:一個(gè)連接請(qǐng)求已經(jīng)到達(dá)肚菠,等待確認(rèn)# SYN_SENT:應(yīng)用已經(jīng)開始舔箭,打開一個(gè)連接# ESTABLISHED:正常數(shù)據(jù)傳輸狀態(tài)# FIN_WAIT1:應(yīng)用說它已經(jīng)完成# FIN_WAIT2:另一邊已同意釋放# ITMED_WAIT:等待所有分組死掉# CLOSING:兩邊同時(shí)嘗試關(guān)閉# TIME_WAIT:主動(dòng)關(guān)閉連接一端還沒有等到另一端反饋期間的狀態(tài)# LAST_ACK:等待所有分組死掉netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,"\t",state[key]}'# 查找較多time_wait連接netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
監(jiān)控linux性能命令
top
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">按大寫的 F 或 O 鍵,然后按 a-z 可以將進(jìn)程按照相應(yīng)的列進(jìn)行排序, 然后回車。而大寫的 R 鍵可以將當(dāng)前的排序倒轉(zhuǎn)
</pre>
列名 | 含義 |
---|---|
PID | 進(jìn)程id |
PPID | 父進(jìn)程id |
RUSER | Real user name |
UID | 進(jìn)程所有者的用戶id |
USER | 進(jìn)程所有者的用戶名 |
GROUP | 進(jìn)程所有者的組名 |
TTY | 啟動(dòng)進(jìn)程的終端名层扶。不是從終端啟動(dòng)的進(jìn)程則顯示為 ? |
PR | 優(yōu)先級(jí) |
NI | nice值箫章。負(fù)值表示高優(yōu)先級(jí),正值表示低優(yōu)先級(jí) |
P | 最后使用的CPU镜会,僅在多CPU環(huán)境下有意義 |
%CPU | 上次更新到現(xiàn)在的CPU時(shí)間占用百分比 |
TIME | 進(jìn)程使用的CPU時(shí)間總計(jì)檬寂,單位秒 |
TIME+ | 進(jìn)程使用的CPU時(shí)間總計(jì),單位1/100秒 |
%MEM | 進(jìn)程使用的物理內(nèi)存百分比 |
VIRT | 進(jìn)程使用的虛擬內(nèi)存總量戳表,單位kb桶至。VIRT=SWAP+RES |
SWAP | 進(jìn)程使用的虛擬內(nèi)存中,被換出的大小匾旭,單位kb镣屹。 |
RES | 進(jìn)程使用的、未被換出的物理內(nèi)存大小价涝,單位kb女蜈。RES=CODE+DATA |
CODE | 可執(zhí)行代碼占用的物理內(nèi)存大小,單位kb |
DATA | 可執(zhí)行代碼以外的部分(數(shù)據(jù)段+棧)占用的物理內(nèi)存大小色瘩,單位kb |
SHR | 共享內(nèi)存大小伪窖,單位kb |
nFLT | 頁面錯(cuò)誤次數(shù) |
nDRT | 最后一次寫入到現(xiàn)在,被修改過的頁面數(shù)泞遗。 |
S | 進(jìn)程狀態(tài)惰许。D=不可中斷的睡眠狀態(tài),R=運(yùn)行,S=睡眠,T=跟蹤/停止,Z=僵尸進(jìn)程 |
COMMAND | 命令名/命令行 |
WCHAN | 若該進(jìn)程在睡眠,則顯示睡眠中的系統(tǒng)函數(shù)名 |
Flags | 任務(wù)標(biāo)志史辙,參考 sched.h |
dmesg,查看系統(tǒng)日志
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">dmesg
</pre>
iostat,磁盤IO情況監(jiān)控
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`iostat -xz 1
r/s, w/s, rkB/s, wkB/s:分別表示每秒讀寫次數(shù)和每秒讀寫數(shù)據(jù)量(千字節(jié))汹买。讀寫量過大,可能會(huì)引起性能問題聊倔。
await:IO操作的平均等待時(shí)間晦毙,單位是毫秒。這是應(yīng)用程序在和磁盤交互時(shí)耙蔑,需要消耗的時(shí)間见妒,包括IO等待和實(shí)際操作的耗時(shí)。如果這個(gè)數(shù)值過大甸陌,可能是硬件設(shè)備遇到了瓶頸或者出現(xiàn)故障须揣。
avgqu-sz:向設(shè)備發(fā)出的請(qǐng)求平均數(shù)量。如果這個(gè)數(shù)值大于1钱豁,可能是硬件設(shè)備已經(jīng)飽和(部分前端硬件設(shè)備支持并行寫入)耻卡。
%util:設(shè)備利用率。這個(gè)數(shù)值表示設(shè)備的繁忙程度牲尺,經(jīng)驗(yàn)值是如果超過60卵酪,可能會(huì)影響IO性能(可以參照IO操作平均等待時(shí)間)幌蚊。如果到達(dá)100%,說明硬件設(shè)備已經(jīng)飽和溃卡。
如果顯示的是邏輯設(shè)備的數(shù)據(jù)溢豆,那么設(shè)備利用率不代表后端實(shí)際的硬件設(shè)備已經(jīng)飽和。值得注意的是瘸羡,即使IO性能不理想漩仙,也不一定意味這應(yīng)用程序性能會(huì)不好,可以利用諸如預(yù)讀取犹赖、寫緩存等策略提升應(yīng)用性能讯赏。` </pre>
free,內(nèi)存使用情況
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`free -m
eg:
total used free shared buffers cached
Mem: 1002 769 232 0 62 421
-/+ buffers/cache: 286 715
Swap: 1153 0 1153
第一部分Mem行:
total 內(nèi)存總數(shù): 1002M
used 已經(jīng)使用的內(nèi)存數(shù): 769M
free 空閑的內(nèi)存數(shù): 232M
shared 當(dāng)前已經(jīng)廢棄不用,總是0
buffers Buffer 緩存內(nèi)存數(shù): 62M
cached Page 緩存內(nèi)存數(shù):421M
關(guān)系:total(1002M) = used(769M) + free(232M)
第二部分(-/+ buffers/cache):
(-buffers/cache) used內(nèi)存數(shù):286M (指的第一部分Mem行中的used – buffers – cached)
(+buffers/cache) free內(nèi)存數(shù): 715M (指的第一部分Mem行中的free + buffers + cached)
可見-buffers/cache反映的是被程序?qū)崒?shí)在在吃掉的內(nèi)存,而+buffers/cache反映的是可以挪用的內(nèi)存總數(shù).
第三部分是指交換分區(qū)` </pre>
sar,查看網(wǎng)絡(luò)吞吐狀態(tài)
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# sar命令在這里可以查看網(wǎng)絡(luò)設(shè)備的吞吐率。在排查性能問題時(shí)冷尉,可以通過網(wǎng)絡(luò)設(shè)備的吞吐量,判斷網(wǎng)絡(luò)設(shè)備是否已經(jīng)飽和
sar -n DEV 1
sar命令在這里用于查看TCP連接狀態(tài)系枪,其中包括:
active/s:每秒本地發(fā)起的TCP連接數(shù)雀哨,既通過connect調(diào)用創(chuàng)建的TCP連接;
passive/s:每秒遠(yuǎn)程發(fā)起的TCP連接數(shù)私爷,即通過accept調(diào)用創(chuàng)建的TCP連接雾棺;
retrans/s:每秒TCP重傳數(shù)量;
TCP連接數(shù)可以用來判斷性能問題是否由于建立了過多的連接衬浑,進(jìn)一步可以判斷是主動(dòng)發(fā)起的連接捌浩,還是被動(dòng)接受的連接。TCP重傳可能是因?yàn)榫W(wǎng)絡(luò)環(huán)境惡劣工秩,或者服務(wù)器壓力過大導(dǎo)致丟包
sar -n TCP,ETCP 1` </pre>
vmstat, 給定時(shí)間監(jiān)控CPU使用率, 內(nèi)存使用, 虛擬內(nèi)存交互, IO讀寫
<pre data-tool="mdnice編輯器" style="margin: 10px 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: rgb(0, 0, 0); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`# 2表示每2秒采集一次狀態(tài)信息, 1表示只采集一次(忽略既是一直采集)
vmstat 2 1
eg:
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 0 3499840 315836 3819660 0 0 0 1 2 0 0 0 100 0
0 0 0 3499584 315836 3819660 0 0 0 0 88 158 0 0 100 0
0 0 0 3499708 315836 3819660 0 0 0 2 86 162 0 0 100 0
0 0 0 3499708 315836 3819660 0 0 0 10 81 151 0 0 100 0
1 0 0 3499732 315836 3819660 0 0 0 2 83 154 0 0 100 0` </pre>
- r 表示運(yùn)行隊(duì)列(就是說多少個(gè)進(jìn)程真的分配到CPU)尸饺,我測試的服務(wù)器目前CPU比較空閑,沒什么程序在跑助币,當(dāng)這個(gè)值超過了CPU數(shù)目浪听,就會(huì)出現(xiàn)CPU瓶頸了。這個(gè)也和top的負(fù)載有關(guān)系眉菱,一般負(fù)載超過了3就比較高迹栓,超過了5就高,超過了10就不正常了俭缓,服務(wù)器的狀態(tài)很危險(xiǎn)克伊。top的負(fù)載類似每秒的運(yùn)行隊(duì)列。如果運(yùn)行隊(duì)列過大华坦,表示你的CPU很繁忙愿吹,一般會(huì)造成CPU使用率很高。
- b 表示阻塞的進(jìn)程,這個(gè)不多說季春,進(jìn)程阻塞洗搂,大家懂的。
- swpd 虛擬內(nèi)存已使用的大小,如果大于0耘拇,表示你的機(jī)器物理內(nèi)存不足了撵颊,如果不是程序內(nèi)存泄露的原因,那么你該升級(jí)內(nèi)存了或者把耗內(nèi)存的任務(wù)遷移到其他機(jī)器惫叛。
- free 空閑的物理內(nèi)存的大小倡勇,我的機(jī)器內(nèi)存總共8G,剩余3415M嘉涌。
- buff Linux/Unix系統(tǒng)是用來存儲(chǔ)妻熊,目錄里面有什么內(nèi)容,權(quán)限等的緩存仑最,我本機(jī)大概占用300多M
- cache cache直接用來記憶我們打開的文件,給文件做緩沖扔役,我本機(jī)大概占用300多M(這里是Linux/Unix的聰明之處,把空閑的物理內(nèi)存的一部分拿來做文件和目錄的緩存警医,是為了提高 程序執(zhí)行的性能亿胸,當(dāng)程序使用內(nèi)存時(shí),buffer/cached會(huì)很快地被使用预皇。)
- si 每秒從磁盤讀入虛擬內(nèi)存的大小侈玄,如果這個(gè)值大于0,表示物理內(nèi)存不夠用或者內(nèi)存泄露了吟温,要查找耗內(nèi)存進(jìn)程解決掉序仙。我的機(jī)器內(nèi)存充裕,一切正常鲁豪。
- so 每秒虛擬內(nèi)存寫入磁盤的大小潘悼,如果這個(gè)值大于0,同上呈昔。
- bi 塊設(shè)備每秒接收的塊數(shù)量挥等,這里的塊設(shè)備是指系統(tǒng)上所有的磁盤和其他塊設(shè)備,默認(rèn)塊大小是1024byte堤尾,我本機(jī)上沒什么IO操作肝劲,所以一直是0,但是我曾在處理拷貝大量數(shù)據(jù)(2-3T)的機(jī)器上看過可以達(dá)到140000/s郭宝,磁盤寫入速度差不多140M每秒
- bo 塊設(shè)備每秒發(fā)送的塊數(shù)量辞槐,例如我們讀取文件,bo就要大于0粘室。bi和bo一般都要接近0榄檬,不然就是IO過于頻繁,需要調(diào)整衔统。
- in 每秒CPU的中斷次數(shù)鹿榜,包括時(shí)間中斷
- cs 每秒上下文切換次數(shù)海雪,例如我們調(diào)用系統(tǒng)函數(shù),就要進(jìn)行上下文切換舱殿,線程的切換奥裸,也要進(jìn)程上下文切換,這個(gè)值要越小越好沪袭,太大了湾宙,要考慮調(diào)低線程或者進(jìn)程的數(shù)目,例如在apache和nginx這種web服務(wù)器中,我們一般做性能測試時(shí)會(huì)進(jìn)行幾千并發(fā)甚至幾萬并發(fā)的測試冈绊,選擇web服務(wù)器的進(jìn)程可以由進(jìn)程或者線程的峰值一直下調(diào)侠鳄,壓測,直到cs到一個(gè)比較小的值死宣,這個(gè)進(jìn)程和線程數(shù)就是比較合適的值了伟恶。系統(tǒng)調(diào)用也是,每次調(diào)用系統(tǒng)函數(shù)毅该,我們的代碼就會(huì)進(jìn)入內(nèi)核空間知押,導(dǎo)致上下文切換,這個(gè)是很耗資源鹃骂,也要盡量避免頻繁調(diào)用系統(tǒng)函數(shù)。上下文切換次數(shù)過多表示你的CPU大部分浪費(fèi)在上下文切換罢绽,導(dǎo)致CPU干正經(jīng)事的時(shí)間少了畏线,CPU沒有充分利用,是不可取的良价。
- us 用戶CPU時(shí)間寝殴,我曾經(jīng)在一個(gè)做加密解密很頻繁的服務(wù)器上,可以看到us接近100,r運(yùn)行隊(duì)列達(dá)到80(機(jī)器在做壓力測試明垢,性能表現(xiàn)不佳)蚣常。
- sy 系統(tǒng)CPU時(shí)間,如果太高痊银,表示系統(tǒng)調(diào)用時(shí)間長抵蚊,例如是IO操作頻繁。
- id 空閑 CPU時(shí)間溯革,一般來說贞绳,id + us + sy = 100,一般我認(rèn)為id是空閑CPU使用率,us是用戶CPU使用率致稀,sy是系統(tǒng)CPU使用率冈闭。
- wt 等待IO CPU時(shí)間。