Skip to main content
Question

Change Hostname & Local Hostname of computers


Forum|alt.badge.img+4
  • Contributor
  • 10 replies

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

DBrowning
Forum|alt.badge.img+24
  • Esteemed Contributor
  • 668 replies
  • May 29, 2024
#!/bin/sh sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') hostName=$sn scutil --set LocalHostName "$hostName" scutil --set HostName "$hostName"

Forum|alt.badge.img+7
  • Contributor
  • 38 replies
  • May 29, 2024

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


Forum|alt.badge.img+4
  • Author
  • Contributor
  • 10 replies
  • May 29, 2024
DBrowning wrote:
#!/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


easyedc
Forum|alt.badge.img+16
  • Esteemed Contributor
  • 623 replies
  • May 29, 2024
BKZU wrote:

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? 


easyedc
Forum|alt.badge.img+16
  • Esteemed Contributor
  • 623 replies
  • May 29, 2024
easyedc wrote:

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")

Forum|alt.badge.img+4
  • Author
  • Contributor
  • 10 replies
  • May 30, 2024
easyedc wrote:

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.


A_Collins
Forum|alt.badge.img+11
  • Contributor
  • 85 replies
  • May 31, 2024

jamf setComputerName -name "thenameyouwant" 

works perfectly fine rather than setting all


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • May 31, 2024

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.


Forum|alt.badge.img+4
  • Author
  • Contributor
  • 10 replies
  • June 3, 2024
mm2270 wrote:

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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings