Change Hostname & Local Hostname of computers

BKZU
New Contributor II

Hello, I'm trying to figure out how to change the "Hostname" and "Local Hostname" of our fleet of MacBooks to use the Serial number instead of the name of the computer that ends up being the name of the employees, which is not great for privacy and security.

I tried many scripts from Jamf Nation and also Github but can't get any to work.

I don't want to force users to have their computer name using serial number tho, just hostname and localhostname.

Do you guys have a solution for that? Thank you

9 REPLIES 9

DBrowning
Valued Contributor II
#!/bin/sh

sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

hostName=$sn

scutil --set LocalHostName "$hostName"
scutil --set HostName "$hostName"

BKZU
New Contributor II

Thanks DBrowning, I've tested it and it simply works.

What would I need to add to that script if I also wanted to change the computer name to something like"MBP of Firstname.Lastname" but don't touch at hostname and local hostname?

Thanks

easyedc
Valued Contributor II

That depends on where that answer might be found.  

 

/usr/bin/id -F

 

 Will spit out the User Name that was provided when the account was created.  It depends on if that was filled out correctly.  Garbage in > garbage out. Also - what are you calling the "Computer Name" that you want to rename? Most folks that run renaming scripts touch the following

 

#!/bin/bash

#  Rename NW Serial.sh
#  
#
#  Created by Ed C on 11/5/21.
#  

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

diskutil rename / "${serial}"

 

 Which changes the name in all necessary places and renames the volume name (the icon of the hard drive on the desktop) to match.  You could easily modify that to make the id -F command as a variable, and pass that variable to any of the above locations that you want to change. Does that make sense? 

easyedc
Valued Contributor II

Forgot - you'd probably need to identify the user in question, so it'd be 

# Get User
Logged_In=$( /usr/bin/stat -f "%Su" /dev/console )
Full_Name=$(id -F "$Logged_In")

BKZU
New Contributor II

I'm talking about the name found in the "About" tab of the general pane of system preferences.

a_hebert
Contributor

This is what I use 

 

#!/bin/sh
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')
/usr/sbin/scutil --set ComputerName "${serial}"
/usr/sbin/scutil --set LocalHostName ${serial}
/usr/sbin/scutil --set HostName ${serial}
dscacheutil -flushcache

A_Collins
Contributor

jamf setComputerName -name "thenameyouwant" 

works perfectly fine rather than setting all

mm2270
Legendary Contributor III

I'm surprised no-one has mentioned this, but the jamf binary has a built in way to set the Computer and Hostname to the serial number without the need to grab the serial number some other way, like using system_profiler, which is kind of a slow executable.

/usr/local/bin/jamf setComputerName -useSerialNumber

To see all the options that setComputerName can use, just run jamf help setComputerName in Terminal.

Edit: Forgot to mention that you might have to add some additional commands in there to flush the cache to make sure the name sticks, but I've found this is also the case when just using scutil.

/usr/bin/dscacheutil -flushcache

The Mac tends to hang on to the current name a bit stubbornly unless you force it to forget the old name and adopt the new one you just applied.

BKZU
New Contributor II

Hi thanks for the tip, but this is not what I want, this command changes the computer's name and I don't want to force users on that, I just need the local hostname and hostname to change.