# Setting DNS servers for a NIC



## Deleted member 67029 (Jul 5, 2021)

Hey all

I'm trying to set DNS servers for a NIC in BSD 12.2-RELEASE. I checked for instructions on how to do in the handbook under section 12.5.2. 'Configuring the Network Card' but I couldn't find anything.

How can I go about setting DNS servers for a NIC?


----------



## Deleted member 67029 (Jul 5, 2021)

I think I have figured out how to do it. Section 12.5.2 also states:



> If there is no DHCP server and access to the Internet is needed, manually configure the default gateway and the nameserver:
> `#  sysrc defaultrouter="your_default_router"`
> `#  echo 'nameserver your_DNS_server' >> /etc/resolv.conf`




How would I go about adding a second nameserver though?


----------



## covacat (Jul 5, 2021)

dns settings are system global, not per interface
man resolv.conf


----------



## Alain De Vos (Jul 5, 2021)

For running your own dns server, "man 8 local-unbound".
For configuring the dns lookup  "man 5 resolv.conf"


----------



## mer (Jul 5, 2021)

OP, "static ip address for an interface" is what you needed to lookup. 

As pointed out by covacat resolv.conf settings are system-wide, not per interface.
If you want to add another DNS server to your system, simply add another nameserver line to /etc/resolv.conf

I believe there are ways (maybe even some ports) that could be used to give different DNS servers based on which interface is active.  You see logic like this alot for laptops with wireless and wired interfaces, but that typically is done using DHCP (different DHCP servers, giving different addresses and different DNS servers).

local-unbound (as pointed out by Alain De Vos ) is useful if you want to run a "caching DNS server".  If you don't want to do that, but simply go out to say Google or OpenDNS servers you don't need to worry about local-unbound.


----------



## Deleted member 67029 (Jul 5, 2021)

*mer*

I already setup a static IP address.

Oh that's interesting about resolv.conf being system wide. I wonder what would have happen if you had two NIC's using DHCP and both giving a different pair of nameservers.

If I add another nameserver to my system, wouldn't adding another nameserver line overwrite the previous line? I thought perhaps maybe a second nameserver is added using a comma to the existing line after the first nameserver?

nameserver 1.2.3.4,5.6.7.8


----------



## SirDice (Jul 5, 2021)

Kolusion said:


> I wonder what would have happen if you had two NIC's using DHCP and both giving a different pair of nameservers.


They'll get flipped back and forth. Just like the default gateway (you can only have one _default_ gateway). The last DHCP accepted response wins. 



Kolusion said:


> If I add another nameserver to my system, wouldn't adding another nameserver line overwrite the previous line? I thought perhaps maybe a second nameserver is added using a comma to the existing line after the first nameserver?




```
domain example.com
nameserver 1.2.3.4
nameserver 2.3.4.5
nameserver 3.4.5.6
```
You can have a maximum of 3 servers, you can add more but they'll get ignored (might even generate a warning). See resolv.conf(5).

```
nameserver	 Internet address (in dot notation) of a name server that the
		 resolver should query.	 Up to MAXNS (currently	3) name
		 servers may be	listed,	one per	keyword.  If there are multi-
		 ple servers, the resolver library queries them	in the order
		 listed.  If no	nameserver entries are present,	the default is
		 to use	the name server	on the local machine.  (The algorithm
		 used is to try	a name server, and if the query	times out, try
		 the next, until out of	name servers, then repeat trying all
		 the name servers until	a maximum number of retries are	made).
```


----------



## Alain De Vos (Jul 5, 2021)

The DNS lookup setting is for all interfaces, starting with /etc/nsswitch.conf.
The interaction with DHCP is not fully clear to me.
Maybe like SirDice says it's something like round-robin.


----------



## Zvoni (Jul 5, 2021)

Kolusion said:


> *mer*
> 
> *snipp* I wonder what would have happen if you had two NIC's using DHCP and both giving a different pair of nameservers.
> 
> ...


That's why the others said: For Static IP.
If you have DHCP, your resolv.conf gets "overwritten" with every new boot with a default-version.

I had the same Issue, but DHCP was a must.
The solution was a supersede resp. append entry in /etc/dhclient.conf

EDIT: That was with FreeBSD 12.1
No Idea if they changed anything for 13


----------



## ShelLuser (Jul 5, 2021)

Kolusion said:


> Oh that's interesting about resolv.conf being system wide. I wonder what would have happen if you had two NIC's using DHCP and both giving a different pair of nameservers.


Giving, so you'd run a DHCP server?  Well, nothing special would happen other than one section of your clients using DNS server 'A' whereas the others would use 'B'.

Unless of course you meant _getting_ two different IP addresses. Then once again nothing special would happen; the availabality of the DNS server(s) would depend on your routing table. DNS servers are also used in a "first come, first use" setup, as others mentioned also see the resolv.conf(5) manualpage.



Kolusion said:


> If I add another nameserver to my system, wouldn't adding another nameserver line overwrite the previous line? I thought perhaps maybe a second nameserver is added using a comma to the existing line after the first nameserver?
> 
> nameserver 1.2.3.4,5.6.7.8


See the manualpage, that's not how it works.

Also... while you can have multiple DNS servers only one will be used, the rest will act as fallovers. So if the first doesn't respond then the system will use another.

There's really no magic going on here.


----------



## Alain De Vos (Jul 5, 2021)

That's true. resolvconf takes the DHCP DNS setting and writes it in resolv.conf and sometimes even in /var/unbound.
From there it is just like you manually edited resolv.conf.


----------



## Deleted member 67029 (Jul 5, 2021)

SirDice

Your response confuses me. It looks like you are setting nameservers for a domain? I want to set nameservers for a NIC.


*ShelLuser*

Which manualpage?


----------



## SirDice (Jul 5, 2021)

Kolusion said:


> Your response confuses me. It looks like you are setting nameservers for a domain?  I want to set nameservers for a NIC.


Your machine is part of a (DNS) domain, that's what you typically set in /etc/resolv.conf too. That way you can use "short" names instead of having to use the FQDN. I.e. `ping lanmachine1` instead of `ping lanmachine1.my.local.network`. DNS settings are for the whole system, not per interface.


----------



## Deleted member 67029 (Jul 5, 2021)

SirDice
Thanks, but that wasn't what I was wanting to know.


----------



## SirDice (Jul 5, 2021)

Let me make it perfectly clear then, *there are no per interface DNS settings*, it is a per machine setting.


----------



## Deleted member 67029 (Jul 5, 2021)

Thanks but I wasn't wanting to know that either. My question was how to set a second nameserver for a NIC.


----------



## SirDice (Jul 5, 2021)

For crying out loud, I wrote that in my post and pointed you to the man page where you can look this up.


----------



## Deleted member 67029 (Jul 5, 2021)

Thanks mer for telling me how it's done.


----------



## Deleted member 67029 (Jul 5, 2021)

Sorry SirDic but your post really confuses me. I just wanted to know how to set nameservers for my NIC.


----------



## SirDice (Jul 5, 2021)

Kolusion said:


> I just wanted to know how to set nameservers for my NIC.


Again, there are NO nameserver settings per NIC. This is a system-wide setting, not per interface.


----------



## Deleted member 67029 (Jul 5, 2021)

Ok if you say.


----------



## Deleted member 67029 (Jul 5, 2021)

Oh now I get it. Cool, thanks.


----------



## Deleted member 67029 (Jul 5, 2021)

I thought it was per NIC because that is how it is in Windows.


----------



## SirDice (Jul 5, 2021)

It's not on Windows either, you might configure the settings in the configuration panel of TCP/IP of the interface but it's a system-wide change.


----------



## Alain De Vos (Jul 5, 2021)

This discussion becomes funny. 
If dns was an interface setting it would be part of ifconfig.


----------



## Deleted member 67029 (Jul 5, 2021)

*Sir Dice*

No it's not. I just checked, both my NIC's use different settings.


*Alain De Vos*

You should get out more.


----------



## VladiBG (Jul 5, 2021)

resolvconf(8)
					






					www.freebsd.org


----------



## SirDice (Jul 5, 2021)

Kolusion said:


> No it's not. I just checked, both my NIC's use different settings.


Only the DNS suffix is connection specific. And you typically use the same DNS servers everywhere because the _machine_ is registered to a certain REALM (Windows domain), not the interface. The domain controllers typically serve as DNS servers too because ADS is highly dependent on DNS. 









						Avoid registering unwanted Network Interface Controllers (NICs) in Domain Name System (DNS) - Windows Server
					

This article provides a solution to an issue where unwanted NICs are registered in DNS on a multihomed domain controller.



					docs.microsoft.com


----------



## Deleted member 67029 (Jul 5, 2021)

Yes, but if you have two apps, one set to use NIC 1 and the other to use NIC 2, I would think they will use the corresponding DNS setting for the NIC it is set to use.


----------



## Alain De Vos (Jul 5, 2021)

An app makes a function call to have dns resolved to an IP-address. But this function call is as far as I know not bound to a NIC.
man 1 getaddrinfo.
The windows API can offcourse be different.


----------



## SirDice (Jul 5, 2021)

Kolusion said:


> Yes, but if you have two apps, one set to use NIC 1 and the other to use NIC 2, I would think they will use the corresponding DNS setting for the NIC it is set to use.


It doesn't work that way.


----------



## Zvoni (Jul 6, 2021)

SirDice 
Yes, you said, DNS is a machine-wide setting, and i believe you, but reading the man-page for dhclient, the example there might confuse people to thinking you can set up a DNS per NIC





						dhclient.conf(5)
					






					www.freebsd.org


----------



## Alain De Vos (Jul 6, 2021)

You configure a dhclient for an interface. The application of the structure of the config file is limited to the dhclient service.
The same structure does not apply for the working of the O.S. complete.


----------



## sko (Jul 6, 2021)

Should we throw in multiple FIBs to confuse the troll even more?


----------



## Alain De Vos (Jul 6, 2021)

Interface group.
I remember once a complex DNS problem on a Windows Server.
After analyzing we figured out DNS request send on different interfaces where taking different routes.
And one route was false.


----------

