sudo执行的程序获取了错误的DBUS和env



我需要使用getenv来判断系统是X11还是Wayland,并使用DBUS来实现截屏功能。它们不用sudo也能很好地工作。但是,使用sudo时,getenv得到错误的值,gdbus找不到DBUS。我很确定这是sudo的问题,因为我试过了echo $WAYLAND_DISPLAYsudo echo $Wayland_DISPLAY,d-feetsudo d-feet。他们不使用sudo是对的。但是由于程序中其他功能的要求,我不得不使用sudo。有什么好主意吗?

有什么好主意吗?

默认情况下,sudo在一个新的、新的环境中运行该命令。Fromman 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.

你可以列出你想保留的变量。

sudo --preserve-env=DBUS_SESSION_BUS_ADDRESS,DISPLAY,WAYLAND_DISPLAY,other_variables,etc command

最新更新