Twisted makes it easy to build a DNS server, and until I knew that, I was building a paradise with / etc / hosts. I searched to get rid of the bloated / etc / hosts. Speaking of which, if you use Twisted, why not use that library, Twisted, which is full of various things on that network? I thought. So, when I looked it up, I found that with the twistd command (Python script) that comes in after installing Twisted, if you pass dns as an argument, all you have to do is write a simple zone file.
So I will write a memo about building a DNS server (easy) using Twisted.
--Prepare the environment for Python 2.7.3 --In Version 12.3.0, Library is also supported in Python 3.3 (except for some), but it is in operation From the aspect, I decided to select the latest stable version of 2.x series.
--Install Twisted as a matter of course. --I don't want to pollute the environment, so I prepare the operating environment with easy_install & pip & virtualenv.
--Prepare the zone file --In the format of pyzone, write records such as NS and CNAME in the list and prepare a configuration file. --It seems that BIND9 format is also supported, but pyzone format is easier.
--Start the server with the twistd command --I don't know if the expression "start the server" is correct, but pass the parameter dns to start it.
> easy_install-2.7 pip
> pip install virtualenv
> rehash
> mkdir -p ~/TwisedDNS/
> cd ~/TwistedDNS
> virtualenv python
> source python/bin/activate
(python)> rehash
(python)> pip install twisted
--Unko.zone.
unko.zone
zone = [
SOA(
'unko.com',
mname = 'ns1.unko.com',
serial = 2013011901,
refresh = '1H',
retry = '1H',
expire = '1H',
minimum = '1H'
),
# NS Record
NS('unko.com', 'ns1.unko.com'),
# A Record
A('unko.com', '10.0.0.10'),
A('blog.unko.com', '10.0.1.10'),
# CNAME Record
CNAME('www.unko.com', 'unko.com')
]
--Start the server using the twistd command.
> sudo twistd -n dns --recursive --cache --pyzone unko.zone
# -n is--With the nodaemon option, if this is attached, it will start as a process on the shell instead of the daemon. This time it's a test, so I've enabled it.
# --It feels like recursively querying an external name server with recurcive.
# --cache enables the domain cache.
# --pyzone is used to specify the zone file.
--Use the dig command to make an inquiry.
> dig @127.0.0.1 unko.com
; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 unko.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47522
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;unko.com.INA
;; ANSWER SECTION:
unko.com.3600INA10.0.0.10
> dig @127.0.0.1 blog.unko.com
; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 blog.unko.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6450
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;blog.unko.com.INA
;; ANSWER SECTION:
blog.unko.com.3600INA10.0.1.10
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sat Jan 19 15:48:31 2013
;; MSG SIZE rcvd: 47
> dig @127.0.0.1 www.unko.com
; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 www.unko.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10221
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.unko.com.INA
;; ANSWER SECTION:
www.unko.com.3600INCNAMEunko.com.
unko.com.3600INA10.0.0.10
;; Query time: 3 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sat Jan 19 15:48:36 2013
;; MSG SIZE rcvd: 60
Also try to contact unmanaged domains.
> dig @127.0.0.1 yahoo.co.jp
; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 yahoo.co.jp
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37453
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;yahoo.co.jp.INA
;; ANSWER SECTION:
yahoo.co.jp.224INA124.83.187.140
yahoo.co.jp.224INA203.216.243.240
;; Query time: 13 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sat Jan 19 15:54:20 2013
;; MSG SIZE rcvd: 61
I was able to make a perfect inquiry. If you want to run it as a daemon process, you can take the -n (--nodaemon) option. I think that the twistd.pid file and the twistd.log file are created in the directory where the command is started, so you can use them as appropriate. .. unbound, nsd, and that's [BIND](https://www.isc. If you want to use it in a specific service, that is, in the internal network without using org / software / bind), this is enough.
that's all.
Creating and working with a names (DNS) server
Recommended Posts