I finally resolved this with an alias:
alias sudo='sudo PATH="$PATH" HOME="$HOME" LD_LIBRARY_PATH="$LD_LIBRARY_PATH"'
Be sure to use single quotes, so that the variables are expanded at the time of invocation, not at the time the alias is defined.
- preserves HOME (which otherwise gets set to /root)
- preserves PATH (which otherwise gets set to a 'safe' path in suders)
- preserves the current value of LD_LIBRARY_PATH (which otherwise gets blanked)
(You can see what things get set to with:
sudo bash -c "echo $HOME")
I am working with custom drivers and always have to sudo my test programs to access the driver. I don't want to install test versions of libraries in the system area, so I use LD_LIBRARY_PATH to setup a specific test directory as needed. I cant just set a fixed LD_LIBRARY_PATH, I need to be able to change it and keep the current setting. Preserving PATH and HOME gives me access to my work environment - scripts and directory structure.
This alias avoids having to grant blanket permissions in sudoers... and there is apparently no workaround for LD_LIBRARY_PATH regardless of sudoers settings anyway. It does not work sudo inside of scripts, but those can be hardcoded as needed.