Linux 按chmod或规则输入和设置文件权限
我正在使用evdev和uinput编写一个伪和弦键盘重新映射。我不想给我的普通用户读写/dev/input/event*和/dev/uinput的权限,所以我创建了一个新用户(hopr),其唯一目的是运行程序。当我试图让它工作时,我注意到了一些非常奇怪的行为,我希望有人能解释一下 为了确保这与uinput无关,我使用python evdev编写了一个简单的测试脚本:Linux 按chmod或规则输入和设置文件权限,linux,permissions,udev,evdev,uinput,Linux,Permissions,Udev,Evdev,Uinput,我正在使用evdev和uinput编写一个伪和弦键盘重新映射。我不想给我的普通用户读写/dev/input/event*和/dev/uinput的权限,所以我创建了一个新用户(hopr),其唯一目的是运行程序。当我试图让它工作时,我注意到了一些非常奇怪的行为,我希望有人能解释一下 为了确保这与uinput无关,我使用python evdev编写了一个简单的测试脚本: from evdev import UInput, ecodes KEY_RELEASE = 0L KEY_PRESS = 1L
from evdev import UInput, ecodes
KEY_RELEASE = 0L
KEY_PRESS = 1L
ui = UInput()
ui.write(ecodes.EV_KEY, ecodes.KEY_A, KEY_PRESS)
ui.write(ecodes.EV_KEY, ecodes.KEY_A, KEY_RELEASE)
ui.syn()
ui.close()
我还创建了一个名为hopr的新用户和一个名为uinput的新组,并将hopr添加到组uinput和input中(以读取事件)。计划将/dev/uinput的组更改为uinput,并且仅对该组具有rw权限。这两个用户的组是:
user: user adm cdrom sudo dip plugdev lpadmin sambashare
hopr: hopr input uinput
首先,我只是尝试在不更改组的情况下运行脚本,并且在/etc/udev/rules.d中没有额外的规则。默认情况下,XUbuntu 16.04中对/dev/uinput的权限是用户根和组根的rw
crw-rw----+ 1 root root 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # evdev.uinput.UInputError: "/dev/uinput" cannot be opened for writing
看到我的普通用户能够运行脚本,而新用户却不能,我有点惊讶。接下来,我使用chmod为每个人添加了rw权限:
crw-rw-rw-+ 1 root root 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # OK
crw-rw----+ 1 root uinput 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # evdev.uinput.UInputError: "/dev/uinput" cannot be opened for writing
好的,这是意料之中的。接下来,我再次删除了所有人的rw,并使用chown将组更改为uinput:
crw-rw-rw-+ 1 root root 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # OK
crw-rw----+ 1 root uinput 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # evdev.uinput.UInputError: "/dev/uinput" cannot be opened for writing
同样,这是出乎意料的,但情况变得更糟。接下来,我再次为每个人添加了rw权限,但保留了组的uinput
crw-rw-rw-+ 1 root uinput 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # evdev.uinput.UInputError: "/dev/uinput" cannot be opened for writing
现在我真的很困惑,但是使用chown将组更改回root使其再次工作
crw-rw-rw-+ 1 root root 10, 223 mar 23 09:36 /dev/uinput
user> python test.py # OK
hopr> python test.py # OK
很明显,这里有些事情我不明白,所以我还尝试在/dev/udev/rules.d中使用规则正确设置它:
KERNEL=="uinput*", GROUP="uinput", MODE="0660"
令我大吃一惊的是,现在一切都(几乎)如预期的那样工作了!受限hopr用户可以在没有rw权限的情况下运行程序。唯一的问题是为什么我的普通用户也可以运行它
crw-rw----+ 1 root uinput 10, 223 mar 23 09:58 /dev/uinput
user> python test.py # OK
hopr> python test.py # OK
因此,我的问题是:
你是怎么解决的?显然,我没有。我在udev中使用一个规则文件,普通用户仍然可以(ubuntu20.04)将密钥事件写入uinput,即使它不在uinput组中。我想我只是不再去想它了。重要的是,没有任何用户(除了hopr)可以从键盘上读取事件,这是可以预期的。你是如何解决这个问题的?显然,我没有。我在udev中使用一个规则文件,普通用户仍然可以(ubuntu20.04)将密钥事件写入uinput,即使它不在uinput组中。我想我只是不再去想它了。重要的是,没有任何用户(除了hopr)可以从键盘读取事件,这是可以预期的。