Your submission was sent successfully! Close

You have successfully unsubscribed! Close

Thank you for signing up for our newsletter!
In these regular emails you will find the latest updates about Ubuntu and upcoming events where you can meet our team.Close

Setup the Mosquitto Ubuntu Appliance in a virtual machine


Installation instructions

We will walk you through the steps of setting up your Mosquitto Ubuntu Appliance in a vm with Multipass and get logged in.

What you'll need

  • A PC running Ubuntu 18.04 LTS or later
  1. Install Multipass

    sudo snap install multipass
  2. Launch your Ubuntu Appliance

    Launch your appliance image. Multipass gives your appliance an instance name like happy-frog. To name it yourself add --name <name>.

    multipass launch appliance:mosquitto
  3. Find your appliance

    Find your appliance VM's IP address:

    multipass list
  4. That's it

    Your appliance is now running in a virtual machine. Start and stop it with multipass start <name> and multipass stop <name>. To access the command-line:

    multipass shell mosquitto

We will walk you through the steps of setting up your Mosquitto Ubuntu Appliance in a vm with Multipass and get logged in.

What you'll need

  • A PC running Windows 10 Pro/Enterprise/Education Update 1803 or later, or any version of Windows with VirtualBox.
  1. Install Multipass

    1. Make sure the network you're connected to is marked Private, otherwise Windows will prevent Multipass from starting.

    2. Download Multipass for Windows

    3. Run the installer. You will need to allow the installer to gain Administrator privileges.

  2. Launch your Ubuntu Appliance

    Launch your appliance image. Multipass gives your appliance an instance name like happy-frog. To name it yourself add --name <name>.

    multipass launch appliance:mosquitto
  3. Find and connect to your appliance

    Hyper-V instructions

    On Hyper-V, find your virtual appliance IP address:

    multipass list
    VirtualBox instructions

    On VirtualBox, forward the appliance port to the outside world with this command in an Administrator PowerShell:

    env:USERPROFILE\Downloads\PSTools\PsExec.exe -s $env:VBOX_MSI_INSTALL_PATH\VBoxManage.exe controlvm "<instance name>" --natpf1 "mosquitto,tcp,,1883,,1883"

    For more information about this command see the Multipass documentation.

  4. That's it

    Your appliance is now running in a virtual machine. Start and stop it with multipass start <name> and multipass stop <name>. To access the command-line:

    multipass shell mosquitto

We will walk you through the steps of setting up your Mosquitto Ubuntu Appliance in a vm with Multipass and get logged in.

What you'll need

  1. Install Multipass

    1. Download Multipass for macOS

    2. Activate the downloaded installer in an account with Administrator privileges.

    3. To start Multipass with VirtualBox use this Terminal app command:

      sudo multipass set local.driver=virtualbox
  2. Launch your Ubuntu Appliance

    Launch your appliance image. Multipass gives your appliance an instance name like happy-frog. To name it yourself add --name <name>.

    multipass launch appliance:mosquitto
  3. Find your appliance

    To use VirtualBox's port forwarding run:

    sudo VBoxManage controlvm "<instance name>" --natpf1 "mosquitto,tcp,,1883,,1883"

    You can find more information about what this command means in the Multipass documentation.

  4. That's it

    Your appliance is now running in a virtual machine. Start and stop it with multipass start <name> and multipass stop <name>. To access the command-line:

    multipass shell mosquitto

Start using your Mosquitto Ubuntu Appliance

After installing the Mosquitto image, the Mosquitto broker will be running with the default configuration, listening for connections on port 1883. To test the broker, you can use the mosquitto-pub and mosquitto-sub command-line utilities. This is a basic test example to start using your appliance. If you want more information about Mosquitto and what to do next the best place to go is the Mosquitto website.

mosquitto_pub allows you to publish messages to an MQTT broker, and mosquitto_sub allows you to subscribe to messages from an MQTT broker. Both tools have options to control how they are used so they are useful for a wide variety of tasks. In this case, we will use them for some simple testing.

To subscribe to all messages being published to the MQTT broker on the snap/example topic, run this command in your appliance:

mosquitto_sub -h localhost -t 'snap/example' -v

If your MQTT broker is not running on the same machine as mosquitto_sub, you will need to change the localhost argument to match your MQTT broker host or IP address.

The -t snap/example option sets the topic to subscribe and can be provided multiple times. The -v option means to print both the topic of the message as well as its payload. Now to publish a message to the same topic you use mosquitto_pub:

mosquitto_pub -h localhost -t 'snap/example' -m 'Hello from mosquitto_pub'

In this case, the -m option provides the message payload to be published. If everything works as planned, you should see mosquitto_sub print

snap/example Hello from mosquitto_pub

This is a very simple example but allows testing of the broker operation. Other things you may wish to try are subscribing to wildcard topics that include # or , or subscribing to the $SYS/# topic to see the information the broker is publishing about itself.

Note:

The command line treats # as a special character, and SSYS will be expanded as an environment variable if you do not surround them with single quotes.

Once you have finished your testing, you will want to configure your broker to have encrypted connections and use authentication, or possibly configuring bridges, which allow different brokers to share topics. or many other options. To do this, you need to provide a new configuration file. The snap provides an example configuration file at /var/snap/mosquitto/common/mosquitto_example.conf.

This file contains all of the broker configurations, in a similar manner to the man page. To create your own configuration, copy the example file to /var/snap/mosquitto/common/mosquitto.conf and edit according to your needs. Any additional files required by the configuration, such as TLS certificates and keys, must also be placed in /var/snap/mosquitto/common/ and have their full path provided in the configuration file.