What Is the Maximum Size of a Linux Environment Variable Value

What is the maximum size of a Linux environment variable value?

I don't think there is a per-environment variable limit on Linux. The total size of all the environment variables put together is limited at execve() time. See "Limits on size of arguments and environment" here for more information.

A process may use setenv() or putenv() to grow the environment beyond the initial space allocated by exec.

Here's a quick and dirty program that creates a 256 MB environment variable.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
size_t size = 1 << 28; /* 256 MB */
char *var;

var = malloc(size);
if (var == NULL) {
perror("malloc");
return 1;
}

memset(var, 'X', size);
var[size - 1] = '\0';
var[0] = 'A';
var[1] = '=';

if (putenv(var) != 0) {
perror("putenv");
return 1;
}

/* Demonstrate E2BIG failure explained by paxdiablo */
execl("/bin/true", "true", (char *)NULL);
perror("execl");


printf("A=%s\n", getenv("A"));

return 0;
}

Shell variable - of limited size?

Yes they can be. It depends on your OS and/or the shell flavours and versions. It is safer to use temporary files if you expect variable values to exceed 1-4kB.

EDIT

Also see What is the maximum size of an environment variable value?; this deals with the OS limitation on total environ size (cumulative size of all VARIABLE=VALUEs) which affects exported variables, but the shell itself may have its own limitations re. all (including non-exported) variable sizes.

This being said, unless you have portability in mind, GNU bash is relatively good about not limiting (non-exported) variables' sizes and can very likely hold arbitrary amounts of data as long as malloc can find sufficient memory and contiguous address space. :)

Shell Variable capacity

IIRC, bash does not impose a limit on how much data a variable can store. It is however limited by the environment that bash was executed under. See this answer for a more comprehensive explanation.

Reduce bash environment variables size

set | wc -c (which is a much simpler way to count the size of all shell variables) is not relevant, since most of those variables are not exported and the limit applies only to exported variables and command line arguments.

Try export | wc -c to get a realistic view. On one shell session I happen to have open:

$ set | wc -c
241235
$ export | wc -c
4652

You can also get a variety of useful information from xargs:

$ xargs --show-limits </dev/null
Your environment variables take up 3730 bytes
POSIX upper limit on argument length (this system): 2091374
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2087644
Size of command buffer we are actually using: 131072

Or a simpler report from getconf:

$ getconf ARG_MAX
2097152

It is quite possible that your particular system allows more than ARG_MAX bytes.

Kubernetes: What is the maximum value of env's count in pod?

There is no limits for env's count in pod. You are not limited by count/numbet/etc - you are limited by memory itself.

What you have in any linux os, is execve. There are no shortcuts for different sections, your one is Limits on size of arguments and environment

Most UNIX implementations impose some limit on the total size of the
command-line argument (argv) and environment (envp) strings that may
be passed to a new program.
POSIX.1 allows an implementation to
advertise this limit using the ARG_MAX constant (either defined in
<limits.h> or available at run time using the call
sysconf(_SC_ARG_MAX)). On Linux prior to kernel 2.6.23, the memory
used to store the environment and argument strings was limited to 32
pages (defined by the kernel constant MAX_ARG_PAGES). On architectures
with a 4-kB page size, this yields a maximum size of 128 kB.

On kernel 2.6.23 and later, most architectures support a size limit
derived from the soft RLIMIT_STACK resource limit
(see getrlimit(2))
that is in force at the time of the execve() call. (Architectures with
no memory management unit are excepted: they maintain the limit that
was in effect before kernel 2.6.23.) This change allows programs to
have a much larger argument and/or environment list. For these
architectures, the total size is limited to 1/4 of the allowed stack
size. (Imposing the 1/4-limit ensures that the new program always has
some stack space.) Since Linux 2.6.25, the kernel places a floor of 32
pages on this size limit, so that, even when RLIMIT_STACK is set very
low, applications are guaranteed to have at least as much argument and
environment space as was provided by Linux 2.6.23 and earlier. (This
guarantee was not provided in Linux 2.6.23 and 2.6.24.) Additionally,
the limit per string is 32 pages (the kernel constant MAX_ARG_STRLEN),
and the maximum number of strings is 0x7FFFFFFF.

So, in 2 words,

  • On Linux prior to kernel 2.6.23: maximum size of 128 kB

  • On kernel 2.6.23 and later: most architectures support a size limit derived from the soft RLIMIT_STACK resource

What I also think - there is a limit in 1MB of etcd(this is a place where kubernetes keeps all objects). So in case you use configmaps - you have to remember about this limitation. Limit 1MB is etcd limit itself.

Urls to check:

  1. Size limit for ConfigMap

  2. @Rico answer on stack: Kubernetes ConfigMap size limitation

Python using environment variables

  1. yes they work same, nothing different is mentioned in official documentation.
  2. char limit depends on your system specification. please refer: What is the max length of a python string?
  3. to block the access to the env var you can use "deny"
    eg.
    Deny from env="users"


Related Topics



Leave a reply



Submit