How can we change the machine name before doing a adbinding

Tigerhaven
Contributor

Hi everyone, i would really appreciate anyhelp. we would like to push a policy that changes the machine name before the adbind. the reason we would like to do this so we can bind the machine using that name . the standard we would like to use is MB-(serial Number) which should populate from the machine. thanks

16 REPLIES 16

Look
Valued Contributor III

You could try setting the local names with commands in a script first.
Something like where $WSNAME is the Worskstation name.

scutil --set HostName $WSNAME
scutil --set ComputerName $WSNAME
scutil --set LocalHostName $WSNAME

mm2270
Legendary Contributor II

The commands above from @Look will set the 3 possible names the computer can have, if that's what you were looking for.
However, I think its safe to skip the hostname one, because that can get populated once the Mac is joined to your Active Directory. Typically the HostName ends up being something like "computername.domain.company.com" for an AD joined system.

If you're asking how to script renaming your Macs to the format you specified above, you can use a script as follows:

#!/bin/sh

serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')

MacName="MB-${serial}"

scutil --set ComputerName "$MacName"
scutil --set LocalHostName "$MacName"

From there you can run your AD joining policy or script, etc.
Note that I literally just typed the above script into the post window, so I have not tested it for errors, so make sure to test this out on a few machines first.

alexjdale
Valued Contributor III

I use the same three commands @Look does before binding. I don't skip any because of the whole "Mac sets its own hostname to reflect what it sees in DNS for its IP address because it has an identity crisis" thing.

gachowski
Valued Contributor II

Hey,

Do you really really need the "MB-"? If you can spend some time changing minds that might be the better solution. If you can go with straight serial numbers in Casper : )

We have to use a custom machine name and we run into a lot of issue because of it, this is just the 1st one you will run in to..

What happens when a VIP want the just release apple product that runs OS X but is not a MacBook? Or you later decide that somebody need a MacPro.... Yes you can script all of that but it get complicated fast : )

Is this "Like to have" or "Must have" and if it's must have there should be a good reason : ) we use a custom because that is the "way it's been done" : ) good luck : )

C

PS that said I have I have to do the same thing as Mike : ) "scutil --set"

bentoms
Honored Contributor III
Honored Contributor III

Hi All,

The jamf binary can do this in 1 line:

sudo jamf setComputername -useSerialNumber -prefix MB-

More information:

sudo jamf help setComputername

Usage:   jamf setComputerName [-target <target volume>] [-name <name>]
         [-useMACAddress] [-useSerialNumber] [-suffix <suffix>]
         [-prefix <prefix>] [-fromFile <path to file>]


     -target         The target drive to set the name on

     -name           The new name for the computer

     -useMACAddress      Generate the name using the MAC Address

     -useSerialNumber    Generate the name using the Serial Number

     -prefix         Add this prefix to the MAC Address or Serial Number

     -suffix         Add this suffix to the MAC Address or Serial Number

     -fromFile       The path to a CSV file containing the computer's MAC Address or Serial Number followed by the desired name

casper100
New Contributor II

In our environment, the standard Windows naming convention was machine serial number and they wanted me to adhere to that for AD but ensure that it was obvious it was a Mac for the Help Desk. I use Mnn (M for Mac and a 2 digit number that denotes the type or model and with a typical serial #, it keeps it at the 15 character limit. In the script below, M02 is a 21.5 iMac. I'm not a "from scratch" script writer so I lifted this from somewhere, probably JAMFNation, just to give props 🙂

#!/bin/bash


base="M02"
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
dscacheutil -flushcache

Tigerhaven
Contributor

Thanks @Look @mm2270][~gachowski][~alexjdale][~bentoms][~casper100, Thanks all of you for your help, all the information has been really beneficial. Also anyone from Bay area here?

Tigerhaven
Contributor

@look @mm2270 @gachowski Macbook Air acts weird to this script.

nixonc85
New Contributor III

Just a quick addition to this, if you don't want OS X to try and change your computer name to what it discovers in DNS, you will need to do the following in Yosemite:

/usr/bin/defaults write /System/Library/LaunchDaemons/com.apple.discoveryd.plist ProgramArguments -array-add -string "--no-namechange"

Tigerhaven
Contributor

thanks @nixonc85 is this going to effect any other OS when we run this after running machine name changing script

nixonc85
New Contributor III

@Tigerhaven com.apple.discoveryd is a new service in Yosemite.

For previous versions you want:

vi /etc/hostconfig

HOSTNAME=<your hostname>

Tigerhaven
Contributor

@nixonc85 hi thanks for your reply , in email i got this from you,
For previous versions you want:
/usr/bin/defaults write /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist ProgramArguments -array-add -string "-NoMulticastAdvertisements" but in jamfnation it comes different

CCA Badge

Posted: 31 minutes ago by nixonc85

@Tigerhaven com.apple.discoveryd is a new service in Yosemite.

For previous versions you want:

vi /etc/hostconfig

HOSTNAME=<your hostname>

nixonc85
New Contributor III

@Tigerhaven sorry had a brain fart and amended my post. Ignore the email! d'oh!

Tigerhaven
Contributor

the script above on mac air and yosemite gives
Computer name (customer MacBook Air) does not match the record in the JSS.
Set Computer Name to customer MacBook Air

Tigerhaven
Contributor

@nixonc85 do you have a messenger bro that we can chat

cassielevett
New Contributor

I found mm2270 post really useful.