Met*_*der 86 users permissions files access-control
*nix 用户权限非常简单,但是当您在访问给定文件之前必须考虑所有父目录访问权限时,事情可能会变得混乱。如何检查用户是否有足够的权限?如果不是,那么哪个目录拒绝访问?
例如,假设一个 userjoe和文件/long/path/to/file.txt. 即使file.txtchmoded到777,joe还是要能访问/long/,然后/long/path/再/long/path/to/之前。我需要的是一种自动检查这一点的方法。如果joe没有访问权限,我也想知道他被拒绝的地方。也许他可以访问/long/,但不能/long/path/。
exu*_*sum 95
要直观地验证访问,您可以使用
namei -m /path/to/really/long/directory/with/file/in
Run Code Online (Sandbox Code Playgroud)
这将在垂直列表中输出路径中的所有权限。
或者
namei -l /path/to/really/long/directory/with/file/in
Run Code Online (Sandbox Code Playgroud)
列出所有所有者和权限。其他答案解释了如何以编程方式验证这一点。
小智 20
您可以使用 bash 来执行此操作。
$ cat check-permissions.sh
#!/bin/bash
file=$1
# Handle non-absolute paths
if ! [[ "$file" == /* ]] ; then
path=.
fi
dirname "$file" | tr '/' $'\n' | while read part ; do
path="$path/$part"
# Check for execute permissions
if ! [[ -x "$path" ]] ; then
echo "'$path' is blocking access."
fi
done
if ! [[ -r "$file" ]] ; then
echo "'$file' is not readable."
fi
$ ./check-permissions.sh /long/path/to/file.txt
Run Code Online (Sandbox Code Playgroud)
要为特定用户检查此项,您可以使用sudo.
sudo -u joe ./check-permissions.sh /long/path/to/file.txt
Run Code Online (Sandbox Code Playgroud)
Kev*_*rke 11
如果您具有 root 访问权限,请模拟用户,然后运行test -r(read)、test -w(write) 或test -x(execute) 以检查用户是否可以读取/写入/执行给定文件。
sudo -u otheruser test -w /file/to/test || {
echo "otheruser cannot write the file"
exit 1
}
Run Code Online (Sandbox Code Playgroud)
正如我从你的问题中得到的那样,你应该为不同的用户(不仅仅是 joe)检查它,所以在这种情况下,最简单的方法是通过 sudo 递归检查它,如下所示:
FILE=$1 ; T_USER=$2 ;
if sudo -u $T_USER [ -r "$FILE" ] ; then
echo "original file $1 is readable for $T_USER"
else
while sudo -u $T_USER [ ! -x "$FILE" ] ; do FILE=$(dirname "$FILE") ; done
echo "only $FILE is readable for $T_USER"
fi
Run Code Online (Sandbox Code Playgroud)
用法:
./script.sh /long/path/to/file.txt joe
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
152401 次 |
| 最近记录: |