Skip to main content
Solved

Changing computer name


Forum|alt.badge.img+5

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

 

 

Best answer by Kirill11

Kirill11 wrote:

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


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 original
Did this topic help you find an answer to your question?

15 replies

Forum|alt.badge.img+6
  • Contributor
  • 99 replies
  • February 8, 2022

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


Bol
Forum|alt.badge.img+11
  • Contributor
  • 276 replies
  • February 9, 2022

@Kirill11  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

 


Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 6 replies
  • February 10, 2022
ubcoit wrote:

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


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


Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 6 replies
  • Answer
  • February 10, 2022
Kirill11 wrote:

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


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

Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 6 replies
  • February 10, 2022
Bol wrote:

@Kirill11  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

 


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


Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 6 replies
  • February 10, 2022
ubcoit wrote:

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



Forum|alt.badge.img+6
  • Contributor
  • 99 replies
  • February 10, 2022
Kirill11 wrote:

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


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! ;)


Forum|alt.badge.img+1
  • New Contributor
  • 5 replies
  • February 10, 2022
Kirill11 wrote:


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    

Forum|alt.badge.img+6
  • Contributor
  • 99 replies
  • February 10, 2022
SeyhaSoun wrote:

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.


Forum|alt.badge.img+1
  • New Contributor
  • 5 replies
  • February 10, 2022
ubcoit wrote:

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.


Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 6 replies
  • February 12, 2022
ubcoit wrote:

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! ;)


@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!


Forum|alt.badge.img+8
  • Contributor
  • 125 replies
  • June 25, 2022

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

 

Is the script still working for you? @Kirill11 

##########

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.

Forum|alt.badge.img+3
  • New Contributor
  • 7 replies
  • June 29, 2022
Kirill11 wrote:

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

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. 


Forum|alt.badge.img+8
  • Contributor
  • 125 replies
  • June 29, 2022
jacbgla wrote:

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. 


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}')

 


Forum|alt.badge.img+3
  • New Contributor
  • 7 replies
  • July 1, 2022
bcrockett wrote:

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}')

 


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

 

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings