在 Debian(以及其他 Linux 发行版)里,opendir() 能否成功,取决于目录的权限和调用者的身份。下面分点说明。
opendir() 成功的条件是:
对目标目录拥有“执行(x)权限”
例如:
drwxr-xr-x user user testdir
rwx ✅r-x ✅r-x ✅只要调用者属于其中一类,并能“进入”目录,就可以 opendir。
⚠️ 注意:
ls 看到文件名cd / opendirchmod 755 /path/to/dir
chmod 700 /path/to/dir
chmod 750 /path/to/dir
chown user:group /path/to/dir
如果是服务(如 nginx、自定义 daemon):
User=sudo -u user program用 perror("opendir") 或 strerror(errno) 看原因:
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
DIR *d = opendir("/path");
if (!d) {
perror("opendir");
}
常见错误:
EACCES:权限不足ENOENT:目录不存在ENOTDIR:不是目录如果标准权限不够用:
setfacl -m u:alice:rx /path/to/dir
查看:
getfacl /path/to/dir
777750700如果你是指 某个具体程序 / 服务 / C 代码 opendir 失败,可以把:
ls -ld 输出