Home > Jamf Pro, macOS, Scripting > Using the Jamf Pro agent to set computer name to match the Mac’s hardware serial number on macOS Sonoma
In a number of environments, Mac admins have chosen to use the Mac’s hardware serial number when naming the computer’s hostname (otherwise referred to as the computer name.) This is a task which the Jamf Pro agent includes built-in functionality for, using the following command:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For more information on this functionality, please run the following command on a Mac with the Jamf Pro agent installed:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You should see the help information which is relevant to this command:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
username@computername ~ % jamf help setComputerName | |
Usage: jamf setComputerName [-target <target volume>] [-name <name>] | |
[-useMACAddress] [-useSerialNumber] [-suffix <suffix>] | |
[-prefix <prefix>] [-fromFile <path to file>] | |
-target The target drive to set the name on | |
-name The new name for the computer | |
-useMACAddress Generate the name using the MAC Address | |
-useSerialNumber Generate the name using the Serial Number | |
-prefix Add this prefix to the MAC Address or Serial Number | |
-suffix Add this suffix to the MAC Address or Serial Number | |
-fromFile The path to a CSV file containing the computer's MAC Address or Serial Number followed by the desired name | |
username@computername ~ % |
You can also use the scutil command line tool for this task, if you don’t have the Jamf Pro agent installed. However, for the scutil tool, you would need to run the following commands to match the functionality as provided by the Jamf agent command listed above:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To have the serial number information be provided by the system, the following commands can be used:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For more information, please see below the jump.
To put all of what I discussed above together, I’ve written the following script which does the following:
This script is available below and also available on GitHub via the following link:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Sets the computer name to the machine's serial number. | |
# | |
# If the Jamf agent is installed, the script uses the Jamf agent to set | |
# the computer name to the machine's serial number. | |
# | |
# If the Jamf agent is not installed, the scutil command line tool is used. | |
jamfAgentPath="/usr/local/jamf/bin/jamf" | |
if [[ -x "$jamfAgentPath" ]]; then | |
"$jamfAgentPath" setComputerName -useSerialNumber | |
else | |
machineSerial=$(/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial Number/ { print $4; }') | |
if [[ -n "$machineSerial" ]]; then | |
/usr/sbin/scutil –set ComputerName "$machineSerial" | |
/usr/sbin/scutil –set LocalHostName "$machineSerial" | |
/usr/sbin/scutil –set HostName "$machineSerial" | |
fi | |
fi | |
exit 0 |