Linux運維基礎之mtr命令詳解

Linux運維基礎之mtr命令詳解

mtr 命令是 “My Traceroute” 的縮寫,是一種功能強大的網路診斷工具,結合了 tracerouteping 命令的功能。通過向指定目的地傳送資料包,它可以深入瞭解網路路徑,幫助識別潛在的瓶頸或故障。與 tracerouteping pathping 等工具類似,mtr 命令能提供更全面的網路效能檢視。

系統管理員和網路工程師發現 mtr 命令在排除網路故障時非常方便。無論是監控連線的穩定性還是診斷複雜的網路問題,mtr 命令都可以與 ifconfignetstat 等命令一起使用,提供詳細的分析。

如何安裝 mtr 命令

如果你的 Linux 發行版中尚未包含 mtr 軟體包,你可能需要安裝它。下面是一些常見發行版的安裝和解除安裝方法:

對於基於 Debian 的系統(如 Ubuntu):

使用以下命令安裝 mtr

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get update
sudo apt-get install mtr
sudo apt-get update sudo apt-get install mtr
sudo apt-get update
sudo apt-get install mtr

以後要解除安裝 mtr,可以使用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get remove --purge mtr
sudo apt-get remove --purge mtr
sudo apt-get remove --purge mtr

對於基於 Red Hat 的系統(如 Fedora):

要安裝 mtr,可以使用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo dnf install mtr
sudo dnf install mtr
sudo dnf install mtr

以後要解除安裝 mtr,可以使用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo dnf remove mtr
sudo dnf remove mtr
sudo dnf remove mtr

適用於基於 Arch 的系統:

要安裝 mtr,可以使用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo pacman -S mtr
sudo pacman -S mtr
sudo pacman -S mtr

以後要解除安裝 mtr,可以使用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo pacman -R mtr
sudo pacman -R mtr
sudo pacman -R mtr

如何使用 mtr

1. 基本用法

語法: mtr <hostname>

說明:跟蹤指向給定主機名或 IP 地址的路由。

示例:mtr example.com

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Host Loss% Snt Last Avg Best Wrst StDev
1. router.local 0.0% 10 1.2 1.3 1.1 1.5 0.1
2. isp-gateway 0.0% 10 2.3 2.4 2.2 2.6 0.1
3. example.com 0.0% 10 10.5 10.6 10.4 10.8 0.1
Host Loss% Snt Last Avg Best Wrst StDev 1. router.local 0.0% 10 1.2 1.3 1.1 1.5 0.1 2. isp-gateway 0.0% 10 2.3 2.4 2.2 2.6 0.1 3. example.com 0.0% 10 10.5 10.6 10.4 10.8 0.1
Host              Loss%   Snt   Last   Avg  Best  Wrst StDev
1. router.local    0.0%    10    1.2   1.3   1.1   1.5   0.1
2. isp-gateway     0.0%    10    2.3   2.4   2.2   2.6   0.1
3. example.com     0.0%    10   10.5  10.6  10.4  10.8   0.1

此輸出顯示了到 example.com 的路由,共有三個跳。它提供了每一跳的資料包丟失、已傳送資料包數量和延遲統計的詳細資訊。

2. 使用 ICMP 代替 UDP

語法:mtr --icmp <hostname>

說明:使用 ICMP ECHO 代替 UDP 資料包跟蹤路由。

示例:mtr --icmp example.com

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Host Loss% Snt Last Avg Best Wrst StDev
1. router.local 0.0% 10 1.1 1.2 1.0 1.3 0.1
2. isp-gateway 0.0% 10 2.2 2.3 2.1 2.5 0.1
3. example.com 0.0% 10 10.4 10.5 10.3 10.6 0.1
Host Loss% Snt Last Avg Best Wrst StDev 1. router.local 0.0% 10 1.1 1.2 1.0 1.3 0.1 2. isp-gateway 0.0% 10 2.2 2.3 2.1 2.5 0.1 3. example.com 0.0% 10 10.4 10.5 10.3 10.6 0.1
Host              Loss%   Snt   Last   Avg  Best  Wrst StDev
1. router.local    0.0%    10    1.1   1.2   1.0   1.3   0.1
2. isp-gateway     0.0%    10    2.2   2.3   2.1   2.5   0.1
3. example.com     0.0%    10   10.4  10.5  10.3  10.6   0.1

該命令使用 ICMP ECHO 進行跟蹤,其結果可能與預設的 UDP 不同,特別是在安裝了某些防火牆或過濾器的情況下。

3. 顯示 AS(自治系統)編號

語法:mtr --aslookup <hostname>

說明:跟蹤路由並顯示每跳的 ASN(AS 號)。

示例:mtr --aslookup example.com

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Host Loss% Snt Last Avg Best Wrst StDev ASN
1. router.local 0.0% 10 1.2 1.3 1.1 1.5 0.1 AS12345
2. isp-gateway 0.0% 10 2.3 2.4 2.2 2.6 0.1 AS12345
3. example.com 0.0% 10 10.5 10.6 10.4 10.8 0.1 AS67890
Host Loss% Snt Last Avg Best Wrst StDev ASN 1. router.local 0.0% 10 1.2 1.3 1.1 1.5 0.1 AS12345 2. isp-gateway 0.0% 10 2.3 2.4 2.2 2.6 0.1 AS12345 3. example.com 0.0% 10 10.5 10.6 10.4 10.8 0.1 AS67890
Host              Loss%   Snt   Last   Avg  Best  Wrst StDev ASN
1. router.local    0.0%    10    1.2   1.3   1.1   1.5   0.1 AS12345
2. isp-gateway     0.0%    10    2.3   2.4   2.2   2.6   0.1 AS12345
3. example.com     0.0%    10   10.5  10.6  10.4  10.8   0.1 AS67890

該輸出包括每個跳轉的自治系統(AS)編號,可幫助使用者深入瞭解通往目標的路徑上網路基礎設施的所有權和控制權。

4. 使用 TCP 代替 UDP

語法:mtr --tcp <hostname>

說明:使用 TCP SYN 資料包而不是 UDP 跟蹤路由。

示例:mtr --tcp example.com

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Host Loss% Snt Last Avg Best Wrst StDev
1. router.local 0.0% 10 1.4 1.5 1.3 1.6 0.1
2. isp-gateway 0.0% 10 2.5 2.6 2.4 2.7 0.1
3. example.com 0.0% 10 10.7 10.8 10.5 11.0 0.1
Host Loss% Snt Last Avg Best Wrst StDev 1. router.local 0.0% 10 1.4 1.5 1.3 1.6 0.1 2. isp-gateway 0.0% 10 2.5 2.6 2.4 2.7 0.1 3. example.com 0.0% 10 10.7 10.8 10.5 11.0 0.1
Host              Loss%   Snt   Last   Avg  Best  Wrst StDev
1. router.local    0.0%    10    1.4   1.5   1.3   1.6   0.1
2. isp-gateway     0.0%    10    2.5   2.6   2.4   2.7   0.1
3. example.com     0.0%    10   10.7  10.8  10.5  11.0   0.1

該命令使用 TCP SYN 資料包進行跟蹤,如果 UDP 在路由中被過濾或阻止,該命令會很有用。

5. 指定 Ping 次數

語法:mtr -c <count> <hostname>

說明:向每一跳傳送指定數量的 ping。

示例:mtr -c 5 example.com

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Host Loss% Snt Last Avg Best Wrst StDev
1. router.local 0.0% 5 1.2 1.3 1.1 1.5 0.1
2. isp-gateway 0.0% 5 2.3 2.4 2.2 2.6 0.1
3. example.com 0.0% 5 10.5 10.6 10.4 10.8 0.1
Host Loss% Snt Last Avg Best Wrst StDev 1. router.local 0.0% 5 1.2 1.3 1.1 1.5 0.1 2. isp-gateway 0.0% 5 2.3 2.4 2.2 2.6 0.1 3. example.com 0.0% 5 10.5 10.6 10.4 10.8 0.1
Host              Loss%   Snt   Last   Avg  Best  Wrst StDev
1. router.local    0.0%     5    1.2   1.3   1.1   1.5   0.1
2. isp-gateway     0.0%     5    2.3   2.4   2.2   2.6   0.1
3. example.com     0.0%     5   10.5  10.6  10.4  10.8   0.1

該命令只向每一跳傳送 5 個 ping,以便更快地分析路由。

6. 最後顯示報告

語法:mtr --report <hostname>

說明:在報告模式下執行 mtr,向每一跳傳送一系列 ping,然後在最後顯示報告。

示例:mtr --report example.com

輸出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
HOST: localhost Loss% Snt Last Avg Best Wrst StDev
1. router.local 0.0% 10 1.2 1.3 1.1 1.5 0.1
2. isp-gateway 0.0% 10 2.3 2.4 2.2 2.6 0.1
3. example.com 0.0% 10 10.5 10.6 10.4 10.8 0.1
HOST: localhost Loss% Snt Last Avg Best Wrst StDev 1. router.local 0.0% 10 1.2 1.3 1.1 1.5 0.1 2. isp-gateway 0.0% 10 2.3 2.4 2.2 2.6 0.1 3. example.com 0.0% 10 10.5 10.6 10.4 10.8 0.1
HOST: localhost              Loss%   Snt   Last   Avg  Best  Wrst StDev
1. router.local            0.0%    10    1.2   1.3   1.1   1.5   0.1
2. isp-gateway             0.0%    10    2.3   2.4   2.2   2.6   0.1
3. example.com             0.0%    10   10.5  10.6  10.4  10.8   0.1

該命令用於在測試結束時生成單一報告,而不是持續更新顯示。它通常用於記錄或編寫指令碼。

更多 Linux 命令

下面羅列了最常見的一些 Linux 命令,您可以根據自己的需要查閱對應命令的詳細解析:

目錄操作 rmdir · cd · pwd · exa · ls
檔案操作 cat · cp · dd · less · touch · ln · rename · more · head
檔案系統操作 chown · mkfs · locate
網路 ping · curl · wget · iptables · mtr
搜尋和文字處理 find · grep · sed · whatis · ripgrep · fd · tldr
系統資訊和管理 env · history · top · who · htop · glances · lsof
使用者和會話管理 screen · su · sudo · open

此外,我們還整理 Linux 命令列大全,以幫助大家全面深入地學習 Linux。

評論留言