Renaming Mac without restarting

RickDalton
New Contributor III

I have this script to rename the mac to its serial number during the enrollment process.

ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}' | sed 's/"//g' | scutil --set HostName
ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}' | sed 's/"//g' | scutil --set LocalHostName
ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}' | sed 's/"//g' | scutil --set ComputerName

This script requires a restart to take effect. I have another policy that runs to bind the Mac to the AD but the Mac needs to be named its serial number in order for the AD binding policy to work. Restarting stops the remaining enrollment automation so is there a way to add anything to this script change the computer name without restarting the mac?

11 REPLIES 11

NowAllTheTime
Contributor III

I'm not sure what else you are doing in your script, but these three commands don't require a reboot. The scutil commands are fairly typical for how folks programmatically set the name(s) on Macs. So there has to be something else in your script triggering the restart that you didn't show us here, or you have a mandatory restart set in the policy that runs this script. Unless your doing something else in the script that truly does require a restart then all you need to do is remove the lines from your script that trigger the reboot and/or remove the restart payload of your Jamf policy that deploys this script.

easyedc
Valued Contributor II

Yeah, agreed. Don't need to reboot it. We push this with a policy. And we set the drive's name to the serial as well.

#!/bin/bash
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}"

APFS=$(/usr/sbin/diskutil list | grep "APFS")
Apple_HFS=$(/usr/sbin/diskutil list | grep "Apple_HFS")
Fusion=$(/usr/sbin/diskutil list | grep "virtual")

if [ "$APFS" != "" ]; then
diskutil rename disk1s1 "${serial}"
elif [ "$Apple_HFS" != "" ]; then
diskutil rename disk0s2 "${serial}"
elif [ "$Fusion" != "" ]; then
diskutil rename disk2 "${serial}"
fi

howie_isaacks
Valued Contributor II

Maybe I'm weird, but Iike machine names to include the user's name so it's not a pain to locate someone's Mac on a network quickly. Why rename the drive name to the serial number too? Am I missing something on this? Is there some huge advantage to naming computers with the serial number?

AVmcclint
Honored Contributor

@howie_isaacks If the computer name is too long, Active Directory may complain or just truncate the name anyway. I would also prefer to have the users' names in there as well, but as long as I assign a user to the computer record in JSS, I can display that column in the inventory to find a particular user/computer.

PeterClarke
Contributor II

AVmclient is correct, the Active Directory 'Name Limit' is only 15-Characters !!

And it's "Fussy" about those too… So best if AlphaNumeric only, without any spaces, or symbols. (although ours does include hyphens)
As otherwise sometimes there are problems.

Getting a "Unique" name into only 15-Characters (especially if you want it to also be meaningful), can be challenging..

Ours includes a GeoTagging element, Site-User-Room,
if the room part gets truncated, that less of a problem then if the user identifier get truncated.

easyedc
Valued Contributor II

So the other assumption about the serial number as the computer name is that it never changes. We are in an AD bound environment, where theoretically any user could walk up to a computer and log in. Doesn't happen often, but we do have some groups that co-code on one box. Who to assign it to at that point?

stevewood
Honored Contributor II
Honored Contributor II

Any reason y'all aren't using jamf setComputerName -name <newname> instead of scutil? I used to go the "long" route of using scutil but have started using the jamf binary command.

We have a large environment (9000 enrolled and still going) so we had to take collisions into consideration, which meant using the end user name was not going to cut it. Also, we didn't want to have to unbind and re-bind each time a machine changed hands in agencies where they are still binding (we're moving everyone to NoMAD slowly). So we came up with a naming schema that uses country, major business unit, minor business unit, Mac or Windows, VM, and 7 digits of the s/n starting with the 4th digit. We found by grabbing from the middle of the s/n that we ran into fewer possible collisions (less than 1% in a sample fo 5000 devices).

It doesn't make it super easy to pin a device to a user by looking at the name, but it does give us something very unique (yes s/n alone would have given us that) and helps us narrow down where to look. We can then tie the device as a CI in ServiceNow to a user, or use the user name field of the computer record in Jamf.

howie_isaacks
Valued Contributor II

@AVmcclint Ah. Active Directory. I only bind Macs to AD if there's an onsite file server, or some other system that would benefit from a user logging into their Mac with AD credentials. Only one of our Mac clients has an AD server.

AVmcclint
Honored Contributor

I do miss the days of doing work for smaller companies where I could name the computers descriptive but still generic names like "Designer MacPro 1" "Marketing MBA 3" "Accounting iMac"... Then I'd put the name of the person currently who currently holds that position in an ARD Comment field. People come and go more frequently than computers sometimes, so this maintains some stability and user friendliness in the naming convention in those smaller environments. But now I'm in a 700 PC + 100 Mac AD world. Serial numbers it is.

easyedc
Valued Contributor II

So @stevewood unless I'm wrong, that sounds like a very manual process?

a naming schema that uses country, major business unit, minor business unit, Mac or Windows, VM, and 7 digits of the s/n starting with the 4th digit

For us, the benefit of just using Serial number is automation. But we're not as big as you, and we're national, not international, so country location does not impact. We did at one point do some geographic-based naming with multiple scripts available to name, but that again required more touching to setup. We have one site where they are known to computer hop (devs!, am I right?!) and they wanted to give each workstation a creative name, like Spiderman, or Hulk or whatever latest MCU movie was out, but that was nixed by our security as "potentially unsafe" for some reason.

stevewood
Honored Contributor II
Honored Contributor II

@easyedc for a rename, yes it can be, but during machine provisioning we utilize a script to build the name. The scripts are built per major business unit (for now, although we are collapsing into one script that uses CSV to workout the code) and utilize case statements.

Here's a little bit of the code, scrubbed of course:

serial=$(system_profiler SPHardwareDataType | awk '/Serial Number (system)/ {print $NF}');

## Get Country
country=`$CD standard-dropdown --height 125 --text "Please choose the Country:" --float --no-cancel --items "Canada" "United States" "United Kingdom" --string-output --icon gear`
country=`echo $country | cut -d' ' -f2-`

case ${country} in

'Canada')
    countryCode='CA'
    ;;

'United Kingdom')
    countryCode='GB'
    ;;

'United States')
    countryCode='US'
    ;;

esac

## Get the Agency ##
agency=`$CD standard-dropdown --height 125 --text "Please choose the Agency:" --float --no-cancel --items "Minor1" "Minor2" "Minor3" --string-output --icon gear`
agency=`echo $agency | cut -d' ' -f2-`

## get the code for the name
case ${agency} in

'Minor1')

    agencyCode='MI1'
    ;;

'Minor2')

    agencyCode='MI2'
    ;;

'Minor3')

    agencyCode='MI3'
    ;;
esac

## start gathering data that we will need and set the computer name
unique_id=${serial:3:7}
compName="${country}3${agencyCode}M0${unique_id}"
showCompName=`$CD ok-msgbox --no-cancel --title "Computer Name" --text "Computer name: ${compName}" --icon-file /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns`
ScriptLog "Machine name: $compName"

Like I said, this is coded specifically for each major business unit (we have 6), so we can make some assumptions (major business unit code for example).