- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-08-2022 07:51 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-10-2022 05:41 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2022 09:23 AM - edited 02-10-2022 01:19 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-10-2022 05:39 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-10-2022 05:41 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 06-29-2022 01:52 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 06-29-2022 09:24 AM
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}')

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2022 01:36 AM - edited 07-01-2022 01:36 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-10-2022 08:02 AM
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! ;)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-12-2022 10:16 AM
@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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-10-2022 07:18 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-10-2022 12:29 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-10-2022 01:19 PM
Thanks, copy/paste issue. Fixed above.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-10-2022 02:14 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-08-2022 08:41 PM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 02-10-2022 05:48 AM
This is only part of the script, right? Can you please give me full one? Sorry I'm quite new to scripting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 06-25-2022 09:18 AM
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. |
