서버 설치 · 5 min read · Oct 31, 2025
우분투 22.04에 Mosquitto MQTT 서버 설치하는 방법

Mosquitto는 MQTT 프로토콜의 무료, 오픈 소스 및 경량 서버 구현입니다. 저전력 단일 보드 컴퓨터에서 전체 서버에 이르기까지 모든 장치를 위해 설계되었습니다. MQTT는 TCP/IP 프로토콜 위에서 작동하며, 기존의 인터넷 홈 네트워크를 사용하여 IoT 장치에 메시지를 전송하고 해당 메시지에 응답합니다. C 언어로 작성되어 있어 다른 MQTT 브로커보다 빠르고 효율적입니다.
이 게시물에서는 우분투 22.04에 Mosquitto 서버를 설치하는 방법을 보여줍니다.
전제 조건
- 우분투 22.04가 실행되는 서버.
- 서버에 루트 비밀번호가 설정되어 있어야 합니다.
필수 종속성 설치
시작하기 전에 모든 시스템 패키지를 최신 버전으로 업데이트하고 업그레이드하는 것이 좋습니다. 다음 명령어를 실행하여 업데이트할 수 있습니다:
apt update -y
apt upgrade -y모든 패키지를 업그레이드한 후, 다음 명령어를 실행하여 다른 필수 패키지를 설치합니다:
apt-get install curl gnupg2 wget git apt-transport-https ca-certificates -y모든 필수 패키지가 설치되면 다음 단계로 진행할 수 있습니다.
Mosquitto 서버 설치
기본적으로 Mosquitto 패키지는 우분투 22.04 기본 저장소에 없습니다. 따라서 Mosquitto의 공식 저장소를 APT에 추가해야 합니다. 다음 명령어로 추가할 수 있습니다:
add-apt-repository ppa:mosquitto-dev/mosquitto-ppa -y저장소가 APT에 추가되면 다음 명령어로 설치할 수 있습니다:
apt install mosquitto mosquitto-clients -y설치가 완료되면 다음 명령어로 Mosquitto 상태를 확인할 수 있습니다:
systemctl status mosquitto다음과 같은 출력이 표시되어야 합니다:
? mosquitto.service - Mosquitto MQTT Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-12-06 04:50:33 UTC; 8s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Process: 5491 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 5492 ExecStartPre=/bin/chown mosquitto:mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 5493 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
Process: 5494 ExecStartPre=/bin/chown mosquitto:mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
Main PID: 5495 (mosquitto)
Tasks: 1 (limit: 2242)
Memory: 1.3M
CPU: 23ms
CGroup: /system.slice/mosquitto.service
??5495 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Dec 06 04:50:32 ubuntu2204 systemd[1]: Starting Mosquitto MQTT Broker...
Dec 06 04:50:33 ubuntu2204 systemd[1]: Started Mosquitto MQTT Broker.
MQTT 관리 비밀번호 생성
보안상의 이유로 MQTT에 대한 비밀번호 인증을 구성하는 것이 좋습니다.
다음 명령어로 관리자 사용자 및 비밀번호를 설정합시다:
mosquitto_passwd -c /etc/mosquitto/passwd hitesh다음과 같이 비밀번호를 설정합니다:
Password:
Reenter password:
다음으로, MQTT 구성 파일을 편집하고 포트 및 비밀번호 파일을 정의합니다.
nano /etc/mosquitto/conf.d/default.conf다음 줄을 추가합니다:
listener 1883
password_file /etc/mosquitto/passwd
파일을 저장하고 닫은 후 Mosquitto 서비스를 재시작하여 변경 사항을 적용합니다.
systemctl restart mosquittoMQTT를 사용하여 메시지 전송 및 수신하는 방법
Mosquitto 클라이언트를 사용하여 Mosquitto 서버에 연결한 후 다양한 주제에서 메시지를 전송하고 수신해야 합니다.
메시지를 전송하고 수신하기 전에 주제를 구독해야 합니다. 홈 자동화 애플리케이션에서 다음 주제 중 하나를 사용할 수 있습니다.
- home/lights/sitting_room
- home/lights/kitchen
- home/lights/master_bedroom
- home/lights/kids_bedroom
다음 명령어를 실행하여 home/lights/kids_bedroom 주제를 구독합시다.
mosquitto_sub -u hitesh -P password -t "home/lights/kids_bedroom"다음으로, 새 터미널 인터페이스를 열고 다음 명령어로 home/lights/kids_bedroom 주제에 메시지를 게시합니다.
mosquitto_pub -u hitesh -P password -m "ON" -t "home/lights/kids_bedroom"첫 번째 터미널에서 ON 페이로드를 받게 됩니다:
ON
이제 두 번째 터미널로 가서 같은 주제에 OFF 메시지를 전송합니다.
mosquitto_pub -u hitesh -P password -m "OFF" -t "home/lights/kids_bedroom"첫 번째 터미널에서 다음 메시지를 보게 됩니다:
ON
OFF
작업이 완료되면 다음 단계로 진행할 수 있습니다.
Let’s Encrypt SSL로 Mosquitto 보안 설정
Mosquitto를 SSL로 보호하려면 서버에 Certbot 클라이언트 패키지를 설치해야 합니다.
먼저, 다음 명령어를 사용하여 Snap 패키지 관리자를 설치합니다:
apt install snapd다음으로, 다음 명령어를 사용하여 Snap 패키지를 업데이트합니다:
snap install core
snap refresh core다음으로, 다음 명령어로 Certbot 패키지를 설치합니다:
snap install --classic certbot다음으로, Certbot 바이너리의 심볼릭 링크를 시스템 위치에 생성합니다.
ln -s /snap/bin/certbot /usr/bin/certbot다음으로, 다음 명령어를 실행하여 SSL 인증서를 생성합니다.
certbot certonly --standalone --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m [email protected] -d mosquitto.linuxbuz.com다음과 같은 출력이 표시되어야 합니다:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for mosquitto.linuxbuz.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/mosquitto.linuxbuz.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/mosquitto.linuxbuz.com/privkey.pem
This certificate expires on 2023-03-06.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
다음 명령어를 사용하여 생성된 모든 인증서 파일을 볼 수 있습니다:
ls /etc/letsencrypt/live/mosquitto.linuxbuz.com/다음과 같은 출력이 표시됩니다:
cert.pem chain.pem fullchain.pem privkey.pem README
다음으로, 다음 명령어를 사용하여 Diffie-Hellman 인증서를 생성합니다:
openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048작업이 완료되면 다음 단계로 진행할 수 있습니다.
MQTT가 Let’s Encrypt SSL을 사용하도록 구성
먼저, 다음 명령어를 사용하여 생성된 모든 인증서를 Mosquitto 디렉토리에 복사합니다:
cp /etc/letsencrypt/live/mosquitto.linuxbuz.com/fullchain.pem /etc/mosquitto/certs/server.pem
cp /etc/letsencrypt/live/mosquitto.linuxbuz.com/privkey.pem /etc/mosquitto/certs/server.key다음으로, Mosquitto 인증서에 대한 적절한 소유권을 설정합니다.
chown -R mosquitto: /etc/mosquitto/certs다음으로, Mosquitto 구성 파일을 편집하고 Let’s Encrypt SSL을 정의합니다.
nano /etc/mosquitto/conf.d/default.conf다음 줄을 추가합니다:
listener 8883
certfile /etc/mosquitto/certs/server.pem
cafile /etc/ssl/certs/ISRG_Root_X1.pem
keyfile /etc/mosquitto/certs/server.key
dhparamfile /etc/ssl/certs/dhparam.pem
파일을 저장하고 닫은 후 Mosquitto 서비스를 재시작하여 변경 사항을 적용합니다.
systemctl restart mosquitto다음으로, 다음 명령어를 사용하여 Mosquitto 연결을 확인합니다:
mosquitto_pub -h mosquitto.linuxbuz.com -t "home/lights/kids_bedroom" -m "hello" -p 8883 --capath /etc/ssl/certs/ -u hitesh -P password작업이 완료되면 웹 브라우저를 통해 Mosquitto 연결을 테스트할 수 있습니다.
Mosquitto 웹소켓 구성
다음으로, 브라우저 내에서 MQTT 프로토콜을 사용하기 위해 웹소켓을 구성해야 합니다. Mosquitto 기본 구성 파일을 편집하여 활성화할 수 있습니다:
nano /etc/mosquitto/conf.d/default.conf다음 줄을 추가합니다:
listener 8083
protocol websockets
certfile /etc/mosquitto/certs/server.pem
cafile /etc/ssl/certs/ISRG_Root_X1.pem
keyfile /etc/mosquitto/certs/server.key
dhparamfile /etc/ssl/certs/dhparam.pem
파일을 저장하고 닫은 후 Mosquitto 서비스를 재시작하여 변경 사항을 적용합니다.
systemctl restart mosquitto다음으로, 터미널 인터페이스를 열고 다음 명령어를 실행합니다:
mosquitto_sub -u hitesh -P password -t "home/lights/kids_bedroom"이제 웹 브라우저를 열고 브라우저 기반 MQTT 클라이언트를 사용하여 웹소켓 기능을 테스트합니다. 다음 화면이 표시되어야 합니다:

Mosquitto 서버 호스트, 포트, 사용자 이름, 비밀번호를 입력한 후 Connect 버튼을 클릭합니다. 다음 화면이 표시되어야 합니다:

이제 주제와 메시지를 입력한 후 Publish 버튼을 클릭합니다.
다음으로, 터미널 인터페이스로 돌아가면 다음 출력에서 게시한 메시지를 볼 수 있습니다:
Hi
결론
이 게시물에서는 Mosquitto 서버를 설치하고 우분투 22.04에서 Let’s Encrypt SSL로 보안 설정하는 방법을 설명했습니다. 또한 브라우저 기반 클라이언트를 통해 Mosquitto를 테스트했습니다. 질문이 있으시면 언제든지 문의해 주세요.
새 게시물을 받은 편지함에서 받기
스팸은 없습니다. 언제든지 구독 해지 가능합니다.