scutil scrpit help

Danj
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

type

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

pleae help me if below script is okay!

thank you

!/bin/sh

!/bin/bash

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

3 REPLIES 3

alexjdale
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:

#!/bin/sh

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:

#!/bin/sh

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

RonHunter21
New Contributor II

This is what mines looks like:

!/bin/bash

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

ryan_ball
Valued Contributor

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

computerName="populate_this_however_you_want"
jamf setComputerName -name "$computerName"