How to use the Mosquitto Broker with IPv6

mosquitto-ipv6You can connect to the Mosquitto broker using IPV6 as well as IPv4 provided IPv6 is enabled on your machine.

When you start mosquitto using a basic configuration file like the one shown below mosquitto will listen for connections on both  IPv4 and IPv6.

include_dir /etc/mosquitto/conf.d
log_type error
log_type notice
log_type information
log_type debug
log_type websockets
log_timestamp_format %Y-%m-%dT%H:%M:%S
connection_messages true
log_dest stdout

allow_anonymous true
listener 1883

This is shown in the screen shot below:

mosquitto-running-ipv6

Just like with IPv4 you need to know the IPv6 address of the broker or the domain name if using DNS to connect to it.

With IPv4 the IP address is usually static and preferably fixed via DHCP.

Note DHCP can be used to provide static IP addresses.

With IPv6 the addresses can be automatically assigned or can be  static depending on your network configuration.

The screen shot below shows the IPv6 addresses automatically assigned on a raspberry Pi.

ipv6-addresses-linux

If I reboot the machine the IP addresses will be the same.

So all we need to do is use one of these addresses to connect to the broker.

The address starting with 2a00 (global) would be used by an external client, and  the address start with fe80 (link local) would be used on the local network with no routers.

The address starting with fdaa (unique local) would be used on the local network with or without routers.

Because of the length of these addresses and for ease of use making a DNS entry for these addresses would be the best option.

Below is a screen shot of my DNS entry:

mosquitto-dns-ipv6

You should not that I have an entry for IPv4 and IPv6.

Restricting IP Versions on The Broker

It is possible to restrict the broker to listening on a protocol version using the socket_domain option as shown below:

listener 1883
socket_domain ipv4

will restrict the broker to IPv4

and

listener 1883
socket_domain ipv6

will restrict the broker to IPv6

With no bind address specified for the listener the broker will accept connections on any of the 3 IPv6 addresses. In my case the addresses are:

fdaa:bbcc:ddee:0:d0a7:5928:ac3c:5b56
fe80::a3f2:69f7:ca08:f9f
2a00:23c6:d886:2901:a838:6c7d:851b:8a39

You can get the broker to listen on a particular address only using the following:

listener 1883 fdaa:bbcc:ddee:0:d0a7:5928:ac3c:5b56

Note: it also works with the global address 2a00 but failed to start using the link local address.(not sure why).

Connecting to the broker using a Client

I tried connecting using a Python client and the mosquitto_sub client and using the domain name.

In both cases it connected using IPv6  by default.

This was with the client on a Windows 10 and a raspberry pi machine.

Summary

The mosquitto broker has had support for both IPv4 and IPv6 for a long time and generally to use it there is no need for any extra broker configuration required.

If using IPv6 then Using a Local DNS server is recommended.

Related Tutorials and resources:

 

Please rate? And use Comments to let me know more

Leave a Reply

Your email address will not be published. Required fields are marked *