1

To be brief:

From what I understand the hostname command has an option -f. From the man page:

-f Include domain information in the printed name. This is the default behavior.

As it's the default behavior, I was surprised when my printed name did not include domain information, but simply MY-COMPUTER-NAME.

Some additional information:

I'm unable to change the "Computer Name" in the "Sharing" preferences (it's grayed out as pictured here):

sharing preferences showing computer name set to Hippotigris and a message that says: "Computers on your local network can access your computer at: LM-SJN-21001464

Originally, my "Local Hostname" was set to "Hippotigris" as well, but when I noticed this problem, I changed it to what I was getting from the hostname command:

sharing preferences with an input field for Local Hostname set to LM-SJN-21001464 ending in ".local" grayed out

I found this article entitled "Setting the Mac hostname or computer name from the terminal" but I'm not certain whether doing that will solve my problem or aggrevate it.

What I'm trying to accomplish:

I'm using NodeJS's built-in dns.lookup function in combination with it's built-in os.hostname function. I presume that the hostname function returns the same thing that the hostname command prints (which is what it's doing currently). When I run:

node -e "require('dns').lookup(require('os').hostname() + '.local', console.log)"

It works fine and prints the ip address of my machine on the local network, but when I run

node -e "require('dns').lookup(require('os').hostname(), console.log)"

It logs the error: getaddrinfo ENOTFOUND LM-SJN-21001464

I've asked around and it appears this script without the .local works for most people, so I'm guessing it has something to do with my machine configuration.

  • 2
    Do a Google search for "local mDNS" and have a look at en.wikipedia.org/wiki/.local In short, .local is a bit special, but I don't know if it is this that creates issues for you or not. – Kusalananda Aug 4 '18 at 6:30
2

Thanks to some help on twitter I added ".local" to my search domains in my DNS settings and that fixed my core problem, though hostname (and os.hostname) still return a hostname without the ".local" appended.

So this is an answer to the problem I was having, but not the question I posted and I would still love to understand what's going on and whether I should be concerned.

network preferences in the DNS tab showing "Search Domains" includes ".local"

1

The hostname is the name of the machine itself. The part that comes after the hostname, i.e. .local in hippotigris.local or .kentcdodds.com in hippotrigris.kentcdodds.com is the so-called domain name.

You never should add the domain name to the hostname itself.

As already mentioned in the comments, .local is special: It's the domain for mDNS, which does multicast name resolution in your local LAN segment. This works differently for normal DNS name resolution, and is extensively used by Apple.

So when you do require('dns').lookup() in node, it will lookup using the normal DNS mechanism. However, your hostname isn't registered in whatever you use as the normal DNS server (either the one on your home router, or your ISP's DNS server). Therefore you don't get an answer.

If you append .local, node (or the underlying MacOS libraries) use mDNS instead, which means they can find your hostname.

So that's why you need to append .local in some place, either directly, or automatically in the search path, or by whatever other means (e.g. on Linux, you can configure name search to use both DNS and mDNS, in any order).

As for why the hostname you enter in the GUI window you've shown (Hippotigrisis) is different from the name that's announced via mDNS (LM-SJN-21001464), I am not sure (because the Mac UI gives no hint of what is actually going on) - one would have to find out what information this dialog sets, exactly, and where the other name (which looks like a serial number) is set. Command line tools may help in searching for this.

0

Wow, this is a bit a doozy ...of a question, as there are a couple of things going on here.

Trying not to repeat what others have said

...I'm going to assume you are wanting to access your mDNS name of a Node app you are working with, so you can view the app from a separate box on your LAN, and would prefer to use the mDNS without having to use .local in the address name as opposed to using the IPv4 address, which by the way, tools such as webpack allow setting a 0.0.0.0 hostname in a webpack configuration file to access a dev server build of app on a LAN, ie. access a locally running React app running on your MacBook from a separate mobile device.

Doing a couple AOL keyword searches it seems that the name you are referring to in the sharing pane of the system prefs on macOS can also be a NETBIOS name, and apparently at some point a NETBIOS name can't be longer than 15 chars not sure if this is still the case, hence why your computer name with the LM... is exactly 15 chars when I counted it.

Taking a shot in the dark here 🐦📦, as you didn't describe the devices on your the network in question, ie. is there a domain controller? or is this a home network? work network? or somehow a network that you connect to, and there is a device that is dynamically changing the hostname of your computer when it finishes connecting to that network?

All that said, I to am running macOS, ie. 10.13.x at the moment, and when I run,

hostname -f

it returns my

$HOSTNAME.local

ie

rogue.local

On my home network, I have a router connected to a modem, and that router does not do any domain name type configuration as far as I can tell, I believe the default workgroup name is WORKGROUP.

...ahhh typing out load here, going into Sys Prefs > Network tab > WINS ...look for NetBIOS name, and see what that is set to, you might want to see if that name is changing when you connect to different networks. Finally, if I understand your question correctly you are trying to access your computer from an external device using the hostname of the computer rather than using the IPv4 address, and as others have already said, accessing hostnames on LAN using mDNS relies on that little .local bit to perform its magic 🧙‍♀️. That said, if it was me, I'd keep the .local as a part of what you are working with as it will let others know their accessing your Node app via mDNS.

wins-name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.