beta-tester
Posts: 1258
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

is it possible to have different hostnames on differnt eth interfaces?

Sun Dec 03, 2017 1:31 pm

hi,

i have a RPi, that has the internal network insteface eth0 connected to the switch/router
and an external usb network interface eth1 connected to a test device.
is it possible that my RPi is visible to network_A (switch/router) with hostname_A
and to network_B (test device) as hostname_B?

Code: Select all

        :  :  :                ╔═══════╗
     ╔══╧══╧══╧═╗              ║  RPi  ║
WAN──╢DSL-router╟──────────eth0╢───┐   ║
     ╚══════════╝    hostname_A║   │   ║
         10.0.0.1      10.0.0.2║  NAT  ║
                               ║   │   ║
    ╔═══════════╗              ║   │   ║
    ║Test Device╟──────────eth1╢───┘   ║
    ╚═══════════╝    hostname_B╚═══════╝
      192.168.0.2   192.168.0.1
my RPi provides network services to the network at eth0 so i want to have a phoper hostname to that eth0.
but on eth1 i have a test device, that should not see that same hostname_A of the RPi.
i want, that test device will see my RPi as hostname_B.

my RPi has running Raspbian Stretch, DNSmasq, ...
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

Re: is it possible to have different hostnames on differnt eth interfaces?

Sun Dec 03, 2017 1:50 pm

it's just a matter of your name service to deliver hostname_A for 10.0.0.2 and hostname_B for 192.168.0.1.

beta-tester
Posts: 1258
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: is it possible to have different hostnames on differnt eth interfaces?

Sun Dec 03, 2017 3:00 pm

sparkie777 wrote:
Sun Dec 03, 2017 1:50 pm
it's just a matter of your name service to deliver hostname_A for 10.0.0.2 and hostname_B for 192.168.0.1.
how can i influence the delivery behavior?
(for my case, obviously not by changing /etc/hostname)
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

Re: is it possible to have different hostnames on differnt eth interfaces?

Sun Dec 03, 2017 3:46 pm

you say you use 'DNSmasq'. Why not just set it up there? Since I only use 'bind9' instead I can't provide you further tips for 'DNSmasq'.

epoch1970
Posts: 3882
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: is it possible to have different hostnames on differnt eth interfaces?

Sun Dec 03, 2017 4:21 pm

The host name is the name of the machine you can see on the local console, nowhere else.
A domain name is tied to a network address, this is managed by the naming system.
The DHCP client traditionally reports the host name to the DHCP server, and if the DHCP server updates the name server, the end result is that your domain name is based on the hostname. This is just by convention.

You can control some of that stuff locally:
  • The host name is defined in /etc/hostname
  • Domain names can be statically defined in /etc/hosts
  • File /etc/nsswitch.conf defines priorities when the system looks up for information, in this case the "hosts:" line is relevant. By default the machine will look in /etc/hosts before asking DNS servers for a name or address. (Don't mess with nsswitch.conf, it's old and grumpy.)
Of course if you have a central DNS server, it is the preferred place to define records. By and large, a DNS server can be configured to resolve any sort of name or address, it is not limited to a specific subnet (e.g. you can locally redefine www.google.com if you please)

To answer your question:
  1. I suppose there is a DHCP/DNS service on your router.
    Set RPi's hostname (/etc/hostname) so that you get a pleasing "hostname_A" tied to 10.0.0.2, thanks to your router. In other words, don't change anything.
  2. Define "hostname_B" in /etc/hosts in RPi. Yes, you could do that same thing in Test Device. But we're doing it in RPi because i. this will allow RPi to know a bit more about himself (can't hurt) and ii. dnsmasq by default reads /etc/hosts and can serve these names.
    Something like that:

    Code: Select all

    $ cat /etc/hosts
    127.0.0.1	localhost
    127.0.1.1       gw
    
    ::1		localhost ip6-localhost ip6-loopback
    ff02::1		ip6-allnodes
    ff02::2		ip6-allrouters
    
    # Needed for early/local services
    192.168.0.1	hostname_B.domain-name hostname_B
    # More conf in dnsmasq static hosts file
  3. Restart dnsmasq and you should be done.
    If not, make sure "Test Device" uses 192.168.0.1 as its DNS server, and that you have your dnsmasq config right.

Some extra dnsmasq considerations:
If you want to define a bunch of records and not clutter /etc/hosts, you can ask dnsmasq not to look it up but another file instead. This is what I usually do (and still I need a bit of records in /etc/hosts thanks to systemd's parallel booting prowess. Another subject.)
My /etc/dnsmasq.d/dnsmasq.conf file includes this:

Code: Select all

# We do not read /etc/hosts to resolve queries...
no-hosts
# ... but this is local data like /etc/hosts
# File MUST be in a subdir of CONF_DIR otherwise it will be parsed as a dnsmasq config file, not good.
addn-hosts=/etc/dnsmasq.d/hosts/static_hosts.domain-name

And a line in /etc/dnsmasq.d/hosts/static_hosts.domain-name would look like that:

Code: Select all

192.168.0.1	ntp.my-domain. dns.my-domain. gw.my-domain.
Note the final dot in each name, that instructs the DNS server not to try to append any sort of domain name to the name defined on the line. According to your dnsmasq settings, it could be resolving 192.168.0.1 as ntp.my-domain.my-domain so the final dot makes sure that doesn't happen.

There is also another way of defining DNS records in dnsmasq.conf, using "address=..." but it becomes cumbersome when defining many records.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: is it possible to have different hostnames on differnt eth interfaces?

Sun Dec 03, 2017 4:51 pm

Host name is a function of what's stored in /etc/hostname, it's nothing to do with any domain name(s) assigned to IP interfaces (although they are usually co-incident).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Advanced users”