MTR, or “My Traceroute” (originally named Matt’s Traceroute after its original creator Matt Kimball) is an amazing network diagnose tool that combines the functionality of both ‘ping’ and ‘traceroute’ into one single program. It determines the route and address of each hop between the source and destination, and sends a sequence of ICMP ECHO requests to each hop, in order to measure the quality of the connection at each point. Today we’ll show how to build mtr
from source on macOS.
Most of us on the Mac are already familiar with the Homebrew package manager, a free and open-source software manager that makes it easier to install and configure several applications from the Terminal on your Mac. But in order to reduce clutter and reduce the number of installed dependencies, I opted to build some apps I would usually install with brew
from the sources. One of these apps is precisely mtr
, a definite must have for network diagnosis.
Apple’s macOS has very restrict policies regarding where user software can or cannot be. To keep our system lean and well organised, we will be using the recommended path for local software installs, /usr/local
Preparing the system
Before building and compiling mtr, we will need to install a few missing components. These will be autoconf
, automake
, libtools
and m4
processor. If you haven’t yet, you will also need to install Xcode (available on the App Store) agree to the terms and let it install any missing components it requires.
To install mtr
, let’s start by opening the Terminal and navigate to the /usr/local
folder. We will then create a new temporary folder and set the appropriate ownership so we don’t get errors ahead. The path /usr/local
belongs to the root user, so sudo is required to create new folders or files inside of it, so I’ll be creating the necessary directories that don’t exist yet and set the appropriate ownership.
Don’t forget to replace the user ‘user1’ with your user where applicable.
You should also check if /usr/local/bin
is on your PATH, by issuing the command echo $PATH
. It usually is already set.
echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin
Code language: JavaScript (javascript)
If the path to /usr/local/sbin
is missing, I’ll show how to add it later in this article. For now, let’s start by creating the necessary folders.
cd /usr/local
sudo mkdir include sbin tmp && sudo chown user1:admin include sbin tmp
Password:
Code language: Shell Session (shell)
Now let’s navigate to the temporary directory and download the sources from ftp.gnu.org
cd tmp
curl -OL https://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
curl -OL https://ftp.gnu.org/gnu/automake/automake-1.16.tar.gz
curl -OL https://ftp.gnu.org/gnu/libtool/libtool-2.4.6.tar.gz
curl -OL https://ftp.gnu.org/gnu/m4/m4-1.4.18.tar.gz
Code language: Shell Session (shell)
Next lets uncompress the archives and start building and compiling in the following order.
Autoconf
tar xfz autoconf-2.69.tar.gz
cd autoconf-2.69
./configure -–prefix=/usr/local
make
make install
cd ..
Code language: Shell Session (shell)
Automake
tar xfz automake-1.16.tar.gz
cd automake-1.16
./configure -–prefix=/usr/local
make
make install
cd ..
Code language: Shell Session (shell)
Libtool
tar xfz libtool-2.4.6.tar.gz
cd libtool-2.4.6
./configure -–prefix=/usr/local
make
make install
cd ..
Code language: Shell Session (shell)
M4
tar xfz m4-1.4.18.tar.gz
cd m4-1.4.18
./configure -–prefix=/usr/local
make
make install
cd ..
Code language: Shell Session (shell)
Don’t use sudo. If you do, files will be owned by root, and will break as you won’t be able to build unless using sudo
. The only make install
with sudo
will be that of mtr
which gets a setuid
bit.
Build mtr
from source
So now we’re prepared to build mtr
. The options and process are practically the same and the process is very straightforward.
curl -OL https://www.bitwizard.nl/mtr/files/mtr-0.94.tar.gz
tar xfz mtr-0.94.tar.gz
cd mtr-0.94
./configure -–prefix=/usr/local
make
sudo make install
Code language: Shell Session (shell)
The latest version of mtr
compiled from source on macOS adds a setuid
bit to mtr-packet
which forces the mtr-packet
executable to be ran as the user who owns the file (root). This is needed as it will require the elevated privileges, so we must use sudo
with make install
. And voilá, mtr
is installed and working, you should go ahead and test it.
Adding /usr/local/sbin
to PATH
Finally, depending on your environment configuration, /usr/local/sbin
may or may not be set on your PATH
. To check if it is, you can issue the following command:
echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin
Code language: Shell Session (shell)
The PATH
configuration varies from user to user and the software each user has installed. If /usr/local/sbin
isn’t set, we must add this path to the environment variables, by editing .zshrc
or .bashrc
and adding the command export to add it.
cd
vim .zshrc
Code language: Shell Session (shell)
Content
# Env
export PATH=”$PATH:/usr/local/sbin”
Code language: Vim Script (vim)
And now we can run mtr
to diagnose network connections.
My traceroute [v0.94]
macOS.local (—-:—-:—-:—-:—-:—-:—-:—-) 2021-12-21T10:39:40+0000
Keys: Help Display mode Restart statistics Order of fields quit
– Packets Pings
Host Loss% Snt Last Avg Best Wrst StDev
1. fw.local 0.0% 20 1.2 1.2 1.1 1.5 0.1
2. tunnel.tserv1.lis1.ipv6.he.net 0.0% 20 56.9 57.6 56.4 59.7 0.9
3. 10ge11-20.core1.lis1.he.net 0.0% 20 60.1 56.8 56.0 60.1 0.9
4. google.as15169.ipv6.gigapix.pt 0.0% 20 73.8 58.2 56.3 73.8 3.9
5. 2001:4860:0:25::6 0.0% 20 87.5 68.2 57.4 92.2 10.1
6. 2001:4860::9:4001:2750 0.0% 20 65.0 68.4 64.6 94.3 7.9
7. 2001:4860::9:4001:2751 0.0% 20 65.7 66.7 65.7 68.1 0.7
8. 2001:4860:0:1::f15 36.8% 19 67.1 67.4 66.2 73.0 1.8
9. mad07s10-in-x0e.1e100.net 0.0% 19 66.9 65.9 65.1 68.2 0.9
Code language: Shell Session (shell)
Post last updated at the 21st of December of 2021, tested on macOS Mojave 12.1.