我有一个在Linux移动设备上运行的用户级C ++测试应用程序.其中一项测试涉及启用/禁用打印机纸张传感器,这需要在设备文件上写入具有root权限.有没有办法向我的应用程序授予这种特权?如果没有,是否有解决方法?
I have a user-level C++ test application running on a linux mobile device. One of the test involves enabling/disabling printer paper sensor which requires root privileges writing on a device file. Is there a way to grant my application that kind of privilege? If none, is there a workaround for that?
推荐答案可以做到
按root执行:
chown -v root:root /path/to/yourapp chmod -v 4755 /path/to/yourapp或者
chmod -v u+s /path/to/yourapp或者
man chmod这将不适用于脚本.是的,您应该认真对待 jdizzle 所说的删除不必要的特权.
This will not work with scripts. And yes, you should take seriously what jdizzle said about dropping unnecessary privileges.
解决此问题的另一种方法是使运行该应用程序的用户成为拥有设备文件的组的成员.例如,
Another way to solve this is to make the user who runs the application a member of the group that owns the device file. For example,
ls -la /dev/devicefile crw-rw---- 1 root printer 4, 0 may 6 10:56 /dev/devicefile组的
成员可以读写设备,因此您只需将joe添加到printer组(并重新启动会话).
members of the printer group can read and write to the device, so you just need to add joe to the printer group (and restart the session).
gpasswd -a joe printer如果需要调整devicefile权限,则可能需要编辑udev规则以使其永久.但是chmod应该也可以.
If you need to adjust the devicefile permissions, you probably will need to edit udev rules to make it permanent. But chmod should work too.
其他值得研究的选项: setcap(8) (指南此处)和 sudo(8) .
Other options worth investigating: setcap(8) (nice guide here) and sudo(8).
更多推荐
如何授予对用户应用程序的root访问权限?
发布评论