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)