Networking → How to Install and Configure MRTG on Ubuntu Server
This article will describe how to install and configure Tobi Oetiker’s MRTG (Multi Router Traffic Grapher) on your Ubuntu home server. Once configured, you’ll be able to use it to monitor the traffic in and out of your home network using the SNMP capability in your network’s gateway/router. MRTG generates static HTML pages containing PNG images which provide a visual representation of this traffic. MRTG typically produces daily, weekly, monthly, and yearly graphs. MRTG is written in perl and works on Unix/Linux as well as Windows. MRTG is free software licensed under the GNU GPL.
So, let’s get started.
Download and Install
First, download and install MRTG using apt-get:
sudo apt-get install mrtg
If this is the first time installing MRTG on your server you’ll likely be presented with the following message. Answering “Yes” means that the default configuration file will be installed with permissions set at 640. Answering “No” means that the permissions are set at 644. In this example we’re going to accept the default Yes. No worries though, if you select No the steps in this tutorial will still work.

MRTG installs a sample configuration file, mrtg.cfg, in /etc. This is the file that will eventually hold the information obtained from your gateway\router. MRTG also creates a directory for the html pages it will serve up at /var/www/mrtg. For a full listing of what MRTG installs and where, run the locate command:
sudo updatedb && locate mrtg
Technically speaking, mrtg.cfg could remain in /etc, but just to keep things tidy, and to help facilitate the startup script that will be created later, let’s create a directory for it and move it into that directory:
sudo mkdir /etc/mrtg && sudo mv /etc/mrtg.cfg /etc/mrtg
That’s it for installing MRTG. Now let’s move on and configure it.
Configure
MRTG includes a script called cfgmaker that will help us populate mrtg.cfg with the information obtained from your gateway/router. But before you run cfgmaker, you should set up the snmp community. This usually involves logging into your gateway/router and enabling SNMP. The default community will typically be “public.” If you change the default community to another name though, make note of it. Now, run the following command, substituting the SNMP community name, if you’ve changed it, and adding the IP address of your gateway/router:
sudo cfgmaker --output /etc/mrtg/mrtg.cfg public@your-router's-IP-address
Next, open mrtg.cfg in a text editor and make sure under Global Configiration Options that “WorkDir: /var/www/mrtg” (under Debian) is uncommented, and under Global Defaults, make sure that “Options[_]: growright, bits” is uncommented. Finally, add the following lines under the Global Defaults section:
RunAsDaemon: Yes Interval: 5 Logdir: /var/log/ EnableIPv6: no
What does all this do? RunAsDaemon will enable MRTG to…um… run as a daemon. This is beneficial because MRTG is launched only once, thus the parsing of mrtg.cfg file is done only once, not repeatedly as would be the case if one were to run MRTG as a cron task – another acceptable method for running MRTG. Also, when running as a daemon, MRTG itself is responsible for timing the measurement intervals; therefore, we need to add the Interval option and assign it a value – in this case 5 (minutes). This means that every five minutes MRTG will update its graphs.
Speaking of graphs, by default MRTG graphs grow to the left, so by adding the option “growright” the direction of the traffic visible in MRTG’s graphs flips causing the current time to be at the right edge of the graph and the history values to the left of it. We’ve also chosen the “bits” option, which means that the monitored traffic values obtained from your gateway/router are multiplied by 8 and displayed bits per second instead of bytes per second. Finally, we’ve given MRTG a directory to put its log file, and because many routers do not currently support SNMP over IPv6, we’ve shut that off.
Okay, now it’s time to create the web pages which display the MRTG graphs. Run the following command:
sudo indexmaker --output=/var/www/mrtg/index.html /etc/mrtg/mrtg.cfg
MRTG has been configured. Let’s start it up and see what it displays
Start
There’s something important to keep in mind when starting MRTG, and that is that MRTG requires the environmental variable “LANG” to be C in order to run properly. Since most Linux systems these days, including Ubuntu server, use UTF-8 (run echo $LANG to see what your system uses), let’s change LANG to C and start MRTG using the following command:
sudo env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg
When you run MRTG for the first time you may see a lot of complaints about missing log files. No worries, this is normal for the first 2-3 times you start MRTG this way. If, however, it continues to complain you may want to look into the source of the problem.
Well, that’s it. Now point your browser to http://your-server-address/mrtg and you should see a page that resembles the following. You may have more or less graphs depending on the number interfaces reported by your gateway/router.
Because of the aforementioned options added to mrtg.cfg, you’ll see the graph starting “grow” to the right as the traffic is monitored over time, and the Y axis displayed as bits per second. If you click on any one of these graphs you’ll be taken to another page showing individual graphs for daily, weekly, monthly, and yearly traffic averages, along with the maximum, average, and current bit rate in and out of that particular interface. Only interested in displaying one particular interface? Don’t like the look of the page? No worries, just edit mrtg.cfg and/or /var/www/mrtg until you get it looking the way you want.
Okay, so now that we have MRTG installed, configured and running let’s move on and discuss how to keep it running.
Operate
Starting MRTG by hand is not ideal in the long run. So perhaps after you’ve done some tweaking on MRTG and are satisfied with your results, you can automate the process of running MRTG by creating a startup script in your system startup sequence. Here’s the script that I use:
#!/bin/bash
### START OF SCRIPT
MRTG="/usr/bin/mrtg"
CONFIG="/etc/mrtg/mrtg.cfg"
PIDFILE="/var/run/mrtg.pid"
LOCKFILE="/etc/mrtg/mrtg"
OPTIONS="--daemon"
RETVAL=0
start() {
echo -n $"Enabling MRTG: "
rm -f ${LOCKFILE} 2> /dev/null
env LANG=C ${MRTG} --pid-file=${PIDFILE} --lock-file=${LOCKFILE} ${OPTIONS} ${CONFIG}
RETVAL=$?
echo
}
stop() {
echo -n $"Disabling MRTG: "
kill `cat ${PIDFILE}` && rm -f ${LOCKFILE}
RETVAL=$?
echo
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|force-reload)
restart
;;
status)
if [ -f $LOCKFILE ]; then
echo $"MRTG is enabled."
RETVAL=0
else
echo $"MRTG is disabled."
RETVAL=3
fi
;;
*)
echo $"Usage: $0 {start|stop|status|restart|force-reload}"
exit 1
esac
exit $RETVAL
### END OF SCRIPT
To use this script simply save it as “mrtg” to your home directory. Now make the file executable and move it to /etc/init.d by running the following command:
chmod +x mrtg && sudo mv mrtg /etc/init.d/
Finally, link the script to the appropriate system run-level. For Debian or Ubuntu this should be 2. In most other Linux distributions, it will typically be 4 or 5.
sudo ln -s /etc/init.d/mrtg /etc/rc2.d/S99mrtg sudo ln -s /etc/init.d/mrtg /etc/rc2.d/K99mrtg
Now, let’s start MRTG using our script. If it’s currently running then substitute restart for start in the following command:
sudo /etc/init.d/mrtg start
That’s it. Now if for some reason your server is rebooted, MRTG should fire up automatically.
Conclusion
This concludes the article on how to install and configure MRTG on Ubuntu server. As you can see, MRTG isn’t terribly complicated and proves to be a really nice open source package for monitoring and displaying traffic in and out your home network from virtually anywhere you have a web browser. For a full list of all the configuration options and other information I encourage you to visit the MRTG web site.
References
http://oss.oetiker.ch/mrtg/doc/index.en.html
Tags: Networking, server, ubuntu







September 21st, 2009 at 8:21 pm
Thanks for this great write-up! I am using your startup script, and I think it’s great. I did have some trouble finding the RDDs.pm tool, but a quick “apt-get install librrds-perl” fixed everything!
September 22nd, 2009 at 7:16 am
Thanks for your kind words Tim, glad you enjoyed the post.
September 23rd, 2009 at 11:21 pm
Fantastic. THanks very much :)
combing Google for hours looking for a good tutorial. As a new linux user I’ve found this was the only one that could lead me to get it working :)
And that script is fantastic, before that I wasn’t sure how to stop and start it!!!
Cheers.
September 24th, 2009 at 3:13 pm
Hamish, you’re very welcome.
September 28th, 2009 at 8:13 am
HI, thats great , but I’ve problem with this startup script, after Ubuntu bootup it’s telling me: /etc/rc2.d/S99mrtg: 49: –daemon: not found
And it’s not starup with Ununtu, only manual starup
September 28th, 2009 at 10:09 am
ask123, there are a couple of things you might check:
Make sure that the mrtg.cfg file contains the “RunAsDaemon: yes” line under Global Defaults section;
make sure the script reflects accurately the locations of the directories and files where you’ve set up MRTG;
make sure the symbolic links to the script from /etc/rc2.d are correct (you might also make sure the server is starting up in run level 2);
make sure the mrtg script under /etc/init.d is set is set to be executable by owner, group and other; and finally,
make sure that mrtg.pid file is being written to /var/run.
If for some reason these steps don’t get you running, then you might trying running MRTG under cron.
September 30th, 2009 at 11:05 am
Big props on the great how-to! Very nice, worked like a charm.
September 30th, 2009 at 6:41 pm
pym93, thanks! Glad you enjoyed it.
October 14th, 2009 at 8:08 am
thank you iceflatline for sharing this guide..very useful very convenient very useful
October 26th, 2009 at 11:38 pm
Hi, i’m Indonesian. Many thanks for your great tutorial Bro!
October 27th, 2009 at 11:05 am
You’re very welcome! Thanks for the comment.
March 1st, 2010 at 12:13 am
Thank you for sharing this guide.
#For openSUSE 11.2, SLES
$ ln -s /etc/init.d/mrtg /etc/rc2.d/S99mrtg
$ ln -s /etc/init.d/mrtg /etc/rc2.d/K99mrtg
Change to:
$ ln -s /etc/init.d/mrtg /etc/init.d/rc2.d/S99mrtg
$ ln -s /etc/init.d/mrtg /etc/init.d/rc2.d/K99mrtg
Regards,
http://www.susethailand.com/suseforum/
March 1st, 2010 at 9:00 am
Thanks Sontaya!
March 14th, 2010 at 6:09 am
Hi,
Thanks for the great post?
Any plans for adding password protection to this :>
March 14th, 2010 at 12:24 pm
noobie, thanks for your comment. I assume you mean password protection for MRTG? I have no plans but perhaps the developer does. You may want to post your question to them directly. However, if all you’re interested in is protecting access to the MRTG page(s) themselves you might consider using a simple approach like an .htaccess file in the MRTG folder root.
March 26th, 2010 at 11:52 am
@noobie
You could use an .htaccess in your /var/www/mrtg and setup user/password with the Apache’s “htpasswd” utility, that’s how i password protected mrtg stats on my dedicated server.
April 18th, 2010 at 1:12 pm
Great tutorial!
Many thanks from Romania :)!
April 18th, 2010 at 2:08 pm
sil nic, you’re welcome. How’s things in Romania?
June 29th, 2010 at 8:15 pm
Just wanted to let you know, your guide is great. Wish some of the others put this very simple procedure so simply. Short, sweet and to the point.
Thanks from Albany, NY
June 29th, 2010 at 10:51 pm
Roy, thanks for your kind comments!
July 1st, 2010 at 3:14 am
Many thanks iceflatline. It’s very helpful for me. I am from IRAQ BAGHDAD. I think nobody like me. But still I like my country, any way I need to ask you, how can I add more than 2 routers. I have 2 dedicated comtech VSAT modems and I need to check there traffic from my end.
Regards
Ammar
July 1st, 2010 at 2:02 pm
Ammar, no worries my friend, not all Americans are the same. You are certainly welcome here. Thanks for taking the time to post, and for your question.
It’s quite easy to add other SNMP devices. Simply append the additional URL to the cfgmaker command described above, like this:
See http://oss.oetiker.ch/mrtg/doc/cfgmaker.en.html
July 12th, 2010 at 10:27 am
IceFaltline ,
Really I am very grateful to you for your cooperation with me.
Do you have any idea about how to configure the mrtg on windows?
Regards
Ammar
July 14th, 2010 at 10:08 am
Ammar, no problem.
Purportedly Windows is supported, although I have not tried to install it myself. See this article at the MRTG web site.
July 17th, 2010 at 3:51 am
iceflatline,
I installed it on windows but the problem when I try to access it http://ipaddress/mrtg .. nothing appears to me.
Regards
July 17th, 2010 at 9:26 am
Ammar, it works a little differently under Windows. First make sure you have followed all the steps called for in the MRTG documentation. Then, run perl mrtg .cfg from your ~:\mrtg\bin directory. Let it run for 5-10 minutes to collect data, then run the command again. Navigate to the WorkDir you created when using cfgmaker and you should find a html file there with the results.
This other guide may be helpful for you too.
Good luck!