• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Friday, May 9, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

How to Install Mosquitto MQTT Broker/Server on Ubuntu 16.04

How VPS by How VPS
January 1, 2020
in Linux
0
0
SHARES
108
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Prerequisites
  2. Step One: Install Mosquitto Broker
  3. Step Two: Install the Clients and Test
    1. Install MQTT clients
    2. Subscribe to topic "test"
    3. Publish a message to topic "test"
  4. Step Three: Secure with a Password
  5. Conclusion
  6. Want to contribute?


MQTT is a publish/subscribe model based, “lightweight” messaging protocol over TCP/IP for communication between “Internet of Things” devices such as ESP8266, Raspberry Pi, etc. It is very popular with low resources and battery powered applications such as home automation, security alarm systems and battery-powered sensor networks.

Mosquitto is an open source message broker (or server) that implements MQTT protocols. With its good community support, documentation, and ease of installation it has become one of the most popular MQTT brokers.

Prerequisites

  • An Ubuntu 16.04 server with root access
  • Open port TCP:1883 on firewall

Step One: Install Mosquitto Broker

Update Ubuntu’s package list and install the latest Mosquitto Broker available from it

sudo apt-get update
sudo apt-get install mosquitto

The Mosquitto service will start after installation.

Step Two: Install the Clients and Test

Install MQTT clients

sudo apt-get install mosquitto-clients

Mosquitto clients help us easily test MQTT through a command line utility. We will use two command windows, one to subscribe to a topic named "test" and one to publish a message to it.

Topics are labels used by the broker to filter messages for each connected client. A client program subscribed to a topic "Home1/BedroomTemp" will only listen to messages published to the same topic by other clients.

Subscribe to topic "test"

mosquitto_sub -t "test"

Mosquito_sub is a subscribe client we installed in the previous command. Here we are specifying “-t” followed by a topic name.

Publish a message to topic "test"

Login to the terminal as a second instance and publish a message to the "test" topic.

mosquitto_pub -m "message from mosquitto_pub client" -t "test"

Here the additional parameter “–m” is followed by the message we want to publish. Hit “Enter” and you should see a message from mosquitto_pub client displayed in other terminal where mosquito_sub client is running.

Step Three: Secure with a Password

Mosquitto comes with a password file generating utility called mosquitto_passwd.

sudo mosquitto_passwd -c /etc/mosquitto/passwd dave
Password: password

Create a configuration file for Mosquitto pointing to the password file we have just created.

sudo nano /etc/mosquitto/conf.d/default.conf

This will open an empty file. Paste the following into it.

allow_anonymous false
password_file /etc/mosquitto/passwd

Save and exit the text editor with “Ctrl+O“, “Enter” and “Ctrl+X“.

Now restart Mosquitto server and test our changes.

sudo systemctl restart mosquitto

In the subscribe client window, press “Ctrl+C” to exit the subscribe client and restart it with following command.

mosquitto_sub -t "test" -u "dave" -P "password"

Note the capital -P here.

In the publish client window, try to publish a message without a password.

mosquitto_pub -t "test" -m "message from mosquitto_pub client"

The message will be rejected with following error message.

Connection Refused: not authorised.
Error: The connection was refused.

Now publish a message with the username and password.

mosquitto_pub -t "test" -m "message from mosquitto_pub client" -u "dave" -P "password"

Hit “Enter” and you will see the message in subscribe client window, as in Step Two.

Conclusion

We have now set up a password protected MQTT server. You can use the Public IP of your Ubuntu server as an MQTT broker for your projects.

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article
Previous Post

Setup Nginx-RTMP on Ubuntu 14.04

Next Post

Install Adminer on Debian/Ubuntu

Next Post

Install Adminer on Debian/Ubuntu

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet