普通非 root 用户挂载 USB 设备

发布时间 2023-12-19 12:44:54作者: 飞舞的冰龙

Root 用户可以直接使用 mount 命令挂载硬盘(包括 U 盘),但是普通用户无法使用该命令。

使用 udisksctl 挂载硬盘

udisksctl mount -b /dev/sdx1  # 挂载
udisksctl unmount -b /dev/sdx1  # 卸载
udisksctl power-off --block-device /dev/sdx1  # 断电

udisksctl 可能需要安装才能使用。

apt install udisks2

免密挂载

默认情况下,使用 udisksctl 对用户的用户组有一定的要求,同时需要输入密码进行认证。

创建 /etc/polkit-1/rules.d/10-udisks2.rules

// See the polkit(8) man page for more information
// about configuring polkit.

// Allow udisks2 to mount devices without authentication
// for users in the "sudo" group.
polkit.addRule(function(action, subject) {
    if ((action.id == "org.freedesktop.udisks2.filesystem-mount-system" ||
         action.id == "org.freedesktop.udisks2.filesystem-mount" ||
         action.id == "org.freedesktop.udisks2.filesystem-mount-other-seat") &&
        subject.isInGroup("sudo")) {
        return polkit.Result.YES;
    }
});

文件中的 action 参考使用 udisksctl 命令时提示密码的信息。isInGroup 用于确认用户组。