How-To Guides
Integration Guides

Install the Mosquitto MQTT Broker on Ubuntu

11min

Review the following guide for installing the Mosquitto MQTT broker on Ubuntu. Once installed, you can connect to the following connectors in Manufacturing Connect Edge:

  • MQTT - Generic TCP
  • MQTT - Generic over SSL.

Before You Begin

Make sure you do the following:

  • Verify the Ubuntu OS is 19.10.
  • Use a Virtual Machine on VirtualBox 6.0 to install Ubuntu.
  • Verify that the Virtual Machine has two configured network adapters that are set as follows:
    • Adapter 1 is set to Bridged Adapter
    • Adapter 2 is set to NAT

Step 1: Install Net Tools

To install net tools:

  1. From a command prompt, enter sudo apt install net-tools, and press ENTER.
  2. If asked to provide your password, enter the password and press ENTER.
  3. Enter ifconfig and press ENTER. For this use case, the IP needed is for the Bridged Adapter (enp0s3). It is 192.168.1.11.

    ifconfig command
    ifconfig command
    

Step 2: Install MQTT Broker

To install the MQTT broker:

  1. From a command prompt, enter sudo apt-get update and press ENTER.
  2. If asked to provide your password, enter the password and press ENTER.
  3. Enter sudo apt-get install mosquitto and press ENTER.
  4. Enter Y and press ENTER to continue. The MQTT broker is installed.

Step 3: Install MQTT Client

You can install the MQTT client to test the MQTT broker configuration.

To install the MQTT client:

  1. Enter sudo apt-get install mosquitto-clients and press ENTER.
  2. Enter Y and press ENTER to continue. The MQTT client is installed.

Step 4: Test Initial Installation

You can verify that the MQTT broker and client are correctly installed.

To test the initial installation:

  1. Enter mosquitto_sub -t "test" and press ENTER.
  2. Right-click the Terminal icon and select New Window to open a second terminal window.
  3. Select the second terminal window.
  4. Enter mosquitto_pub -m "message from mosquitto_pub client" -t "test" and press ENTER.
  5. Select the first terminal window to view the message sent from the second terminal window. You should see: message from mosquitto_pub client
  6. Press Ctrl+C to exit the first terminal window.

Step 5: Set Up Username and Password

To set up a username and password:

  1. Enter sudo mosquitto_passwd -c /etc/mosquitto/passwd <username> and press ENTER. For example: sudo mosquitto_passwd -c /etc/mosquitto/passwd JohnSmith
  2. Enter a password for the username at the prompt and press ENTER.
  3. Re-enter the password and press ENTER.
  4. Enter sudo nano /etc/mosquitto/conf.d/default.conf and press ENTER to edit the default.conf file. The default.conf file opens in the nano editor. You can enforce refusal of connections from anonymous users and provide the path of the file holding all users and passwords from this file.
  5. Copy or enter the following two lines into the file. allow_anonymous false password_file /etc/mosquitto/passwd
  6. Press Ctrl+O to save the changes.
  7. Verify the file name.

    Mosquitto broker file name
    Mosquitto broker file name
    
  8. Press ENTER. The changes are verified.
  9. Press Ctrl+X to exit the nano editor.
  10. From the terminal window, enter sudo ufw allow 1883 and press ENTER. The rule is applied. This rule ensures that the firewall allows connections on port 1883. This is the default port for the Mosquitto MQTT broker and is used for a connection using the username and password.
  11. Enter sudo systemctl restart mosquitto and press ENTER. Mosquitto restarts so the new configuration and rule can be used.

Step 6: Test Username and Password Requirements

To test the username and password requirements:

  1. Enter mosquitto_sub -t "test" -u "<username>" -P "<userpassword>" and press ENTER. For example: mosquitto_sub -t "test" -u "JohnSmith" -P "XYZcorp@1"
  2. Open a second terminal window if you have closed it and select it.
  3. Enter mosquitto_pub -t "test" -m "message from mosquitto_pub client" and press ENTER to try to connect a publisher without using the correct username and password. The connection is refused. You should see: connection error: Connection Refused: not authorized.
  4. Enter mosquitto _pub -t "test" -m "message from mosquitto_pub client" -u "<username>" -P "<password>" and press ENTER to connect using the correct username and password.
  5. Select the first terminal window. View the message: message from mosquitto_pub client in the first Terminal window.
  6. Press Ctrl+C to exit the subscription stage in the first Terminal window.

Step 7: Enable SSL/TSL

You can create certificates and keys in the Ubuntu system home directory.

Important: If you decide not to use the bash file for the following task and instead use either the commands individually or follow other guides available, you must use the FQDN (Fully qualified domain name) for the CN (Common Name). Never use the IP address.

Refer to the following content for step 2.

Text


To enable SSL/TSL:

  1. From a terminal window, enter nano and press ENTER.
  2. Copy the content above and paste it into the nano editor.
  3. Press Ctrl+O to save the changes.
  4. Enter generate-CA.sh to provide the file name and press ENTER. The file is created.
  5. Press Ctrl+X to exit the nano editor. The file is in your default directory.
  6. Enter sudo chmod 774 <filename> and press ENTER to set permissions. For example: sudo chmod 774 generate-CA.sh
  7. Enter sudo ./<filename> and press ENTER execute the bash file and create the certificates. For example: Example: sudo ./generate-CA.sh The bash file is executed. You should see: JohnSmith@JohnSmith-virtualBox: ~$ ./generate-CA.sh Generating a RSA private key The certificates and key files are in your home directory.
  8. Enter sudo cp ca.crt /etc/mosquitto/ca_certificates and press ENTER to copy the ca.crt to the correct folder.
  9. Enter sudo cp <your FQDN>.crt /etc/mosquitto/certs and press ENTER to copy the file containing the FQDN.crt to the correct folder. For example: sudo cp JohnSmith-VirtualBox.crt /etc/mosquitto/certs
  10. Enter sudo cp <your FQDN>.key /etc/mosquitto/certs to copy the server certificate which has the file name <your FQDN>.key. For example: sudo cp JohnSmith-VirtualBox.key /etc/mosquitto/certs
  11. Enter sudo nano /etc/mosquitto/conf.d/default.conf and press ENTER to allow for SSL encryption. The default.conf file opens.
  12. Move your cursor down using the arrow keys and paste the following lines: # Plain MQTT protocol listener 1883 # End of plain MQTT configuration # MQTT over TLS/SSL listener 8883 cafile /etc/mosquitto/ca_certificates/ca.crt certfile /etc/mosquitto/certs/<your FQDN>.crt keyfile /etc/mosquitto/certs/<your FQDN>.key require_certificate false # End of MQTT over TLS/SLL configuration
  13. Press Ctrl+O to save the changes.
  14. Press ENTER, and then press Ctrl+X to exit the nano editor.
  15. Enter sudo ufw allow 8883 and press ENTER to allow SSL encrypted connections on port 8883. The rule is applied.
  16. Enter sudo systemctl restart mosquitto and press ENTER to run the Mosquitto broker using the new configuration and rule.

Step 8: Test Certificates, Username, and Password Requirements

To test the certificates, username, and password requirements:

  1. Enter mosquitto_sub --cafile /etc/mosquitto/ca_certificates/ca.crt -h <Host IP> -t "test" -p8883 -d -u "<username>" -P "<password>" and press ENTER to create a subscription. For example: mosquitto_sub --cafile /etc/mosquitto/ca_certificates/ca.crt -h 192.168.1.11 -t "test" -p 8883 -d -u "JohnSmith" -P "XYZcorp@1"
  2. Open a second terminal window if previously closed and select it.
  3. Enter mosquitto_pub -h <Host IP> -t "test" -m "hello" -p 8883 -d -u "<username>" -P "<password>" --cafile /etc/mosquitto/ca_certificates/ca.crt and press ENTER to publish a message. For example: mosquitto_pub -h 192.168.1.11 -t "test" -m "hello" -p 8883 -d -u "JohnSmith" -P "XYZcorp@1" --cafile /etc/mosquitto/ca_certificates/ca.crt
  4. Select the first Terminal window to view the message hello.
  5. Press Ctrl+C to exit the subscription stage in the first terminal window.