Red Antigua Logo
Yet another piece of web.
Search this site (by Google)
Tools    (top)
Your IP
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
MSYS2 - UNIX environment for MS Windows 32/64 bits
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

DNS setup
DNS setup
Using /etc/hosts
Using BIND as a caching-only DNS server

This is not a complete DNS setup tutorial.
It covers how to how hostnames can be resolved using /etc/hosts instead of running a DNS server such as BIND.
It also covers how to set up a caching-only DNS server, to fasten up your DNS queries.


Using /etc/hosts (top)

Let's say you have setup Apache (or any web server) on the local host, and want to access a document:
http://0/myfirsttest.html
http://0.0.0.0/myfirsttest.html
http://127.0.0.1/myfirsttest.html
http://localhost/myfirsttest.html
0.0.0.0 and 0 refer to the local host, and 127.0.0.1 refers to the loopback interface.
The difference between 0.0.0.0 and 127.0.0.1 is that the former refers to the local host's physical interface(s), while the latter refers to a software interface.
So on a computer with no NIC, only 127.0.0.1 will work.

From the man page of the loopback interface, man lo, we can read:
"The loop interface is a software loopback mechanism which may be used for performance analysis, software testing, and/or local communication."
So, in the end, 127.0.0.1 also refers to the local host.
The loop interface acts as any network interface, which means you can perform the following operations on it, for example:
ifconfig lo0 down  (you can't ping 127.0.0.1 now)
ifconfig lo0 up
ifconfig lo0 1.2.3.4  (your loopback IP changed to 1.2.3.4, try to ping 1.2.3.4)
ifconfig lo0 127.0.0.1
which brings down, up, sets, and restores lo0's IP, respectively.
Conclusion: It isn't surprising that you can see a local host's web page using the IPs in the examples above.
But what about localhost?
Why does localhost resolve to a local IP?
No DNS server is configured to resolve localhost to your computer's IP (you may even be disconnected from the Net, unable to query a DNS server).

Let's have a look at the hosts line in /etc/nsswitch.conf:
hosts: files dns
This means that address lookups are done first trying to query local files, and only if that fails trying to query a DNS server.
Check man nsswitch.conf for details.

nsswitch.conf generates host.conf automatically:
# Auto-generated from nsswitch.conf
hosts
dns
Here we can see explicitly that address lookups query the /etc/hosts file.

Now it's time to modify /etc/hosts.
The format for each line is row is
IP                      hostname [alias ...]
/etc/hosts:
::1                     localhost. localhost www my.little.computer bengt.dennis.com
127.0.0.1               localhost. localhost www my.little.computer bengt.dennis.com
Note that there is a '.' (dot) after the hostname 'localhost.'.
(This is to make some versions of sendmail happy.)

Now you can access your web documents these ways:
http://localhost/myfirsttest.html
http://www/myfirsttest.html
http://my.little.computer/myfirsttest.html
http://bengt.dennis.com/myfirsttest.html
because they all resolve to the loopback interface, IP 127.0.0.1 (IPv6 address ::1).

To separate the web contents between different domains resolving to the same server, have a look at this Apache virtual hosts example.

So now we know about three different sources that affect the access to localhost.
  • lo0
  • /etc/nsswitch.conf
  • /etc/hosts
Here are three ways to disable access to localhost:
  • lo0 down
  • Comment out the 'files' line in /etc/nsswitch.conf
  • Removing the word 'localhost' line in /etc/hosts


Using BIND as a caching-only DNS server (top)

This configuration was tested on FreeBSD 7.2.

Files to be modified:
  • /etc/rc.conf
  • /etc/resolv.conf
  • /etc/hosts
  • /etc/namedb/named.conf

/etc/rc.conf should include these lines:
hostname="localhost"
named_enable="YES"

/etc/resolv.conf should start with the loopback IP, then the ISP's name servers:
nameserver 127.0.0.1
nameserver 192.168.1.1
nameserver 192.168.1.2
The ISP's nameservers will serve as backup servers in case the local DNS will not respond.
They will also use as forwarders (see below).

/etc/hosts should include these lines (you can also use the example above):
::1                     localhost. localhost
127.0.0.1               localhost. localhost
Check that localhost resolves correctly:
ping localhost
/etc/namedb/named.conf is the only BIND file which has to be modified:
    // uncomment this line to not resolve any names in our server
    forward only;

    // uncomment these lines to point to the ISP's DNS servers
    forwarders
    {
        192.168.1.1; 192.168.1.2;
    };

    // add this line to configure a caching only nameserver
    zone "0.0.127.in-addr.arpa" { type master; file "master/localhost-reverse.db"; };
We don't have to touch /etc/namedb/master/localhost-reverse.db.
Just assure it looks like this:
$TTL 3h
@ SOA localhost. nobody.localhost. 42 1d 12h 1w 3h
        ; Serial, Refresh, Retry, Expire, Neg. cache TTL

        NS      localhost.

1.0.0   PTR     localhost.

1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 PTR localhost.

That's it for configuration.

Now start named:
/etc/rc.d/named start
Check that the named process is running correctly:
ps axww|grep named
3867  ??  Ss     0:00.04 /usr/sbin/named -t /var/named -u bind
tail /var/log/messages
May 30 13:34:12 localhost named[4223]: starting BIND 9.4.3-P2 -t /var/named -u bind
May 30 13:34:12 localhost named[4223]: command channel listening on 127.0.0.1#953
May 30 13:34:12 localhost named[4223]: command channel listening on ::1#953
May 30 13:34:12 localhost named[4223]: the working directory is not writable
May 30 13:34:12 localhost named[4223]: running
The message the working directory is not writable seems to be ignorable, according to this forum discussion.


You can test the caching-only server like this:
dig @127.0.0.1 google.com
The first time, there may be a short delay during the query before the response.
The second time, the query should respond must faster.
That means that you queries are caching properly.

You can use any hostname or alias configured in /etc/hosts for your queries:
dig @localhost google.com
dig @www google.com
dig @my.little.computer google.com
dig @bengt.dennis.com google.com

Read more
Domain Name System (DNS) - for FreeBSD
Official BIND documentation
DNS HOWTO: A caching only name server

Last modified: Wed Sep 7 12:35:39 EDT 2016