1 分钟阅读 次阅读

概要:本文记录两台 Ubuntu 20.04 机器桌面图标”是否需要右键允许启动”表现不一致的排查过程:说明 .desktop 文件的信任机制(+x 权限 + gvfs 的 metadata::trusted 标记,且标记按用户隔离),并定位到根因是第三方定制的桌面图标扩展把信任初始化硬编码为 true、同时注释了右键菜单项与快捷方式箭头徽标,最后给出可迁移的排障速查命令与手动改造方法。

1 背景

两台同为 Ubuntu 20.04 的机器,实际使用中暴露出一个明显差异:在 A 机上,放到桌面的安装包/自写启动器图标首次双击需要右键”允许启动”;而在 B 机上图标默认就能直接执行,右键菜单里甚至看不到”允许启动/不允许启动”选项。本文记录排查过程与根因。


2 桌面图标的本质与信任机制

Linux 桌面图标是 .desktop 文件——本质是纯文本配置,关键字段 Exec= 指定双击执行的任意命令,因此具有与可执行文件同等的危险性。GNOME 从 3.28 起对用户桌面目录中的 .desktop 默认不信任(下载、U 盘等来源易混入恶意文件),首次双击会要求用户显式确认”允许启动”。

是否”可启动”由桌面图标扩展(Desktop Icons,即 DING)判断,条件有两个:

  1. 文件有可执行权限位(chmod +x)
  2. metadata::trusted = true——信任标记,但不写入文件本身,而是按用户分别存储在 ~/.local/share/gvfs-metadata/,由 gvfs 管理

对应命令:

# 写入信任标记(gio set = 写)
gio set ~/桌面/xxx.desktop metadata::trusted true
# 查询(gio info = 读)
gio info -a metadata::trusted ~/桌面/xxx.desktop

系统目录 /usr/share/applications/ 下的启动器被无条件信任,不需要这两条。


3 排查中踩到的关键坑

信任标记按用户隔离。 用 root 执行 gio set 时,标记写入的是 root 自己的元数据库,图形用户 user 会话读到的仍是 false。表现为:gio info 查到 true,但双击依旧无效。正确姿势必须以图形用户身份、并带上其会话总线:

chmod +x /home/user/桌面/xxx.desktop
runuser -u user -- env DISPLAY=:0 \
  DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/<uid>/bus \
  gio set /home/user/桌面/xxx.desktop metadata::trusted true

即便标记与权限都正确,若右键”不允许启动”过,DING 会缓存旧状态,需要 Alt+F2 → r 重启 Shell 或注销重登才会重读。


4 根因:桌面图标扩展被改动的三处

对比两机的 fileItem.js(通常位于 sudo vim /usr/share/gnome-shell/extensions/desktop-icons@csoriano/fileItem.js,包名形如 gnome-shell-extension-desktop-icons-ng、…-ubuntu-desktop),版本号差异也很直观:A 机为 20.04.0-3~ubuntu20.04.6,B 机为 20.04.0-3~ubuntu20.04.6+xxx1——版本号尾部的额外后缀说明这个包被第三方重新打包过。定制方共改动三处:

4.1 信任初始化硬编码(第 264 行)——”默认可执行”的根本原因

A 机(官方原版):

this._trusted = fileInfo.get_attribute_as_string("metadata::trusted") == "true";

B 机(第三方定制版):

this._trusted = true; //fileInfo.get_attribute_as_string('metadata::trusted') == 'true';

读取逻辑被注释,直接置 true。于是 trustedDesktopFile 恒真,双击 doOpen() 无条件走 launch_uris_as_manager() 直接启动——只要文件带 +x 权限即可,trusted 标记被彻底无视。

4.2 右键菜单项整段注释——”允许启动”选项消失

_recreateMenu() 中,添加”Allow Launching / Don’t Allow Launching”菜单项的 if 分支被 /* */ 注释。既然信任永远成立,切换入口也一并移除。

修改前:

this._actionTrash = this._menu.addAction(_('Move to Trash'), () => this._onMoveToTrashClicked());
if (this._isValidDesktopFile && !Extension.desktopManager.writableByOthers && !this._writableByOthers) {
    this._menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
    this._allowLaunchingMenuItem = this._menu.addAction(this._allowLaunchingText,
                                                        () => this._onAllowDisallowLaunchingClicked());
}
break;

修改后:

this._actionTrash = this._menu.addAction(_('Move to Trash'), () => this._onMoveToTrashClicked());
/*
if (this._isValidDesktopFile && !Extension.desktopManager.writableByOthers && !this._writableByOthers) {
    this._menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
    this._allowLaunchingMenuItem = this._menu.addAction(this._allowLaunchingText,
                                                        () => this._onAllowDisallowLaunchingClicked());
}
*/
break;

4.3 快捷方式箭头徽标移除(第 455 行)

_createEmblemedStIcon() 中,原版会给”受信任的 .desktop“图标叠加一个快捷方式小箭头徽标(emblem-symbolic-link),作为”可启动快捷方式”的视觉区分;定制版将此分支注释,图标不再显示箭头(符号链接/损坏链接的徽标保留)。

修改前:

let itemIcon = Gio.EmblemedIcon.new(icon, null);

if (this._isSymlink) {
  if (this._isBrokenSymlink)
    itemIcon.add_emblem(
      Gio.Emblem.new(Gio.ThemedIcon.new("emblem-unreadable")),
    );
  else
    itemIcon.add_emblem(
      Gio.Emblem.new(Gio.ThemedIcon.new("emblem-symbolic-link")),
    );
} else if (this.trustedDesktopFile) {
  // ← 原版:可信的 .desktop 加箭头
  itemIcon.add_emblem(
    Gio.Emblem.new(Gio.ThemedIcon.new("emblem-symbolic-link")),
  );
}

return new St.Icon({ gicon: itemIcon, icon_size: Prefs.get_icon_size() });

修改后:

let itemIcon = Gio.EmblemedIcon.new(icon, null);

if (this._isSymlink) {
  if (this._isBrokenSymlink)
    itemIcon.add_emblem(
      Gio.Emblem.new(Gio.ThemedIcon.new("emblem-unreadable")),
    );
  else
    itemIcon.add_emblem(
      Gio.Emblem.new(Gio.ThemedIcon.new("emblem-symbolic-link")),
    );
} /* else if (this.trustedDesktopFile) {       // ← 定制版:整个分支被注释
    itemIcon.add_emblem(Gio.Emblem.new(Gio.ThemedIcon.new('emblem-symbolic-link')));
} */

return new St.Icon({ gicon: itemIcon, icon_size: Prefs.get_icon_size() });

5 结论与影响

项目 A 机(官方原版扩展) B 机(第三方定制版扩展)  
桌面图标可启动条件 +x 且 metadata::trusted=true 只需 +x,trusted 被忽略 sudo vim /usr/share/gnome-shell/extensions/desktop-icons@csoriano/fileItem.js
首次双击 弹”允许启动”确认 直接运行  
右键菜单”允许启动” 有 无  
可信快捷方式箭头 有 无  

B 机上若图标双击变成打开文本编辑器,基本只可能是缺少 x 权限(如跨文件系统拷贝丢权限),chmod +x 即可,无需任何 trusted 操作。


6 可迁移的排障速查

# 1) 看 desktop 文件本身配置(Exec/Icon/Name…)
cat ~/桌面/xxx.desktop
# 2) 看权限位(缺 x 即双击变编辑)
ls -l ~/桌面/xxx.desktop
# 3) 看信任标记(需以图形用户身份查,否则看到的是自己库里的值)
runuser -u user -- gio info -a metadata::trusted ~/桌面/xxx.desktop

在官方原版扩展上要复刻”免允许启动”体验,没有现成软件包可用(官方源里只有原版包),需手动改扩展代码:把对应位置的信任读取改为硬编码 true(可再按需注释菜单分支与徽标分支),随后 Alt+F2 → r 生效;注意后续 apt upgrade 可能覆盖改动。

留下评论