Installing a MQTT and access it from external ip
here are the steps to install Mosquitto and Mosquitto clients on Ubuntu and configure the MQTT broker to be accessible externally without requiring a username and password:
Step 1: Update Your Package List
First, update your package list to make sure you have the latest information on the newest versions of packages and their dependencies.
bashCopy codesudo apt updateStep 2: Install Mosquitto and Mosquitto Clients
Install the Mosquitto broker and client tools using the following commands:
bashCopy codesudo apt install mosquitto mosquitto-clientsStep 3: Enable and Start the Mosquitto Service
Enable Mosquitto to start on boot and then start the service:
bashCopy codesudo systemctl enable mosquitto
sudo systemctl start mosquittoStep 4: Configure Mosquitto to Allow External Access
Edit the Mosquitto configuration file to allow connections from external devices. Open the configuration file with a text editor:
bashCopy codesudo nano /etc/mosquitto/mosquitto.confAdd the following lines to the configuration file to allow anonymous connections and listen on all network interfaces:
confCopy codeallow_anonymous true
listener 1883 0.0.0.0Save the file and exit the text editor (Ctrl+O, Enter, Ctrl+X).
Step 5: Restart the Mosquitto Service
Restart the Mosquitto service to apply the changes:
bashCopy codesudo systemctl restart mosquittoStep 6: Configure Firewall (If Applicable)
If you have a firewall enabled, you need to allow traffic on port 1883 to ensure the MQTT broker can be accessed externally. Use the following command to allow traffic on port 1883:
bashCopy codesudo ufw allow 1883Step 7: Test the MQTT Broker
You can use Mosquitto clients to test the MQTT broker. Open a terminal and use the following commands to publish and subscribe to topics.
Subscribe to a Topic:
bashCopy codemosquitto_sub -h <your-server-ip> -t test/topicPublish a Message to the Topic:
Open another terminal and use the following command:
bashCopy codemosquitto_pub -h <your-server-ip> -t test/topic -m "Hello, MQTT"Replace <your-server-ip> with the IP address of your Ubuntu server running Mosquitto.
Last updated