Computer name

jamfmdm
New Contributor

We are facing issue with the computer name when we are trying to enroll it to our jss. what we did is we reimage all our harddrive using imaging tool and name the computer name as "UAS", and now when we are trying to change it to other computer name it will change but after restarting the macbook it will go back again to "UAS" or "UAS (2)" but on the jss it did not change, only on the computer name of the macbook.

29 REPLIES 29

chet_bishop
New Contributor III

You have to change the name in 3 different places on the computer in order for it to stick. Below is the 3 commands we use to change computer names.

scutil --set HostName $COMPUTERNAME
scutil --set LocalHostName $COMPUTERNAME
scutil --set ComputerName $COMPUTERNAME

easyedc
Valued Contributor II

Here's the script we run. We set the workstation name by the serial number. This will check for new machines ("Macintosh HD") or machines that got wiped and not renamed by disk utility ("Untitled 1"). Also, and I am sure someone else on here can elaborate why, but 10.10 (I think it was) started setting the boot disk as disk0 vs disk1. It also sets the hard drive name to the serial number for easier identification.

#!/bin/bash

serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'`
/usr/sbin/scutil --set ComputerName "${base}${serial}"
/usr/sbin/scutil --set LocalHostName "${base}${serial}"
/usr/sbin/scutil --set HostName "${base}${serial}"

HDDName=`/usr/sbin/diskutil list | grep "Macintosh HD" | grep disk0`
HDDCSName=`/usr/sbin/diskutil list | grep "Macintosh HD" | grep disk1`
UntName=`/usr/sbin/diskutil list | grep "Untitled 1" | grep disk0`
Unt2Name=`/usr/sbin/diskutil list | grep "Untitled 1" | grep disk1`


if [ "$HDDName" != "" ]; then
    diskutil rename disk0s2 "${base}${serial}"
elif [ "$HDDCSName" != "" ]; then
    diskutil rename disk1 "${base}${serial}"
elif [ "$UntName" != "" ]; then
    diskutil rename disk0s2 "${base}${serial}"
elif [ "$Unt2Name" != "" ]; then
    diskutil rename disk1 "${base}${serial}"
fi

stevewood
Honored Contributor II
Honored Contributor II

@jamfmdm the reason you are seeing the name not change in the JSS is because you are most likely not doing a recon after changing the computer name. Using one of the methods that others have posted (utilizing scutil), the last step is to recon the computer with jamf recon. If you do not do this, the JSS will not know of the name change until the next scheduled recon.

jamfmdm
New Contributor

@chet.bishop i will run that command on terminal right? before or after enrolling the macbook we need to run that command?

jamfmdm
New Contributor

@stevewood after changing the computer name we should run jamf recon to the computer that we made change?

easyedc
Valued Contributor II

@jamfmdm What I do is enroll the workstation via a Quick Add package, and set the rename script as self service policy along with a few other-post image actions (AD Bind, etc). And once that's done, I run a recon then. You can push this script via Casper Remote with a checked-box on Update Inventory and it will catch the name change then, too. You could also add a

jamf recon

at the end of the script, which will do it every time. I find that unnecessary/overkill because I'm usually doing other things that will require a recon as well, so I try to keep that action at a minimum to save time.

BTW - why are you naming each computer with the same name? Isn't that a confusing workflow?

stevewood
Honored Contributor II
Honored Contributor II

@jamfmdm yes, after you have changed the name on the computer, you need to run jamf recon on the computer to change the name in the JSS.

jamfmdm
New Contributor

@stevewood but after enrolling the computer i could not change the computer name anymore. its grayed out. is there any command i could force the computer name on the jss going to the computer?

jamfmdm
New Contributor

@easyedc i'm not naming them as same. what i'm doing i reimage all our harddrive from a fresh yosemite os then we are changing the computer name as per user name. then after the enrollment it will change again to the default computer name.

easyedc
Valued Contributor II

How are you imaging? If you're using Casper Imaging, it can write the name for you based on a few variables.

chet_bishop
New Contributor III

@jamfmdm here is the full script we use and then we just created a policy within self service to run. Otherwise you can run those commands individually to change them in terminal. Scripting it is much easier though :)

#!/bin/sh

# Set CocoaDialog Location
###CD="/Path/to/CocoaDialog.app/Contents/MacOS/CocoaDialog"
CD_APP="/private/var/ahc-umn/bin/CocoaDialog.app"
CD="$CD_APP/Contents/MacOS/CocoaDialog"

# Dialog to enter the computer name and the create $COMPUTERNAME variable
rv=($($CD standard-inputbox --title "Computer Name" --no-newline --informative-text "Enter the computer name you wish to set"))
COMPUTERNAME=${rv[1]}

# Set Hostname using variable created above
scutil --set HostName $COMPUTERNAME
scutil --set LocalHostName $COMPUTERNAME
scutil --set ComputerName $COMPUTERNAME

# Dialog to confirm that the hostname was changed and what it was changed to.
tb=`$CD ok-msgbox --text "Computer Name Changed!" 
--informative-text "The computer name has been changed to $COMPUTERNAME" 
--no-newline --float`
if [ "$tb" == "1" ]; then
echo "User said OK"
elif [ "$tb" == "2" ]; then
echo "Canceling"
exit
fi

tdilossi
Contributor

I use ARD to change the name of the machine. Send a unix command just make sure you are sending it as root.

sudo scutil --set LocalHostName "NEW NAME HERE"
sudo scutil --set HostName "NEW NAME HERE"
sudo scutil --set ComputerName "NEW NAME HERE"
sudo scutil --get LocalHostName
sudo scutil --get HostName
sudo scutil --get ComputerName
killAll cfprefsd

stevewood
Honored Contributor II
Honored Contributor II

@jamfmdm as @chet.bishop posted above, you can use a script as part of a Self Service policy to change the name. His script uses cocoaDialog to ask the user what name you want the computer to be, renames the computer, and then displays the new computer name, again using cocoaDialog. The only thing I would say about this method is to either place jamf recon at the end of the script (perhaps before displaying the name) or make sure that the policy has "Update Inventory" selected on the Maintenance tab of the policy:

61de10038e004d0dae8731e8e6678837

You can also simply open Terminal and then issue the following commands, filling in <computername> with the name you want the computer to be:

sudo scutil --set ComputerName <computername>
sudo scutil --set HostName <computername>
sudo scutil --set LocalHostName <computername>
jamf recon

That will rename the computer and then recon the computer, which will change the name in the JSS.

To answer your last question to me, whether you can force the name change from the JSS, the answer is yes. If you set the name in the JSS, you can update your inventory update policy (mine is "Update Inventory") or create a new policy to update inventory, and on the Maintenance tab choose "Update Inventory" and "Reset Computer Names". You can see that I have both of those checked in my Update Inventory policy that I posted above.

Hope that helps.

jamfmdm
New Contributor

@chet.bishop @stevewood i will enable it to our selfservice what should i put for the policy trigger?

jamfmdm
New Contributor

@chet.bishop the script you made will ask the user what computer name they want to put? but when i tried to use it using self service there is an error. do i need to put any parameter?

jamfmdm
New Contributor

is there any way or script that will make the the computer name as the username in the inventory of our jss?

bentoms
Esteemed Contributor
Esteemed Contributor

@jamfmdm You could do that using the API... But why based on Username?

Typically we set names to the Macs SerialNumber... & the JAMF binary can do this easily.

This has the benefit of being the same name that's on the device as well as (by coincidence), falling in with AD's character limitiations for Computer Objects.

Then, if you want to track who's Mac is who's.. You can search for the username in the JSS.

jamfmdm
New Contributor

@bentoms it is more easier on our part who owns or use the laptop. and it is more easier to push some application that teacher need to be installed.

jamfmdm
New Contributor

This is the error message that we are getting if the computer will change automatically.40725059b955476f8479d33596f1e420
that will just pop out 6436a31b86a24952851a3a5a844c80ac the computer name will just change

jamfmdm
New Contributor

@chet.bishop hi. i'm trying the script that you gave me but still not working.i even tried to find the cocoadialog location but it's not showing to me. i think your script is good for our environment if that will work.

chet_bishop
New Contributor III

@jamfmdm We install cocoadialog on all of our machines. Do you have the cocoadialog app installed on your devices? If not, I believe you can download it from github and install the package.

Are you receiving an error at a certain part?

jamfmdm
New Contributor

@chet.bishop it's working now. thanks! but are still facing issue with changing name automatically. but the that you gave is good to our environment. :)

mfoley
New Contributor

I tried running the script posted by easyedc within a policy and I get this message:

Executing Policy Computer Name Change
Running script Computer Name Change...
Script exit code: 1
Script result: Volume must be mounted

What do I need to do to resolve this?

thanks

chris_kemp
Contributor III

I used to run a disk renaming script at my last gig, I think it's a bit more straightforward, no need to try to test for which disk it thinks it is:

#!/bin/sh

export VolumeName=`diskutil info / | grep "Volume Name" | cut -c 30-`

if [ "$VolumeName" != "Macintosh HD" ];
then diskutil renameVolume "$VolumeName" "Macintosh HD"
fi

quedayone
Contributor

Long live Apple Script!

#!/usr/bin/osascript
-- script to change computer name so it is NOT new users macbook pro
-- Ask for new computer name

set COMPUTERNAME to text returned of (display dialog "Please enter new Computer Name:" default answer "unameXXmbpXX" with icon 2)
display dialog "New Computer name will be: " & COMPUTERNAME
--
# Set Hostname, LoacalHostName, and ComputerName using variable created above
do shell script "scutil --set HostName " & COMPUTERNAME
do shell script "scutil --set LocalHostName " & COMPUTERNAME
do shell script "scutil --set ComputerName " & COMPUTERNAME
--
--get new names and set as new variable 
--
set newHostName to do shell script "scutil --get HostName"
set newLocalHostName to do shell script "scutil --get LocalHostName"
set newComputerName to do shell script "scutil --get ComputerName"
--
-- Display dialog to confirm computer names were changed.
display dialog "The computer Host Name has been changed to: " & newHostName & "

The computer Local Host Name has been changed to: " & newLocalHostName & "

The computer Name has been changed to: " & newComputerName buttons {"Roger That"} default button "Roger That" with icon 2

bcrockett
Contributor

quedayone,

I like your script! I plan to run it through a self-service policy during my next deployment.

Cheers,

~B

Snickasaurus
Contributor

This is the script I used as part of a post image process. It checks to see if the model is a "MacBook" of any kind and stamps
either an "L" if so, or a "D" if not, to the file located in the following command.

touch /private/var/log/fv2.log

It then renames the machine with that letter + serial number. A post image policy checks that file and if an "L" is found it kicks off another policy that starts FileVault encryption. Use the whole thing, take bits and pieces or ignore all together and use one of the above fine methods. Perhaps it will help someone

Also don't forget. Aside from running the "scutil" command to change the name you also have to run:

defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName

Or you will see a different name on the network than what the machine shows you at the terminal.

#!/bin/sh

# Name    : postImage0_SetComputerName.sh
# Author  : Snickasaurus
# Date    : 20141113
# Purpose : Set machine name and stamp fv2 file.

# Variables
theSerial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
theModel=$(system_profiler SPHardwareDataType | grep 'Model Name:' | awk '{ print $3 }')

# Logging
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>>/private/var/log/postImage0-"$(date "+%Y%m%d-%H%M%S")".log 2>&1

# Begin script
touch /private/var/log/fv2.log

# Echo the current name settings to the log file.
echo "-----"
echo "-"
echo "-"
echo "Current ComputerName is: $(scutil --get ComputerName)"
echo "Current HostName is: $(scutil --get HostName)"
echo "Current LocalHostName is: $(scutil --get LocalHostName)"
echo "Current NetBIOSName is: $(defaults read /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName)"
echo "-"
echo "-"
echo "-----"

# Check for the model type and set it's name accordingly.
if [[ "$theModel" == "MacBook" ]]
    then
    scutil --set ComputerName "L$theSerial"
    scutil --set HostName "L$theSerial"
    scutil --set LocalHostName "L$theSerial"
    defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName "L$theSerial"
    echo "L" > /private/var/log/fv2.log
else
    echo "D" > /private/var/log/fv2.log
    scutil --set ComputerName "D$theSerial"
    scutil --set HostName "D$theSerial"
    scutil --set LocalHostName "D$theSerial"
    defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName "D$theSerial"
fi

# Space out the name lines.
echo " "
echo " "

# Echo the new name settings to the log file.
echo "-----"
echo "-"
echo "-"
echo "New ComputerName is: $(scutil --get ComputerName)"
echo "New HostName is: $(scutil --get HostName)"
echo "New LocalHostName is: $(scutil --get LocalHostName)"
echo "New NetBIOSName is: $(defaults read /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName)"
echo "-"
echo "-"
echo "-----"

exit 0

easyedc
Valued Contributor II

Since this thread is getting bumped, Is anyone having issues with having a single script and HS/APFS/Fusion Drives? My script works when run locally, but does not work when run by my JSS a policy.

#!/bin/bash

base="MK"
serial=$(/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}')
/usr/sbin/scutil --set ComputerName "${base}${serial}"
/usr/sbin/scutil --set LocalHostName "${base}${serial}"
/usr/sbin/scutil --set HostName "${base}${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 "${base}${serial}"
elif [ "$Apple_HFS" != "" ]; then
diskutil rename disk0s2 "${base}${serial}"
elif [ "$Fusion" != "" ]; then
diskutil rename disk2 "${base}${serial}"
fi

yadin
Contributor

I'm not seeing the "Update Inventory" option under Maintenance, or "jamf recon" put in under Execute command in Files and Processes actually work as stated in this thread. The logs indicate recon is run, however the name of the computer in JAMF never updates. The result is rather disastrous at this point as it is causing names to not only be out of sync, but get broken by another policy that should fix names that are not right. It's critical that JAMF show the proper name of a device so that smart groups work accurately, we need a solution for this.

This is compounding the frustration that JAMF completely lacks the ability to name a DEP placeholder and therefore have the proper name pushed to the machine at enrollment. Profile Manager does this, baffling that JAMF can't. As a result, we have a script run at enrollment that names the machine based on DNS lookup. Works fine. However, JAMF then fails to ever get this name from the device. A second policy renames machines to their record name if they don't match a convention in a smart group. This is designed so that if DNS is not correct at enrollment, we can correct the name manually in the console, it will push down to the machine. Because JAMF is not updating the record properly, the policy is pushing generic names down to machines that have proper names. It's a real mess, and I can't figure out how something so basic, and critical to AD binding, is so severely lacking. Directory binding at enrollment is impossible since pre-stage devices can't be named (so why have a feature that can't be used?), and naming past that seems entirely unreliable.