Install mqtt mosquitto broker linux (ubuntu) for iot

João Nascimento
3 min readAug 21, 2020

--

In this tutorial we will install the mosquitto mqtt broker in linux (server/desktop) and a client in the same machine.

First we need to add the repository:

  • sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

Second we need to update

  • sudo apt-get update

And then, we install de mosquitto broker

  • sudo apt-get install mosquitto

In last we install the client, you can install this in other machine

  • sudo apt-get install mosquitto-clients

Now, some commands that help you to know what is going on with mosquitto service

It will be helpfull

To stop the service

  • sudo systemctl stop mosquitto.service

To start the service

  • sudo systemctl start mosquitto.service

To restart the service

  • sudo systemctl restart mosquitto.service

To debug the service start like this

  • mosquitto -v

Where are the configuration files of service??

In ubuntu normally you can find in

  • /etc/mosquitto/conf.d/default.conf

Like this example, open with vim:

  • vim /etc/mosquitto/conf.d/default.conf

Here you can change several properties of service, like the port, and many others…

By default the broker will start listening on port 1883

In this example we have 1883 port only for local connection and 8883 for outside connections, we don’t show the lines down “listener 8883” because this example have ssl certificate and have the path and the name of the client, for the tutorial you can leave your like it is.

Now, we can test the service

First we need to create a channel, that will receive the menssages.

This example are made for the broker and client in the same machine.

Open 2 terminals, then in one you type:

  • mosquitto_sub -d -t TopicYouWant

Now your broker subscribe the topic TopicYouWant, now you call in other terminal type:

  • mosquitto_pub -d -t TopicYouWant -m “The message you want to send”

You can see the first terminal receive the message.

The service is running, and working fine….

Securing with User Password

If you want to secure the connections of your users with username and password, you can do like this:

  • sudo mosquitto_passwd -c /etc/mosquitto/passwd <usernameYouWant>

if you want to see what is made, type:

  • vim /etc/mosquitto/passwd

And you will see what the command add a line with encrypted password, in the file.

Now go to the config file:

  • sudo vim /etc/mosquitto/conf.d/default.conf

First type :

  • password_file /etc/mosquitto/passwd

Add the line:

#anonymous access, only can receive messages

  • topic read $SYS/#

#anonymous only can read topic

  • topic read topicYouWant/#

#readwrite to the loop

user <usernameYouWant>

  • topic topicYouWant/#

If you want to block all anonymous user you can type:

  • allow_anonymous false

Now try sending message again without user and pass, to see what happens.

restart mosquitto

  • sudo systemctl restart mosquitto.service

Open 2 terminals, then in one you type:

  • mosquitto_sub -d -t TopicYouWant

Now your broker subscribe the topic TopicYouWant, now you call in other terminal type:

  • mosquitto_pub -d -t TopicYouWant -m “The message you want to send”

As you can see, the message wasn’t send

Let’s try with user and password

Open 2 terminals, then in one you type:

  • mosquitto_sub -h localhost -u <usernameYouWant> -P <passwordYouType> -t TopicYouWant

Now your broker subscribe the topic TopicYouWant, now you call in other terminal type:

  • mosquitto_pub -h localhost -t TopicYouWant -u <usernameYouWant> -P <passwordYouType> -m “message you want send”

As you can see, the message was send…

If you like this article, please clap, please.

Soon I will add an advanced article about mqtt with ssl encryption, and clients side encryption.

Thanks…

--

--

Responses (1)