# setting up /etc/hosts

## matttah

Hi guys-

How do i setup /etc/hosts so that anytime i go to *.localhost it goes to the 127.0.0.1?  I am trying to setup wildcard subdomains for my localcomputer for a project.

Daum

----------

## nixnut

Moved from Installing Gentoo to Networking & Security.

networking question, so moved here.

----------

## MarioCorleone

like 

```
~ # ping localhost

PING localhost (127.0.0.1) 56(84) bytes of data.

64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.060 ms

```

???

----------

## richard.scott

Hi matttah

I think the only way I can think of to do this would be to run a DNS (aka net-dns/bind) on the host and then change the /etc/resolv.conf to point to  that nameserver.

It's not as hard as you'd think either!

Rich.

----------

## matttah

Alright looking at that.  

Daum

----------

## matttah

Ok installed bind, setup the following:

named.conf 

```

zone "testing" IN {

        type master;

        file "pri/testing.zone";

        allow-update { none; };

};

```

pri/testing.zone

```

$TTL 1W

@       IN      SOA     ns.testing. root.testing.  (

                                      2002081601 ; Serial

                                      28800      ; Refresh

                                      14400      ; Retry

                                      604800     ; Expire - 1 week

                                      86400 )    ; Minimum

@               IN      NS      ns

ns              IN      A       127.0.0.1

ns              IN      AAAA    ::1

```

then also added nameserver 127.0.0.1 to my resolv.conf, restarted the name service, and no luck.  What am i doing wrong?

Daum

----------

## richard.scott

Your missing any A records, which is an entry for a name to and IP.

You can test this if you install bind-tools and use the "dig" command to do the following:

```
dig @127.0.0.1 ns.testing
```

You'll need to add in the following at the end of this file, and restart the daemon:

```
*     IN     A     127.0.0.1
```

You should then be able to do this:

```
dig @127.0.0.1 anything.testing
```

If that works then change the nameserver lines in your /etc/resolv.conf to point to 127.0.0.1

----------

## matttah

```

dig @127.0.0.1 ns.testing

; <<>> DiG 9.4.1-P1 <<>> @127.0.0.1 ns.testing

; (1 server found)

;; global options:  printcmd

;; connection timed out; no servers could be reached

```

is what happens when i run that command.

What do i add, sorry I don't see.

Daum

----------

## richard.scott

I guess you've done this before using the dig command:

```
/etc/init.d/named start
```

And then check that its running on port 53:

```
# netstat -nat | grep LISTEN | grep 53 | grep 127

tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN

tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN

```

The service on port 953 is a control port for the rndc command that comes with bind.

If you see the above, you should be able to do this:

```
# dig @127.0.0.1 ns.localhost

; <<>> DiG 9.4.1-P1 <<>> @127.0.0.1 ns.localhost

; (1 server found)

;; global options:  printcmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31065

;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:

;ns.localhost.                  IN      A

;; ANSWER SECTION:

ns.localhost.           604800  IN      A       127.0.0.1

;; AUTHORITY SECTION:

localhost.              604800  IN      NS      ns.localhost.

;; ADDITIONAL SECTION:

ns.localhost.           604800  IN      AAAA    ::1

;; Query time: 1 msec

;; SERVER: 127.0.0.1#53(127.0.0.1)

;; WHEN: Thu Mar 13 11:14:42 2008

;; MSG SIZE  rcvd: 88
```

Let me know how you get on??

----------

## matttah

It seem sthat works now.  So does the dig for the anything.testing.  Now the only problme is getting my browser/apache to allow me to view it.  When i do hi.testing:PORT (port is what port i'm running apache to) I get:

```

An error occurred while loading http://hi.testing:9992:

Unknown host hi.testing

```

I also updated my resolv.conf to have an additional nameserver 127.0.0.1

Thanks,

Daum

----------

## richard.scott

 *matttah wrote:*   

> It seem sthat works now.  So does the dig for the anything.testing.  Now the only problme is getting my browser/apache to allow me to view it.  When i do hi.testing:PORT (port is what port i'm running apache to) I get:
> 
> ```
> 
> An error occurred while loading http://hi.testing:9992:
> ...

 

Are you browsing from the same machine that you have been running "dig" and "bind" on?

----------

## matttah

yeah, from the same machine.

Matt

----------

## richard.scott

I'm guessing your browsing from a desktop?

Can you ping hi.testing from a shell?

There is a chance something may have cached the settings from /etc/resolv.conf when starting.... you could try restarting X or the browser?

----------

## matttah

Hm it seems i can't ping hi.testing, although I did notice the bind service wasn't running so i started /etc/init.d/named and it still isn't working.

Daum

----------

## richard.scott

Are there any "named" processes running? If not then there is no DNS server listening for your request so nothing will be pingable.

Have a look in your logs for output from named to see why its not running.

This may help:

http://gentoo-wiki.com/HOWTO_Setup_a_DNS_Server_with_BIND

----------

## matttah

it is running, I just forgot to add it to default in start up so when i rebooted it didn't.

10776 ?        Ss     0:00 /usr/sbin/named -u named -n 1

Is how it is running.

Daum

----------

## bunder

is your resolv.conf pointing to the right dns server?

cheers

----------

## matttah

```
# Generated by dhcpcd for interface eth0

nameserver 192.168.1.1

nameserver 127.0.0.1

```

Is what i have for it.

Daum

----------

## richard.scott

I'm assuming here that your using DHCP for your IP address??

If so, you'll have something like this in your /etc/conf.d/net

```
config_eth0=( "dhcp" )
```

To stop dhcp over writing your /etc/resolv.conf you'll need to add this to /etc/conf.d/net

```
dhcpcd_eth0="-R"
```

and then change the /etc/resolv.conf to be like this:

```
nameserver 127.0.0.1

nameserver 127.0.0.1

```

It's worth having the same entry in there twice just incase the first one times out for any reason (DNS can sometimes do that)

This should make the machine use your local DNS server for looking up names so its best to check once you've set this up that things like www.google.co.uk and www.bbc.co.uk are still available.

----------

## matttah

Hm, I did what you said, restarted eth0 interface, and still no luck in pinging testing.

Daum

----------

## richard.scott

Please can you see what the output from these two netstat commands is:

```
# netstat -nau | grep 53

udp        0      0 127.0.0.1:53            0.0.0.0:*

rjs bin # netstat -nat | grep 53

tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN
```

Also, can you past up your /etc/bind/named.conf file?

----------

## matttah

```

netstat -nau | grep 53

udp        0      0 127.0.0.1:53            0.0.0.0:*

netstat -nat|grep 53

tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN

tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN

```

named.conf

```

options {

        directory "/var/bind";

        // uncomment the following lines to turn on DNS forwarding,

        // and change the forwarding ip address(es) :

        //forward first;

        //forwarders {

        //      123.123.123.123;

        //      123.123.123.123;

        //};

        listen-on-v6 { none; };

        listen-on { 127.0.0.1; };

        // to allow only specific hosts to use the DNS server:

        //allow-query {

        //      127.0.0.1;

        //};

        // if you have problems and are behind a firewall:

        //query-source address * port 53;

        pid-file "/var/run/named/named.pid";

};

// Briefly, a zone which has been declared delegation-only will be effectively

// limited to containing NS RRs for subdomains, but no actual data beyond its

// own apex (for example, its SOA RR and apex NS RRset). This can be used to

// filter out "wildcard" or "synthesized" data from NAT boxes or from

// authoritative name servers whose undelegated (in-zone) data is of no

// interest.

// See http://www.isc.org/products/BIND/delegation-only.html for more info

//zone "COM" { type delegation-only; };

//zone "NET" { type delegation-only; };

zone "." IN {

        type hint;

        file "named.ca";

};

zone "localhost" IN {

        type master;

        file "pri/localhost.zone";

        allow-update { none; };

        notify no;

};

zone "testing" IN {

        type master;

        file "pri/testing.zone";

        allow-update { none; };

};

zone "127.in-addr.arpa" IN {

        type master;

        file "pri/127.zone";

        allow-update { none; };

        notify no;

};

```

Daum

----------

## richard.scott

ok, could you post your "testing.zone" files too?

I have tested the following on my server and it works for me:

```
 # cat localhost.zone

$TTL 1W

@       IN      SOA     ns.localhost. root.localhost.  (

                                      2002081601 ; Serial

                                      28800      ; Refresh

                                      14400      ; Retry

                                      604800     ; Expire - 1 week

                                      86400 )    ; Minimum

@               IN      NS      ns

ns              IN      A       127.0.0.1
```

```
 # dig @127.0.0.1 ns.localhost

; <<>> DiG 9.4.1-P1 <<>> @127.0.0.1 ns.localhost

; (1 server found)

;; global options:  printcmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25512

;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:

;ns.localhost.                  IN      A

;; ANSWER SECTION:

ns.localhost.           604800  IN      A       127.0.0.1

;; AUTHORITY SECTION:

localhost.              604800  IN      NS      ns.localhost.

;; ADDITIONAL SECTION:

ns.localhost.           604800  IN      AAAA    ::1

;; Query time: 1 msec

;; SERVER: 127.0.0.1#53(127.0.0.1)

;; WHEN: Thu Mar 27 19:50:41 2008

;; MSG SIZE  rcvd: 88

```

----------

## matttah

testing.zone

```

$TTL 1W

@       IN      SOA     ns.testing. root.testing.  (

                                      2002081601 ; Serial

                                      28800      ; Refresh

                                      14400      ; Retry

                                      604800     ; Expire - 1 week

                                      86400 )    ; Minimum

@               IN      NS      ns

ns              IN      A       127.0.0.1

ns              IN      AAAA    ::1

```

Daum

----------

## richard.scott

what's the output from this:

```
#dig @127.0.0.1 ns.localhost
```

or this

```
dig @127.0.0.1 ns.testing
```

----------

## matttah

```

dig @127.0.0.1 ns.testing

; <<>> DiG 9.4.1-P1 <<>> @127.0.0.1 ns.testing

; (1 server found)

;; global options:  printcmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37866

;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:

;ns.testing.                    IN      A

;; ANSWER SECTION:

ns.testing.             604800  IN      A       127.0.0.1

;; AUTHORITY SECTION:

testing.                604800  IN      NS      ns.testing.

;; ADDITIONAL SECTION:

ns.testing.             604800  IN      AAAA    ::1

;; Query time: 1 msec

;; SERVER: 127.0.0.1#53(127.0.0.1)

;; WHEN: Thu Mar 27 16:38:57 2008

;; MSG SIZE  rcvd: 86

```

```

dig @127.0.0.1 ns.localhost

; <<>> DiG 9.4.1-P1 <<>> @127.0.0.1 ns.localhost

; (1 server found)

;; global options:  printcmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 16625

;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:

;ns.localhost.                  IN      A

;; ANSWER SECTION:

ns.localhost.           604800  IN      A       127.0.0.1

;; AUTHORITY SECTION:

localhost.              604800  IN      NS      ns.localhost.

;; ADDITIONAL SECTION:

ns.localhost.           604800  IN      AAAA    ::1

;; Query time: 1 msec

;; SERVER: 127.0.0.1#53(127.0.0.1)

;; WHEN: Thu Mar 27 16:40:36 2008

;; MSG SIZE  rcvd: 88

```

Daum

----------

## richard.scott

Ok, the good news there is that your DNS server looks to be working fine   :Very Happy: 

what happens when you do the following:

```
#ping ns.localhost
```

You should see it as pingable if /etc/resolv.conf is ok and /etc/nsswitch.conf hasn't been fiddled with

Check that the "host:" line in /etc/nsswitch.conf should be like this:

```
cat /etc/nsswitch.conf | grep host

hosts:       files dns
```

----------

## matttah

Hm the ping worked the nsswitch.conf is fine.  

```
ping ns.localhost

PING ns.localhost (127.0.0.1) 56(84) bytes of data.

64 bytes from daum.daums (127.0.0.1): icmp_seq=1 ttl=64 time=0.023 ms

```

Still ping testing is getting unknown host.

Daum

----------

## richard.scott

 *matttah wrote:*   

> Hm the ping worked the nsswitch.conf is fine.  
> 
> ```
> ping ns.localhost
> 
> ...

 

That ping is working fine!

At which part of it do you think your getting "unknown host"?

----------

## matttah

When I try to ping hi.testing for example I get unknown host.  My main goal here is to be able to address my local computer without using ip.

Daum

----------

## richard.scott

 *matttah wrote:*   

> When I try to ping hi.testing for example I get unknown host.  My main goal here is to be able to address my local computer without using ip.
> 
> Daum

 

Ah, that'll be (just looked back at your previous post) that you don't have the entry in your 'testing.zone' file.

Add something like this into it:

```
hi              IN      A       127.0.0.1 
```

where "hi" is the name of the host you want to add.....or this for a wildcard entry:

```
*              IN      A       127.0.0.1 
```

you'll need to also need to increment the "serial" number at the top of the file.

This is usually in date format with two digits to represent a daily update. So for a date of 01/04/2008 you could use this number: 2008040101. This is a UK format date and serial code, but as long as its a larger number than the current one it should be ok.

Once you've saved this you'll need to reload the zone file into memory with one of the following commands:

```
rndc reload testing
```

or to fully restart the whole nameserver

```
rndc reload
```

or

```
/etc/init.d/named restart
```

Once you've done this it should work to ping:

abc.testing

hi.testing

apache.testing

mysql.testing

etc...

----------

## matttah

Yes!

Thanks!

Daum

----------

