Naming New Macs

ampeelman
New Contributor

I'm wondering how others are handling setting hostnames of new Macs coming into their environment automatically with JAMF. Here's my scenario:

I have 20 new machines being deployed in my environment. After enrollment (or during the process), I want the hostnames/computer name set to our typical naming convention. For example, we'll say "NEWMAC01"," NEWMAC02", etc. I then want them to bind to our AD.

Without going to each one individually, or using remote on each machine individually, I'm not quite sure what the best method to accomplish this would be. Any suggestions or help is greatly appreciated!

6 REPLIES 6

allanp81
Valued Contributor

Assuming that they will have some human interaction when first configured, then you can cobble together something with bash/applescript that can prompt for the machine name and then the scutil command to set the names.

Landry
New Contributor

We run a "Rename" policy that's triggered by enrollment. Basically just a bash script that renames the machine with our standard prefix "Company + last 7 of serial number"

Then the rename script triggers a policy to set the NTP server setting and bind the machine to AD. With SplashBuddy on top so that the tech/user can get visual feedback of progress.

znilsson
Contributor II

Here's the script I use. I put this in a policy that's scoped to a smart group that looks for misnamed Macs, the criteria there can be anything you want.

#!/bin/sh

SerialNumber=`/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial/ { print $NF }' `

# Is this a portable? 0 = no, 1 = yes
BOOK=`/usr/sbin/system_profiler SPHardwareDataType | awk -F ": " '/Model Name/ { print $NF }' | grep Book | wc -l`

# Set system name variable based on the above two variables
if [ $BOOK == 1 ]
then
    NAME=WPL$SerialNumber
else
    NAME=WPD$SerialNumber
fi

/usr/sbin/scutil --set ComputerName $NAME
/usr/sbin/scutil --set HostName $NAME
/usr/sbin/scutil --set LocalHostName $NAME

tsmith182
New Contributor II

-Prompt for new computer name- snip of code that I have used. This runs and is triggered at login. Then use scutil to rename computer. Below is just an example to give an idea of what I have done.

#!/bin/sh
newCompName=$(osascript -e 'set compName to the text returned of (display dialog "Enter New Computer Name" default answer "")')
(path to scutil) --set HostName $newCompName
(path to scutil)  --set LocalHostName $newCompName
(path to scutil)  --set ComputerName $newCompName
(path to system setup) -setcomputername $newCompName

ampeelman
New Contributor

Thanks a lot everyone. All of these are very helpful. I think @tsmith182 has the answer that best suits my needs.

mistacabbage
Contributor

I use this script

#!/bin/bash

# Matt Kinabrew
# 2017.06.30
#
# Craig Kabis craigkabis@gmail.com
# Updated on 2019-03-03


# Get the current names before we make any changes
echo
echo "The ComputerName is currently set to: "
/usr/sbin/scutil --get ComputerName
echo

echo "The LocalHostName is currently set to: "
/usr/sbin/scutil --get LocalHostName
echo

echo "The HostName is currently set to: "
/usr/sbin/scutil --get HostName
echo

echo "***************************************************************"

# If system_profiler indicates that the system is not a Parallels Desktop Virtual Machine...
if [ $(system_profiler SPHardwareDataType | /usr/bin/grep -c "Model Identifier: Parallels") == 0 ]; then

# TO DO
# If system_profiler indicates that the system is not a VMware Fusion Virtual Machine...

# TO DO
# If system_profiler indicates that the system is not a Oracle VirtualBox Virtual Machine...

    # ...and if the boot volume is on a device that is not internal...
    if [[ $(/usr/sbin/diskutil info $(/sbin/mount | /usr/bin/grep "on / " | /usr/bin/cut -f 1 -d " " | sed "s//dev///g" | /usr/bin/cut -c 1-5) | /usr/bin/grep -c "Internal")  == "0" ]]; then

        # ...then set the hostname to "OSX-" + the last 7 characters of the MAC address for interface en0, capitalized, + "-EX".
        hostnameNew="OSX-"$(/sbin/ifconfig | /usr/bin/grep -A 3 "en0:" | /usr/bin/grep ether | /usr/bin/sed "s/ ether //g" | /usr/bin/sed "s/://g" | /usr/bin/sed "s/ //g" | /usr/bin/awk '{print toupper($0)}' | /usr/bin/sed 's/.*(.......)/1/')"-EX"

    else

        # ...then set the hostname to "OSX-" + the last 8 characters of the serial number of the computer + "-U".
        hostnameNew="OSX-"$(system_profiler SPHardwareDataType | /usr/bin/grep Serial | /usr/bin/sed 's/.*(........)$/1/')"-U"
    fi

else

    # ...then set the hostname to "OSX-" + the last 7 characters of the MAC address for interface en0, capitalized, + "-VM".
    hostnameNew="OSX-"$(/sbin/ifconfig | /usr/bin/grep -A 3 "en0:" | /usr/bin/grep ether | /usr/bin/sed "s/ ether //g" | /usr/bin/sed "s/://g" | /usr/bin/sed "s/ //g" | /usr/bin/awk '{print toupper($0)}' | /usr/bin/sed 's/.*(.......)/1/')"-VM"

fi


# Change the capital letters to lowercase letters
hostnameNewLowercase=$(echo $hostnameNew | tr '[A-Z]' '[a-z]')


# Echo out the new hostname so you can see what it is if you are running the script from the command line.
echo
echo "The new hostname will be:"
echo $hostnameNewLowercase
echo


# Set the new hostname.
/usr/sbin/scutil --set ComputerName $hostnameNewLowercase
/usr/sbin/scutil --set LocalHostName $hostnameNewLowercase
/usr/sbin/scutil --set HostName $hostnameNewLowercase

exit 0