<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Wojno: Tag gentoo</title>
    <link>http://christopher.wojno.com/articles/tag/gentoo</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Exploration through Code</description>
    <item>
      <title>A DNS Server to Call My Own</title>
      <description>&lt;p&gt;I&amp;#8217;ve been itching to set up my own &lt;span class="caps"&gt;DNS&lt;/span&gt; server for a while now. Why? I&amp;#8217;ve come up with three reasons:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;Speed&lt;/li&gt;
		&lt;li&gt;Convenience&lt;/li&gt;
		&lt;li&gt;Security&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;The first one is pure fluff. My home network doesn&amp;#8217;t have nearly enough traffic to make it worth it. The second has merit. It would be nice if I could name machines on the network and have them resolve correctly. I could also use it to mask external addresses. So I could make stuff up and have it resolve locally. So I could make, oh, doubleclick.net resolve to 127.0.0.1. Now, no one on my network will get those advertisements anymore. Sure, I have it set up in the hosts file now, but I&amp;#8217;m like any other network administrator&amp;#8230; No, not lazy, but clever.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m working with Linux Gentoo 2.6.19 here on my local network. There is no chance that I will corrupt any legitimate records as nobody outside my network will be able to query my &lt;span class="caps"&gt;DNS&lt;/span&gt; server. I have my favorite editor: Vim at my side. Named (Bind) is currently at version &lt;span class="caps"&gt;BIND 9&lt;/span&gt;.4.1-P1.&lt;/p&gt;


	&lt;h1&gt;Install bind&lt;/h1&gt;


	&lt;p&gt;First, edit your /etc/portage/packages.use file. Add a line that says:&lt;/p&gt;


&lt;pre&gt;net-dns/bind -ipv6 -ldap postgres -ssl threads -mysql -bind-mysql -odbc&lt;/pre&gt;

	&lt;p&gt;This means: I don&amp;#8217;t want &lt;span class="caps"&gt;IPV6&lt;/span&gt; support (my router doesn&amp;#8217;t support it&amp;#8230; sadly). Don&amp;#8217;t use ldap. Add support for postgres (my favorite database). Don&amp;#8217;t include &lt;span class="caps"&gt;SSL&lt;/span&gt; support (I&amp;#8217;m assuming everyone trusts my server on the local network). Use threads to handle many requests simultaneously (I suppose I could turn this off as the server load will not be very large). Finally, don&amp;#8217;t include mysql bindings or &lt;span class="caps"&gt;ODBC&lt;/span&gt;.  Save that file.&lt;/p&gt;


	&lt;h2&gt;Emerge&lt;/h2&gt;


	&lt;p&gt;Using Gentoo&amp;#8217;s emerge system:&lt;/p&gt;


&lt;pre&gt;%emerge net-dns/bind&lt;/pre&gt;

	&lt;p&gt;It should install without any further intervention.&lt;/p&gt;


	&lt;h2&gt;Firewall (IPTables)&lt;/h2&gt;


	&lt;p&gt;I use the IPTables firewall to protect my server from local and foreign attacks. I like it because it gives me a lot of control over what goes in and out. I also don&amp;#8217;t like it because it is very complicated. If you have a firewall, you need to poke holes in it for port 53 in the following ways:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;Outgoing &lt;span class="caps"&gt;UDP&lt;/span&gt; connections TO port 53 from your server to the &lt;span class="caps"&gt;DNS&lt;/span&gt; servers you normally use&lt;/li&gt;
		&lt;li&gt;Incoming &lt;span class="caps"&gt;UDP&lt;/span&gt; connections TO your server on any port from the &lt;span class="caps"&gt;DNS&lt;/span&gt; servers you normally use for established &lt;span class="caps"&gt;UDP&lt;/span&gt; connections&lt;/li&gt;
		&lt;li&gt;Same as #1 with &lt;span class="caps"&gt;TCP&lt;/span&gt; connections&lt;/li&gt;
		&lt;li&gt;Same as #2 with &lt;span class="caps"&gt;TCP&lt;/span&gt; connections&lt;/li&gt;
		&lt;li&gt;Incoming &lt;span class="caps"&gt;UDP&lt;/span&gt; connections from the local network on port 53&lt;/li&gt;
		&lt;li&gt;Outgoing &lt;span class="caps"&gt;UDP&lt;/span&gt; connections to the local network on any port for established &lt;span class="caps"&gt;UDP&lt;/span&gt; connections&lt;/li&gt;
		&lt;li&gt;Same as #5 with &lt;span class="caps"&gt;TCP&lt;/span&gt; connections&lt;/li&gt;
		&lt;li&gt;Same as #6 with &lt;span class="caps"&gt;TCP&lt;/span&gt; connections&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;The above table is derrived from nixCraft&amp;#8217;s article: &lt;a href="http://www.cyberciti.biz/tips/linux-iptables-12-how-to-block-or-open-dnsbind-service-port-53.html"&gt;Linux Iptables block or open &lt;span class="caps"&gt;DNS&lt;/span&gt; / bind service port 53&lt;/a&gt; I added some modifications, however. Here&amp;#8217;s an example configuration:&lt;/p&gt;


	&lt;h3&gt;firewall.sh&lt;/h3&gt;


&lt;pre&gt;#!/bin/bash
IPTABLES='/sbin/iptables'
LOCALNET='--src-range 192.168.1.2-192.168.1.254'
INTIF1='eth0'
DNSSERVERS='a.b.c.d a.b.c.e' # 2 IP addresses for your ISP's DNS servers

# PREAMBLE
$IPTABLES -F #flush old rules
$IPTABLES -X #clear old chains
$IPTABLES -A INPUT -s 127.0.0.1 -j ACCEPT #always trust requests from the server
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #always trust active connections

# ... (other rules here)

# BIND/NAMED
# Outgoing Recursive Requests
for ip in $DNSSERVERS
do
        iptables -A OUTPUT -p udp -d $ip --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT
        iptables -A INPUT -i $INTIF1 -s $ip -p udp --dport 53 -m state --state ESTABLISHED -j ACCEPT
        iptables -A OUTPUT -p tcp -d $ip --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT
done
# incoming request configuration
# accept local queries
iptables -A INPUT -i $INTIF1 -m iprange $LOCALNET -p udp --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT

# block out all other Internet access
$IPTABLES -A INPUT -j DROP&lt;/pre&gt;

	&lt;p&gt;The for loop was nixCraft&amp;#8217;s idea. Very clever, however, instead of using the IP address of the server, I fell back to the interface card. Then, no matter what your IP address, you&amp;#8217;ll be able to control access to the &lt;span class="caps"&gt;DNS&lt;/span&gt; server.&lt;/p&gt;


	&lt;p&gt;Update your firewall rules by executing your firewall script.&lt;pre&gt;%./firewall.sh&lt;/pre&gt;&lt;/p&gt;


	&lt;h2&gt;IPTables example explained&lt;/h2&gt;


	&lt;p&gt;This example actually works as is. But if you&amp;#8217;re using &lt;span class="caps"&gt;SSH&lt;/span&gt; for access, you need to add in an &lt;span class="caps"&gt;SSH&lt;/span&gt; hole: &lt;pre&gt;$IPTABLES -A INPUT --protocol tcp --dport 22 -j ACCEPT&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;Now, the beginning of the script defines (in order) the script used to interpret the file (#!/bin/bash), the iptables program (stored as a variable for ease in name or location changes), &lt;span class="caps"&gt;LOCALNET&lt;/span&gt; (an IP address range specifying your local network, generally, 192.168.1.1 is the gate way and packets may appear to originate from your router if it&amp;#8217;s a piece of junk. 192.168.1.255 is broadcast, so no need to include that address either), &lt;span class="caps"&gt;INTIF1&lt;/span&gt; is the &lt;span class="caps"&gt;NIC&lt;/span&gt; card on my server. Use ifconfig to figure out what yours is (or iwconfig if you have a wireless server because you&amp;#8217;re crazy that way). Next, I define the &lt;span class="caps"&gt;DNS&lt;/span&gt; servers from my &lt;span class="caps"&gt;ISP&lt;/span&gt;. I didn&amp;#8217;t list them here because my &lt;span class="caps"&gt;ISP&lt;/span&gt; probably would not appreciate that. Use &lt;a href="http://www.opendns.com"&gt;OpenDNS&lt;/a&gt; if you&amp;#8217;re really in a bind (pun not intended).&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s skip to the #BIND/NAMED section. Here, I&amp;#8217;ve looped through each &lt;span class="caps"&gt;DNS&lt;/span&gt; server from my &lt;span class="caps"&gt;ISP&lt;/span&gt;. I&amp;#8217;ve opened up the &lt;span class="caps"&gt;UPD&lt;/span&gt; and &lt;span class="caps"&gt;TCP&lt;/span&gt; ports to allow recursive look ups generated by my local network traffic. After that loop, I open up port 53 locally. Notice I&amp;#8217;ve used the interface (-i #INTIF1). This restricts where requests may originate. You really only need this if you have more than one &lt;span class="caps"&gt;NIC&lt;/span&gt; card on the box.&lt;/p&gt;


	&lt;h1&gt;Configuration&lt;/h1&gt;


	&lt;p&gt;Before we can test, there&amp;#8217;s one more thing we need to do: configure named/bind to listen to us. By default, bind is configured to only accept connections made &lt;span class="caps"&gt;ON THE SERVER&lt;/span&gt;. That doesn&amp;#8217;t help when you want other computers on your network to be able to make requests. Open: /etc/bind/named.conf in vim. You need to change the listen-on directive to include your local network address. I just used &amp;#8220;any&amp;#8221; as my firewall will deny any other requests.&lt;/p&gt;


&lt;pre&gt;...
options {
  /* ... other configurations here ... */
  listen-on {any;};
};&lt;/pre&gt;

	&lt;p&gt;Make sure you include the semi-colon after &amp;#8220;any&amp;#8221; or you&amp;#8217;ll get named complaining at you. Restart named (bind). &lt;pre&gt;%sudo /etc/init.d/named restart&lt;/pre&gt;&lt;/p&gt;


	&lt;h1&gt;Testing your server&lt;/h1&gt;


	&lt;p&gt;You should now be able to test &lt;span class="caps"&gt;DNS&lt;/span&gt; lookups from another machine. From you local (non-DNS server), type:&lt;pre&gt;%dig @DNSSERVER_IP_ADDRESS www.google.com&lt;/pre&gt; Replace &lt;span class="caps"&gt;DNSSERVER&lt;/span&gt;_IP_ADDRESS with the IP address of the server on which you just configured and installed bind/named. You should get something similar:&lt;pre&gt;; &amp;lt;&amp;lt;&amp;gt;&amp;gt; DiG 9.4.1-P1 &amp;lt;&amp;lt;&amp;gt;&amp;gt; @DNSSERVER_IP_ADDRESS www.google.com -t A
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; -&amp;gt;&amp;gt;HEADER&amp;lt;&amp;lt;- opcode: QUERY, status: NOERROR, id: 13465
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 7, ADDITIONAL: 0

;; QUESTION SECTION:
;www.google.com.            IN    A

;; ANSWER SECTION:
www.google.com.        604748    IN    CNAME    www.l.google.com.
www.l.google.com.    249    IN    A    64.233.169.104
www.l.google.com.    249    IN    A    64.233.169.103
www.l.google.com.    249    IN    A    64.233.169.147
www.l.google.com.    249    IN    A    64.233.169.99&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;If you&amp;#8217;re using Windows, you need to use nslookup (command line application). The syntax is slightly different. If you&amp;#8217;re using Gentoo, you need to first install the bind tools (emerge net-dns/bind-tools).&lt;/p&gt;


	&lt;p&gt;That&amp;#8217;s it! You now have a working local &lt;span class="caps"&gt;DNS&lt;/span&gt; server merely acting as a cache.&lt;/p&gt;</description>
      <pubDate>Sun, 30 Dec 2007 15:56:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:7ea47163-4315-4fa6-9f05-bbb30b030215</guid>
      <author>Christopher Wojno</author>
      <link>http://christopher.wojno.com/articles/2007/12/30/a-dns-server-to-call-my-own</link>
      <category>How-Tos</category>
      <category>bind</category>
      <category>dns</category>
      <category>named</category>
      <category>setup</category>
      <category>cache</category>
      <category>install</category>
      <category>configure</category>
      <category>gentoo</category>
      <category>linux</category>
      <category>server</category>
    </item>
  </channel>
</rss>
