Set system and network prefs from the Terminal

Dec 16, '04 09:37:00AM

Contributed by: dreness

Apple has included two very useful command line utilities, networksetup and systemsetup, as part of Mac OS X Server for some time now (since 10.2?). These are utilities that provide (in most cases the only) command-line counterparts to many of the preference panes, such as Network (very thoroughly, I might add), Date & Time, Energy Saver (yes, there is also pmset), Startup Disk, and some of Sharing.

I have discovered that these two utilities are now included with the freely-available Apple Remote Desktop Client, and they are perfectly willing to run in Mac OS X Client (not just Mac OS X Server). Since they are now freely available, I can recommend these to all the admins and shell scripters in the house. These tools make use of the appropriate frameworks / APIs, and absolutely represent the Apple-sanctioned way of getting the job done. As mentioned, these tools have been in production use in OS X Server for a while, and I have used them a bunch and never had any problems.

Install Remote Desktop via the link above (if you don't have it already; check your Sharing prefs panel), and have a look in /System -> Library -> CoreServices -> RemoteManagement -> ARDAgent.app -> Contents -> Support. Now type sudo ./networksetup -help and sudo ./systemsetup -help for the full scoop on everything these two programs can do.

Read the rest of the hint for a little more detail on these programs, as well as a warning about trying to get them to run on 10.2.

[robg adds: These really are very powerful programs, as you'll see if you read the help screens. I made a symbolic link to these files from my user's bin folder, so that they're on my path and are easily run (though you'll want to do so with sudo).]

Running otool -L to see what frameworks networksetup uses reveals a bunch of Apple's private frameworks, in addition to the public SystemConfiguration framework that does most of the work here (lines wrapped for narrower display):

root@pimp $ otool -L networksetup
networksetup:
  /System/Library/PrivateFrameworks/Admin.framework/Versions/A/Admin
     (compatibility version 1.0.0, current version 1.0.0)
  /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
     (compatibility version 300.0.0, current version 500.0.0)
  /System/Library/Frameworks/SystemConfiguration.framework/Versions/
     A/SystemConfiguration (compatibility version 1.0.0,
     current version 84.0.0)
  /System/Library/PrivateFrameworks/Apple80211.framework/Versions/
     A/Apple80211 (compatibility version 1.0.0, current version 2.0.0)
  /System/Library/PrivateFrameworks/NetworkConfig.framework/Versions/
     A/NetworkConfig (compatibility version 1.0.0, current version 1.0.0)
  /System/Library/Frameworks/SecurityFoundation.framework/Versions/
     A/SecurityFoundation (compatibility version 1.0.0, current version 6.0.0)
  /usr/lib/libSystem.B.dylib
     (compatibility version 1.0.0, current version 71.0.0)
Note of potential doom: Read this entire paragraph before doing anything mentioned herein. I almost posted a hint about halfway through 10.2 about how to get these utilities to work in 10.2. Turns out that the 10.2 versions of the utils would simply check for the existence of /System -> Library -> CoreServices -> ServerVersion.plist, and if it wasn't there, it would bail assuming that it was not being run on server (this was deduced by running strings(1) on the systemsetup program itself).

I found that by simply touching ServerVersion.plist, the utility would not only run, but work perfectly. I just had to make sure to delete ServerVersion.plist when I was done, otherwise other things like installers and preference panes would think I was running server also, and behave differently. However, given that the utilities were not freely available, it seemed like there wasn't much point in posting about it to a Mac OS X-centric community.

Incidentally, trying the ServerVersion.plist thing nowadays will make Panther hate you long time. I'm not kidding. You will need to reinstall OS X if you try it (I tried it twice just to be sure!). As soon as that file is detected (and various things must be looking fairly frequently), you will lock, and you'll never see your desktop again until after spinning the install CDs :)

Comments (22)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20041215002018178