среда, 16 мая 2012 г.

Кэширующий DNS-server на Debian

Предположим есть машина со свежеустановленным Debian. Доустанавливаем необходимые пакеты:

# aptitude install bind9 vlan

Далее настраиваем сеть. В моей ситуации имеется два VLAN'а: один смотрит во внешку (VLANID 20), второй во внутреннюю сервисную сеть (VLANID 10):

# cat /etc/network/interfaces

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 0.0.0.0

# Managment
auto vlan10
iface vlan10 inet static
  address 10.10.0.10
  netmask 255.255.0.0
  vlan_raw_device eth0

# Link to world
auto vlan20
iface vlan20 inet static
  address x.x.x.x
  netmask 255.255.255.252
  gateway x.x.x.y
  dns-nameservers 127.0.0.1
  vlan_raw_device eth0


Приступаем к настройке bind'а:

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

acl ournets {
  127.0.0.1;
  x.x.x.x/24;
  y.y.y.y/24;
  z.z.z.z/24;
};

options {
  directory "/var/cache/bind";

  dnssec-validation auto;

  auth-nxdomain no; # conform to RFC1035
  listen-on-v6 { none; };

  listen-on {127.0.0.1; x.x.x.x;};
  allow-query {"ournets";};
};


В acl ournets {} перечислены сети, которым разрешено делать запросы - опция allow-query {"ournets";}. В listen-on {} задаются интерфейсы, на которых нужно слушать запросы. Также проверим что у нас прописаны корневые серверы в файле named.conf.default-zones:

zone "." {
  type hint;
  file "/etc/bind/db.root";
};


Перечитаем настройки:

# /etc/init.d/bind9 reload

Проверяем:

# nslookup ya.ru localhost
Server: localhost
Address: 127.0.0.1#53

Non-authoritative answer:
Name: ya.ru
Address: 87.250.251.3
Name: ya.ru
Address: 93.158.134.3
Name: ya.ru
Address: 93.158.134.203
Name: ya.ru
Address: 213.180.193.3
Name: ya.ru
Address: 213.180.204.3
Name: ya.ru
Address: 77.88.21.3
Name: ya.ru
Address: 87.250.250.3
Name: ya.ru
Address: 87.250.250.203


Осталось настроить iptables. Я просто разрешаю domain запросы извне и ssh из сервисной сети. Для удобства правила пропишем в init-скрипте:

# cat /etc/init.d/iptables
PATH='/sbin'

case "$1" in
start)
echo "Starting iptables"

iptables -F

iptables -P FORWARD DROP
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT

iptables -A INPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -p icmp -j ACCEPT

iptables -A INPUT -i vlan10 -p tcp --dport 22 -j ACCEPT

iptables -A INPUT -i vlan20 -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -i vlan20 -p udp --dport 53 -j ACCEPT
;;

stop)
echo "Stopping iptables"

iptables -F

iptables -P FORWARD ACCEPT
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
;;

*)
echo "Usage: /etc/init.d/iptables {start|stop}"
exit 1
;;
esac

exit 0


Делаем его исполняемым и запускаем:

# chmod +x /etc/init.d/iptables
# update-rc.d iptables defaults
#
# /etc/init.d/iptables start


Все.

Источники:
http://irternus.blogspot.com/2012/01/bind9-dns.html
http://www.linuxgeek.ru/2011/01/iptables-debian.html

Update: В последних версиях Debian лучше использовать пакет iptables-persistent вместо самописного скрипта

Комментариев нет:

Отправить комментарий