Q: "Set a systemwide environment variable in a FreeBSD jail."
A: Set the variable both in /etc/profile
and /etc/csh.cshrc
.
Details
Quoting from 3.9. Shells
How to set an environment variable differs between shells. In tcsh(1) and csh(1), use setenv to set environment variables. In sh(1) and bash, use export to set the current environment variables.
All examples below were executed in a jail
FreeBSD test_01.example.org 12.0-RELEASE FreeBSD 12.0-RELEASE r341666 GENERIC i386
Users with default shell sh
and bash
/etc/profile
is the right place to set global environment. For example
$ cat /etc/profile
NODE_ENV=production; export NODE_ENV
Quoting from profile(4)
All users who have the shell, sh(1), as their login command have the commands in these files executed as part of their login sequence.
For example, the user admin
$ grep admin /etc/passwd
admin:*:1001:1001:User &:/home/admin:/bin/sh
executes /etc/profile
" as part of its login sequence" and sets the environment
$ echo $NODE_ENV
production
This environment will be preserved both in csh
and tcsh
$ csh
admin@test_01:~ % echo $NODE_ENV
production
It will be also preserved by su
$ su
root@test_01:/home/admin # echo $NODE_ENV
production
There is no export
command in csh
and tcsh
. This explains the error
$ csh
admin@test_01:~ % source /etc/profile
NODE_ENV=production: Command not found.
export: Command not found.
Users with default shell csh
and tcsh
/etc/csh.cshrc
is the right place to set the global environment for csh
and tcsh
. For example
root@test_01:~ # cat /etc/csh.cshrc
setenv NODE_ENV production
Default shell of root
is by default csh
root@test_01:~ # grep root /etc/passwd
root:*:0:0:Charlie &:/root:/bin/csh
This sets the environment
root@test_01:~ # echo $NODE_ENV
production