TIL

[리눅스] DNS 개념 및 네임서버 설정 2주 7일차-1

엥이게되네 2023. 5. 2. 21:46
728x90

1. domain과 hostname

domain : 세상 사람들에게 알려진 나의 이름이다 (ex. naver.com)

hostname : 네트워크 내부의 컴퓨터 이름이다 (ex. www.naver.com)

name server : 작은 서비스, 관리자가 마음대로 설정할 수 있다. 밑줄 친 부분이다(ex. mail.naver.com , cafe.naver.com) 

 

DNS : Domain Name Server, 호스트의 도메인주소를 ip(네트워크) 주소로 변경해 주거나, 반대로 호스트 주소를 ip주소로 변경 해줄 수 있다.

 

여러 서비스들을 같은 도메인으로 실행하기 위해서 같은 서버에서 실행된다면 비효율적일 것이다.

따라서 같은 도메인을 여러 서버에서 사용하기 위해 혹은 같은 서버에서 여러개를 사용하기 위해 네임서버가 사용된다.

 

1.1. 네임서버 구조

출처 : cloudflare.com

네임서버가 어떻게 이루어져 있는지 위 그림을 통해 대략적으로 확인해볼 수 있다.

 

네임서버의 작동은 root 서버에서 시작한다. root 서버는 최상위 서버로 전세계에 13개 정도 밖에 없는것으로 알려져 있다.

 

만약 mail.google.com을 접속하기 위해서는 어떤 순서로 서버로 접속할 수 있을까

 

먼저 네임서버는 먼저 루트 서버 부터 시작한다고 했다. 

그 다음 루트 서버에서는 .com 네임서버로 이동시킨다.

.com 네임서버에서는 하위 네임서버인 google.com 서버로 이동시키고 다시 요청한 도메인인 mail 네임 서버로 이동하여 요청한 접속 수행을 완료한다.

 

 

1.2. DNS 관련 명령어

1) nslookup : 입력하는 주소에 대한 ip 주소를 가져온다. ip를 입력할 경우 컴퓨터의 hostname을 볼 수 있다. cmd와 linux모두 사용 가능하다

 

nslookup으로 확인한 naver.com의 IP

2) $ /etc/hosts  : vi를 이용하여 ip에 대한 정보를 입력할 수 있다.

3) $ grep domain /etc/services  :  모든 프로그램들의 포트번호를 알 수 있다.

 


2. 우분투에 네임서버를 설치해보자

네임서버는 bind를 이용하여 구현 된다. 

 

직접 네임서버를 구현해보고 여러 서비스를 한 개 의 도메인에서 이용할 수 있게 설정을 해보자.

 

먼저 bind 를 설치하고 시스템을 시작시켜보자

 

$apt install -y bind9 bind9utils bind9-doc dnsutils
$systemctl start bind9		#bind 서비스 시작
$systemctl restart bind9	#bind 서비스 수정 후 재시작

 

bind 구성 파일을 알아보자 /etc/bind로 이동 후 여러 구성 파일을 확인할 수 있다.

$ cd /etc/bind
/etc/bind$ ls
bind.keys  db.local              
db.0       named.conf			named.conf.options
db.127     named.conf.default           rndc.key           
db.255     named.conf.default-zones     zones.rfc1918    
db.empty   named.conf.local

 

주요 구성파일들은 다음과 같다

 

$ nl named.conf
 1  // This is the primary configuration file for the BIND DNS server named.
 2  //
 3  // Please read /usr/share/doc/bind9/README.Debian.gz for information on the
 4  // structure of BIND configuration files in Debian, *BEFORE* you customize
 5  // this configuration file.
 6  //
 7  // If you are just adding zones, please do that in /etc/bind/named.conf.local
 
 8  include "/etc/bind/named.conf.options";	#글로벌 DNS 옵션
 9  include "/etc/bind/named.conf.local";	#지역 확인
10  include "/etc/bind/named.conf.default-zones";#localhost와 같은 기본 영역, 역방향 및 루트 힌트를 확인할 수 있다.

 

2.1. 직접 구성해보자

 

Domain: nc.co.kr
200.100.50.10 ns.nc.co.kr
200.100.50.20 www.nc.co.kr
200.100.50.30 ftp.nc.co.kr
200.100.50.40 mail.nc.co.kr
CNAME web www

 

/etc/bind/named.conf.default-zones 을 수정하자

 

 $vi named.conf.default-zones
 
 # 파일 하단에 다음 코드를 추가해준다.
 
 40	zone "nc.co.kr" {	#nc.co.kr은 nc.zone에서 응답하겠다.
 41     type master;
 42     file "/etc/bind/nc.zone";	# nc.zone 파일을 추가한다. IP -> HOST
 43 };
 44
 45 zone "50.100.200.in-addr.arpa" { # ip는 역방향으로 작성한다.
 46     type master;
 47     file "etc/bind/nc.rev";	# nc.rev 파일을 추가한다. HOST -> IP
 48 };

 

그 다음 기존 파일을 복사하고 이름을 바꾼 후 다음과 같이 수정해보자

 

nc.zone 은 host 주소를 IP 주소로 변환하는 역할을  한다.

 

$cp db.local nc.zone
/etc/bind$ vi nc.zone

  1 ;
  2 ; BIND data file for local loopback interface
  3 ;
  4 $TTL    604800
  5 @   IN  SOA nc.co.kr. master.nc.co.kr. ( # 이부분을 수정해준다
  6                   2     ; Serial
  7              604800     ; Refresh
  8               86400     ; Retry
  9             2419200     ; Expire
 10              604800 )   ; Negative Cache TTL
 11 ;
 12 @   IN  NS  nc.co.kr.	 	# 여기서 부터 수정해준다
 13     IN  A   200.100.50.10 		# 설정한 기본 도메인의 IP
 14 ns  IN  A   200.100.50.10 		# ns.nc.co.kr은 이 IP로 받는다. 기본도메인
 15 www IN  A   200.100.50.20 		# www.nc.co.kr은 이 IP로
 16 ftp IN  A   200.100.50.30 		# ftp는 이 IP로
 17 mail    IN  A 200.100.50.40 	# mail은 이 IP로
 18 web     IN  CNAME   www		# web으로 입력하면 www로 리다이렉트 (canonical name)

 

마찬가지로 다음 파일도 수정해주자

 

nc.rev는 반대로 IP  주소를 host주소로 변환하는 역할을  한다.

 

$cp nc.zone nc.rev # 기존 파일을 복사한다
$vi nc.rev

$vi nc.rev

  1 ;
  2 ; BIND data file for local loopback interface
  3 ;
  4 $TTL    604800
  5 @   IN  SOA nc.co.kr.   master.nc.co.kr. (
  6                   2     ; Serial
  7              604800     ; Refresh
  8               86400     ; Retry
  9             2419200     ; Expire
 10              604800 )   ; Negative Cache TTL
 11 ;
 12 @   IN  NS  ns.nc.co.kr.	# 여기서 부터 수정해준다.
 13 @   IN  A   200.100.50.10
 14     IN  PTR nc.co.kr.	# PTR은 IP->Host를 수행
 15 10  IN  PTR ns.nc.co.kr.	# 10은 설정하려는 IP주소의 마지막 부분이다
 16 20  IN  PTR www.nc.co.kr.
 17 20  IN  PTR web.nc.co.kr.
 18 30  IN  PTR ftp.nc.co.kr.
 19 40  IN  PTR mail.nc.co.kr. # web을 www로 리다이렉트 한다.

 

관련 서비스를 재시작하고 확인해보자

 

먼저 IP -> Host를 확인해보자

 

$ systemctl restart named	# nameserver 수정 후 재시작
$ nslookup				# 명령어 입력
> server 127.0.0.1		# 입력 자기 자신의 루프백 주소
Default server: 127.0.0.1
Address: 127.0.0.1#53
> nc.co.kr			# 입력 
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   nc.co.kr		
Address: 200.100.50.10		# IP 변경 확인
> www.nc.co.kr			# 입력

Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   www.nc.co.kr
Address: 200.100.50.20		# IP 변경 확인
> ftp.nc.co.kr			# 입력

Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   ftp.nc.co.kr
Address: 200.100.50.30		# IP 변경 확인
> mail.nc.co.kr			# 입력

Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   mail.nc.co.kr	
Address: 200.100.50.40		# IP 변경 확인
> web.nc.co.kr			# 입력

Server:         127.0.0.1
Address:        127.0.0.1#53

web.nc.co.kr    canonical name = www.nc.co.kr.
Name:   www.nc.co.kr
Address: 200.100.50.20		# IP 변경 확인

 

 

HOST -> IP 를 확인해보자

 

$nslookup
> server 127.0.0.1			# 자신의 루프백 주소 입력
Default server: 127.0.0.1
Address: 127.0.0.1#53
> ns.nc.co.kr				# 입력
;; communications error to 127.0.0.1#53: timed out
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   ns.nc.co.kr
Address: 200.100.50.10			# IP 변경 확인
> www.nc.co.kr				# 입력
;; communications error to 127.0.0.1#53: timed out
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   www.nc.co.kr		
Address: 200.100.50.20			# IP 변경 확인
> ftp.nc.co.kr				# 입력
;; communications error to 127.0.0.1#53: timed out
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   ftp.nc.co.kr
Address: 200.100.50.30			# IP 변경 확인
> mail.nc.co.kr				# 입력
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   mail.nc.co.kr			
Address: 200.100.50.40			# IP 변경 확인

++)

어제는  HOST -> IP 가 작동이 안되고 오류만 나왔다.

 

실습을 하면서 /etc/bind/named.conf.default-zones 에 기존에 추가했던 .zone과 .rev 파일을 삭제하지 못했기 때문이다.

 

삭제하고 수정한 파일명을 추가하니 nslookup으로 작동이 잘 되는것을 확인했다.

 

728x90