Mac OS X: Replicating md5sum Output Format

The md5sum program is used to calculate and verify 128-bit MD5 hashes. This program is installed by default in most Unix, Linux, and Unix-like operating systems including BSD. Mac OS X is a BSD variant and it also includes the md5sum program. However, the program is called md5 instead of md5sum and outputs an MD5 checksum in a different format than the standard md5sum program.

Here's what the standard md5sum output looks like:

$ md5sum test.txt
d0ea20794ab78114230ba1ab167a22c2 test.txt

Now here's what the output of md5 on Mac OS X looks like:

$ md5 test.txt
MD5 (test.txt) = d0ea20794ab78114230ba1ab167a22c2

While this normally wouldn't be a big deal, it can cause major issues if you're trying to run scripts that were written for a Unix-like environment which expect the default md5sum format.

Thankfully, md5 has a switch that reverses the output:

$ md5 -r test.txt
d0ea20794ab78114230ba1ab167a22c2 test.txt

If you'd like to permanently change md5's behavior to mimic that of md5sum, you have two options:

The first is to simply add the following alias to ~/.profile:

alias md5sum='md5 -r'

Now when you type 'md5sum test.txt', the command will be replaced with 'md5 -r test.txt'. However, this may not work with your scripts.

The second solution, and my preferred method, is to create a small script called md5sum that contains the following:

/sbin/md5 -r "$@"

I then make this script executable (chmod +x md5sum) and put it in /sbin/. Now, whenever a script calls md5sum, the small bash script above is used and it produces output identical to that of md5sum on other Unix systems.

Removing the GMail "On Behalf Of" Sender Header

Like many of you, I have several email accounts and while using GMail as my primary email client would be nice, one of the things that has kept me from doing so is the annoying "On Behalf Of" that the GMail SMTP servers add to outgoing email. Some of the accounts are work related and the "On Behalf Of" comes across to me as unprofessional.

GMail On Behalf Of

That "On Behalf Of" part is caused by the Sender: header that GMail's SMTP servers add:

Sender: [email protected]
Date: Sun, 13 Dec 2009 10:33:18 -0500
Subject: Re: Project Details
From: [email protected]
To: [email protected]

Having your personal GMail address show up looks totally unprofessional and prevents you from being able to keep your personal email, uh, well, personal. Unfortunately, Google has already said it's part of their SMTP server specs, so they won't change it.

For a long time, your only option was to use a desktop email client like Outlook or Thunderbird to send email using your own domain's SMTP server. However, this meant accessing your GMail account using IMAP/POP and basically defeated the purpose of the web client (unless you didn't mind switching between the web and desktop client).

When Google offered GMail for Domains (now called Google Apps for Domains), I eagerly set up a free account using one of my domains and tested to see if email sent still included the Sender header. Sadly it did and I abandoned the idea of using GMail as my primary email client.

A few days ago I accidentally discovered that now both the standard GMail and Google Apps for Domains have the ability to specify your own SMTP server when you add an external email address to your GMail account. (Here's the announcement from a few months ago on Google's Blog.) This means you can receive email from [email protected] and also reply to emails from within GMail as [email protected] and the receiver won't know that you're using GMail!

Here's how to get this set up:

Login to GMail and click Settings at the top right. Then click the Accounts and Import tab:

GMail Settings - Accounts and Import

Now make sure the "Reply from the same address the message was sent to" is selected and click "Send mail from another address" to add your external email account.

GMail - Send mail from another address

Fill in the name you want to show up when you send email from this account. This will probably be your name, but it might also be something like "Company Support" or "Sales". Now enter your full external email address in the second box and then click Next Step.

GMail - Add another address - Step 1

On the next screen, select the second option box, "Send through SMTP servers" and fill in the SMTP Server, Username, and Password fields. When you click Add Account, Google will try connecting to the SMTP server using the credentials you supplied. If it successfully connects, you'll be brought back to the Accounts and Import tab with your new account added. Otherwise, Google will display the error it had when trying to verify the SMTP connection details.

GMail - Add another address - Step 2

Now you should see the new account listed on the Accounts and Import tab, along with a note at the bottom showing that email will be sent using the SMTP server you specified:

GMail - New account listed on Accounts and Import tab

Now, you can send and receive email using this external account and the receiver won't see that annoying "On Behalf Of" message! Your GMail address won't even be visible in the email headers if they choose to view the email source (they'll see that the email was routed through a Google server, but Google is well known enough that they probably won't care).

Mac OS X: Fixing 'Always Open With'

After installing the TextMate editor I wanted all shell scripts (files ending with the .sh extension) to open in TextMate. Unfortunately, selecting Open With -> Other... from the context menu, choosing an application, and then clicking the Always Open With checkbox doesn't change the default application for all files with that extension, but rather only changes that specific file.

If you want to change the default application used to open all files with a specific extension, the steps are slightly different:

Select the file and choose File -> Get Info (or Cmd+i) and expand the Open With section:

Get Info

From here, select the application you want to use for opening those file types and then click Change All. This will update the OS X Launch Services Database, which is consulted when opening files. Now all files with that extension will be opened with the application you selected.

Preparing a Mac for Resale with OS X Leopard

When reselling a Mac, it's nice to give the new owner an experience similar to buying a new Mac. This means reinstalling a clean copy of OS X and, if you're super nice, installing the latest software updates and additional software. Finally, the Setup Assistant needs to be reset so that it runs when the new owner turns on the computer.

Note: The steps below were used to prepare a MacBook Pro running OS X Leopard for resale. Although the steps are similar for other versions of OS X, I have only tested them with Leopard. (Update: Matt mentioned in the comments that he successfully used these steps on OS X 10.6.)

Backup Your Data

If the Mac you're reselling is your own, you should do a full system backup before proceeding.

My favorite method for doing this is to use an external USB hard drive and Carbon Copy Cloner (free). Carbon Copy Cloner can be used to take a snapshot of your entire drive and make it bootable. This means you can actually boot from your backup drive, given you're booting from identical hardware. At the very least, you'll have an identical copy of everything on the computer.

You can also use Time Machine to backup your data. I don't use Time Machine, so I cannot recommend it.

The bottom line is, you want to backup anything important on the Mac you're reselling because the next steps will erase everything.

Reinstall Mac OS X Leopard

  1. Insert Mac OS X Leopard Install Disc
  2. Restart the computer
  3. Hold down the Option key while the computer is restarting. The system will then prompt you to select a startup disk. Choose the Mac OS X Leopard Install Disc.
  4. On the "Select a Destination" screen, click "Options" and choose "Erase and Install". This will ensure a clean installation of Mac OS X Leopard.
  5. Follow the remaining steps to install Mac OS X; they are all self explanatory.
  6. When the installation finishes, the Mac will reboot and you'll be presented with the Setup Assistant.

If you're not interested in installing the software updates or any additional software, press Command+Q to quit the Setup Assistant and choose Shutdown. The next time you turn on the machine, the Setup Assistant will launch again.

Install Software Updates & Additional Software

If you want to be super nice to the new owner of your Mac, you'll need to complete the Setup Assistant and create a temporary user. We will use that user to download and install the software updates as well as install any additional applications (Firefox, MS Office, etc.). Once we're done updating the system and installing software, we'll delete the temporary user and reset the Setup Assistant.

  1. Complete the Setup Assistant and create the first user account (we'll delete this user and reset the Setup Assistant later, so it doesn't matter what you use). Make note of the shortname for the user you create as we'll need that in the next section (e.g., user "Raam Dev" will probably have a shortname of "raamdev").
  2. At this point, you should be logged into the newly reinstalled system using the temporary account you created.
  3. Connect to the Internet (using a wired or wireless connection)
  4. Run Software Update (Apple Icon -> Software Update) and install any available updates. This may take awhile!
  5. After the system reboots, run Software Update again. There will most likely be more updates to install. Repeat this process until there are no more updates to install.
  6. Install any additional applications, being sure to install for "All Users" if prompted. Also, make sure the new applications are installed in /Applications/ and not ~/Applications/

Clean Up and Reset the Setup Assistant

Now that we've installed the latest updates and installed any additional software, we need to clean things up. First, we'll delete the temporary user we created and then we'll reset the Setup Assistant to ensure it launches the next time we boot up.

First, you'll need to start the Mac in Single User mode by restarting and holding down Command+S. This mode does not have a graphical user interface (GUI) and is entirely command-based.

Once you're in Single User mode, run the following commands and press Enter at the end of each line. (Note: The $ represents the command prompt; you don't actually type the $.)

By default, the hard drive is mounted as read-only in Single User mode. Before we continue, we need to make the disk writable:

[code lang="bash"]
$ mount -uw /

Next, we clean up that temporary user we created. Replace USERNAME with the shortname of the user you created in the previous section.

[code lang="bash"]
$ rm -R /Library/Preferences/
$ rm -R /Users/USERNAME/
$ /bin/launchctl load /System/Library/LaunchDaemons/ &
$ dscl . -delete /Users/USERNAME

And lastly, we need to reset the Setup Assistant so it runs the next time we start the Mac:

[code lang="bash"]
$ rm /var/db/.AppleSetupDone

Now that everything is cleaned up and the Setup Assistant has been reset, we can shutdown the system and ship or deliver the Mac to its new owner!

[code lang="bash"]
$ shutdown -h now

If you want to boot the Mac to make sure it looks the way it should when the user gets it (i.e., the intro video and Setup Assistant start), just make sure you press Command+Q and choose Shutdown.