Saturday, May 5, 2012

How to enable the root account login on OS X and other Unix and Linux systems

For security reasons, many Unix and Linux systems, including OS X and Ubuntu, install with a locked or disabled root account by default, with no password (not to be confused with a blank password). As such, it isn't possible to log directly into root shell or use su. Instead, the default user is given administrator access. That being said, below are three ways I found to access the root shell in the rare case it may need to be done.

Note: This presupposes the user already has administrator/superuser rights. Because of this, for most users the root account does not generally need to be enabled.

  • The following will open an sh or bash process with full root privileges: sudo sh sudo bash
  • The following will log you in with full root privileges: sudo su - If one runs su alone, it will prompt for the root password, but with sudo first it instead prompts for the current user's password. The dash at the end loads the root user environment.
  • The following sets a password for the root user, thereby enabling normal login for root: sudo passwd root On some systems, OS X not included, the following will reverse the last command, both disabling the root login and removing its password hash: sudo passwd -dl root
Some systems, including OS X, also have a special command, dsenableroot.
  • To enable root login: dsenableroot
  • To disable root login: dsenableroot -d

For more information:
  • Apple's official instructions for various versions of OS X using the GUI to enable root can be found here.
  • Ubuntu's site has a great article on various uses and misconceptions about sudo and root, as well as a few useful tricks, that can be found here.

How to solve "sudo: cannot get working directory" error

Periodically, you may get the following error:
sudo: cannot get working directory
This error means that the directory you are supposedly in no longer exists. You can fix this by simply moving to a different directory, i.e. your home:

How to install the MySQLdb Python module on OS X Lion

These instructions have been tested on a machine running OS X Lion and Python 2.7.1, but should work for most any Intel setup. Most of the setup is explained in the README file included with the source, and the error at the end was solved per a thread here.
  1. Download MySQL for Python (the MySQLdb module) source from and extract the tar.gz file.
  2. Open Terminal and navigate to the source folder.
  3. Type the following to build the module: python build
  4. Then, install the module by typing the following: sudo python install
  5. In a perfect world, this would be all you'd need to do. However, if your computer was like mine, when you try to import the newly-installed module, it'll through the following error: >>> import MySQLdb Traceback (most recent call last): File "", line 1, in File "MySQLdb/", line 19, in import _mysql File "build/bdist.macosx-10.7-intel/egg/", line 7, in File "build/bdist.macosx-10.7-intel/egg/", line 6, in __bootstrap__ ImportError: dlopen(/Users/username/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Users/username/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/ Reason: image not found To fix this error, we'll simply make a symlink where libmysqlclient.18.dylib is expected to be to where it really is: sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib Note: you can also fix this issue by changing your path during install as per this suggestion.