Posted on 05-29-2024 03:21 AM
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
Posted on 05-29-2024 04:40 AM
#!/bin/sh
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
hostName=$sn
scutil --set LocalHostName "$hostName"
scutil --set HostName "$hostName"
Posted on 05-29-2024 08:57 AM
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
Posted on 05-29-2024 09:35 AM
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?
Posted on 05-29-2024 09:39 AM
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")
Posted on 05-30-2024 01:20 AM
I'm talking about the name found in the "About" tab of the general pane of system preferences.
Posted on 05-29-2024 07:57 AM
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
Posted on 05-30-2024 09:52 PM
jamf setComputerName -name "thenameyouwant"
works perfectly fine rather than setting all
05-31-2024 02:52 PM - edited 05-31-2024 02:55 PM
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.
Posted on 06-03-2024 01:27 AM
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.