Quick Fix: Invalid MIT-MAGIC-COOKIE-1 When “sudo”

When you try opening an app using the command “sudo <app_name>” in order to obtain root (administrator) privileges, you may encounter a strange looking error: Invalid MIT-MAGIC-COOKIE-1

Updated Feb 9, 2016Quick Fix
Strange MIT-MAGIC-COOKIE-1 error

It can happen upon installing a fresh new distro that you have or haven’t used before, sometimes it may happen upon updating your current working-as-it-should distro…

In the end, it doesn’t matter ‘when’ or even ‘why’ exactly it happens (we’ll get to that in a moment), the important thing is – how can you fix it, right?

Fixing Invalid MIT-MAGIC-COOKIE-1

According to online resources it seems that the primary suggestion for solving this error has something to do with .Xauthority file that resides in Home folder.

Most of the resources you may encounter will advise you to simply delete the file in order for the problem to disappear.

This may work for ssh connections, however, it doesn’t seems to do the trick when it comes to the error popping up upon sudo.

So what does works?

Simply, open up sudoers file and edit a specific configuration in it like so:

sudo nano /etc/sudoers

remove the ‘#’ sign from the line where it says ‘ Defaults env_keep += “HOME” ‘ so you’ll end up having the configuration looking like the following image.

Change sudoers conf

Of course, as the comment above it suggests, other programs may also use HOME to find configuration files and this may lead to privilege escalation!

So I wouldn’t advise you do so in case you have important, confidential files stored on your machine. Then again, If you do, you’d probably wouldn’t be reading this anyhow.

Another Method

This one might be just little bit more tricky.

If you’re looking to enable sudo open up Xorg programs, yet, you prefer not to include the entire HOME folder as a viable authentication mechanism; you can simply edit the sudoers file like this: ‘ Defaults env_keep += “XAUTHORITY” ‘.

Nonetheless, the tricky part here is that you must have $XAUTHORITY variable defined on your distro as well, otherwise this will get you nowhere.

To do that for one session you may type the following in Terminal:

export XAUTHORITY=/home/<YOUR-USER-NAME>/.Xauthority

Or you can store the variable permanently in $HOME/.bashrc or more globally in /etc/environment, the choice is yours.