Newsroom Update

Beginning in May, a special Today at Apple series titled “Made for Business” will offer small business owners and entrepreneurs free opportunities to learn how Apple products and services can support their growth and success. Learn more >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Set ComputerName with a LaunchDaemon

Hi, my first post here.

I work in a university in germany and manage about 80 Macs. The MAC-address of all machines is registered in DHCP which corresponds with a fixed IP-address and name in DNS. I have been using a script for several years which reads the name out of DNS and sets this name as ComputerName, LocalHostName and Hostname.

In 10.6 SnowLeopard, I installed this as a "StartUpItem" and it worked beautifully. When I re-image the macs at the beginning of a semester with NetRestore, I restart them a second time and they are all named correctly. So far so good.

But "StartUpItem" is deprecated, we should use a LaunchDaemon instead. I tried to do this on our transition to Mavericks, but failed. Here is the script. It is named "setdnsname.sh", it is owned by root and executable. I placed it in /Library/Scripts:


#!/bin/sh

### NOTICE:

# Set HOSTNAME=-AUTOMATIC- in /etc/hostconfig first

# This script has to run with root permissions

# The next two lines below force the waiting for the network and that DNS services are available:

ipconfig waitall

scutil -w State:/Network/Global/DNS -t 5


# Read the IP-Address from network interface en0 (ethernet cable):

IPADDRESS=`/usr/sbin/ipconfig getifaddr en0`


# Resolve FQDN from IP-Address and strip everything after (including) the first dot.

# Result is the pure name:

COMPNAME=`/usr/bin/host $IPADDRESS 2>/dev/null | /usr/bin/awk '{ print $5 }' | /usr/bin/awk -F. '{ print $1 }'`


# Set this name as HostName, LocalHostName and ComputerName:

scutil --set HostName "${COMPNAME}"

scutil --set LocalHostName "${COMPNAME}"

scutil --set ComputerName "${COMPNAME}"


When I run this script manually (sudo /Library/Scripts/setdnsname.sh), it works as expected. But not as a LaunchDaemon. After reboot, the old name sticks as if the script failed and never runs.


I placed a corresponding plist in /Library/LaunchDaemons. Here is the plist:


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST

1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>set_DNS_name</string>

<key>ProgramArguments</key>

<string>/Library/Scripts/setdnsname.sh</string>

<key>RunAtLoad</key>

<true/>

<key>KeepAlive</key>

<false/>

</dict>

</plist>


Anybody with an idea what does not work here?


Jan

OS X Yosemite (10.10.2)

Posted on Feb 26, 2015 2:32 AM

Reply
4 replies

Feb 26, 2015 12:31 PM in response to Brian Nesse

As from what I remember the label can be whatever string _but_ it might be good to use the formaula you mentioned since that's the standard way of doing it and it's easier if everyone's doing the same. Also it might be good practise to use the same label as the name of the property list file so that it's easy to find the .plist file when seeing the process presented somewhere else.

Feb 27, 2015 3:22 AM in response to Brian Nesse

Thank you for your help. But I am afraid this is not the solution to the problem.

I changed the name of the plist-file to "de.hs-hannover.setdnsname.plist".

I changed the name of the Label in the plist-file to "de.hs-hannover.setdnsname".

Permissions are

-rw-r--r-- 1 root wheel


Reboot - no change. The names do not change. Running the shell-script manually after login works as expected.

Maybe DNS services are not available fast enough? I changed

scutil -w State:/Network/Global/DNS -t 5

to

scutil -w State:/Network/Global/DNS -t 15

but this seemed to make no difference.

Set ComputerName with a LaunchDaemon

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.