본문 바로가기

리눅스

우분투에 dnsmasq를 설치하고 구성하는 방법

728x90

우분투에 dnsmasq를 설치하고 구성하는 방법

dnsmasq는 작고 가벼운 DNS 서버 및 DHCP 서버입니다. 이를 사용하여 로컬 네트워크에서 DNS 쿼리 및 DHCP 클라이언트를 관리할 수 있습니다.

dnsmasq 삭제

sudo systemctl disable stop dnsmasq
sudo apt-get autoremove --purge -y dnsmasq
sudo apt-get autoremove -y

1. dnsmasq 설치

sudo apt-get update
sudo apt-get install -y dnsmasq
  • dnsmasq 버전 정보 확인
dnsmasq --version
$ dnsmasq --version
Dnsmasq version 2.90  Copyright (c) 2000-2024 Simon Kelley
Compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset no-nftset auth cryptohash DNSSEC loop-detect inotify dumpfile

This software comes with ABSOLUTELY NO WARRANTY.
Dnsmasq is free software, and you are welcome to redistribute it
under the terms of the GNU General Public License, version 2 or 3.

2. dnsmasq 설정

dnsmasq의 주요 설정 파일은 /etc/dnsmasq.conf에 있습니다.

이 파일을 수정하여 DNS 및 DHCP 설정을 구성할 수 있습니다.

예를 들어, /etc/dnsmasq.conf 파일을 열고 필요한 설정을 수정합니다.

sudo vim /etc/dnsmasq.conf

설정을 수정한 후에는 dnsmasq 서비스를 다시 시작하여 변경 사항을 적용합니다.

 

  • dnsmasq 서비스 상태 확인
sudo systemctl status dnsmasq
$ sudo systemctl status dnsmasq
● dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
     Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-03-06 13:25:26 KST; 1h 18min ago
    Process: 944 ExecStartPre=/etc/init.d/dnsmasq checkconfig (code=exited, status=0/SUCCESS)
    Process: 952 ExecStart=/etc/init.d/dnsmasq systemd-exec (code=exited, status=0/SUCCESS)
    Process: 961 ExecStartPost=/etc/init.d/dnsmasq systemd-start-resolvconf (code=exited, status=0/SUCCESS)
   Main PID: 960 (dnsmasq)
      Tasks: 1 (limit: 4558)
     Memory: 584.0K
        CPU: 74ms
     CGroup: /system.slice/dnsmasq.service
             └─960 /usr/sbin/dnsmasq -x /run/dnsmasq/dnsmasq.pid -u dnsmasq -7 /etc/dnsmasq.d,.dpkg-dist,.dpkg-old,.dpkg-new --local-service --tr>
  • dnsmasq 설정 파일 구문 확인
dnsmasq --test
$ dnsmasq --test
dnsmasq: syntax check OK.

LISTEN 포트 확인

netstat -nlp | grep dnsmasq
$ netstat -nlp | grep dnsmasq 
tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN      960/dnsmasq
tcp6       0      0 :::53                   :::*                    LISTEN      960/dnsmasq
udp        0      0 0.0.0.0:53              0.0.0.0:*                           960/dnsmasq
udp6       0      0 :::53                   :::*                                960/dnsmasq
sudo ss -lp "sport = :domain"
$ sudo ss -lp "sport = :domain"
Netid      State       Recv-Q    Send-Q    Local Address:Port    Peer Address:Port    Process
udp        UNCONN      0         0               0.0.0.0:domain       0.0.0.0:*        users:(("dnsmasq",pid=960,fd=4))
udp        UNCONN      0         0                  [::]:domain          [::]:*        users:(("dnsmasq",pid=960,fd=6))
tcp        LISTEN      0         32              0.0.0.0:domain       0.0.0.0:*        users:(("dnsmasq",pid=960,fd=5))
tcp        LISTEN      0         32                 [::]:domain          [::]:*        users:(("dnsmasq",pid=960,fd=7))
728x90

3. DNS 쿼리 및 DHCP 클라이언트 관리

dnsmasq는 로컬 네트워크에서 DNS 쿼리 및 DHCP 클라이언트를 관리하는 데 사용됩니다. 적절한 설정을 통해 DNS 레코드를 추가하고 DHCP 클라이언트에 IP 주소를 할당할 수 있습니다.

4. 추가 설정

필요에 따라 /etc/dnsmasq.d/ 디렉토리에 추가적인 설정 파일을 만들어 사용자 정의 설정을 추가할 수 있습니다. 각 설정 파일은 .conf로 끝나야 합니다.

mkdir -p /etc/dnsmasq.d
sudo vim /etc/dnsmasq.d/custom.conf

이 디렉토리의 설정 파일은 메인 설정 파일에서 자동으로 로드됩니다.

5. dnsmasq 서비스 관리

dnsmasq 서비스를 시작, 중지 또는 다시 시작하려면 다음 명령어를 사용합니다.

 

  • 서비스 시작
sudo systemctl start dnsmasq
  • 서비스 중지
sudo systemctl stop dnsmasq
  • 서비스 재시작
sudo systemctl restart dnsmasq
  • 서비스 상태 확인
sudo systemctl status dnsmasq

추가 설정(선택)

1. DNS 설정

Dnsmasq를 사용하여 로컬 DNS 쿼리를 처리하도록 구성할 수 있습니다.

vim /etc/dnsmasq.conf
# 로컬 DNS 영역 정의
local=/yourdomain.com/

# DNS 쿼리를 처리할 DNS 서버 지정
server=8.8.8.8
server=8.8.4.4

위에서 yourdomain.com은 로컬 도메인 이름으로 바꿔주세요. 또한 server 옵션에는 실제 사용할 DNS 서버의 IP 주소를 지정합니다.

2. DHCP 설정

Dnsmasq를 사용하여 로컬 네트워크에 DHCP 서버를 설정할 수도 있습니다. 이를 위해서는 /etc/dnsmasq.conf 파일에서 DHCP 구성을 추가해야 합니다.

 

예를 들어, 다음과 같이 로컬 네트워크의 DHCP 영역을 정의할 수 있습니다.

dhcp-range=192.168.0.50,192.168.0.150,12h

이렇게하면 192.168.0.50에서 192.168.0.150 사이의 IP 주소가 DHCP 클라이언트에게 제공되며, 임대 기간은 12시간입니다.

3. dnsmasq 서비스 재시작

sudo systemctl restart dnsmasq

 

dnsmasq를 사용하여 로컬 네트워크에서 DNS 및 DHCP 서비스를 관리할 수 있습니다.

 

728x90