In this tutorial we will look at how you install and run the mosquitto MQTT broker on a Linux server running for example Raspberry Pi or Ubuntu.
Here are the steps I used on Ubuntu.
- sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
- sudo apt-get update
- sudo apt-get install mosquitto
- sudo apt-get install mosquitto-clients
- sudo apt clean
For Raspberry Pi the instructions are taken from here:
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key sudo apt-key add mosquitto-repo.gpg.key
Then make the repository available :
cd /etc/apt/sources.list.d/
Then , depending on which version of debian you are using:
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-buster.list
Then update apt information:
sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients
Upgrading on Pi
Running
sudo apt-get install mosquitto
should update the existing installation but it may not upgrade as it is dependent on the repository which is different for the version of PI you are Running.
Stopping and Starting
Mosquitto is installed as a service and should start automatically after install
To Stop and start the service I needed to use
- sudo service mosquitto stop
- sudo service mosquitto start #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:
mosquitto -v
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
Testing Using Mosquitto
If you are doing tests with mosquito then I recommend you stop the installed service and run mosquito manually from the command line.
If you do it this way you can create various configuration files for testing.
In addition, I would recommend you place the test configuration files and other files like password files in the local use folder as it avoids any permission errors, and they are easily edited without requiring root permissions.
Starting Mosquitto Using a Configuration file
The configuration file (mosquitto.conf) that comes with the install is mainly completely commented out as mosquitto doesn’t need a configuration file to start.
This file is loaded by the mosquitto when starting as a service.
Before you edit the default configuration file it is a good idea to create a copy of this file.
However I wouldn’t use this file as a basis for you own as if you do it is difficult to find any of you changes that a buried inside this file.
If you have done this then you might find my python configuration file tool useful.
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
Verbose Mode
This is useful for troubleshooting use
mosquitto -v
Mosquitto 2..0.2 and above
Mosquitto v2 introduced some important changes that affect first time users in particular.
By default it requires authentication and doesn’t listen on a network address.
The following simple configuration file will make mosquitto start like previous versions:
listener 1883 allow_anonymous true
Connection Limits
You can limit the number of client connections in the mosquitto.conf file but the default configuration doesn’t impose limits.
However the OS does and it is 1024 for each user. This limit is the max number of open files allowed.
This can be changed for each user by editing the limits.conf file (/etc/security/limits.conf).
Note: You will need to restart the system for the setting to take affect
You can check the limits by using the ulimit -Sn (soft) or ulimit -Hn (hard command as shown above.
However this only affects mosquitto started from the command line. If you start mosquitto as a service then you need to edit the /lib/systemd/system/mosquitto.service file.
and adding the line
LimitNOFILE=4000 #(example limit)
Note: You will need to restart the system for the setting to take affect
If you the go to the command line you can check the limits by getting the process id and using the command:
cat /proc/<pid>/limits
as shown below:
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.
Examples:
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
pgrep 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
mosquitto_sub
and a publisher client
mosquitto_pub
Use
mosquitto_sub –help
They are useful for some quick tests and troubleshooting.
See Using The Mosquitto_pub and Mosquitto_sub Client Tools- Examples
Other Tools
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
Hi Steves.
Now able to communicate between pi and ubuntu. pi a broker and ubuntu as client. SSL communication
pi (running broker)
1607426140: New connection from 10.42.0.1 on port 8883.
1607426140: Client disconnected due to protocol error.
ubuntu
abhishek@dell-Inspiron-3584:~$ mosquitto_pub –cafile /home/abhishek/ssl_certificates/certs/ca/ca.crt -h 10.42.0.118 -t “topic” -m “Abhishek” -p 8883
Error: A TLS error occurred.
Same message getting while subscribing mosquitto_pub.
Mosquitto Conf file
port 8883
require_certificate true
cafile /home/pi/ssl_certificates/certs/ca/ca.crt
certfile /home/pi/ssl_certificates/certs/broker/broker.crt
keyfile /home/pi/ssl_certificates/certs/broker/broker-nopass.key
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
Please help
Sorry but is it working ok now?
Rgds
Steve
Hi Steve,
I really enjoy reading your explanation of MQTT. Its crystal clear! But I on my netstat it didn’t appear like yours. I wish I can upload pictures here but it’s okay.
My result didn’t appear port 1883, it appears other numbers than 1883:
58972, 52620,57972, 57970 and many more
Did my mosquitto install well? Thank you in advance!
Not sure but if you start mosquitto from a command line using mosquitto -v you either get it running or it complains that the port is already used in which case it is already running
Hi Steve
i am getting a error
1601973829: mosquitto version 1.6.10 starting
1601973829: Using default config.
1601973829: Opening ipv4 listen socket on port 1883.
1601973829: Error: Address already in use
address already in use,please help me how to solve
HI
You already have mosquitto running. When you install mosquitto on windows or linux it installs as a service and is started automatically. On linux you can stop it using sudo service mosquitto stop on windows you need to open the services console.
I have installed mosquitto broker on ubuntu I used apt-get update,apt-get upgrade followed by installation of mosquitto and mosquitto-clients. I had installed it once and since some commands werent working i removed then using sudo apt-get purge –remove mosquitto* and sudo apt-add-repository –remove ppa:mosquitto-dev/mosquitto-ppa.I am able to publish subscribe to topics but some of the commands like systemctl,mosquitto_passwd and mosquitto -v commands dont seem to work.I get command not found.Have i done anything wrong during installation?Thankx in advance.
Hi
It looks like you have removed the broker and left the clients. I would install again.
to start and stop use
sudo service mosquitto stop
and not systemctl
rgds
steve
I have installed it again..both mosquitto and mosquitto-clients.I am able to publish and subscribe to topics.sudo service mosquitto stop command works fine sometimes it prompts me to enter the password and sometimes works fine.But sometimes throws an error.also i have observed that mosquitto_hub and mosquitto_sub being installed under /usr/bin folder but cannot find mosquitto_passwd.
Tried this command wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key and i get Cannot write to ‘mosquitto-repo.gpg.key’ (Permission denied).
did you use sudo?
Hi Steve
‘Thanks for your guide, when I enter mosquitto -v it returns ‘command not found’, however if I put sudo in front of it, it works ?
What have I done wrong, I was sure I followed your instructions to the letter ?
Hi
You shouldn’t need sudo. Try the install again. Also try whereis mosquitto it maybe that you don’t have permissions in the folder where it has been installed.
Rgds
Steve
Thanks for this it was very useful. I think it should be “sudo service mosquitto start” instead of “sudo service start mosquitto”, as service puts the object before the command.
Yes you are correct I will find it and correct it.Tks
Steve
Well done. Simply understanding from very basic level ans a beginner.
thanks, every explained.
Thanks so much for this article, every other install guide misses completely the step to import the repos, which is baffling because otherwise nothing works. Thanks for being thorough in your explanation.
Agreed. Thanks for this easy to follow information!