In this tutorial we will look at how you install and run the mosquitto MQTT broker on a Linux server running Ubuntu.
We will look at installing Mosquitto on a local server running Ubuntu LInux and also an AWS linux server instance (cloud server) also running Ubuntu Linux.
Install Preparations for a Cloud Install (AWS)
The install and testing procedure is the same regardless of weather it is a local install or a cloud install.
For a local install jump to the Install instructions.
For a cloud based install you need to do some pre-install preparations.
Note: I an using the AWS free tier in this tutorial
First you need to create a virtual server . Amazon have a very good and detailed tutorial here.
When you create the new instance it is automatically started.
Connecting to the Instance
Managing and installing software on the server instance is done using the command line.
To connect to a command line on your server instance you will need to use SSH.
The instructions for connecting to the instance using SSH are also part of the tutorial on creating the instance.
On Windows SSH is provided by PUTTY.
Here are the details for using PUTTY on Windows
You need to be aware of a few things.
When you get the command shell with the login as prompt then you need to login as the user ubuntu and don’t need a password.
Also even though you need to use the sudo command you don’t need a password.
The default security policy blocks all ports except the one used by SSH (port 22). It also restricts login to you current IP address and this can change in the future which will mean that you will need to edit the rule.
You will need to open the ports you will be using on your broker (normally port 1883 ) see later.
If you try to ping the instance to check it is running then it probably won’t work either as you need to allow the ICMP packets.
To edit the policy go to the far right and click on the policy under security groups
You need to edit the inbound rules.The screen shot below show my modified security policy.
When you stop and start the server instance the IP address changes and so does the domain name.
If you look at the instance you can see the domain name and IP address.
Note: Other virtual servers will also have IP/port restrictions that need changing.
Installing The Mosquitto Broker
The download links and install directions for Linux are here.
I followed the install instructions for Ubuntu but they seemed to miss a few important steps. Here are the steps I used.
- sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
- sudo apt-get update
- sudo apt-get install mosquitto
- sudo apt-get install mosquitto-clients
It should automatically start mosquitto.
To Stop and start the service I needed to use
- sudo service stop mosquitto
- sudo service start mosquitto #see note later
Most sites I discovered where using the format.
- sudo /etc/init.d/mosquitto stop
However it seems newer installations use upstart initialization system instead of the init.d initialization system.
The stop/start scripts start the mosquitto broker in the background and also use the default mosquitto.conf file in the /etc/mosquitto/ folder.
The mosquitto binary is located in the /usr/sbin folder
If you want to see the control messages on the console then you need to start the mosquitto broker manually from a command line.
You first need to to stop the broker from running, and then type:
Testing The Install
To test it is running use command:
- netstat –at
You should see the Mosquitto broker running on port 1883 as shown in the screen shot above.
Below is a screen shot showing testing using the mqtt-ping command
Starting Mosquitto Using a Configuration file
The configuration file (mosquitto.conf) that comes with the install only has a few lines for logging.
To start mosquitto using a configuration file use the following command:
- mosquitto -c filename
You can find the mosquitto.conf template file in the /etc/mosquitto/ folder.
It is a good idea to create a copy of this file before editing it.
Note: For testing it is easier to use a configuration file in your home directory rather than the /etc/mosquitto folder as you need root permissions to edit files in this folder.
I use: /home/steve/mos/mosquitto.conf
This is useful for troubleshooting. Logging is already enabled in the default config file so all you need do is start mosquitto with this config file.
Important Note: on Linux although you stop mosquito from running using the command
- sudo stop mosquitto
to start it use:
- mosquitto -c /etc/mosquitto/mosquitto.conf
You can also use a command line switch -v to enable logging:
Running Multiple Mosquitto Brokers
You can configure a broker to listen on several ports, but to create multiple brokers with their own configurations then you will need to start multiple instances of mosquitto. See Quick Guide to the Mosquitto.conf file and this video on running multiple brokers.
Start mosquitto and listen on port 1883
mosquitto -p 1883
Start mosquitto as a daemon and listen on port 1884
mosquitto -p 1884 -d
Start mosquitto as a daemon and use the mosquitti-2.conf file.
mosquitto -c /etc/mosquitto/mosquitto-2.conf -d
Useful Linux Commands
To stop Mosquitto when running as a daemon:
ps -aux | grep mosquitto
kill -9 PID (that you get from above command)
Mosquitto Client Scripts
On Linux they need to be installed using
- sudo apt-get install mosquitto-clients
There is a simple subscriber client
and a publisher client
They are useful for some quick tests. See Using The Mosquitto_pub and Mosquitto_sub Client Tools- Examples
MQTTlens is also very useful for troubleshooting and quick testing. It is an add-on for the chrome browser.
It lets you publish and subscribe to topics using a web interface, and is much easier to use than the command line clients.
MQTT-Ping -Python Tool written by me that works like the traditional IP ping command tool.
Note: Starting will mosquitto version 1.6 mosquitto now supports MQTT v5.
Mosquitto Configuration Tutorials
- Configuring and Testing MQTT Topic Restrictions
- Quick Guide to The Mosquitto.conf File With Examples
- Configuring Username and Password Authentication
- MQTT TLS Security – Mosquitto Broker Configuration
- Understanding and Configuring Logging on Mosquitto
- Understanding and Configuring Bridging on Mosquitto
- Configure Mosquitto Bridge With SSL Encryption- Examples
Other Related Articles and Resources:
- How to Install the Mosquitto MQTT broker on Windows.
- MQTT for Beginners
- MQTT and Mosquitto WebSockets Working Notes
- MQTT Hosting,Brokers and Server