Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question:

Question: MacBook name changes depending on network

I've run into an interesting situation that I do not understand.


I have some custom multi-user software that I've created for a client that has some item checkin/checkout functionality within the system that depends on the machine name (it doesn't allow someone to lock the item on more than one device at a time, to prevent data loss due to it's ability to make edits offline).


What I've run into is a users MacBook Pro who's network name changes depending on what they are connected to. Now, I've seen various users MacBook names change from something like Garys-MacBook-Pro to Garys-MacBook-Pro.local to Garys-MacBook-Pro.att.net depending on whether they're at the office or at home or whatever. This I can handle and makes total sense. I just strip the name down to the actual machine name "Garys-MacBook-Pro" and only base my comparison on that.


Where I've come into a real issue, is I've seen this users MacBook network name change almost completely when he jumped onto a hotel network vs. his own MiFi hotspot. When he was on his MiFi hotspot his machine name changed to "Garys-MBP.local", but when he's on the hotel internet it stayed "Garys-MacBook-Pro.local".


Why would the actual name of the device change like this depending on the connection? I understand the network suffix changing based on network... but not the actual device name itself. He named his machine "Garys-MacBook-Pro", but why the switch to "Garys-MBP" when on the hotspot? Is there some location based feature of OS X that I am unaware of that may be at play here?


Thanks!

MacBook Pro, iOS 11.2.6, null

Posted on

Reply
Question marked as Solved
Answer:
Answer:

Why don't you use the serial number? You can get the data out in xml if that would be easier to parse. Maybe


mac $ system_profiler SPHardwareDataType
Hardware:

    Hardware Overview:

      Model Name: Mac mini
      Model Identifier: Macmini7,1
      Processor Name: Intel Core i5
      Processor Speed: 2.6 GHz
      Number of Processors: 1
      Total Number of Cores: 2
      L2 Cache (per Core): 256 KB
      L3 Cache: 3 MB
      Memory: 16 GB
      Boot ROM Version: MM71.0220.B29
      SMC Version (system): 2.24f32
      Serial Number (system): C07PQ092G1J1
      Hardware UUID: 42D8474A-F6AD-5C5D-8028-91E898498B4B

mac $



It might be better to split on the ":". Using colrm may be error prone.


mac $ system_profiler SPHardwareDataType | grep "Serial Number" | colrm 1 30
C07PQ092G1J1
mac $

Posted on

Question marked as Helpful

Mar 13, 2018 8:53 AM in response to cosborn In response to cosborn

What are you using to get the hostname?


Let's say we have a computer name of odin that is set in System Preferences : Sharing. It is on a local home network, and there is no VPN connection. At no time below, does the computer name in the Sharing preference automatically change.


From the Terminal command-line, the following commands will return:


networksetup -getcomputername

odin


scutil --get ComputerName

odin


scutil --get LocalHostname

odin


scutil --get HostName

HostName: not set


uname -n

odin.local


hostname

odin.local


python -c "import socket;print(socket.gethostname())"

odin.local


# the fully qualified domain name (fqdn)

python -c "import socket;print(socket.getfqdn())"

odin.local


But now, I make a VPN connection to the local university, so what changes? The DNS.


uname -n

nnnnnnnnn.guest.xxxxx.net


hostname

nnnnnnnnn.guest.xxxxx.net


python -c "import socket;print(socket.gethostname())"

nnnnnnnnn.guest.xxxxx.net


python -c "import socket;print(socket.getfqdn())"

nnnnnnnnn.guest.xxxxx.net


I believe you need to be checking one of the above where the hostname does not change when connected to an external network that will assign a new client DNS network name.

There’s more to the conversation

Read all replies

Page content loaded

Question marked as Helpful

Mar 13, 2018 8:53 AM in response to cosborn In response to cosborn

What are you using to get the hostname?


Let's say we have a computer name of odin that is set in System Preferences : Sharing. It is on a local home network, and there is no VPN connection. At no time below, does the computer name in the Sharing preference automatically change.


From the Terminal command-line, the following commands will return:


networksetup -getcomputername

odin


scutil --get ComputerName

odin


scutil --get LocalHostname

odin


scutil --get HostName

HostName: not set


uname -n

odin.local


hostname

odin.local


python -c "import socket;print(socket.gethostname())"

odin.local


# the fully qualified domain name (fqdn)

python -c "import socket;print(socket.getfqdn())"

odin.local


But now, I make a VPN connection to the local university, so what changes? The DNS.


uname -n

nnnnnnnnn.guest.xxxxx.net


hostname

nnnnnnnnn.guest.xxxxx.net


python -c "import socket;print(socket.gethostname())"

nnnnnnnnn.guest.xxxxx.net


python -c "import socket;print(socket.getfqdn())"

nnnnnnnnn.guest.xxxxx.net


I believe you need to be checking one of the above where the hostname does not change when connected to an external network that will assign a new client DNS network name.

Mar 13, 2018 8:53 AM

Reply Helpful (1)

Mar 13, 2018 8:53 AM in response to cosborn In response to cosborn

I'm getting the machine name via a call to "hostname".


I had the client do the scutil commands earlier today, and they all return what I would expect, the "Garys-MacBook-Pro" name. But what I noticed was that his terminal prompt reflected the "Garys-MBP" name that hostname returns while he's on his hotspot. So it's registered somewhere in the system (he probably started the computer off as Garys-MBP and changed the name to Garys-MacBookPro later on, and the terminal name and whatever other internals to his home directory aren't reflecting that change).


I can get around this if I can consistently poll the system for this "Garys-MBP" name, and add it to a list of valid names for his machine (rather than relying on just hostname alone). Is there a command-line way of getting that terminal name?

Mar 13, 2018 8:53 AM

Reply Helpful

Mar 13, 2018 9:47 AM in response to cosborn In response to cosborn

The issue is two-fold.

  1. How did the client configure their name in the external MiFi dongle, which it will use to change the observed hostname when connected? If they set that to what their computer name is in the Sharing preferences, it may display what is intended.
  2. The default PS1 prompt in the Bash shell is getting set to hostname (\h) which strips everything after the first dot in the fully qualified domain name (FQDN). So in my previous post, that would be the nnnnnnnnn part of nnnnnnnnn.guest.xxxxx.net. If one replaces the (\h) with (\H), then the entire FQDN name is shown as part of the Terminal PS1 prompt.


For item 2, I used one of the hostname queries that does not change during a DNS network change. In my ~/.bash_profile (actually my ~/.bashrc) file, I made these changes, which retains the short computer name, and the working directory (\w) information.


MyHost="$(networksetup -getcomputername)"

export PS1="$MyHost: \w$ "


If one then does either of the following at the Terminal prompt, depending on the edited file, then the Bash PS1 prompt will change to the desired hostname entry. These can be skipped and quitting the Terminal and restarting it will also make this change when these files are initially read by Bash.


source ~/.bash_profile

source ~/.bashrc



Personally, I only make edits to ~/.bashrc, and then reference it in ~/.bash_profile as:


[[ -s ~/.bashrc ]] && source ~/.bashrc


The PS1 Bash shell prompt now appears as the following, despite the VPN connection:


odin: ~$


and not


nnnnnnnnn: ~$

Mar 13, 2018 9:47 AM

Reply Helpful
Question marked as Solved

Mar 13, 2018 12:06 PM in response to cosborn In response to cosborn

Why don't you use the serial number? You can get the data out in xml if that would be easier to parse. Maybe


mac $ system_profiler SPHardwareDataType
Hardware:

    Hardware Overview:

      Model Name: Mac mini
      Model Identifier: Macmini7,1
      Processor Name: Intel Core i5
      Processor Speed: 2.6 GHz
      Number of Processors: 1
      Total Number of Cores: 2
      L2 Cache (per Core): 256 KB
      L3 Cache: 3 MB
      Memory: 16 GB
      Boot ROM Version: MM71.0220.B29
      SMC Version (system): 2.24f32
      Serial Number (system): C07PQ092G1J1
      Hardware UUID: 42D8474A-F6AD-5C5D-8028-91E898498B4B

mac $



It might be better to split on the ":". Using colrm may be error prone.


mac $ system_profiler SPHardwareDataType | grep "Serial Number" | colrm 1 30
C07PQ092G1J1
mac $

Mar 13, 2018 12:06 PM

Reply Helpful (1)

Mar 13, 2018 12:12 PM in response to VikingOSX In response to VikingOSX

Think I'm going to see about using the serial number of the machine instead, as suggested below. That won't change no matter what network I connect to, and I should be able to pull it consistently cross-platform using the serial-number npm package in my project. https://github.com/es128/serial-number

Mar 13, 2018 12:12 PM

Reply Helpful

Mar 13, 2018 12:25 PM in response to rccharles In response to rccharles

In Awk, the number of default, white-space delimited fields in a line is kept as a count in the NF variable. In the case of the Serial number output line in the following, the serial number is always the last field. One pipe, not two.


system_profiler SPHardwareDataType | awk '/Serial/ {print $NF}'

Mar 13, 2018 12:25 PM

Reply Helpful

Mar 13, 2018 3:27 PM in response to cosborn In response to cosborn

scutil --get ComputerName

should get the name from System Preferences -> Sharing -> Computer Name


'hostname' is affected by a reverse DNS lookup on your DHCP assigned IP address. If the local subnet has a DNS name for your IP address then 'hostname' will be using that.


The serial number will also be consistent.

Mar 13, 2018 3:27 PM

Reply Helpful
User profile for user: cosborn

Question: MacBook name changes depending on network