Mini Mac Computer Name Reverts to Default

Perkyier
New Contributor

Hello all! I'm new to JAMF. We have installed Mac Mini machines to be content caches for our iPads in the school system. They are set to configure themselves when they are plugged into the network and powered up. The default name is mac mini. I go and manually rename each one after it comes up. But for some reason, a couple of days later, the name has reverted to mac mini. I look in the history in JAMF and it only shows when it was created and each time I have renamed it. Any logs I can look at other than the history for the machine? Any ideas?

1 ACCEPTED SOLUTION

AJPinto
Honored Contributor II

We are doing basically the same thing, just backwards. I prefer to write my scripts to be MDM agnostic, that way they can be run locally without issue and not needing JAMF. The JAMF recon at the end of my script, updates JAMF as to what the devices hostname is.

 

My script is a little long, but mainly because it uses a lot of variables, comments and "error" checking. The bones of my script is 3 lines. If you are not setting all 3 of these, you are not seeing the hostname correctly.

scutil --set ComputerName "$serialNumber"	
scutil --set HostName "$serialNumber"
scutil --set LocalHostName "$serialNumber"

The rest of the script is gathering what the serial number is, and making sure each of the 3 lines above actually need to be run with a ton of comments. This way you are not needlessly making changes. With a finally check to make sure the hostname actually changed and it will error if the hostname did not change.

 

View solution in original post

4 REPLIES 4

AJPinto
Honored Contributor II

For the Mac Mini's I suggest using a script to set the host names. MacOS needs its hostname set in 3 locations or it wont stick. Below is the script I use to name our Macs. I have it set to run on enrollment and on a recurring check in against a group for devices that don't meet our naming connections so I can set it and forget it. I use the Serial Number for the host name, but you should be able to update this script to meet your needs.

 

#!/usr/bin/env bash
#*=============================================================================
#* Script Name: 
#* Created: 
#* Author:
#*=============================================================================
#* Purpose: Requests input and renames computer based on Naming Convention 
#*=============================================================================


#*=============================================================================
#* GLOBAL VARIABLES
#*=============================================================================

computerName=$(scutil --get ComputerName)
hostName=$(scutil --get HostName)
localHost=$(scutil --get LocalHostName)
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
serialNumberII=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | tr -d '"')

#*=============================================================================
#* FUNCTIONS
#*=============================================================================


#*=============================================================================
#* SCRIPT BODY
#*=============================================================================
userInfo

## Check & Update Computer Name
if [ "$computerName" == "$serialNumber" ]
then
	echo "Computer name matches serial number, $serialNumber"
else
	echo "Current Computer Name: $computerName"
	echo "Computer Name does not meet standards"
	echo "Changing Computer Name to match Serial Number"
	scutil --set ComputerName "$serialNumber"	
fi; $DIV2

## Check & Update Host Name
if [ "$hostName" == "$serialNumber" ]
then
	echo "Host Name matches serial number, $serialNumber"
else
	echo "Current Host Name: $hostName"
	echo "Host Name does not meet standards"
	echo "Changing Host Name to match Serial Number"
	scutil --set HostName "$serialNumber"
	
fi; $DIV2


## Check & Update Local Host
if [ "$localHost" == "$serialNumber" ]
then
	echo "Local Host matches serial number, $serialNumber"
else
	echo "Current Local Host: $localHost"
	echo "Local Host does not meet standards"
	echo "Changing Local Host to match Serial Number"
	scutil --set LocalHostName "$serialNumber"
	
fi; $DIV2


## Final Check
computerNameII=$(scutil --get ComputerName)
hostNameII=$(scutil --get HostName)
localHostII=$(scutil --get LocalHostName)

echo "Results:"; $DIV3
echo "Serial number: $serialNumber"
echo "Computer Name: $computerNameII"
echo "Host Name: $hostNameII"
echo "Local Host: $localHostII"
if [[ "$computerNameII" == "$serialNumber" ]] && [[ "$hostNameII" == "$serialNumber" ]] && [[ "$localHostII" == "$serialNumber" ]]
then 
	echo "Computer Name satisfies naming standards"
	$DIV1; exit 0
else
	echo "Computer does not meet naming standards"
	echo "More troubleshooting will be necessary."
    $DIV1; exit 1
fi 

## Perform final Recon to update computer name on Jamf
sudo jamf recon
#*=============================================================================
#* END OF SCRIPT
#*=============================================================================

 

jel-gherson
New Contributor II

Whilst the script provided by @AJPinto is valid, it is unnecessarily complex and also fails to update the matching information in Jamf Pro inventory.

The value you want to set the name to will depend on your own choices - as an example I have a script which reads the name of the 'asset' from an asset database called SnipeIT but for simplicity I will use the Mac serial number as an example. Therefore the core of the script will be as follows.

#!/bin/bash
#
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
/usr/local/bin/jamf setComputerName -name "$serialNumber"
exit

The jamf command not only sets all three computer names in one but also sends the new name to Jamf Pro to update the inventory record.

AJPinto
Honored Contributor II

We are doing basically the same thing, just backwards. I prefer to write my scripts to be MDM agnostic, that way they can be run locally without issue and not needing JAMF. The JAMF recon at the end of my script, updates JAMF as to what the devices hostname is.

 

My script is a little long, but mainly because it uses a lot of variables, comments and "error" checking. The bones of my script is 3 lines. If you are not setting all 3 of these, you are not seeing the hostname correctly.

scutil --set ComputerName "$serialNumber"	
scutil --set HostName "$serialNumber"
scutil --set LocalHostName "$serialNumber"

The rest of the script is gathering what the serial number is, and making sure each of the 3 lines above actually need to be run with a ton of comments. This way you are not needlessly making changes. With a finally check to make sure the hostname actually changed and it will error if the hostname did not change.

 

vegard
New Contributor II

Hi. Don't know if this will apply to you, but for our AppleTVs this worked.

- In the default DEP profile, Set Device Name to the variable %Name%. This will make the name stick after a refresh or reboot.

 

Regards, Vegard