Take the 2-minute tour ×

I recently upgrading to Snow Leopard. I have noticed that some files written by MacPorts are installed with the wrong permission -- they are written with a umask of 0077. I think I have narrowed down the problem:

  1. The port command is invoked via sudo.
  2. My .bashrc file specifies a umask of 0077.
  3. On older versions of OS X (10.5 and below), sudo used the umask of the root user (which was 0022); however, now it uses my umask of 0077.

Is there anyway to have sudo use the old behavior? Right now, it's kind of annoying because I have to use sudo to run simple commands like port installed, port outdated, etc.

(The problem is described in more detail in this MacPorts ticket.)


I discovered the umask option for sudo, and in /etc/sudoers I added the following line:

Defaults umask=0022

However, this did not function as desired, because the real umask used by sudo is the union of the user mask with this default mask.

share|improve this question
add comment

5 Answers

up vote 6 down vote accepted

I ended up adding the following to my .bashrc configuration script:

# Mimic old behavior of `sudo` on OS X Snow Leopard
sudo() {
    umask 0022
    command sudo "$@"
    umask $old
share|improve this answer
add comment

Mac OS X 10.7 (Lion) finally has a version of sudo that supports umask_override. For the record, this works for me:

Defaults umask_override
Defaults umask=0022
share|improve this answer
Can you make this work on a per command basis? I would like to umask 0022 normally, but then invoke umask 0006 in a single circumstance, but the above ignores that. –  Michael Sep 15 '13 at 23:59
add comment

how about:

sudo22() {
   local UMASK=`umask`;
   umask 22;
   sudo "$@";
   umask $UMASK
share|improve this answer
add comment

With your .bashrc

if [[ $EUID -eq 0 ]]; then
   umask 0022
   umask 0077
share|improve this answer
Good idea! Unfortunately, a little investigation shows that Snow Leopard's sudo doesn't actually (re-)source .bashrc, but inherits the current setting from the user that invoked sudo. –  mipadi Dec 7 '09 at 18:26
You'll have to use the function workaround as listed in the other answer then. –  Darren Hall Dec 7 '09 at 22:29
add comment

For the record: the current version of sudo as a new option 'umask_override', which should prevent the umask's from being merged, so you should be able to lower the umask, too. Sadly, Mac OS X 10.6.6 does not seem to sport this version of sudo ...

share|improve this answer
add comment

Your Answer


