1

I need to set a systemwide environment variable (i.e. NODE_ENV=production) in a FreeBSD jail.

I did try setting it in /etc/profile, but when I sourced it

root@www:/ # source /etc/profile

I got

export: Command not found.

It works on the host system.

I also tried setting it /.chsrc. But that only makes the variable availble to root but not any other users of the jail.

LongHike
  • 157
  • 1
  • 6
  • You appear to be running a shell that supports the `source` built-in but not `export`. E.g. `csh`. Try `setenv` instead. – Richard Smith Jan 14 '20 at 11:23

1 Answers1

2

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
Vladimir Botka
  • 5,138
  • 8
  • 20
  • Which shell uses the nginx process, e.g.? – LongHike Feb 03 '20 at 18:35
  • It depends on how Nginx is started. FreeBSD uses the rc(8) system. See [12.4. Managing Services in FreeBSD](https://docs.freebsd.org/en_US.ISO8859-1/books/handbook/configtuning-rcd.html). Review `/usr/local/etc/rc.d/nginx` how to customize the environment. [Practical rc.d scripting in BSD](https://docs.freebsd.org/en_US.ISO8859-1/articles/rc-scripting/) might also help you. – Vladimir Botka Feb 15 '21 at 08:24