HostName differs from LocalHostName/ComputerName

jboth
New Contributor

So we are 1-1 Macbook Airs from 5-12. We attempted to use LAN School last year but had huge issues with computers not showing up in the teachers class lists. I have figured out the reason this was happening, but need some help in finding a way to efficiently fixing it.

So there is the HostName, LocalHostName and ComputerName. The ComputerName and LocalHostName are correct on the machines but on a good number of them the HostName is incorrect. It seems that LAN School and Terminal use the HostName thus why we had issues getting stuff working when they differed from what we thought they were.

Does anyone have any insight on a way to change this en masse?

1 ACCEPTED SOLUTION

htse
Contributor III

based on what you've said, the ComputerName will be the same as the HostName, and the ComputerName's have already been set correctly. so as a test, you'll want to get the ComputerName first

scutil --get ComputerName

if that returns the correct value, then it would only take one line to set the HostName to the same value

scutil --set HostName `scutil --get ComputerName`

I would second @calumhunter with the naming convention, with the concern being you might get some exotic errors with special characters.

View solution in original post

11 REPLIES 11

htse
Contributor III

if DNS is accurate for the clients, use the grab the hostname from the hostname command and set it in SystemConfig using scutil --set hostname

jboth
New Contributor

Sorry. I'm not very versed in commands yet. Could you give an example of what it would look like?

Sudo scutil --grab ComputerName
Sudo scutil --set HostName

The computer names are all set correctly now so if it sets the host name to what it grabs I would think it would work..

calumhunter
Valued Contributor

depending on how you set your computer names you could just script it

i usually set the computer names based on serial number

theres actually 3 computer names to set

scutil --set ComputerName $serial
scutil --set LocalHostName $serial
scutil --set Hostname $serial

where $serial is the serial number of the machine

from man scutil : Supported preferences include:

ComputerName The user-friendly name for the system.

LocalHostName The local (Bonjour) host name.

HostName The name associated with hostname(1) and gethostname(3).

jboth
New Contributor

We name ours 2015-last-first for example. And the computer name is accurate. And I want 1 script I can push out to all clients that will grab what I want and set it to that for each machine. So I don't have to adjust the script for each machine. If all three names are the same I would think my LAN school wouldn't run into issues. And if somehow that host name changes again it will just fix itself.

calumhunter
Valued Contributor

I usually have a launchd script that runs once a day to reset the computer names, i use scutil and set all 3 names the same

I worry about your naming convention, does last-first mean lastname-firstname of the computer user?
How do you envisage getting this information in order to script it?

htse
Contributor III

based on what you've said, the ComputerName will be the same as the HostName, and the ComputerName's have already been set correctly. so as a test, you'll want to get the ComputerName first

scutil --get ComputerName

if that returns the correct value, then it would only take one line to set the HostName to the same value

scutil --set HostName `scutil --get ComputerName`

I would second @calumhunter with the naming convention, with the concern being you might get some exotic errors with special characters.

jboth
New Contributor

While testing the HostName wasn't correct on certain ones. Sometimes the host name was only 2014-last and left out the rest or in some cases it was something totally different like 2016-last. So it wouldn't show up in LAN school because I was inserting it into the class via machine name (2014-last-first) but the HostName was 2014-last. As soon as I did a scutil --set HostName 2014-last-first, it showed up in LAN School and showed the correct name in terminal. SO if I know the computer names are all correct I just want to make sure HostName and localhostname keep getting prompted to "sync" with the ComputerName and I shouldn't have problems.

We name them this way because it's easy to manage them in our opinion. They are all labeled with their names and it's easy to just search for their graduation year and last-first in jss. We haven't ran into issues with using this convention. If someone's name has an apostrophe we just don't use it in the computer name.

jboth
New Contributor

Thanks htse! I just tested your suggestion and it worked great. Now I just need to figure out how to make that run each day on every machine via casper. I appreciate everyones input!

donmontalvo
Esteemed Contributor III

@htse wrote:

I would second @calumhunter with the naming convention, with the concern being you might get some exotic errors with special characters.

My bigger concern is your DNS group hunting you down and jumping up and down on top of you for changing a value that is supposed to be set by DNS. But then getting DNS folks to fix things on their end is like dreaming of winning Lotto... ;)

--
https://donmontalvo.com

scottb
Honored Contributor

@donmontalvo: Isn't that why any of this is necessary? Maybe there are OS X reasons as well, but most of the IT guys on "that side" of things tend to dismiss Macs as a rule. We don't even bother asking any more...

alexknelson
New Contributor

I'm using the following script to rename my macs, but am finding some macs aren't holding onto the rename.

#!/bin/bash

## Laptop or Desktop
TYPE=$(system_profiler SPHardwareDataType | grep -i "MacBook" | wc -l)
[[ $TYPE -gt 0 ]] && T="L" || T="D"

## Get the Serial Number
SERIAL=$(system_profiler SPHardwareDataType | grep -i Serial | grep -i system | awk '{print $NF}' | cut -c5-)

## Put it all together now...
NEWNAME="${T}${SERIAL}"

## Name the computers
scutil --set ComputerName "$NEWNAME"
scutil --set HostName "$NEWNAME"
scutil --set LocalHostName "$NEWNAME"

## Create dummy receipt to mark complete
touch /Library/Receipts/com.company.renameComplete.bom

## Update Inventory
/usr/local/bin/jamf recon