How to keep environment variables when using sudo

前端 未结 6 757
滥情空心
滥情空心 2020-11-22 01:55

When I use any command with sudo the environment variables are not there. For example after setting HTTP_PROXY the command wget works fine without sudo

6条回答
  •  时光取名叫无心
    2020-11-22 02:36

    First you need to export HTTP_PROXY. Second, you need to read man sudo carefully, and pay attention to the -E flag. This works:

    $ export HTTP_PROXY=foof
    $ sudo -E bash -c 'echo $HTTP_PROXY'
    

    Here is the quote from the man page:

    -E, --preserve-env
                 Indicates to the security policy that the user wishes to preserve their
                 existing environment variables.  The security policy may return an error
                 if the user does not have permission to preserve the environment.
    

提交回复
热议问题