How to keep environment variables when using sudo
The trick is to add environment variables to sudoers
file via sudo visudo
command and add these lines:
Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"
taken from ArchLinux wiki.
For Ubuntu 14, you need to specify in separate lines as it returns the errors for multi-variable lines:
Defaults env_keep += "http_proxy"
Defaults env_keep += "https_proxy"
Defaults env_keep += "HTTP_PROXY"
Defaults env_keep += "HTTPS_PROXY"
How to make my environment variables available for sudo commands?
sudo has option -E
or long-option --preserve-env
for this.
Usage: sudo -E command args ...
Of course, you would need to export the variable from parent shell, in order for it to be visible to sudo
.
Mac OS X no environment variables when using sudo
From man sudo:
-E
The -E (preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the -E option is specified and the user does not have permission to preserve the environment.
If you want variables to preserve it's values, call sudo -E tns doctor
. You can also call sudo "ANDROID_HOME=$ANDROID_HOME" "JAVA_HOME=$JAVA_HOME" tns doctor
to export only these two variables.
Environment variable not accessible with Python with sudo
Sudo runs with different environment.
To keep current environment use -E
flag.
sudo -E python test.py
-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.
How to export environment variable to background process run using sudo?
Normally sudo replaces the current environment with the environment of the new user, for security reasons. Use sudo -E
to preserve the calling environment. Or you can pass variables on the command line, sudo NAME=jake
.
Is it possible to make the Bash source command keep environment variables passed in manually?
I don't think it is. But you can modify your .env file as follows to prevent it from overwriting NODE_ENV
.
TZ=UTC
NODE_ENV=${NODE_ENV:-development}
Not able to set environment variable for sudo -u user
you need to run the db2profile when you sudo
sudo -u icinga sqllib/db2profile; '/usr/lib//nagios/plugins/check_db2_health' ...
Related Topics
How to Display Number to Two Decimal Places in Bash Function
Defining a Variable With or Without Export
Environment Variable Substitution in Sed
How to Install Latest Version of Git on Centos 8.X/7.X/6.X
Appending a Line to a File Only If It Does Not Already Exist
How to Setup Public-Key Authentication
Which Linux Ipc Technique to Use
Free Git Server With Web Gui a La Bitbucket/Github
Linux Bash: Multiple Variable Assignment
Linux/Unix Command to Determine If Process Is Running
Convert String to Hexadecimal on Command Line
Using Printf in Assembly Leads to Empty Output When Piping, But Works on the Terminal
The 'Eval' Command in Bash and Its Typical Uses
How to Quickly Sum All Numbers in a File
Maximum Number of Processes in Linux
Linux Command to Print Directory Structure in the Form of a Tree