Skip to main content

I have a seemingly common issue.



When checking the hostnames with scutil --get ComputerName and it's LocalHostName and HostName commands, the HostName is not always set. As such, the hostname tends to be set to some random name.



Is there a command I can use that will automatically match the HostName to a ComputerName or LocalHostName variable? I'd like to push this out remotely in bulk over ARD.

Hi. You have half the answer already in your post. Since you can grab the ComputerName from the Mac in a script with:



scutil --get ComputerName


You can use that as a variable to set the HostName string to the same with a scutil --set command. For example:



#!/bin/sh

## This creates a variable string with the result of the Computer Name
CompName=$(scutil --get ComputerName)

## Now use that to set the HostName to match the ComputerName string
scutil --set HostName "$CompName"


Keep in mind the above is very simplistic. It does no error checking or making sure the ComputerName is even set to something valid (though I don't know if such a check would be needed), but it is the basic framework of how you would do this.



You could also simplify this further with a one-liner if needed:



scutil --set HostName "$(scutil --get ComputerName)"

Here is the basic script we use. It seems to have fixed a few random issues we were having.



#!/bin/sh
#!/bin/bash



# Get the Computer name using scutil

setName=`scutil --get ComputerName`



scutil --set LocalHostName ${setName}
scutil --set HostName ${setName}

echo HostName and LocalHostName set to ${setName}

exit 0

thanks that was exactly what I needed (scutil --set HostName "$(scutil --get ComputerName)") The simpler the better.



any thought on matching the DNS Name in ARD to the computername or hostname via a script? Or is this something that has to be fixed on the dhcp/dns server side? Or after making the hostname change, does it just take a while for the changes to reflect in DNS? Such as a DHCP Renewal?


What you're describing is a DHCP issue. Hostnames, once picked up, often get retained within DHCP and will get assigned to the machine that picks up that IP the next time. Its a pain no doubt. Ask anyone that has used ARD and they will say they've seen the same thing.
A DHCP renewal may help, but I don't know for sure to be honest.


https://jamfnation.jamfsoftware.com/discussion.html?id=3153#responseChild14747


Reply