Skip to main content

Hello all I'm migrating on to another network. I was wondering if anyone has a script on renaming a computer name?

man scutil

It has options there to "--Set ComputerName | LocalHostName | HostName"
Depends on which one you are looking to set, or all.


Currently we have the name of IT then four digit number. We are going to MINCB-NMXxxx for a notebook. For a desktop it will be MINCB-DMXxxx.


It bugged me that I didn't know how to script this, so I Googled it for you.
This should do the trick in 10.6+ and assumes you want the last 4 digits of the computer name to be the last for of the machine's serial number.

#!/bin/sh

# ensure this is run as root
if [ `whoami` != root ]
then
    echo Please run this script as root or using sudo
    exit
fi

# Set a variable with the last 4 digits of SN
L4SN=`/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial/ { print $NF }' | tail -c 5`

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

# Set system name variable based on the above two variables
if [ $BOOK == 1 ]
then
    NAME=MINCB-NM$L4SN
else
    NAME=MINCB-DM$L4SN
fi

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

Surely you'd want unique names. There is no reason why the last 4 digits of the serial number will provide you with a unique number or are indeed numbers.

You could have machines with the following serial numbers:

W99999NN1B
W99998NN1B
W99997NN1B
CK1111NN1B
CK1112NN1B

Obviously make your own decision on the likelihood of that happening!!!


Very good point Sean, certainly a short-sighted assumption on my part there.


I create asset tags for all our macs.

mpXYZ for laptops, mdXYZ for desktops.
XYZ is a 0-9 number (If you are in a school, you may need to add WXYZ since you'll have more machines than I do.

That allows me 999 unique computer names for laptop and desktops and small enough characters to be NetBIOS compliant since we bind to MS Active Directory.

PS: before you ask, if I'm still at my job after I've cycled out 1000 laptops and desktops, I have seriously erred in my career choice :P


Thanks for your help guys! This is a very big time saver.


Guys,

I am looking for something similar. I already have a rather basic script that returns, ComputerName, LocalHostName, and HostName.

Now, I want to add to the script, with interactive function.

Enter the "new" ComputerName (which will then also be used for LocalHostName and HostName), gets set by scutil --set, then return / list the new names for all three.

thanks,

jk

#!/bin/bash

function GREEN { tput setaf 2
}

function ENDColor { tput op
}

echo ComputerName

GREEN
scutil --get ComputerName ENDColor
echo LocalHostName

GREEN
scutil --get LocalHostName
ENDColor

echo HostName

GREEN
scutil --get HostName
ENDColor

echo
tput setaf 5
echo "Make sure the names all match - if they don't, tell a grown up near you!"
tput op

echo
echo
echo "press Enter to close this window"
read