How to setup a Mosquitto MQTT Server and receive data from OwnTracks

Mosquitto is an open-source message broker service that uses the MQTT protocol to send and receive messages, typically with IOT (Internet of Things) devices.

OwnTracks is an open-source GPS location history logging service (the main components are apps for iPhone and Android.) OwnTracks takes care of sending the data, and recommends using Mosquitto as the framework on the receiving or “broker” side.

This is the process I went through to get a Mosquitto server up and receiving data on a DigitalOcean droplet, I customized the server for OwnTracks but most steps except config should apply for any use of Mosquitto server.

REQUIREMENTS: It runs on Raspberry Pi, so I figured a 512MB Ubuntu 14.04 x64 Droplet would be sufficient.

1. Create user “mosquitto”

Mosquitto wants to run as user mosquitto, adduser mosquitto

adduser mosquitto

2. Install Mosquitto

SSH into the droplet, do an update, and then install mosquitto dependencies

apt-get update
apt-get install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc

You could try installing via apt-get, it didn’t work for me so I downloaded the latest release of mosquitto listed here:

cd /home/mosquitto
tar xvzf mosquitto-1.4.8.tar.gz
cd mosquitto-1.4.8

Run make to compile and make install to install

make install

3. Setup Mosquitto

Create a mosquitto user/password: the command below will create a user owntracks, you can change

mosquitto_passwd -c /etc/mosquitto/pwfile owntracks

you will be prompted to enter a password.

Create the directory where persistence db files will be stored, change owner to mosquitto:

mkdir /var/lib/mosquitto/
chown mosquitto:mosquitto /var/lib/mosquitto/ -R

Create a config file by copying the example file:

cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
editor /etc/mosquitto/mosquitto.conf

At the end of the config file, add a block of all suggested config changes specific to OwnTracks (replace <yourIP> with the IP address of the droplet)

listener 8883 <yourIP>
persistence true
persistence_location /var/lib/mosquitto/
persistence_file mosquitto.db
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous false
password_file /etc/mosquitto/pwfile

Finally be sure to run:


4. Run/Test Mosquitto

Run the mosquitto server with this command:

mosquitto -c /etc/mosquitto/mosquitto.conf

It should start running without error, then in another window: Replace <YourIP> and <YourPassword> with your own stuff

mosquitto_sub -h <YourIP> -p 8883 -v -t 'owntracks/#' -u owntracks -P <YourPassword>

If everything went correctly you should see no errors executing this command, and in the window where mosquitto is running should acknowledge the connection. if so create an upstart file to autorun mosquitto:

vim /etc/init/mosquitto.conf


description "Mosquitto MQTT broker"
start on net-device-up
exec /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

NEXT STEP: Now that you have Mosquitto broker working, your next step is to setup OwnTracks on your phone to speak to your broker - The TL;DR is you need to install the App, go to preferences, select PRIVATE mode, and set the connection details to match the IP, user, and password specs you just setup.

UPGRADES: I’m working on getting Let’s Encrypt TLS certificates to work with this, I will post when I have that working.


Thanks for the post. Just curious to know the progress of configuring TLS certificates.

I followed these step on my Kali Linux… everything seems smoothly until last step as “make install”, I got below errors. Please check and help.

oot@kalievo:/home/mosquitto/mosquitto-1.4.8# make install set -e; for d in lib client src; do make -C ${d}; done make[1]: Entering directory ‘/home/mosquitto/mosquitto-1.4.8/lib’ make -C cpp make[2]: Entering directory ‘/home/mosquitto/mosquitto-1.4.8/lib/cpp’ make[2]: Nothing to be done for ‘all’. make[2]: Leaving directory ‘/home/mosquitto/mosquitto-1.4.8/lib/cpp’ make[1]: Leaving directory ‘/home/mosquitto/mosquitto-1.4.8/lib’ make[1]: Entering directory ‘/home/mosquitto/mosquitto-1.4.8/client’ make[1]: Nothing to be done for ‘all’. make[1]: Leaving directory ‘/home/mosquitto/mosquitto-1.4.8/client’ make[1]: Entering directory ‘/home/mosquitto/mosquitto-1.4.8/src’ cc -Wall -ggdb -O2 -I. -I… -I…/lib -DVERSION=““1.4.8"” -DTIMESTAMP=”“2017-03-28 23:28:13-0700"” -DWITH_BROKER -DWITH_TLS -DWITH_TLS_PSK -DWITH_UUID -DWITH_BRIDGE -DWITH_PERSISTENCE -DWITH_MEMORY_TRACKING -DWITH_SYS_TREE -DWITH_EC -c read_handle_server.c -o read_handle_server.o read_handle_server.c: In function ‘mqtt3_handle_connect’: read_handle_server.c:367:4: warning: ‘ASN1_STRING_data’ is deprecated [-Wdeprecated-declarations] context->username = _mosquitto_strdup((char *)ASN1_STRING_data(name_entry->value)); ^~~~~~~ In file included from /usr/include/openssl/bn.h:31:0, from /usr/include/openssl/asn1.h:24, from /usr/include/openssl/objects.h:916, from /usr/include/openssl/evp.h:27, from /usr/include/openssl/x509.h:23, from /usr/include/openssl/ssl.h:50, from …/lib/mosquitto_internal.h:27, from ./mosquitto_broker.h:42, from read_handle_server.c:22: /usr/include/openssl/asn1.h:553:1: note: declared here DEPRECATEDIN_1_1_0(unsigned char *ASN1_STRING_data(ASN1_STRING *x)) ^ read_handle_server.c:367:77: error: dereferencing pointer to incomplete type ‘X509_NAME_ENTRY {aka struct X509_name_entry_st}’ context->username = _mosquitto_strdup((char *)ASN1_STRING_data(name_entry->value)); ^~ Makefile:54: recipe for target ‘read_handle_server.o’ failed make[1]: *** [read_handle_server.o] Error 1 make[1]: Leaving directory ‘/home/mosquitto/mosquitto-1.4.8/src’ Makefile:21: recipe for target ‘mosquitto’ failed make: *** [mosquitto] Error 2 root@kalievo:/home/mosquitto/mosquitto-1.4.8#

Wonderful information Which Android app is used owntracks subscribe the data published by mqtt Please give a full details about user side settings and app

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Awesome, thanks. The only addition I can make is the mosquitto binary is dropped in /usr/local/sbin/mosquitto and not /usr/sbin/mosquitto. I changed my upstart script to

description "Mosquitto MQTT broker"
start on net-device-up
exec /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Testing email notificaitons

Ok, Only been doing Pi stuff for a few weeks now. Got MQTT up and running however it’s not autostarting.

Did the vim /etc/init/mosquitto.conf. I see the content as described in the file. What am I missing here? Is there something that should be done after you create this file?

thanks for sharing , i’m beginner on this ,when im following above steps ended with below error, when i run mosquitto following the step 4 …its says cannot assign requested addresss

root@mosquitto-mqtt-server:/home/mosquitto/mosquitto-1.4.8# mosquitto -c /etc/mosquitto/mosquitto.conf 1534919266: mosquitto version 1.4.8 (build date 2018-08-22 06:23:58+0000) starting 1534919266: Config loaded from /etc/mosquitto/mosquitto.conf. 1534919266: Opening ipv4 listen socket on port 8883. 1534919266: Error: Cannot assign requested address

Awesome post - Extremely helpful. I had COMPLETELY forgot I had a spare Droplet running. Saved me a lot of head-ache. My only ‘tiny’ bit of info would be to explain what the parameters mean.

mosquitto_passwd -c /etc/mosquitto/pwfile owntracks

It’s 3AM and I’m quite tired but it took me a few minutes to find out why it would only ever authenticate one member - Then I noticed that -c and assumed 'Probably stands for create. Which usually means ‘over-write’. Sure enough!

For real though, greatly appreciate it.

Thanks for this. Worked a charm (although it took me a while to find owntracks app). I have my droplet in Singapore and i connect from China - local connection is 75M download and ~8 up. Response using iOS app called MQTTool is almost instant. Once again thank you. Saved me a load of time.

This comment has been deleted

Awesome! Very clear and easy tutorial. Continue like that. Thank you very much for sharing!

Thanks for sharing your process to set up a Mosquitto MQTT Server