Linux – Preserve Environment Variables when using sudo

Sometimes we may want to preserve the environment variables of the original user when using sudo.

If you want to preserve all env variables, you could use the -E option. But use it with caution.

sudo -E bash -c 'echo $PATH'


A better approach is to list those env variables which u want to preserve in the sudoers config file. For example, i want to keep the HTTP_PROXY and HTTPS_PROXY.

Defaults env_keep +="HTTP_PROXY"
Defaults env_keep +="HTTPS_PROXY"


So every time you use the sudo command, the HTTP_PROXY and HTTPS_PROXY are kept.

Done =)



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.