Unix 权限入门:
Every file has an user
. This is a user on the system. Every file also has a group
. This is a group on the system. A user
can be in one or more group
s. A file has exactly one user
and one group
that "own" the file.1
那么数字是什么样的0444
mean?
第一个数字用于一些特殊标志,例如sticky
, setuid
, setgid
。我们现在不需要为此烦恼。只需记住将其设置为0
接下来的三个数字表示三种权限:其中一个表示user
, group
, and other
(所有不是user
or group
), 以该顺序。
为了设置权限,我们使用从零到七的数字(octal数字)。这实际上是一个bitmask https://en.wikipedia.org/wiki/Mask_%28computing%29. 1
is for execute
, 2
is for write
, 4
is for read
.
In a table it looks like:2
N Description ls output
0 No read, no write, no execute ---
1 No read, no write, execute --x
2 No read, write, no execute -w-
3 No read, write, execute -wx
4 Read, no write, no execute r--
5 Read, no write, execute r-x
6 Read, write, no execute rw-
7 Read, write, execute rwx
read
and write
应该是不言自明的。execute
意味着你可以运行一个文件./ls
(这不是安全措施,可以顺便绕过)。请注意,目录是alsoUnix 系统(例如 Linux)上的文件。目录必须具有execute
如果您希望能够设置位cd
进去。
您最常使用的号码是:
- 7、完全访问
- 6、除了执行之外的完全访问
- 4、只读。
所以,如果你看看你的命令os.chmod(path, 0444)
我们看到您已设置只读访问权限all用户。这不是你想要的。
正确的权限取决于user
and group
拥有该文件。如果文件有not属于您想要禁止访问的用户,并且是not在文件所属的组中,您可以使用:
os.chmod(path, 0440)
如果我们看一下上表,我们会发现这意味着:
- 读、写、不执行
user
.
- 读、写、不执行
group
.
- 没有权限
other
.
如果文件有not属于您想要禁止访问的用户,并且is在文件所属的组中,您可以使用:
os.chmod(path, 0400)
这将使其可读user
only。请注意,这may有副作用,因为小组中的其他人现在也无法阅读它。
但是,如果该文件属于该用户,则需要更改该文件user
。这可以通过os.chown() https://docs.python.org/3/library/os.html#os.chown功能。例如。:
os.chown(path, 'martin')
os.chmod(path, 0400)
1: You can use ACLs if you want to assign more users or groups to a file, but in >95% there is no need to, and it only adds complexity that may be difficult to manage. It's often disabled by default.
2: Table lifted from the FreeBSD handbook https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/permissions.html