# Hostname and server with multiple domains



## drhowarddrfine (Sep 29, 2016)

This is a back to basics question, I guess, for things I've long forgotten. If I set up a server for four web sites, each with their own unique domain name, what should the `hostname` be in /etc/rc.conf?

Note that this same server is going to be handling email for them in their own domains, too. A lot of my reading talks about setting the hostname, then, to `mail.example.com` but, since it's also a web site server, I'm not sure what I should put there.

And to complicate things a bit more, this is a VPS with two static IPv4 addresses. The plan is to put one of the sites on one IP by itself.


----------



## Snurg (Sep 29, 2016)

AFAIK you can set it to what you like. I do not know of any need to have the same hostnames for the internal and external nets.

You set up the domain and subdomains that your servers listen to in their respective .conf files.
E.g. httpd.conf and squid.conf (if you use squid as reverse proxy) for the virtual hosts. The same with yourmailserverprogram.conf.


----------



## kpa (Sep 29, 2016)

Yes you can use whatever you like in hostname as long as the name resolves to an address during the system startup. The usual trick is to match hostname to an entry in /etc/hosts:


```
hostname="myhost.mydomain.tld"
```


```
127.0.0.1 myhost.mydomain.tld
```

If you have multiple domains you then have to tell Sendmail in /etc/mail/local-host-names which ones are accepted as the destination domains for incoming mail.

https://www.freebsd.org/doc/handbook/sendmail.html


----------



## drhowarddrfine (Sep 29, 2016)

kpa Yeah, that's what I'm having trouble with in my tinkering so far. I have the four names in local-host-names, and that works. Someone else set the hostname to "www" and left it that way but, when it worked, it just added to my confusion.

So I'm thinking of doing:


```
hostname="www.domain1.com"
```

and then in /etc/hosts


```
127.0.0.1 localhost www.domain1.com www.domain2.com www.domain3.com www.domain4.com

107.xxx.xxx.xxx domain1.com www.domain1.com
... etc.
```

Would that be right if they wanted naked domains? I don't think I can leave the www off the hostname. I'm having trouble with email spf and dmarc complaining about host names not matching up due to mail from domain1.com but sender is localhost.domain1.com (using sendmail). Now that I type that, I think I realized that I was using localhost.domain1.com somewhere. 

Like I said, I just started tinkering with this and I'm doing so much reading and testing I think I'm getting myself turned around.


----------



## kpa (Sep 29, 2016)

If you want user@domain1.com to be a valid address the domain1.com domain has to be in /etc/mail/local-host-names, same for any other "naked" domain. You don't need the entry for the naked domain1.com domain in /etc/hosts because that's never looked up by Sendmail. Anyone sending mail to domain1.com would look up the MX records for domain1.com and connect to whatever host(s) is set there for mail delivery and Sendmail doesn't then care if the naked domain then resolves to an address or not when processing the message.


----------



## drhowarddrfine (Sep 29, 2016)

That clears up some things, thanks. What about the www names? Leave those in the localhost line? What about the actual address lines? Do I need those?

I'm thinking about what happens when I'm ssh'ed in and want to send an email which, again, probably doesn't matter to sendmail but other applications might care?

I've read in a couple of places where some people think such a setup should just have localhost on the 127.0.0.1 line and nothing else.


----------



## kpa (Sep 29, 2016)

The names that you need in /etc/hosts are the ones that a service might try to use on system startup when DNS is not yet available, basically the name you have set as hostname in rc.conf(5) . Otherwise you can trust that DNS resolves the names for the services that run on your server.

Since you have a static IP address for your server I would do this instead:

/etc/rc.conf:


```
hostname="www.domain1.com"
```

/etc/hosts:


```
...leave everything else untouched...

107.x.x.x www.domain1.com www
```


----------



## drhowarddrfine (Sep 29, 2016)

By "everything else" do you mean as I show it on the localhost line earlier?

I notice that in the raw mail it shows mail to domain2.com as coming through localhost.domain1.com.


----------



## kpa (Sep 29, 2016)

I meant leave everything else as it would be on a freshly installed FreeBSD.

That localhost.domain1.com is from the fact that now 127.0.0.1 reverse lookup probably returns the name localhost.domain1.com. Another reason not to add too many extra entries in /etc/hosts, only the bare minimum.


----------



## drhowarddrfine (Sep 29, 2016)

I'm real close now. You cleared some things up. I'll tinker some more tomorrow but I'm real close now. Thanks.


----------



## drhowarddrfine (Sep 29, 2016)

I'm thinking that my problem(s) lie with trying to handle mail from two IP addresses with one instance of sendmail. I'm trying to add SPF and DMARC. They work when I send email from the server itself but not when someone else sends me an email. Then I get a "soft fail" though the email goes through.

Related, every email continues to be sent by domain1.com. This is why I think my SPF issue doesn't work, at least in part.


----------



## Snurg (Sep 29, 2016)

drhowarddrfine said:


> What about the www names? Leave those in the localhost line? What about the actual address lines? Do I need those?


Just FYI: 
`man 5 nsswitch.conf`
Default setting: /etc/hosts overrides DNS. Including hostnames there is useful for local testing of servers that are not yet in DNS. Otherwise bad if there are DNS problems and you do not notice because of entries in /etc/hosts.

The www names are resolved by DNS. The client connects that IP. It sends the actual site it requests in the HTTP header. The reverse proxy and/or the web server evaluates the HTTP header and redirects to the appropriate virtual host. What you have in /etc/hosts is thus irrelevant.



drhowarddrfine said:


> I've read in a couple of places where some people think such a setup should just have localhost on the 127.0.0.1 line and nothing else.


A reason for this: see above.


----------



## drhowarddrfine (Sep 29, 2016)

The only thing in /etc/hosts is this right now:


```
127.0.0.1       localhost localhost.my.domain

107.x.x.1  www.domain1.com www
107.x.x.2  www.domain2.com www
```

Sendmail handles mail for four web sites properly except I can't get the hostnames to match with the mail server. The mail server is just domain1.com, so john@domain1.com. However, when I send mail from Yahoo to john@domain2.com, the raw email shows it being served by localhost.domain2.com but the mail goes through. SPF doesn't like that, though.

I've tried pointing the MX records at each site individually, domain1.com and domain2.com, and tried just domain1.com with no apparent change.

Now, I know I've been fooled waiting for propagation changes and I have probably lost track of what I've tried so, if something doesn't make sense, feel free to point things out.


----------

