因此,我有一个python脚本,需要在环境变量中定义区域设置,如:export LC_ALL=C.UTF-8。因此,我想我只需要创建一个脚本,如下所示:
#!/bin/bash
export LC_ALL=C.UTF-8
sudo python3 example.py但是python脚本抱怨环境变量没有正确设置。在执行脚本之前,当我手动运行这些命令或导出环境变量时,程序可以工作。这里一定有我错过的东西。
发布于 2020-10-15 07:04:07
在理论上,您是正确的:使用export标记的环境变量将被导出到子进程(即:从这个shell启动的程序)。
然而。
sudo有点不同,因为它具有高度的安全性敏感性。毕竟,如果您使用正确的变量(想想LD_PRELOAD),您可以完全改变使用超级用户权限运行的命令的行为。
因此,sudo积极防止将环境变量传递给sudoed进程。
幸运的是(对您来说),您可以更改保留了哪些env-vars (至少,如果系统的安全策略允许的话)。
来自man 8 sudo
-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.
--preserve-env=list
Indicates to the security policy that the user wishes to add the
comma-separated list of environment variables to those preserved
from the user's environment. The security policy may return an
error if the user does not have permission to preserve the
environment. This option may be specified multiple times.https://stackoverflow.com/questions/64366336
复制相似问题