Rename Script issues on macOS Monterey

New Contributor

Hello, I am having issues with the following script that worked great as a policy for Big Sur and older OS/Macs to rename Macs, but for some reason, Macs with M1/Monterey will not execute the rename commands as a Policy Script.



/usr/sbin/scutil --set ComputerName "Test-Mac"
/usr/sbin/scutil --set HostName "Test-Mac"
/usr/sbin/scutil --set LocalHostName "Test-Mac"

exit 0



New Contributor III

We switched to using the jamf binary to rename and that has worked out for our M1 macs and Monterey just fine.

jamf setcomputername -useSerialNumber


You could do something similar:

jamf setcomputername -name "Test-Mac"


The jamf command will take care of all 3 scutil commands that you were manually running.


Maybe someone else has something better, but this has been working for me on M1 mac with 12.2

Honored Contributor

Yep, do this once a week per Mac and it works great.  One line, all three names changed.

You can use any of the Apple System Information you wish to and it seems pretty foolproof.

Contributor II

This has been working for us without issue to date.

/usr/sbin/scutil --set HostName "$serialNumber"
sleep 3
/usr/sbin/scutil --set LocalHostName "$serialNumber"
sleep 3
/usr/sbin/scutil --set ComputerName "$serialNumber"
sleep 3
/usr/bin/dscacheutil -flushcache

Valued Contributor II

I have also found a pause is needed between scutil set computer name commands on Monterey. A sleep 1 between each one is getting the job done for us.

New Contributor

what's the general though on this one? It launches a rename box, type in the name and its set. And yes its three different scripts I've found over time on the jamf forums and crammed into one.  Working for me.. set it as a self service policy. 



# Get serial number
serialNumber=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'`

# Set name to serial number (in case name is not set by user)
jamf setcomputername -name "$serialNumber"

/usr/bin/dscacheutil -flushcache

# Get currently logged in user
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

# Get the logged in UID
loggedInUID=$(id -u $loggedInUser)

# Prompt for Computer Name as the user
/bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser whoami
computerName=$(/bin/launchctl asuser "${loggedInUID}" sudo -iu "${loggedInUser}" /usr/bin/osascript<<EOL
tell application "System Events"
with timeout of 60 seconds
set answer to text returned of (display dialog "Set Computer Name" with title "MyOrganization" default answer "$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')")
end timeout
end tell
# Check to make sure $computerName is set
if [[ -z $computerName ]]; then
echo "Computer Name not set. Exiting..."
exit 0
# Set name using variable created above
computerName=`echo $computerName | tr '[:lower:]' '[:upper:]'`
jamf setcomputername -name "$computerName"

/usr/bin/dscacheutil -flushcache

echo "Computer Name set to $computerName"
# Confirm Computer Name
/bin/launchctl asuser "${loggedInUID}" sudo -iu "${loggedInUser}" /usr/bin/osascript<<EOL
tell application "System Events"
display dialog "Computer Name set to " & host name of (system info) buttons {"OK"} default button 1 with title "MyOrganization" giving up after 15
end tell