# what are hostnames, dnsdomainname and /etc/hosts

## macnix

Okay, I feel the need to flaunt my n00b status, so here goes...

While I'm starting to feel comfortable with Linux (having begun using it after enjoying the power of the CLI thanks to OS X), I am having a dastardly time figuring out hostnames. 

Now I know how to set them, as there are numerous references scattered about the web, and even a few Gentoo-specific docs here in these fora, but what I don't understand is what to set them to. I've had good luck figuring many other things out, but in this area I usually just follow the instruction without knowing why or how they work.

So could someone please give me a basic rundown of what hostnames are, what they do, and how they relate to domain names (in the sense of ICANN-registered identities).

Here's my specific situation, if you could use my details as examples...

I have a remote VDS server set up, connected to the Internet (using DHCP to get internet access from the host computer).

I will be running:

Apache2, serving multiple domains via virtual name hosting

an IMAP server, servicing these domains

Postfix, to deliver the mail

etc...

My server gets DNS info from my hosting provider. DNS services for my registered domains are provided by ZoneEdit.

Here is what I envision:

Let's give my VDS the name "maddog"

My IP is 299.123.45.67

Hosting the domains (that I have registered): 

example.com

example.net

exampleABC.com

(We can use example.com as the 'main' domain if needed)

So, with my current understanding, my hostname is 'maddog', my dnsdomainname is 'example.com' and my --fqdn is 'maddog.example.com'

How does 'hostname'  'dnsdomainname'  and '/etc/hosts' all play into this (btw, what would I want in /etc/hosts here?)

How does 'www.example.com' play into this? How does 'maddog.example.com' work; i.e. what is it, the true 'name' of my box on the net? 

How does Postfix deal with all this? It asks for $mydomain and $myhostname, but then do I just put 'example.com' and 'maddog' in there and use virtual mail delivery for the other domains?

I don't know, I just can't wrap my head around all this. I get confused with local hostnames, and 'hostnames' out there on the Internet...

Thanks for listening to my craziness, and I appreciate any help!

----------

## smart

hostname per se is the name you give your machine, i.e. maddog. It will be the name of your machine you'll define it like your mother did yours.

You'll put that name in /etc/hostname, and you will not add a trailing CR.

In /etc/dnsdomainname you will put your "main" domainname, which, if you got an "inside" network would be that one. It is the domainname your server usually does "it's own" operations with. If you're mounting NFS shares, a good start would be to use the same one as the NFS server.

In /etc/hosts, you will put your own machines name, you might want to add machines you want the little extra fast name resolutions and maybe you will add such for which you want extra reliability beyond the loss of a DNS server.

Most just go with the own name under the localhost adress

127.0.0.1 maddog.main.com maddog localhost

Every other hostname/FQDN is an alias. If you have, say three NICs in your machine, you will probably have three different IP addresses on them. With different names. For each. On each of these cards, might decide to add virtual addresses. Several. With different names. For each. But you also might just want to add more names. For the same addresses. Several. For each....

You'll simply add these out of your wish. You'll add them in a DNS server, simply having this name resolved to an address that points to one of the adresses that your machine hosts. As many as you like.

Since your descriptions points to a webby, to make them all available as separate entities under one and the same port of your webserver, you'll have to tell that webserver about them as virtual servers.

What other sofwtares want from you in what place... depends on them.

----------

## chrisis

 *macnix wrote:*   

> Okay, I feel the need to flaunt my n00b status, so here goes...
> 
> While I'm starting to feel comfortable with Linux (having begun using it after enjoying the power of the CLI thanks to OS X), I am having a dastardly time figuring out hostnames. 
> 
> Now I know how to set them, as there are numerous references scattered about the web, and even a few Gentoo-specific docs here in these fora, but what I don't understand is what to set them to. I've had good luck figuring many other things out, but in this area I usually just follow the instruction without knowing why or how they work.
> ...

 

OK, let's deal with that first.

As mentioned, the "hostname" is just a friendly name you give your computer.  In your case u've chosen "maddog" which is just fine.  Other computers on your network will have different hostnames.

For example, on my network I have four computers:

1. b3ast (my desktop)

2. beauty (my wife's desktop)

3. b0x (my server)

4. cmsm00th (my firewall)

They all have a single dnsdomainname -- example.com (of course its not that but ya get my meaning).

As you know, computers don't communicate using hostnames -- they communicate using IP addresses.  So really the hostnames are for my benefit, and whenever I connect to a computer, altho I specify a hostname, the actual communication occurs AFTER the hostnames are resolved to IP addresses.

So for eg an action like "browse shares on beauty" is understood by my computer as "browse shares on 192.168.1.10"

How does this resolution work?  This is where /etc/hosts comes in.  /etc/hosts basically does for a small network like mine the same job as a DNS server does for a large network, eg the internet.  Remember, all a DNS server does is resolve hostnames to IP addresses.  I may browse to "www.gentoo.org" but what actually happens is my request gets resolved to www.gentoo.org's IP address and that is how my computer "finds" the server.  Incidentally, in my example here, "www" would be the hostname and "gentoo.org" would be the domain name.  (More on that later).

But, I digress!  Let's get back to the /etc/hosts file.  If we look at /etc/hosts on my desktop computer (b3ast), these are the entries:

```

127.0.0.1      localhost

192.168.1.2  b3ast

192.168.1.3  b0x

192.168.1.10 beauty

192.168.1.1  cmsm00th

```

Now, whenever I refer to "b0x" the TCP/IP stack reads the "b0x" entry in my /etc/hosts file and resolves it to "192.168.1.3".  Incidentally, you can have more than one hostname refer to the same IP address.  So I could add an entry like

[code]

192.168.1.1 gw

[/gw]

Now, both "gw" and "cmsm00th" will resolve to the same IP address.

So to sum up, /etc/hosts is a file that contains hostnames and their corresponding IP addresses, and it is used to resolve the two -- but usually only on a small network.

Now, how do dns domain names fit in?  On my network I have "example.com" as the dnsdomainname.  That means each computers fully qualified dns name would be:

b3ast.example.com

beauty.example.com

b0x.example.com

cmsm00th.example.com

HOwever, I don't bother to specify this in my /etc/hosts file because, well, I just don't.  I could if I wanted to, but since I never "ping b3ast.example.com" cos it is faster for me to just "ping b3ast" I don't bother.  However the fully qualified dns name becomes important when people outside my network want to connect to a computer inside my network.

Let's say you are hosting a web server, www.example.com.  Internally, you'd probably just refer to the server using "www", if that's the friendly name you gave it, or you'd refer to it with "myname" if you have set the hostname to "myname".  Whatever the case, remember: the hostname, EVEN THE FULLY QUALIFIED DNS NAME, is all just there to make things more friendly to humans.  The trick is to have whatever the friendly name is to resolve to whatever IP address you want it to resolve to.

For example, you'd have a DNS provider resolve www.example.com to the IP address of your computer that is hosting web services.  You, however, might have called that computer "maddog" on your internal network, in which case your other computers would have an entry in their /etc/hosts files that would resolve "maddog" to whatever the IP address is too.  So externally ppl would browse to www.example.com, but internally, you would browse to maddog.

OK, now you need to read a DNS primer.  Here is one: How DNS works

So to summarize so far:

1.  hostnames are just friendly names for humans to use to refer to computers

2.  hostnames are always resolved to IP addresses

3.  you can have more than one hostname resolve to the same IP address

4.  DNS is setup to efficiently resolve FQDNs to IP Addresses.

Let's look at your specific examples now...

 *Quote:*   

> 
> 
> Here's my specific situation, if you could use my details as examples...
> 
> I have a remote VDS server set up, connected to the Internet (using DHCP to get internet access from the host computer).
> ...

 

Alarm bells!  DHCP is a protocol for dynamically assigning IP addresses.  If you are hosting a server, you really don't want to have a dynamically assigned IP address (because "dynamic" IP addresses change...).  WHat you need is for your computer that is serving stuff to the internet to have a fixed IP address.  Sort that out first.

In any case, in order for computers out there on the Internet to access that computer, you will have to setup port forwarding on your gateway computer.  More theory:

Every service on the internet is served via specific ports.  This is so that one server, with on IP address, can provide many services.  Here are some examples:

1. Port 80: http

2. Port 21: ftp

3. Port 25: smtp

In other words, a browser requests information from Port 80 of a server.  By default, web servers are set up to respond to requests on port 80.  My ftp client requests information from port 21.  And so on...

For your situation, what this means is that your gateway computer (the one with the ACTUAL connection to the internet) will have to FORWARD requests to port 80 to maddog.  You will also forward requests to port 143 (IMAP) to maddog.  Same goes for port 25 (postfix) etc.  This is based on what you said here:

 *Quote:*   

> 
> 
> I will be running:
> 
> Apache2, serving multiple domains via virtual name hosting
> ...

 

So what this means is that you would setup friendly names with ZoneEdit that look something like:

www.example.com

imap.example.com

smtp.example.com

etc

but they would ALL RESOLVE TO THE SAME IP ADDRESS.  Remember you can do that!  However, mail clients that refer to "imap.example.com" will send requests to that IP address via port 143, browsers via port 80 etc.  You see, the names really are just friendly names!  You COULD tell your mail client to use www.example.com, because it would still use port 143 by default anyway, but that defeats the purpose of having "friendly" names...  :Smile: 

 *Quote:*   

> 
> 
> Here is what I envision:
> 
> Let's give my VDS the name "maddog"
> ...

 

Yes, and also maddog.example.net as well as maddog.exampleABC.com.  Remember, you need your ZoneEdit setup to refer to COMPUTERS, not domains.  So you would have records setup for each maddog.*.  You'd use apache to distinguish between the different hostnames that ppl would enter into the browser, but more on that later.

 *Quote:*   

> 
> 
> How does 'hostname'  'dnsdomainname'  and '/etc/hosts' all play into this (btw, what would I want in /etc/hosts here?)
> 
> 

 

Hopefully you get this from my LONG explanations above!

 *Quote:*   

> 
> 
> How does 'www.example.com' play into this? How does 'maddog.example.com' work; i.e. what is it, the true 'name' of my box on the net? 
> 
> 

 

The "true" name, as you should understand by now, of your box on the net, is whatever you tell ZoneEdit is the true name!  Again: you can have multiple hostnames referring to one IP address.  So you would have an entry for www.example.com, even one for maddog.example.com etc.

As I mentioned, the particular hostname that a person uses when they use a web browser is then handled by apache.  I.E: I open a browser and type in "www.example.com".  The name is resolved to your IP address, and my browser sends a request to that IP address for a web page.  Apache is listening (on port 80), and reads the request.  Apache has been setup to serve a different page when requests are made of www.example.com, or www.example.net or whatever.  It uses a referer or something (can't remember the exact term) to serve pages based on the hostname that was requested.

 *Quote:*   

> 
> 
> How does Postfix deal with all this? It asks for $mydomain and $myhostname, but then do I just put 'example.com' and 'maddog' in there and use virtual mail delivery for the other domains?
> 
> 

 

Sorry, don't know how postfix works.  I'm actually trawling at the moment for how to handle mail sent to multiple domains hosted on one box.  If I find anything I'll let ya know  :Smile: 

 *Quote:*   

> 
> 
> I don't know, I just can't wrap my head around all this. I get confused with local hostnames, and 'hostnames' out there on the Internet...
> 
> Thanks for listening to my craziness, and I appreciate any help!

 

Hopefully this has helped.  The next step is to ask questions based on stuff I haven't explained well (or u just don't get  :Smile: )

----------

## ryker

I just wanted to say thanks to chrisis for the wonderful explanation.  I was having trouble grasping the whole hostname dnsdomainname thing as well.  This thread has helped a lot.  This thread should be marked sticky, or should be included in a faq.

----------

## chrisis

 :Very Happy:   <-- chrisis beaming.  It was my pleasure!

----------

