<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Avijit Sahoo</title>
    <description>The latest articles on DEV Community by Avijit Sahoo (@avijitsahoo00).</description>
    <link>https://dev.to/avijitsahoo00</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1282223%2F52754d3b-1c82-4eae-9376-90f1066c187e.jpg</url>
      <title>DEV Community: Avijit Sahoo</title>
      <link>https://dev.to/avijitsahoo00</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/avijitsahoo00"/>
    <language>en</language>
    <item>
      <title>Migrating Secure MQTT (mosquitto) server with same SSL key on Ubuntu 22.04 between cloud platforms.</title>
      <dc:creator>Avijit Sahoo</dc:creator>
      <pubDate>Fri, 16 Feb 2024 08:05:41 +0000</pubDate>
      <link>https://dev.to/avijitsahoo00/migrating-secure-mqtt-mosquitto-server-with-same-ssl-key-on-ubuntu-2204-between-cloud-platforms-5fmm</link>
      <guid>https://dev.to/avijitsahoo00/migrating-secure-mqtt-mosquitto-server-with-same-ssl-key-on-ubuntu-2204-between-cloud-platforms-5fmm</guid>
      <description>&lt;p&gt;To successfully migrate to an MQTT cloud, it is imperative that all certificate keys are migrated and the DNS is updated to the new server. Thereafter, execute the commands mentioned below to ensure a smooth transition.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, create a Linux virtual machine.&lt;/li&gt;
&lt;li&gt;After logging into the new virtual machine.&lt;/li&gt;
&lt;li&gt;Install Mqtt command&amp;nbsp;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update
sudo apt-get install mosquitto mosquitto-clients
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Check whether the Mqtt service is running or not using this command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status mosquitto
# command1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
Active: active (running) since *******–**–** **:**:** UTC; **h ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
…….. …….. ……..
…… ……
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your terminal appears like this, then you're all set!&lt;br&gt;
Also, you can check using this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mosquitto_sub -h localhost -t test  # command1
# terminal1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open another terminal and paste this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mosquitto_pub -h localhost -t "test" -m "hello world"  # command2
# terminal2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output in terminal1
mosquitto_sub -h localhost -t test # command1
hello world 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now you can configure the domain name&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to your domain hosting service and update the domain 'mqtt.yourdomainname.com' or whatever format you have.&lt;/li&gt;
&lt;li&gt;To complete the process, you need to add the 'A' record along with the public IP address of your new virtual machine.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After setting up the DNS, wait for the TTL seconds that you have specified before. This is because your computer may cache the domain's DNS.&lt;br&gt;
Now you run the command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mosquitto_pub -h mqtt.yourdomainname.com -t "test" -m "hello world from mylocalhost" # command3
# run on local terminal this command
# Output in terminal1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mosquitto_sub -h localhost -t test # command1
hello world
# after run command3
hello world from mylocalhost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set username password for Mqtt login&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo mosquitto_passwd -c /etc/mosquitto/passwd username # used own username &amp;gt; write password &amp;gt; rewrite password &amp;gt; enter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add into &lt;strong&gt;&lt;em&gt;"/etc/mosquitto/mosquitto.conf"&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/mosquitto/mosquitto.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And paste this into this file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.... ....
........ 

listener 1883

allow_anonymous false
password_file /etc/mosquitto/passwd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try these commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mosquitto_sub -h localhost -t test -u "username" -P "password"
# terminal4

# Open another terminal and paste this command
mosquitto_pub -h localhost -t "test" -m "hello world with password" -u "username" -P "password"
# terminal5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output in terminal4
mosquitto_sub -h localhost -t test -u "username" -P "password"
hello world with password
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try from the local machine&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# open local machine terminal
mosquitto_pub -h mqtt.yourdomainname.com -t "test" -m "hello world with password from localmachine" -u "username" -P "password"
# terminal6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output in terminal4
mosquitto_sub -h localhost -t test -u "username" -P "password"
hello world with password
hello world with password from localmachine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If any error occurs reinstall and redo the process. If not, continue…&lt;/p&gt;

&lt;p&gt;After that, We open the old VM and copy all encryption keys.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd path/to/encription/key #replace path/to/encription/key your ssl encription key path
# like ex-&amp;gt; ' /etc/ssl/certs/'
ls
# output look like ca.crt  ca.csr  ca.key  ca.srl  server.crt  server.csr  server.key 
# Copy all file and save in local machine 
cat ca.crt
-----BEGIN CERTIFICATE-----
....................................
.....................................
..................................
-----END CERTIFICATE-----

cat ca.csr 
-----BEGIN CERTIFICATE-----
..................................
...........................

............
......................

...........................
........

# like that copy all the file one by one in local machine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can log in to the new VM and run these commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /etc/mosquitto/
mkdir key
cd key

# start creating file
sudo nano ca.crt # after run paste file content from local machine. crtl + x &amp;gt; Y &amp;gt; enter

sudo nano ca.csr # after run paste file content from local machine. crtl + x &amp;gt; Y &amp;gt; enter

sudo nano ca.key # after run paste file content from local machine. crtl + x &amp;gt; Y &amp;gt; enter

sudo nano ca.srl # after run paste file content from local machine. crtl + x &amp;gt; Y &amp;gt; enter

sudo nano server.crt # after run paste file content from local machine. crtl + x &amp;gt; Y &amp;gt; enter

sudo nano server.csr # after run paste file content from local machine. crtl + x &amp;gt; Y &amp;gt; enter

sudo nano server.key # after run paste file content from local machine. crtl + x &amp;gt; Y &amp;gt; enter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod 644 . # set all file rw-rw-r--
chmod 600 ca.key # set this file rw-------
chmod 600 server.key # set this file rw-------


sudo chown mosquitto:mosquitto /etc/mosquitto/key/server.key 
# it's here for, server.key only use by mosquitto service. 
# so I did that. If you have anthore configuration use other chmod command.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that open &lt;em&gt;&lt;strong&gt;"/etc/mosquitto/mosquitto.conf"&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/mosquitto/mosquitto.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And paste this into this file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.... ....
........ 

listener 8883

allow_anonymous false
password_file /etc/mosquitto/passwd

cafile /etc/mosquitto/key/ca.crt
certfile /etc/mosquitto/key/server.crt
keyfile /etc/mosquitto/key/server.key
tls_version tlsv1.2 # and write other version of ssl you want add.
# now save it. crtl + x &amp;gt; Y &amp;gt; enter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart mosquitto
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your Mosquitto service runs successfully then well done.&lt;br&gt;
Now run these commands on the local machine&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /path/to/ # write file path for local machine's ca.crt file
mosquitto_sub -h mqtt.yourdomainname.com -p 8883  --cafile "ca.crt" -t test  -u "username" -P "password"

# open another terminal on local machine
cd /path/to/ # write file path for local machine's ca.crt file
mosquitto_pub -h mqtt.yourdomainname.com -p 8883  --cafile "ca.crt" -t test -m "Hello from local to another local" -u "username" -P "password"

# if the command run successfully! Then the server is production ready.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you get any errors. Open &lt;em&gt;/var/log/mosquitto/mosquitto.log&lt;/em&gt;, It gives error details.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
