Change hostname of enrolled Mac

dlprentice
New Contributor III

Hi Guys!

Is there a simple, and effective way to change a hostname of a joined Mac via Jamf? We have a little bit too much "David's MacBook Air" going on :)

1 ACCEPTED SOLUTION

Tribruin
Valued Contributor II

Reset Computer Name renames the computer to the name in Jamf. You could just rename every computer in Jamf and then have a policy that runs regularly to Reset Computer Name. Not sure how easy that is. If you have a lot of computers, you could try MUT to update the names.

You could also run the script/policy to set the computer name and still use to the Reset Computer Name policy to fix it, if user's try and change it.

View solution in original post

12 REPLIES 12

Tribruin
Valued Contributor II

You have a lot of options here. The "easiest" way is to write a script that using jamf setComputerName -name <<newComptuerName>> and assign it to a policy.This will update the computer name. How do you want to name the computers?

Include an Inventory Update in your policy to ensure the new name is captured by Jamf.

dlprentice
New Contributor III

I just noticed something when going into the Inventory Update.

Would the "Reset Computer Names" under Maintenance not have the desired effect?

Tribruin
Valued Contributor II

Reset Computer Name renames the computer to the name in Jamf. You could just rename every computer in Jamf and then have a policy that runs regularly to Reset Computer Name. Not sure how easy that is. If you have a lot of computers, you could try MUT to update the names.

You could also run the script/policy to set the computer name and still use to the Reset Computer Name policy to fix it, if user's try and change it.

tlarkin
Honored Contributor

I just force the hostname (all 3 of them) to the serial number on a daily basis since all our users are admins and with sudo you can change whatever hostname you want.

howie_isaacks
Valued Contributor II

For my clients I use a naming scheme that uses the company's initials, "M" for Mac, and then the user's first and last initials... AC-M-HI... Acme Corp, Mac, Howie Isaacks... I did not want to have to manually rename Macs every time one is enrolled, so I wrote this script with some help from other Jamf Nation members. When ever I get around to it, I will make a version of the script that will allow us to define a variable for the first part of the script for the company so we don't have to manually modify this script every time it's added to a Jamf Pro server. We would just go into the policy containing the script and set the company initials in the field provided. I have this script run as the first policy that runs at check-in after the user account is setup and the desktop appears. It appears to run at some point either before or after DEPNotify is finished. This script was useful when I needed to mass rename a lot of Macs. There were some users with the same initials, so we dealt with those manually. There were only a few so it wasn't a bit deal. 

 

#!/bin/sh

#Rename the Mac using the first and last initial of the user's name. Before using this script, replace "XX" in line 12 with the client company's initials

#Who is the current logged in user?
currentUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
echo $currentUser

#Generate computer name - Replace "XX" with the client company's initials
firstInitial=$(finger -s $currentUser | head -2 | tail -n 1 | awk '{print toupper ($2)}' | cut -c 1)
lastInitial=$(finger -s $currentUser | head -2 | tail -n 1 | awk '{print toupper ($3)}' | cut -c 1)
computerName=$"XX-M-$firstInitial$lastInitial"

echo $computerName

#Send the computer name to inventory record
scutil --set ComputerName $computerName
scutil --set LocalHostName $computerName
scutil --set HostName $computerName

#Clear the directory service cache then run a current inventory to send the new Mac name to Jamf Pro
dscacheutil -flushcache
/usr/local/jamf/bin/jamf recon

#Howie Isaacks with help from Jamf Nation | F1 Information Technologies | 09/27/21

 

Hello! This is pretty helpful, but I do not know enough about scripting to use this as a template (yet). I do basic Kaseya scripts here at work and am still coming up to speed on learning this stuff. :)

We are looking to have Jamf push the Macbook name based on this formula: first-last-serial#ofMacbook.

So Alex Bryant with Macbook serial FVFVG2G4Q6L7 should be named: ALEX-BRYANT-FVFVG2G4Q6L7

Would it be possible for you (or someone on this thread) to post how this script could be altered to produce that? Thank you very much!

To get the serial number, this command should do it:

 

serial=$(system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }')

 

The variable "serial" can be changed to what ever you want it to be. After this command is ran, typing echo $serial will produce the serial number. Later in the script when you use "$serial" in the script that will input the serial number into the line of script you need it to be in.

 

If you type this command in Terminal it should show your Mac's serial number. 

 

system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }'

 

Let me know if you would like a script that names the Mac the way you described above. When I have a moment today I can write it for you. 

You wanted the user's name to be in all caps. I am trying to figure out how to get AWK to output the first and last name as all caps but here's what I have so far. It outputs a computer name that includes the first name, last name, and serial number separated by dashes. It then runs the commands to rename the computer and updates the inventory. It may not be necessary to have the "sleep 5" commands but I found that sometimes waiting a few seconds after each scutil command used in a script makes the process more reliable.

#!/bin/sh

#What is the serial number of the computer?
serialNumber=$(system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }')

#who is the current logged in user?
currentUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`

#What is the first name of the user?
firstName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $1 }')

#What is the last name of the user?
lastName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $2 }')

#Genrate computer name
computerName=$firstName-$lastName-$serialNumber
echo $computerName

#Set the computer name. Wait 5 seconds after each command.
scutil --set ComputerName $computerName
sleep 5
scutil --set LocalHostName $computerName
sleep 5
scutil --set HostName $computerName
sleep 5

#Clear the directory service cache then run a current inventory to send the new Mac name to Jamf Pro
dscacheutil -flushcache
/usr/local/jamf/bin/jamf recon

I ran this script through CodeRunner and the output was as expected. It was my first name, last name, and my Mac's serial number separated by dashes. I suggest NOT using a user's full name in a computer name. Initials or some shortened version will protect the user's identity when they're on a public WiFi network.

howie_isaacks
Valued Contributor II

This is a little harder than I thought it would be. Still working on it as I have time.

howie_isaacks
Valued Contributor II

I couldn't let this go. I wanted to create a script that would do exactly what @redfinjamfadmin asked for above. Here it is:

 

#!/bin/sh

#What is the serial number of the computer?
serialNumber=$(system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }')

#who is the current logged in user?
currentUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`

#What is the first name of the user?
firstName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $1 }')

#What is the last name of the user?
lastName=$(dscl . -read /Users/$currentUser RealName | cut -d: -f2 | sed -e 's/^[ \t]*//' | grep -v "^$" | /usr/bin/awk '{ print $2 }')

#Convert firstName and lastName to all caps
firstNameCAP=$(echo $firstName | tr '[a-z]' '[A-Z]')
lastNameCAP=$(echo $lastName | tr '[a-z]' '[A-Z]')

#Genrate computer name
computerName=$firstNameCAP-$lastNameCAP-$serialNumber
echo $computerName

#Set the computer name.
scutil --set ComputerName $computerName
scutil --set LocalHostName $computerName
scutil --set HostName $computerName

#Clear the directory service cache then run a current inventory to send the new Mac name to Jamf Pro
dscacheutil -flushcache
/usr/local/jamf/bin/jamf recon

 

I was trying to get awk to convert the characters in the first and last name to all caps but I couldn't find a good way. I did some research and found the translate characters command (tr) and it has the ability to do this. If you reverse the a-z function and make it '[A-Z' '[a-z]' it will change all caps to lowercase.

I hope this helps someone! Everyone at Jamf Nation has always helped me when I need it so I'm happy to help when ever I can.

 

 

Newer Jamf admin here and I wanted to say thanks for this script! It's super helpful as we begin to standardize hostnames.

My pleasure! Practice makes perfect. I first posted to this thread almost 2 years ago. I have since learned a lot of different ways to name computers. There's always someone here with the knowledge and generosity you need to find the way to get something to work. Everyone here is awesome! They have saved me a lot of headaches for almost 10 years since I've been part of Jamf Nation.