“No protocol specified” when running vim with sudo

environment-variablessudovim

Recently I start to get "No protocol specified" when using sudo vim. It's just a warning I guess, because everything was working normally (I can open, edit and save files). The message doesn't appear if I use sudo -E vim so I think I did something wrong when editing /etc/profile recently, but I'm not sure. How can I fix this?

Best Answer

A recent upgrade changed default settings for sudo. If anyone have this problem check your sudoer configuration. From an example in the man page:

# Run X applications through sudo; HOME is used to find the
# .Xauthority file.  Note that other programs use HOME to find
# configuration files and this may lead to privilege escalation!
Defaults env_keep += "DISPLAY HOME"

Make sure you have something like that in /etc/sudoers (use visudo to edit the file if you need to).

EDIT: I don't know exactly since when, but at least xauth 1.0.9 supports the environment variable XAUTHORITY. Setting that and leaving HOME untouched, also fixes the protocol warning and is the better solution, as no world-writeable IPC resources (sockets/pipes) are created pointing to root resources (one avenue for privilege escalation). xauth doesn't automatically export the variable to the environment, so the best way is to set it in your shell's initialization.