linux - sudo执行的程序获取错误的DBUS和env
问题描述
我需要用getenv来判断系统是X11还是Wayland,用DBUS实现截屏功能。他们在没有 sudo 的情况下工作得很好。但是,当使用 sudo 时,getenv 得到了错误的值,gdbus 无法定位 DBUS。我很确定这是 sudo 的问题,因为我尝试了
echo $WAYLAND_DISPLAY
and sudo echo $Wayland_DISPLAY
, d-feet
and sudo d-feet
。当他们不使用 sudo 时,他们是对的。但是由于程序中其他功能的要求,我不得不使用sudo。有什么好主意吗?
解决方案
有什么好主意吗?
默认情况下,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
推荐阅读
- javascript - js密码生成器,如何将返回值组合在一个字符串中
- python - 如何处理弹出浏览器?(使用机器人框架,python)
- angular - 角度传递数据到动态创建和编译的组件
- c# - 更新 IEnumerable 中的项目
- typescript - TypeScript 类型继承:如何将具有父类型的变量分配给具有子类型的变量
- r - 如何将 Y 轴上的 0 与绘图网格对齐?
- angular - 如何在打字稿中单击表格中的按钮时获取动态表格行数据
- javascript - 在角度应用程序中登录后显示用户名
- python - 无法在 tf.graph (saved_model) 中定位输入张量
- c# - 如何解决 Twitter API 错误“无效媒体”