scutil scrpit help

New Contributor

I am really new to scripting, and i was thinking if i can make a script for computer name, Localhostname, and hostname.

i used to just open terminal then


sudo scutil --set Computer Name XXX
sudo scutil --set LocalHostName
sudo scutil --set HostName

pleae help me if below script is okay!

thank you



Enter the Computer name using scutil

setName=scutil --set ComputerName

scutil --set LocalHostName ${setName}
scutil --set HostName ${setName}

echo HostName and LocalHostName set to ${setName}

exit 0


Valued Contributor III

Oh boy. Lots of stuff there will break your script.

First, you need a way for the script to know what name you want. Besides that, the script would look more like this:


scutil --set ComputerName $setName
scutil --set HostName $setName
scutil --set LocalHostName $setName

If you are trying to read in the ComputerName and set that for the other two names, that would look like:


setName=$(scutil --get ComputerName)
scutil --set HostName $setName
scutil --set LocalHostName $setName

New Contributor II

This is what mines looks like:


Simple script that names the computer.

Last 8 characters of the Serial Number:

definedComputerName="/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial Number/ { print $NF }' | rev | cut -c 1-8 | rev"

/usr/sbin/scutil --set ComputerName ${definedComputerName}
/usr/sbin/scutil --set LocalHostName ${definedComputerName}
/usr/sbin/scutil --set HostName ${definedComputerName}

exit 0

Valued Contributor

If you are talking about Jamf managed Macs, you could also use the Jamf binary to accomplish this with less hassle:

jamf setComputerName -name "$computerName"