sudo to other user doesn’t preserve DISPLAY variable

I’ve got an /etc/sudoers file that works just fine in all versions of Fedora prior to F18, but we’ve noticed that when upgrading to F18 (and installing FreeIPA, though the cases I’ll show here were run with sudo processing done using this file to minimize variables), sudo is not preserving the DISPLAY variable as requested by sudo:

# sudo -V
:
Environment variables to preserve:
        PYTHONPATH
        :
        HOSTNAME
        DISPLAY
        COLORS
:
# exit
[user2]$ echo $DISPLAY
:1
[user2]$ sudo su - user1
[user1]$ echo $DISPLAY
DISPLAY: Undefined variable.
[user1]$

Usually, we use ssh -Y to pipe displays back and forth, but this is preventing us from doing so. I’ve been troubleshooting this for several days without success. Has anyone else experienced this, or have any ideas?

Update: Further testing revealed this may not be a sudo problem, but may be related to su itself:

[user2]$ sudo -u user1 echo $DISPLAY
:1
[user2]$

Does this change things? Why would “su” start stripping away (parts of) the environment?

Answer

While sudo preserves the variables, your use of su wipes them again. Don’t use su, it’s entirely unnecessary. If you want a shell, use sudo -i

Attribution
Source : Link , Question Author : wortmanb , Answer Author : Dennis Kaarsemaker

Leave a Comment