如何在 Debian Linux 中允许用户 www-data 访问 i2c?

How to allow i2c access by user www-data in Debian Linux?

我有一个访问 /dev/i2c-1 的程序。当 运行 作为 root 时,它工作正常。现在,我必须能够通过在 Apache 服务器上使用 PHP 脚本来 运行 这个。程序启动并且 运行s OK,但是用户 www-data 无法访问 i2c-1。没有权限!按照建议,我已将 www-data 添加到 i2c 组 "adduser www-data i2c",但这没有帮助。我在单板嵌入式设备上使用命令行 Debian Linux。我怎样才能做到这一点,同时至少保持最低限度的安全性?

gI2c1File = open("/dev/i2c-1", O_RDWR);
if (gI2c1File < 0) {
    file_logError("Error opening i2c-1 interface", strerror(errno));
    return 0;
} 

仅将 apache 用户添加到组中是不够的;您还必须确认“/dev/i2c-1”目录的组权限允许读取和写入(或您要对其执行的任何任务。)

作为 root,您应该执行以下命令:

chmod g+rw /dev/i2c-1

此命令将更改此指定目录的权限,以允许其组中的用户对其进行读写。