22

On Slackware, using sbopkg permits one to build a package from source. Repos is not big as Debian, but it's nice.

Some software can use environment variables, for example on the VICE c64 emulator, if the variable FFMPEG is set to yes, it will enable ffmpeg recording the emulator.

I tried to use

$ export FFMPEG=yes; sudo sbopkg -B -i vice

but ffmpeg is disabled.

Instead I had to use

$ su -
$ export FFMPEG=yes
$ sbopkg -B -i vice

which works.

How to use environment variables with sudo?

37

You may use sudo's -E option:

FMPEG=yes sudo -E sbopkg -B -i vice

From the manual:

-E, --preserve-env

Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to preserve the environment.

Note that this exports all your existing environment variables. It is safer to only export the environment variables you need with the following syntax:

sudo FMPEG=yes sbopkg -B -i vice

  • Thanks but it doesn't work(the .-E option) the other line is ok so because first answer i select as correct. – elbarna Jan 16 '17 at 18:22
10

sudo sanitizes the variables in the environment before invoking the given command. You will have to ask it to preserve the variables in the environment for your command to work.

Additionally, you will have to export the FMPEG variable before using sudo (export FMPEG=yes; sudo), assign it in the same go as invoking sudo (FMPEG=yes sudo), or use env (env FMPEG=yes sudo).

To preserve the environment variables, use -E (or --preserve-env):

$ env FMPEG=yes sudo -E sbopkg -B -i vice

It's also possible to set specific environment variables like this:

$ sudo FMPEG=yes sbopkg -B -i vice

If this fails due to the security policy in place, bring up a root shell and set the variable there:

$ sudo -s
# env FMPEG=yes sbopkg -B -i vice
# exit
6

note that: (export a=b; command) is equivalent to a=b command. Note the brackets.

Then if we apply the variable not to sudo but direct to sbopkg, so instead of FFMPEG=yes sudo sbopkg -B -i vice we can do sudo FFMPEG=yes sbopkg -B -i vice

If a security police prevent this, then: sudo bash -c "FFMPEG=yes sbopkg -B -i vice"

(Don't use -E, it us insecure as it will leak all sorts of unexpected variables.)

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.