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 =)

Reference:

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s