首页 > 解决方案 > sudo执行的程序获取错误的DBUS和env

问题描述

我需要用getenv来判断系统是X11还是Wayland,用DBUS实现截屏功能。他们在没有 sudo 的情况下工作得很好。但是,当使用 sudo 时,getenv 得到了错误的值,gdbus 无法定位 DBUS。我很确定这是 sudo 的问题,因为我尝试了 echo $WAYLAND_DISPLAYand sudo echo $Wayland_DISPLAY, d-feetand sudo d-feet。当他们不使用 sudo 时,他们是对的。但是由于程序中其他功能的要求,我不得不使用sudo。有什么好主意吗?

标签: linuxsudo

解决方案


有什么好主意吗?

默认情况下,sudo在全新的环境中运行该命令。来自man 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

推荐阅读