Install MQTT broker Mosquitto on Ubuntu 18.04

I made an MQTT broker on the server set up with Ubuntu 18.04, so a work memo.

Preparation

In working on this article, we assume the following:

--The Ubuntu 18.04 server is already installed. --Work as a user other than the root user and with sudo privileges. --You can SSH to the Ubuntu 18.04 server. --Ubunbu18.04 The server is assigned a subdomain. In this article, we will assume mqtt.example.com. --Port 80 is available. No other program is using port 80.

Now to work

Step 1-Install the software

First of all, we will install the programs necessary for the work.

$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt install certbot mosquitto mosquitto-clients

Step 2-Download SSL certificate

Allow 80 for the firewall.

$ sudo ufw allow 80

Then use Certbot to issue an SSL certificate.

sudo certbot certonly --standalone --preferred-challenges http -d mqtt.example.com

mqtt.example.com is just a sample, so please use the domain you assigned to the server.

When you run this command, you will be prompted to enter your email address and accept the terms of use. Please work according to the procedure. If the process is successful, you will see a message indicating where the certificate is stored.

Using the certificate issued here, we will set up Mosquitto from the next.

Step 3 --Mosquitto settings

Username and password authentication is used for security when connecting to Mosquitto.

First, let's create a password setting file. It can be created using the mosquitto_passwd command. your-username is used for connection authentication, so set any name you like.

You will be asked to enter the password twice, so be careful not to make a mistake.

$ sudo mosquitto_passwd -c /etc/mosquitto/passwd your-username

Next, create a Mosquitto configuration file.

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

Then enter the following text: As usual, replace mqtt.example.com with your own domain.

allow_anonymous false
password_file /etc/mosquitto/passwd

listener 8883
certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem

listener 8083
protocol websockets
certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem

Save the file and close it when you are done.

In this file, the following settings are described.

--Disable anonymous login --Use password for connection authentication --Set port 8883 to TCP connection using SSL --Set port 8083 to WebSocket connection using SSL

Restart Mosquitto with the following command for the settings to take effect.

$ sudo systemctl restart mosquitto

Confirm that it can be started normally with the following command.

$ sudo systemctl status mosquitto
● mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
   Loaded: loaded (/etc/init.d/mosquitto; generated)
   Active: active (running) since Mon 2018-07-16 15:03:42 UTC; 2min 39s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 6683 ExecStop=/etc/init.d/mosquitto stop (code=exited, status=0/SUCCESS)
  Process: 6699 ExecStart=/etc/init.d/mosquitto start (code=exited, status=0/SUCCESS)
    Tasks: 1 (limit: 1152)
   CGroup: /system.slice/mosquitto.service
           └─6705 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Next, set the firewall.

$ sudo ufw allow 8883
$ sudo ufw allow 8083

Step 4-Automatic update of Certbot

Basically, the Mosquitto settings should be completed and you should be able to connect.

However, Let's Encrypt has an expiration date, and it is convenient to have it automatically renewed. Set the automatic update.

Open the Certbot configuration file with the following command.

$ sudo nano /etc/letsencrypt/renewal/mqtt.example.com.conf

Then add the following line.

renew_hook = systemctl restart mosquitto

When you're done, save and close the file.

Then, check if there is a syntax error with the following command.

$ sudo certbot renew --dry-run

Step5-Check operation

When you reach this point, check the operation.

For mqtt.example.com, your-username, and your-password, enter the values ​​you set earlier. The following command subscribes to the topic test.

$ mosquitto_sub -h mqtt.example.com -t test -p 8883 --capath /etc/ssl/certs/ -u "your-username" -P "your-password"

Let's send a message to the test topic from the following command by launching another window.

$ mosquitto_pub -h mqtt.example.com -t test -m "hello world" -p 8883 --capath /etc/ssl/certs/ -u "your-username" -P "your-password"

If you can receive the message safely, it is working properly.

That's all for the work. Thank you for your hard work.

Recommended Posts

Install MQTT broker Mosquitto on Ubuntu 18.04
Install Ruby on Ubuntu 20.04
Install Homebrew on Ubuntu 20.04
Install OpenJDK7 (JAVA) on ubuntu 14.04
Install Cybozu Office 10 on Ubuntu 20.4
Install Docker on Ubuntu Server 20.04
Install zabbix agent (5.0) on Ubuntu 18.04
Install MAV Proxy on Ubuntu 18.04
Install Java on WSL Ubuntu 18.04
Install Ubuntu Desktop 20.10 on RaspberryPi4
Install Arduino IDE on Ubuntu 20.04
Install raspi-config on Ubuntu 20.04 (LTS)
Install PlantUML on Intellij on Ubuntu
Install Ubuntu Server 20.04 on Btrfs
Note: Install PostgreSQL 9.5 on Ubuntu 18.04
Install AWS IoT Greengrass on Ubuntu
Install JDK and JRE on Ubuntu 16.10
Install ngrok on ubuntu16.04 using Vagrant
How to install WildFly on Ubuntu 18.04
Install SonarQube on ECS instance on Ubuntu 16.04
Install ruby on Ubuntu 20.04 with rbenv
Headless install of Ubuntu 20.10 on Raspberry Pi
Install OpenJDK (Java) on the latest Ubuntu
I want to install PHP 7.2 on Ubuntu 20.04.
Install Realtek network driver on Ubuntu Note
tmux on Ubuntu
Wine settings on Ubuntu 18.04 (install WOLF RPG Editor)
Build and install Wireshark Development Release (3.3.1) on Ubuntu
How to install network drivers on standalone Ubuntu
How to install NVIDIA driver on Ubuntu 18.04 (Note)
How to install multiple JDKs on Ubuntu 18.04 LTS
Install and switch between multiple Javas on Ubuntu
Install the latest version of Jenkins on Ubuntu 16
Screen recording on Ubuntu 20.04
Web Bluetooth on Ubuntu20.04
Install gradle on mac
Install Corretto 8 on Windows
Install OpenJDK on macOS
Try DisplayLink on Ubuntu 20.04
Reinstall Kubernetes on Ubuntu 19.10
Install Java on Mac
Install Golang on CentOS 8
Use Flutter on Ubuntu
Install Neo4j 4.1.3 on centOS
Install ROS Noetic ubuntu20.04
Install Docker on Manjaro
How to install NVIDIA driver on Ubuntu ssh destination
Install Gradle with ubuntu16.04
Install PostgreSQL 12 on Centos8
Install nginx on centOS7
Install lombok on SpringToolSuite4
Install kuromoji on CentOS7
Setting JAVA_HOME on Ubuntu
Put JetBrains on Ubuntu
Install Mattermost on CentOS 7
Install PostGIS 2.5.5 on CentOS7
Use mkdir on ubuntu
Install jpndistrict on CentOS 7
Apache2 on Ubuntu20.04 LTS
Install openjdk11 on mac
Use cpplapack on ubuntu