[一日30分 인생승리의 학습법] Ubuntu 24.04에 Mosquitto MQTT 서버를 설치하는 방법
2025.08.25 00:40
[一日30分 인생승리의 학습법] Ubuntu 24.04에 Mosquitto MQTT 서버를 설치하는 방법
Mosquitto MQTT는 MQTT 프로토콜을 구현하는 오픈 소스 메시지 브로커입니다. 저대역폭, 고지연 네트워크를 위해 설계된 경량의 게시-구독 네트워크 프로토콜입니다. 기기 간의 효율적인 통신을 지원하여 사물 인터넷(IoT) 애플리케이션 및 최소한의 오버헤드로 안정적인 메시지 전달이 필요한 기타 시나리오에 이상적입니다. Mosquitto는 TLS 암호화 및 인증과 같은 다양한 보안 기능을 지원하여 안전한 데이터 전송을 보장합니다. 또한 수천 개의 동시 연결을 처리할 수 있어 대규모 배포에 적합합니다.
이 튜토리얼에서는 Ubuntu 24.04에 Mosquitto MQTT 서버를 설치하는 과정을 안내해드리겠습니다.
1단계 – 필수 패키지 설치
먼저, 패키지 목록을 업데이트하고 설치 과정에 필요한 필수 패키지를 설치해야 합니다. 터미널을 열고 다음 명령을 실행하여 필요한 패키지가 있는지 확인하세요. 대부분의 패키지는 기본적으로 설치되어 있을 수 있습니다.
apt-get update
apt-get install curl gnupg2 wget git apt-transport-https ca-certificates -y
2단계 – Mosquitto PPA 추가
다음으로, Mosquitto PPA(Personal Package Archive)를 시스템에 추가해야 합니다. PPA는 최신 버전의 Mosquitto를 제공합니다. 다음 명령을 실행하세요.
add-apt-repository ppa:mosquitto-dev/mosquitto-ppa -y
이 명령은 Mosquitto PPA를 시스템의 소프트웨어 소스에 추가합니다.
3단계 – Mosquitto 및 Mosquitto 클라이언트 설치
이제 PPA가 추가되었으므로 Mosquitto와 클라이언트를 설치할 수 있습니다. 다음 명령을 실행하세요.
apt install mosquitto mosquitto-clients -y
이 명령은 Mosquitto 서버와 Mosquitto 클라이언트 유틸리티(mosquitto_sub 및 mosquitto_pub)를 설치합니다.
Mosquitto가 올바르게 설치되고 실행 중인지 확인하려면 systemctl 명령을 사용하여 상태를 확인하세요.
systemctl status mosquitto
다음과 비슷한 출력이 표시됩니다.
● mosquitto.service - Mosquitto MQTT Broker
Loaded: loaded (/usr/lib/systemd/system/mosquitto.service; enabled; preset: enabled)
Active: active (running) since Mon 2025-05-12 06:23:36 UTC; 9s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Process: 49200 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 49202 ExecStartPre=/bin/chown mosquitto:mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 49204 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
Process: 49206 ExecStartPre=/bin/chown mosquitto:mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
Main PID: 49208 (mosquitto)
Tasks: 1 (limit: 4609)
Memory: 1.0M (peak: 1.5M)
CPU: 25ms
CGroup: /system.slice/mosquitto.service
└─49208 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
May 12 06:23:36 ubuntu systemd[1]: Starting mosquitto.service - Mosquitto MQTT Broker...
May 12 06:23:36 ubuntu systemd[1]: Started mosquitto.service - Mosquitto MQTT Broker.
출력은 Mosquitto 서비스가 활성화되어 실행 중임을 나타냅니다.
4단계 – Mosquitto 구성
기본적으로 Mosquitto는 익명 연결을 허용하도록 구성되어 있습니다. 보안상의 이유로 Mosquitto 브로커에 비밀번호를 설정하는 것이 좋습니다.
다음 명령을 실행하여 암호 파일을 만들고 사용자를 추가하세요.
mosquitto_passwd -c /etc/mosquitto/passwd hjethva
방금 생성한 사용자( hjethva )에 대한 비밀번호를 입력하고 확인하라는 메시지가 표시됩니다.
Password:
Reenter password:
비밀번호 파일에 대한 올바른 소유권을 설정하세요:
chown mosquitto:mosquitto /etc/mosquitto/passwd
다음으로, 리스너와 비밀번호 파일을 지정하는 구성 파일을 만듭니다. nano로 구성 파일을 엽니다.
nano /etc/mosquitto/conf.d/default.conf
다음 줄을 추가합니다.
listener 1883
password_file /etc/mosquitto/passwd
이러한 줄은 Mosquitto가 포트 1883에서 수신하고 인증을 위해 지정된 암호 파일을 사용하도록 구성합니다.
이러한 변경 사항을 적용한 후 Mosquitto 서비스를 다시 시작하여 새 구성을 적용합니다.
systemctl restart mosquitto
5단계 – Mosquitto 테스트
모든 것이 올바르게 작동하는지 확인하기 위해 mosquitto_pub 및 mosquitto_sub 명령을 사용하여 Mosquitto를 테스트합니다.
터미널 창을 열고 주제를 구독하세요. YOUR_PASSWORD 값을 변경하는 것을 잊지 마세요.
mosquitto_sub -u hjethva -P YOUR_PASSWORD -v -t "hello/topic"
다른 터미널 창에서 같은 주제에 대한 메시지를 게시합니다. YOUR_PASSWORD 값을 변경하는 것을 잊지 마세요.
mosquitto_pub -u hjethva -P YOUR_PASSWORD -t 'hello/topic' -m 'hello MQTT'
구독자 터미널에서 다음 출력을 볼 수 있습니다.
hello/topic hello MQTT
이는 Mosquitto 서버가 올바르게 작동하고 있음을 확인합니다.
결론
이 튜토리얼에서는 Ubuntu 24.04에 Mosquitto MQTT 서버를 설치하고 구성하는 방법을 다루었습니다. 또한 비밀번호를 사용하여 서버를 보호하는 방법을 시연하고 Mosquitto 클라이언트 도구를 사용하여 설정을 테스트했습니다. Mosquitto는 IoT 및 메시징 애플리케이션에서 널리 사용되는 강력하고 가벼운 MQTT 브로커입니다. 이 설정을 통해 이제 직접 MQTT 기반 솔루션을 구축할 수 있습니다.
How to Install Mosquitto MQTT Server on Ubuntu 24.04
Mosquitto MQTT is an open-source message broker that implements the MQTT protocol. It is a lightweight, publish-subscribe network protocol designed for low-bandwidth, high-latency networks. It facilitates efficient communication between devices, making it ideal for Internet of Things (IoT) applications and other scenarios requiring reliable message delivery with minimal overhead. Mosquitto supports various security features, such as TLS encryption and authentication, ensuring secure data transmission. Its ability to handle thousands of concurrent connections makes it suitable for large-scale deployments.
In this tutorial, we will walk you through the process of installing the Mosquitto MQTT Server on Ubuntu 24.04.
Step 1 – Installing Required Packages
First, we need to update our package list and install some essential packages required for the installation process. Open your terminal and run the following command to make sure the packages you need are available – please note, most of these may already be installed by default:
apt-get update
apt-get install curl gnupg2 wget git apt-transport-https ca-certificates -y
Step 2 – Adding the Mosquitto PPA
Next, we need to add the Mosquitto PPA (Personal Package Archive) to our system. The PPA provides the latest version of Mosquitto. Run the following command:
add-apt-repository ppa:mosquitto-dev/mosquitto-ppa -y
This command adds the Mosquitto PPA to your system’s software sources.
Step 3 – Installing Mosquitto and Mosquitto Clients
Now that the PPA has been added, we can install Mosquitto and its clients. Execute the following command:
apt install mosquitto mosquitto-clients -y
This command installs the Mosquitto server and the Mosquitto client utilities (mosquitto_sub and mosquitto_pub).
To verify that Mosquitto has been installed correctly and is running, use the systemctl command to check its status:
systemctl status mosquitto
You should see an output similar to this:
● mosquitto.service - Mosquitto MQTT Broker
Loaded: loaded (/usr/lib/systemd/system/mosquitto.service; enabled; preset: enabled)
Active: active (running) since Mon 2025-05-12 06:23:36 UTC; 9s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Process: 49200 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 49202 ExecStartPre=/bin/chown mosquitto:mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 49204 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
Process: 49206 ExecStartPre=/bin/chown mosquitto:mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
Main PID: 49208 (mosquitto)
Tasks: 1 (limit: 4609)
Memory: 1.0M (peak: 1.5M)
CPU: 25ms
CGroup: /system.slice/mosquitto.service
└─49208 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
May 12 06:23:36 ubuntu systemd[1]: Starting mosquitto.service - Mosquitto MQTT Broker...
May 12 06:23:36 ubuntu systemd[1]: Started mosquitto.service - Mosquitto MQTT Broker.
The output indicates that the Mosquitto service is active and running.
Step 4 – Configuring Mosquitto
By default, Mosquitto is configured to allow anonymous connections. For security reasons, it is recommended to set up a password for your Mosquitto broker.
Run the following command to create a password file and add a user:
mosquitto_passwd -c /etc/mosquitto/passwd hjethva
You will be prompted to enter and confirm a password for the user you just created (hjethva).
Password:
Reenter password:
Set the correct ownership for the password file:
chown mosquitto:mosquitto /etc/mosquitto/passwd
Next, create a configuration file to specify the listener and password file. Open the configuration file with nano:
nano /etc/mosquitto/conf.d/default.conf
Add the following lines:
listener 1883
password_file /etc/mosquitto/passwd
These lines configure Mosquitto to listen on port 1883 and use the specified password file for authentication.
After making these changes, restart the Mosquitto service to apply the new configuration:
systemctl restart mosquitto
Step 5 – Testing Mosquitto
To ensure everything is working correctly, we will test Mosquitto using the mosquitto_pub and mosquitto_sub commands.
Open a terminal window and subscribe to a topic – remember to change the YOUR_PASSWORD value.
mosquitto_sub -u hjethva -P YOUR_PASSWORD -v -t "hello/topic"
In another terminal window, publish a message on the same topic – – remember to change the YOUR_PASSWORD value.
mosquitto_pub -u hjethva -P YOUR_PASSWORD -t 'hello/topic' -m 'hello MQTT'
You should see the following output in the subscriber terminal:
hello/topic hello MQTT
This confirms that the Mosquitto server is working correctly.
Conclusion
In this tutorial, we have covered the installation and configuration of the Mosquitto MQTT server on Ubuntu 24.04. We also demonstrated how to secure the server with a password and tested the setup using Mosquitto client tools. Mosquitto is a powerful and lightweight MQTT broker that is widely used in IoT and messaging applications. With this setup, you can now start building your own MQTT-based solutions.
[출처] https://www.atlantic.net/dedicated-server-hosting/how-to-install-mosquitto-mqtt-server-on-ubuntu-24-04/
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.

