I need set a password to environment variable but the problem is password include sharp sign and when i want use this variable just show me till first sharp sign. for example assume he#l#lo
is my password and when i set it to /etc/environment
with PASSWORD_API="he#l#lo"
then i print it just show he
and leave the character after #
. but when i using export PASSWORD_API="he#l#lo"
from terminal all if fine!what is problem by adding environment variable from \etc\environment
i can't change the password so if you have any idea please help me . thank you
3 Answers
As far as I can tell, this is a limitation of how the pam_env
module reads lines of the environment file (before even attempting to parse them into name=value
pairs).
Specifically, in the _pam_assemble_line
function (in libpam/pam_handlers.c
):
/*
* we are only interested in characters before the first '#'
* character
*/
while (*s && *s != '#')
++s;
There appears to be no provision for treating quoted or escaped strings any differently.
If you need to set an environment variable with such characters, then you will need to do it elsewhere - for example, creating a file /etc/profile.d/password-api.sh
containing the line
export PASSWORD_API="he#l#lo"
which should get sourced by the default /etc/profile
.
Of course as I'm sure you're aware, storing passwords ANYWHERE in plain text is a bad idea - if your application provides another option (such as using key-based authentication, or adding the password securely to an agent) you should use it.
Remove the spaces around the equal sign:
$ PASSWORD_API="he#l#lo"
$ echo $PASSWORD_API
he#l#lo
-
i do changes and then restarting my laptop that but it's again show me
he
– mehdiAug 7, 2019 at 10:35 -
I have tried a lot of ways of setting environment but the export only seems to work and to run a bunch exports everytime it reboots you run a shell script in a cron job.
Adding systemd Environment Variable https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile= this shows how it is done in the environment file which can only be used to read non # values otherwise it break on # value.
Create a shell script with:
export Key="Value"
Then run:
crontab -e
Select the editor you will use and move on to add your script to run on reboot.
@reboot sh /home/username/profile.d/env.sh
Save the file and reboot. Anything you add in env.sh will run same goes for environment variables. They will be set easily and there won't be any breaks due to #
.
printenv
in terminal. 2-in python shell i need value so i usingpassword = os.environ.get('PASSWORD_API')
and thenprint(password)
.