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
#!/bin/sh
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
hostName=$sn
scutil --set LocalHostName "$hostName"
scutil --set HostName "$hostName"
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
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
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?
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?
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")
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?
I'm talking about the name found in the "About" tab of the general pane of system preferences.
jamf setComputerName -name "thenameyouwant"
works perfectly fine rather than setting all
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.
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.
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.