Diferencia entre revisiones de «Exemple complet de DNS»

De Jose Castillo Aliaga
Ir a la navegación Ir a la búsqueda
(Página creada con «Imaginem que tenim una xarxa com aquesta: --------- | DNS2 | --------- \...»)
 
Sin resumen de edición
Línea 12: Línea 12:


L'exemple es provarà en contenidors [[LXD]]. Les IP són les següents:
L'exemple es provarà en contenidors [[LXD]]. Les IP són les següents:
+----------+---------+--------------------------------+------+------------+-----------+
|  NOMBRE  | ESTADO  |              IPV4              | IPV6 |    TIPO    | SNAPSHOTS |
+----------+---------+--------------------------------+------+------------+-----------+
| dns1    | RUNNING | 10.20.30.100 (eth1)            |      | PERSISTENT | 0        |
+----------+---------+--------------------------------+------+------------+-----------+
| dns2    | RUNNING | 10.20.30.200 (eth1)            |      | PERSISTENT | 0        |
+----------+---------+--------------------------------+------+------------+-----------+
| firewall | RUNNING | 10.150.173.32 (eth0)          |      | PERSISTENT | 0        |
|          |        | 10.20.30.254 (eth1)            |      |            |          |
+----------+---------+--------------------------------+------+------------+-----------+
| pc1      | RUNNING | 10.20.30.1 (eth1)              |      | PERSISTENT | 0        |
+----------+---------+--------------------------------+------+------------+-----------+
| pc2      | RUNNING | 10.20.30.2 (eth1)              |      | PERSISTENT | 0        |
+----------+---------+--------------------------------+------+------------+-----------+


L'script utilitzat per a aquesta pràctica és aquest:
L'script utilitzat per a aquesta pràctica és aquest:
<pre class="code">
#!/bin/bash
#                  ---------
#                  |  DNS2 | 
#                  --------- 
#                          \            ----------
#                      /switch1/--------|Firewall|
#                        /    \        ----------
#                  ---------    -----------
#                  |DNS1  |    |  PC1-2  |
#                  ---------    -----------
#
lxc profile create xarxa
lxc profile set xarxa security.privileged true
lxc launch images:alpine/3.6/amd64 firewall
lxc launch images:alpine/3.6/amd64 pc1  --profile=xarxa
lxc launch images:alpine/3.6/amd64 pc2  --profile=xarxa
lxc launch ubuntu:16.04 dns1 --profile=xarxa
lxc launch ubuntu:16.04 dns2 --profile=xarxa
apt update
apt install bridge-utils
echo "#Configuracio" > /etc/network/interfaces.d/99-conf.cfg
for i in {1..2}
do
echo -e "\n\nauto switch$i\niface switch$i inet static\naddress 192.168.99.$i\nnetmask 255.255.255.0\nbridge-ports none" >> /etc/network/interfaces.d/99-conf.cfg
brctl addbr switch$i
ifup switch$i
done
lxc config device add firewall eth1 nic nictype=bridged parent=switch1 name=eth1
lxc config device add pc1 eth1 nic nictype=bridged parent=switch1 name=eth1
lxc config device add pc2 eth1 nic nictype=bridged parent=switch1 name=eth1
lxc config device add dns1 eth1 nic nictype=bridged parent=switch1 name=eth1
lxc config device add dns2 eth1 nic nictype=bridged parent=switch1 name=eth1
lxc exec pc1 -- sh -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.1\nnetmask 255.255.255.0\ngateway 10.20.30.254" >> /etc/network/interfaces'
lxc exec pc2 -- sh -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.2\nnetmask 255.255.255.0\ngateway 10.20.30.254" >> /etc/network/interfaces'
lxc exec firewall -- sh -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.254\nnetmask 255.255.255.0" >> /etc/network/interfaces'
lxc exec dns1 -- bash -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.100\nnetmask 255.255.255.0\ngateway 10.20.30.254" >> /etc/network/interfaces'
lxc exec dns2 -- bash -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.200\nnetmask 255.255.255.0\ngateway 10.20.30.254" >> /etc/network/interfaces'
lxc exec pc1 -- reboot
lxc exec pc2 -- reboot
lxc exec firewall -- reboot
lxc exec dns1 -- reboot
lxc exec dns2 -- reboot
sleep 10
lxc exec firewall -- apk update
lxc exec firewall -- apk add iptables
lxc exec firewall -- iptables -A FORWARD -j ACCEPT
lxc exec firewall -- iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
lxc exec firewall -- rc-update add iptables
lxc exec firewall -- /etc/init.d/iptables save
</pre>
== Instal·lació del servidor DNS ==
<pre class="code">
# apt update
# apt install bind9
</pre>
== Configuració del dns1 com a servidor primari i mestre ==
El primer que s'ha de fer és decidir el seu comportament com a servidor [[DNS]]:
<pre class="code">
# vi /etc/bind/named.conf.options
</pre>
Per defecte, quan s'instal·la, el servidor [[DNS]] '''Bind9''' és un servidor de noms d'una zona i no fa '''forwarding'''. Es pot descomentar e indicar la llista de servidors als que consultarà.
Per defecte és un servidor caché dels noms que resol. Per a resoldre noms, al no ser forwarder, consulta als servidors arrel.
En el nostre cas, hem tingut que comentar la línia del '''dnssec''', ja que no anem a configurar-lo per defecte.
Abans de fer res, es pot comprovar el seu correcte funcionament amb dig:
<pre class="code">
dig @127.0.0.1 www.upv.es
</pre>
Si la resposta és correcta, el servidor de noms sap consultar als servidors arrel i fer de caché.
'''La primera zona''':
Els nostre servidor '''dns1''' serà primari i mestre de la zona '''solar.com'''. Aquesta seria la configuració inicial de '''named.conf.local''':
<pre class="code">
zone "30.20.10.in-addr.arpa" {
  type master;
  file "/etc/bind/db.30.20.10";
  };
zone "solar.com." {
  type master;
  file "/etc/bind/db.solar.com";
  };
</pre>
I el contingut tant de '''db.solar.com''' com de '''db.30.20.10''':
<pre class="code">
$ttl 38400
solar.com.  IN    SOA  sol.solar.com. kepler.solar.com. (
              345325 ; Serie
              10800  ; Refresc
              3600  ; Retry
              604800 ; Expire
              38400 ) ; Negative ttl
solar.com.      IN NS    sol.solar.com.
solar.com. IN NS    galileo.solar.com.
sol.solar.com. IN A      10.20.30.100
galileo.solar.com. IN A      10.20.30.200
dns1.solar.com.         IN CNAME  sol.solar.com.
dns2.solar.com.         IN CNAME  galileo.solar.com.
mail.solar.com. IN MX 10  sol.solar.com.
www.solar.com IN CNAME  10.20.30.100
solar.com IN CNAME  10.20.30.100
mercurio IN CNAME  sol
venus IN CNAME  galileo
tierra IN A      10.20.30.1
luna IN CNAME  tierra
marte IN A   10.20.30.2
phobos IN CNAME  marte
$ttl 38400
@  IN    SOA  sol.solar.com. kepler.solar.com. (
              345325 ; Serie
              10800  ; Refresc
              3600  ; Retry
              604800 ; Expire
              38400 ) ; Negative ttl
@       IN NS    sol.solar.com.
@ IN NS    galileo.solar.com.
100 IN PTR   sol
200                    IN PTR    galileo.solar.com.
1 IN PTR   tierra
2 IN PTR    marte
</pre>
Com es pot veure, algunes línies tenen el FQDN i altre sols el nom relatiu de la màquina o la @ per indicar el domini.

Revisión del 01:09 15 nov 2017

Imaginem que tenim una xarxa com aquesta:

                 --------- 
                 |  DNS2 |  
                 ---------  
                         \             ----------
                      /switch1/--------|Firewall|----- INTERNET
                        /    \         ----------
                 ---------    -----------
                 |DNS1   |    |  PC1-2  |
                 ---------    -----------

L'exemple es provarà en contenidors LXD. Les IP són les següents:

+----------+---------+--------------------------------+------+------------+-----------+
|  NOMBRE  | ESTADO  |              IPV4              | IPV6 |    TIPO    | SNAPSHOTS |
+----------+---------+--------------------------------+------+------------+-----------+
| dns1     | RUNNING | 10.20.30.100 (eth1)            |      | PERSISTENT | 0         |
+----------+---------+--------------------------------+------+------------+-----------+
| dns2     | RUNNING | 10.20.30.200 (eth1)            |      | PERSISTENT | 0         |
+----------+---------+--------------------------------+------+------------+-----------+
| firewall | RUNNING | 10.150.173.32 (eth0)           |      | PERSISTENT | 0         |
|          |         | 10.20.30.254 (eth1)            |      |            |           |
+----------+---------+--------------------------------+------+------------+-----------+
| pc1      | RUNNING | 10.20.30.1 (eth1)              |      | PERSISTENT | 0         |
+----------+---------+--------------------------------+------+------------+-----------+
| pc2      | RUNNING | 10.20.30.2 (eth1)              |      | PERSISTENT | 0         |
+----------+---------+--------------------------------+------+------------+-----------+

L'script utilitzat per a aquesta pràctica és aquest:

#!/bin/bash
 
#                  --------- 
#                  |  DNS2 |  
#                  ---------  
#                          \             ----------
#                       /switch1/--------|Firewall|
#                         /    \         ----------
#                  ---------    -----------
#                  |DNS1   |    |  PC1-2  |
#                  ---------    -----------
#
 
lxc profile create xarxa
lxc profile set xarxa security.privileged true
 
 
lxc launch images:alpine/3.6/amd64 firewall 
lxc launch images:alpine/3.6/amd64 pc1  --profile=xarxa
lxc launch images:alpine/3.6/amd64 pc2  --profile=xarxa

lxc launch ubuntu:16.04 dns1 --profile=xarxa
lxc launch ubuntu:16.04 dns2 --profile=xarxa
 
apt update
apt install bridge-utils 
 
echo "#Configuracio" > /etc/network/interfaces.d/99-conf.cfg 
for i in {1..2}
do
	echo -e "\n\nauto switch$i\niface switch$i inet static\naddress 192.168.99.$i\nnetmask 255.255.255.0\nbridge-ports none" >> /etc/network/interfaces.d/99-conf.cfg
brctl addbr switch$i
ifup switch$i
done
 
lxc config device add firewall eth1 nic nictype=bridged parent=switch1 name=eth1
 
lxc config device add pc1 eth1 nic nictype=bridged parent=switch1 name=eth1
lxc config device add pc2 eth1 nic nictype=bridged parent=switch1 name=eth1
 
lxc config device add dns1 eth1 nic nictype=bridged parent=switch1 name=eth1
lxc config device add dns2 eth1 nic nictype=bridged parent=switch1 name=eth1

lxc exec pc1 -- sh -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.1\nnetmask 255.255.255.0\ngateway 10.20.30.254" >> /etc/network/interfaces'
lxc exec pc2 -- sh -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.2\nnetmask 255.255.255.0\ngateway 10.20.30.254" >> /etc/network/interfaces'
lxc exec firewall -- sh -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.254\nnetmask 255.255.255.0" >> /etc/network/interfaces'
lxc exec dns1 -- bash -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.100\nnetmask 255.255.255.0\ngateway 10.20.30.254" >> /etc/network/interfaces'
lxc exec dns2 -- bash -c 'echo -e "\n\nauto eth1\niface eth1 inet static\naddress 10.20.30.200\nnetmask 255.255.255.0\ngateway 10.20.30.254" >> /etc/network/interfaces'
lxc exec pc1 -- reboot 
lxc exec pc2 -- reboot 
lxc exec firewall -- reboot 
lxc exec dns1 -- reboot 
lxc exec dns2 -- reboot 
sleep 10
lxc exec firewall -- apk update
lxc exec firewall -- apk add iptables
lxc exec firewall -- iptables -A FORWARD -j ACCEPT
lxc exec firewall -- iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
lxc exec firewall -- rc-update add iptables 
lxc exec firewall -- /etc/init.d/iptables save

Instal·lació del servidor DNS

 # apt update
 # apt install bind9

Configuració del dns1 com a servidor primari i mestre

El primer que s'ha de fer és decidir el seu comportament com a servidor DNS:

 # vi /etc/bind/named.conf.options 

Per defecte, quan s'instal·la, el servidor DNS Bind9 és un servidor de noms d'una zona i no fa forwarding. Es pot descomentar e indicar la llista de servidors als que consultarà.

Per defecte és un servidor caché dels noms que resol. Per a resoldre noms, al no ser forwarder, consulta als servidors arrel.

En el nostre cas, hem tingut que comentar la línia del dnssec, ja que no anem a configurar-lo per defecte.

Abans de fer res, es pot comprovar el seu correcte funcionament amb dig:

dig @127.0.0.1 www.upv.es 

Si la resposta és correcta, el servidor de noms sap consultar als servidors arrel i fer de caché.

La primera zona:

Els nostre servidor dns1 serà primari i mestre de la zona solar.com. Aquesta seria la configuració inicial de named.conf.local:

zone "30.20.10.in-addr.arpa" {
  type master;
  file "/etc/bind/db.30.20.10";
  };
zone "solar.com." {
  type master;
  file "/etc/bind/db.solar.com";
  };

I el contingut tant de db.solar.com com de db.30.20.10:

$ttl 38400
solar.com.  IN     SOA   sol.solar.com. kepler.solar.com. (
              345325 ; Serie
              10800  ; Refresc
              3600   ; Retry
              604800 ; Expire
              38400 ) ; Negative ttl
solar.com.       	IN NS     sol.solar.com.
solar.com.		IN NS     galileo.solar.com.
sol.solar.com.		IN A      10.20.30.100
galileo.solar.com.	IN A      10.20.30.200
dns1.solar.com.	        IN CNAME  sol.solar.com.
dns2.solar.com.	        IN CNAME  galileo.solar.com.
mail.solar.com.		IN MX 10  sol.solar.com.
www.solar.com		IN CNAME  10.20.30.100
solar.com		IN CNAME  10.20.30.100
mercurio		IN CNAME  sol
venus			IN CNAME  galileo
tierra			IN A      10.20.30.1
luna			IN CNAME  tierra
marte			IN A	  10.20.30.2
phobos			IN CNAME  marte


$ttl 38400
@  IN     SOA   sol.solar.com. kepler.solar.com. (
              345325 ; Serie
              10800  ; Refresc
              3600   ; Retry
              604800 ; Expire
              38400 ) ; Negative ttl
@		       	IN NS     sol.solar.com.
@			IN NS     galileo.solar.com.
100			IN PTR	  sol
200                     IN PTR    galileo.solar.com.
1			IN PTR	  tierra
2			IN PTR    marte

Com es pot veure, algunes línies tenen el FQDN i altre sols el nom relatiu de la màquina o la @ per indicar el domini.