Changing computer name

Kirill
New Contributor II

Hello, I would like to change computer host and localhost name to specific values after enrollment.

I need:

  • computer name to be  "name.surname"
  • localhost name to be  "name-surname.local"
  • host name to be "name-surname"

With this script I managed to change computer to correct value but localhost name is not changing. Can somebody help me finding out where is a mistake please?

#gets current logged in user
getUser=$(ls -l /dev/console | awk '{ print $3 }')

#gets named
firstName=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($2)}')
lastName=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($3)}')
computerName=$firstName.$lastName
hostName=$firstName"-"$lastName

#set all the names in all the places
scutil --set ComputerName "$computerName"
scutil --set HostName "$hostName"
scutil --set LocalHostName "$hostName"

sudo jamf recon

 

 

1 ACCEPTED SOLUTION

Kirill
New Contributor II

I use this as you recommended 

#!/bin/sh

#gets current logged in user
getUser=$(ls -l /dev/console | awk '{ print $3 }')

# Get the Serial Number of the Machine
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

#gets named
firstName=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($2)}')
lastName=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($3)}')
computerName=$firstName.$lastName
hostName=$firstName"-"$lastName

#set all the names in all the places
scutil --set ComputerName "$computerName"
sleep 3
scutil --set HostName "$hostName"
sleep 3
scutil --set LocalHostName "$hostName"
sleep 3

/usr/bin/dscacheutil -flushcach

View solution in original post

15 REPLIES 15

ubcoit
Contributor II

Try slowing things down, add a 'sleep 3' after each scutil commands.  I also run the following after all three run. 
/usr/bin/dscacheutil -flushcache

Kirill
New Contributor II

Thank you for this solution but it works not always for some reason. Every second time it changes name to setup.user and setup-user even i created "Jamf Test" user as Full Name on PreStage enrollment

Kirill
New Contributor II

I use this as you recommended 

#!/bin/sh

#gets current logged in user
getUser=$(ls -l /dev/console | awk '{ print $3 }')

# Get the Serial Number of the Machine
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

#gets named
firstName=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($2)}')
lastName=$(finger -s $getUser | head -2 | tail -n 1 | awk '{print tolower($3)}')
computerName=$firstName.$lastName
hostName=$firstName"-"$lastName

#set all the names in all the places
scutil --set ComputerName "$computerName"
sleep 3
scutil --set HostName "$hostName"
sleep 3
scutil --set LocalHostName "$hostName"
sleep 3

/usr/bin/dscacheutil -flushcach

jacbgla
New Contributor II

Does this script set the computername to the serial number? 

What if i wanted something like "Lab-Mac-SerialNumber" 

Just setting up DEP for the first time and this is the bit i am stuck on. 

bcrockett
New Contributor III

No, however, you could modify it so that it does.  Write the script so that the output of the line below = Lab-Mac-SerialNumber

 

$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

 

jacbgla
New Contributor II

Hi .. Thanks

Was able to get this version working... See Below

Now I have another Issue where the machines are not installing the Jamf Binary so the machines are un-managed only via DEP. If i do them myself, everything works fine. 

 

#!/usr/bin/env bash

# Get the Serial Number of the Machine
sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

sn="Lab-C119-Mac-$sn"

# Set the ComputerName, HostName and LocalHostName
scutil --set ComputerName $sn
scutil --set HostName $sn
scutil --set LocalHostName $sn

# Set the computer name in Jamf to reflect what is set locally on the computer
/usr/local/bin/jamf setComputerName -name $sn
/usr/local/bin/jamf recon

echo "Computer name has been changed to $sn"

exit 0

 

 

ubcoit
Contributor II

I'm guessing here but since this appears to be happening at enrollment and sometimes you get the naming you want, I think this is a timing issue.  I think the user you are expecting hasn't been created yet or the system hasn't logged in as it yet or something else.

For my DEP machines, which aren't domain joined, I rename them at enrollment complete by their serial number, that way we can actually find them easily in the JSS.  Then later, I have a script available in Self Service and at login to prompt to rename the machine.  Our techs still "setup" machines for our clients as we haven't moved completely to the out of box experience.

If it is a timing thing, perhaps naming your rename policy to zzz.rename-mac will help you out since zzz will be the last thing to run after enrollment.

Notice below in your screenshot that the "user at login" is mbsetupuser.  Might be a clue as to what's going on, might not be! ;)

Kirill
New Contributor II

@ubcoit I think you are right, script runs straight after enrollment completed, but account is not created yet. I changed policy to run after log in and it works :) Than you very much!

Kirill
New Contributor II

Screen Shot 2022-02-10 at 17.17.05.pngScreen Shot 2022-02-10 at 17.15.30.png

In the script you posted earlier, there's a typo. It should be 

 

 

/usr/bin/dscacheutil -flushcache

 

 

Your script shows it as 

/usr/bin/dscacheutil -flushcach    

Thanks, copy/paste issue.  Fixed above.

The script you posted also showed the incorrect syntax. Did you verify that it's -flushcache in your script? I've just used 

dscacheutil -flushcache

in my script.

Bol
Contributor III

@Kirill  Try using the Jamf binary which will set all three. I use on enrollment complete trigger with new devices and works well.

scutil --get ComputerName; scutil --get HostName; scutil --get LocalHostName
OldName
OldName
OldName

sudo jamf setComputerName -name "NewName"
Set Computer Name to NewName

scutil --get ComputerName; scutil --get HostName; scutil --get LocalHostName
NewName
NewName
NewName

 

Kirill
New Contributor II

This is only part of the script, right? Can you please give me full one? Sorry I'm quite new to scripting.

bcrockett
New Contributor III

When I try running this script I get the following error below. 

 

Is the script still working for you? @Kirill 

##########

Executing Policy return the username
Running script change computer host and localhost name to specific values after enrollment....
Script exit code: 64
Script result: Usage: dscacheutil -h
dscacheutil -q category [-a key value] dscacheutil -cachedump [-buckets] [-entries [category]] dscacheutil -configuration dscacheutil -flushcache dscacheutil -statistics
Error running script: return code was 64.