58
votes

Per the Windows and Linux threads, what commands do you find most useful in Mac OS X Server (or Client)?

8
  • agreed, this site seems to be far more negative that stackoverflow, people seem poised to 'down' any post they don't think meets their standards - it's enough to make me walk away.
    – Chopper3
    May 12, 2009 at 13:15
  • thx, chopper3. maybe it was voted down for being mac related. i can understand not wanting to see a lot of apple stuff, but really just add /ignore "mac" to your tags, and it's sorted
    – username
    May 12, 2009 at 13:23
  • 2
    there's loads of apple specific stuff on this site, people here just seem to enjoy being negative.
    – Chopper3
    May 12, 2009 at 13:29
  • 2
    to be expected i suppose... we are sysadmins :-)
    – username
    May 12, 2009 at 13:32
  • 1
    I like your username...username :)
    – Chopper3
    May 12, 2009 at 13:35

55 Answers 55

1
2
3
votes

Truly Mac only: I saw 'open .' above, but open will open any document or app you pass to it.

Generic Unix that I use the most: sudo is pretty handy for changing system config files, etc, which I actually edit in vi.

kill can't be overstated when something hangs (or when another user is still logged into my desktop in the background and I want to log them out, nasty I know but it's my desktop)

ssh/scp - I love that in OSX I can just open a terminal and connect to any of our servers. That alone made me happy to drop Windows.

ifconfig/ping/whois/nmap etc

3
  • 1
    Well, open is not so truly Mac only... On Windows this command is named start (based on the file extension for files, though internet addresses are supported as well).
    – Arjan
    Aug 11, 2009 at 15:04
  • gnome-open is Gnome's analogous command for this. Dec 1, 2009 at 22:00
  • I believe you should usually use xdg-open rather than gnome-open because it's portable to other desktop environments (XFCE, KDE, etc).
    – Glyph
    Jun 8, 2012 at 21:51
3
votes

If you want to know what sort of line endings a file has, just run

file /path/to/your-file

ex:

$ file imports/sample-students.txt 
imports/sample-students.txt: ASCII text, with CR line terminators
3
votes

scutil --dns

Will display the order for DNS resolution. Useful for when you're creating or debugging your Network settings.

3
votes
lsof -i 

lists Internet ports that are open. Sample output:

COMMAND     PID    USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
SystemUIS   223 clinton   11u  IPv4 0x3e21b08      0t0  UDP *:*
ARDAgent    262 clinton   16u  IPv4 0x3e21be0      0t0  UDP *:net-assistant
ARDAgent    262 clinton   18u  IPv4 0x5f01a68      0t0  TCP *:net-assistant (LISTEN)
AppleVNCS   263 clinton    4u  IPv6 0x3e274bc      0t0  TCP *:vnc-server (LISTEN)
Opera     48365 clinton   20u  IPv4 0x5f01e64      0t0  TCP WTD-Staff-BlackmoreC:57094->stackoverflow.com:http (ESTABLISHED)
Opera     48365 clinton   23u  IPv4 0x5b12a68      0t0  TCP WTD-Staff-BlackmoreC:57095->stackoverflow.com:http (ESTABLISHED)

Use sudo if you want the ports open by all users, and the flags -n and -P will disable name resolution and port names and give you numbers instead.

2
votes
ifconfig | grep cast

is great for getting your IP address, and

ifconfig en1 | grep eth

works well for getting a MAC address.  Using en1 will (almost always) get the Airport's MAC address, which is handy if only known devices are allowed onto your wireless network.  Conversely, if you use en0 -- the built-in ethernet port -- you can then look the machine up in your Open Directory, like so:

ldapsearch -x -h odm -b "cn=computers,dc=odm,dc=pretendoco,dc=com" "macAddress=00:0d:93:b5:82:88"

(Assuming your server was 'odm' and the realm is ODM.PRETENDCO.COM).

2
votes

ipconfig is sometimes useful:

Getting current IP address on interface:

$ ipconfig getifaddr en1
10.110.2.25

Getting the DHCP information that was last received:

$ ipconfig getpacket en1
op = BOOTREPLY
htype = 1
flags = 0
hlen = 6
hops = 0
xid = 143857879
secs = 0
ciaddr = 0.0.0.0
yiaddr = 10.110.2.25
siaddr = 0.0.0.0
giaddr = 0.0.0.0
chaddr = 00:aa:bb:cc:dd:ee
sname = 
file = 
options:
Options count is 7
dhcp_message_type (uint8): ACK 0x5
server_identifier (ip): 1.1.1.1
lease_time (uint32): 0xe10
router (ip_mult): {10.110.0.1}
domain_name_server (ip_mult): {1.1.1.1, 1.1.1.2}
subnet_mask (ip): 255.255.252.0
end (none): 
2
votes

This question overlaps quite a bit with this one about tools a UNIX administrator cannot live without. Many of the command-line tools for Mac OS X have UNIX roots, such as df, du, and which; however there are notable exceptions that have no obvious UNIX equivalents, such as osascript, open, pbcopy, pbpaste, and say.

2
votes

How to enable Time Machine to backup to a NAS.

defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1

Find MAC address

ifconfig en0 | grep ether | sed s/://g | sed s/ether//

Command to create sparsebundle to copy over to NAS

sudo hdiutil create -nospotlight -library SPUD -size 160g -fs "Case-sensitive Journaled HFS+" -type SPARSEBUNDLE -volname "<VOLNAME>" ./<HOSTNAME>_<MAC_ADDRESS>.sparsebundle

Works like charm backing up to my ReadyNAS.

2
votes

It is a freeware third-party tool, but

edit

is really handy. It is a command-line tool that you can optionally install the first time you run TextWrangler. [Other text editors (BBEdit, TextMate) likely provide something analogous.]

edit path/to/somefile

Opens up the file in TextWrangler, and will let you authenticate if you don't normally have permission to edit it. (You can even do it from an ssh session, and it'll open it for the logged-in graphical user).

Better still is that you can pipe things to it.

lsof -i | edit

for example, will show you your open network connections and open them up in TextWrangler, where you can search (and scroll) through them easily.

1
  • 1
    There is a similar Textmate version called "mate" which is installed with the editor.
    – bjtitus
    Jul 5, 2009 at 23:32
2
votes

Gain a root shell without enabling the root user (as Apple itself requires sometimes in their docs in order to do some "geeky" stuff, ie to set system-wide language)

sudo bash

This gives you a root shell where you can do everything you want as root, without having to prepend every command with the sudo command.

This may be dangerous, but we are sysadmins, we know what we are doing, don't we? :)

2
  • 2
    I prefer "sudo -s", it's shorter and I think it may do some specific processing to start the shell "properly".
    – w00t
    Aug 18, 2009 at 0:22
  • Yes, but I prefer explicitly run bash for more than a reason: - may be there is no env $SHELL defined - may be $SHELL or /etc/password define a different shell (sh, ksh, etc) - I don't think "-s" would do any magic in running the shell "properly" But you are right, this is an alternative. +1 for you
    – drAlberT
    Aug 18, 2009 at 7:36
2
votes

killall -9 appname force quits an app. Pretty useful if you can't get into Activity Monitor.

2
votes

Apart from the usual Unix suspects I find the following useful :-

  • dsconfigad - Edits the Active Directory settings and binds a computer

  • defaults - Edit preferences

  • plutil - Converts plist to and from text

  • softwareupdate - run Software Update from the command-line

  • installer - install packages from the command-line

  • networksetup - set and get various things such as the computer name

If you master those and the usual Unix stuff you have all you need for Mac administration from the command-line.

2
votes

I just found out that there's a nifty bash construct that you can use instead of the seq command missing in Mac OS:

echo { 18..21 }
1
  • OSX has the BSD-derived jot utility that romps all over seq. Check it out. Mar 18, 2011 at 3:28
2
votes

A bit pedestrian for this audience, no doubt, but I use:

screencapture -i -c

to grab whatever I want from the screen It's really handy and does things that Grab won't allow me to grab.

1
vote

du -d 1 -h

Displays disk usage statistics for the current directory in human readable form.

man [command]

One of the most used commands. Tells you how to use everything else.

1
vote
sudo slapconfig -destroyldapserver

Force Demote an LDAP Replica to Standalone. If your Open Directory Master is misconfigured, sometimes trying to demote an Open Directory Replica using Server Admin will fail (eg: you might find your Replica server refuses to demote). You can use slapconfig to force it to demote on these occasions.

1
vote
 which program

searched the path for program, and tells you which executable is invoked if you run program without specifying a full path.

It is usually most useful as a shorthand for typing a path.

$ which python
/usr/bin/python
$ ls -l `which python`
lrwxr-xr-x  1 root  wheel  72 30 Jan 22:56 /usr/bin/python -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python
2
  • "type -a foo" will show you all programs named foo, and works for all Bourne shells.
    – w00t
    Aug 18, 2009 at 0:20
  • type will also include aliases and functions
    – Tyilo
    Jan 23, 2012 at 16:10
1
vote
sqlite3 foo.db

Starts an interactive SQLite session. If foo.db does not exist, it will be created.

1
vote

These are basic, but handy if you have amnesia:

Who am I?

id

Where am I standing?

pwd

What building am I?

hostname

What's its address?

ifconfig |grep inet
1
vote

OS X specific things I haven't seen mentioned:

mdfind uses the Spotlight search indexes from the commandline, so you can do full-text searches without using the Search pulldown.

networksetup - will show you all the crazy syntax it supports; this is the commandline equivalent of the Network preference pane.

sudo launchctl list - shows the running launchctl jobs. remember launchctl stop X.XX.X will just 'stop' (kill) the currently running instance of a persistent process; to really make it stop running use launchctl unload /System/Library/LaunchDaemons/X.XX.X.plist.

1
vote

What DHCP licenses does the client hold?

sudo ls -lt /private/var/db/dhcpclient/leases

List the firewall allow/denies:

sudo ipfw list

Generate a high-entropy password (double click on a promising 16 character string to copy/paste):

head -c90 /dev/random | uuencode -m pwd | hexdump -C
1
vote

Ever wanted to know what the IP address of another Mac that you can see is in the Finder is on your LAN thanks to Bonjour?

dscacheutil -q host -a name OtherMac.local

dscacheutil is surprisingly useful and versatile. You can do a lot more with it than just flush the DNS cache (as mentioned elsewhere here), i.e. user lookups and cache stats and dumps.

0
votes
locate "foo"

Displays any file on your system with "foo" in its filename. I must use this almost as often as I use Spotlight.

1
0
votes
 sudo reboot

reboots the computer.

0
votes

Maybe not the most "useful" command but say is pretty fun.

For example say I love serverfault to have it read back what you type after "say."

1
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .