2

On Ubuntu 16.10, I set a system wide environment variable in /etc/environment file. The variable is a string that has a length of thousands of characters (the string is a comma-separated list of small alpha codes). The problem is that the environment variable gets truncated.

Python code to generate the content of /etc/environment file with a variable named X:

fp = open('/etc/environment','w')
fp.write('X="' + ",".join([str(i) for i in range(2000)]) + '"')
fp.close()

Logout shell, login again and show environment variable

echo $X

On my machine, result is:

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72

Someone told me about a 128K limit on environment variables but my variable is relatively small in length. Can anyone explain me why it gets truncated and more important, what can I do to remedy to this situation?

EDIT: output of dpkg -l libpam-modules:

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                           Version                      Architecture                 Description
+++-==============================================-============================-============================-==================================================================================================
ii  libpam-modules:amd64                           1.1.8-3.2ubuntu2             amd64                        Pluggable Authentication Modules for PAM
  • Can you confirm that your code successfully placed the entire value in the file? – Jeff Schaller Jul 6 '17 at 15:37
  • My file contains only the line X="1,2,3,...,2000" – Patrick Jul 6 '17 at 15:53
  • 1
    I would prefer to define system wide env variable in one place instead of in each user's home directory – Patrick Jul 6 '17 at 18:28
  • 3
    It looks like pam_env from that package limits the var=value assignments to 1024 bytes each. You can get the source (apt-get source libpam-modules) and modify it, or modify /etc/profile to have your shell read in /etc/environment as if were shell commands. – Mark Plotnick Jul 6 '17 at 21:32
  • 1
    The second solution seems easier. Can I ask you how to modify /etc/profile? Thanks again Mark. – Patrick Jul 7 '17 at 1:35
1

I'm not sure it's exactly what Mark suggested but I updated my /etc/environment file to have now the following syntax:

export X="1,2,..." 

and I added a line in /etc/profile :

source /etc/environment

Don't know if it's the good way but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.