Script to name computers to SerialNumber

New Contributor II

This post is for people who are looking to name their computers to it's serial number. The script can be run with a policy at recurring check-in. Obviously there are multiple ways to set the computer name and the jamf binary can do it as well.

If anyone wants to add in the comments what they use for naming computers, I'm sure fellow Jamf Admin's would not mind.

#!/usr/bin/env bash

# Get the Serial Number of the Machine
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

# Set the ComputerName, HostName and LocalHostName
scutil --set ComputerName $sn
scutil --set HostName $sn
scutil --set LocalHostName $sn

Valued Contributor

You can also use the Jamf binary to do this in one go:

/usr/local/bin/jamf setComputerName -useSerialNumber

To add to the discussion, this is our post DEP method of prompting for the machine name during setup:

currentUser=$(ls -l /dev/console | cut -d " " -f 4)
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
#Prompt for Tag and sanitize
assetTag=$(sudo -u $currentUser -H osascript -e 'display dialog "Please enter this machines asset tag:" default answer "" buttons {"Submit"} default button "Submit"')
assetTagSanitized=$(echo "$assetTag" | awk 'BEGIN { FS = ":" } ; {print toupper ($3)}' | tr -d '[:space:]' | cut -c1-15) 
assetTagCheckHead=$(echo "$assetTagSanitized" | cut -c1-3)
assetTagCheckTail=$(echo "$assetTagSanitized" | cut -c4-15)

#If the provided name is not entered or it is not in our format, just use the machines Serial Number
if [[ "$assetTagSanitized" = "" || ! "$assetTagCheckHead" = "$headCriteria" || ! "$assetTagCheckTail" -gt 0 ]]; then

/usr/local/bin/jamf setComputerName -name "$assetTagSanitized"

#let it sleep so as to ensure the system has enough time to propagate the name change.
/bin/sleep 15

This isn't perfect but it gives us a decent base line and then we mop up any stragglers.

Valued Contributor II

We also change the name of the mounted Hard Drive. The MK is for Mac workstation (we call Windows OS WK...). It's a little ugly, I get, but it seems to handle where various file and storage formats, (fusion, APFS, JFHS, etc). It has started to fail on me recently with fusion drives, need to figure that out.


serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'`
/usr/sbin/scutil --set ComputerName "${base}${serial}"
/usr/sbin/scutil --set LocalHostName "${base}${serial}"
/usr/sbin/scutil --set HostName "${base}${serial}"

APFS=`/usr/sbin/diskutil list | grep "APFS"`
Apple_HFS=`/usr/sbin/diskutil list | grep "Apple_HFS"`
Fusion=`/usr/sbin/diskutil list | grep "virtual"`

if [ "$APFS" != "" ]; then
    diskutil rename disk1s1 "${base}${serial}"
elif [ "$Apple_HFS" != "" ]; then
diskutil rename disk0s2 "${base}${serial}"
elif [ "$Fusion" != "" ]; then
diskutil rename disk2 "${base}${serial}"

Contributor II

We had one to allow the IT department or user change the name from Self Service.


NewComputerName="$(osascript -e 'Tell application "System Events" to display dialog "Enter the Asset ID Number:" default answer ""' -e 'text returned of result' 2>/dev/null)"

jamf setComputerName -name "$NewComputerName"

jamf recon -assetTag "$NewComputerName"

New Contributor II

Thank you for sharing this script.

I also use after the scutil command this line at the end:

dscacheutil -flushcache

Have a nice day

Esteemed Contributor II

This came up today, we're using ioreg which is a bit faster than parsing system_profiler:


serialNumber=$( ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}' )

if [[ ${serialNumber} != "" ]]; then
    echo "Serial Number is $serialNumber, setting ComputerName/LocalHostName/Hostname."
    /usr/sbin/scutil --set ComputerName ${serialNumber}
    /usr/sbin/scutil --set LocalHostName ${serialNumber}
    /usr/sbin/scutil --set HostName ${serialNumber}
    echo "Serial Number is blank"

exit 0


@andrew.nicholas what trigger do you use? Id like to streamline the computer naming during DEP if possible. Right now the techs use a self service script to bind and set the name, but if I would set it to serial before all that it would make the "imaged and ready to use" machines not sit there with "somebody's macbook pro"

New Contributor III
New Contributor III

@ScottSimmons You could just run the script at enrollment complete (if you are just trying to name the machine as the serial number).

New Contributor III


The easiest way to rename the boot disk is with renameVolume. Target the root / volume.

/usr/sbin/diskutil renameVolume / "${COMPUTERNAME}"


Don't forget to run

jamf recon

at the end of your rename script so the new name gets reflected jamf database

New Contributor

How would i go about scripting something with this logic?

if serial number is "123456" then change host name to "computer1"

this would be helpful for managing a mac lab where im constantly deploying system images to large numbers of computers. any suggestions?

New Contributor

did you ever find how to do this ?


New Contributor III

@csksea check this link out.

New Contributor
How would i go about scripting something with this logic? if serial number is "123456" then change host name to "computer1"

First, export the Jamf computer list in .csv

oh wait...

Contributor II

Does anyone know what would have to change in a script if we wanted to append the serial number to a naming convention? We currently name our computers (Mac and PC's) with model-asset tag (for example "iMac-123456" or "mbp-123456"). Is there a way to keep the first part and append the serial number at the end? For example "iMac-SerialNumber".

Valued Contributor

@joethedsa You could use something like the below.

model=$(system_profiler SPHardwareDataType | grep "Model Name" | awk -F ":" '{ sub(" ",""); print $2}' | tr -d ' ')
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

Contributor II

@andrew.nicholas , thanks for the response. Since our environment has different models (iMacs, MacBook Pro, MacBook Air, MacMini), can you think of a way to add some smarts to the script where if it finds a particular text string like "iMac" it would then prefix the name with something predetermined? For example, if the command has identified an iMac, it would prefix it "im" so the rename of the computer would be something like "im-serialnumber" or if it identified a MacBook, it would name it "mb-serialnumber", etc.

Alternatively maybe there could be some interaction with the renaming of it so a policy would run, a dialogue window (oascript?) would display for text input. The person would type "im" or "mb" or "mini" etc. This text input would be the suffix to the rest of the computer name.

Valued Contributor

A case statement is exactly what you're looking for.

New Contributor

Below is what we're using...thanks to one of the fellow members here. L or D for laptop or desktop and then 8 characters from the serial number.


## 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...

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

## Create dummy receipt to mark complete
touch /Library/Receipts/

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