Linux的权限
使用者
Linux中,使用文件的对象一般为三类,[u],[g],[o]。分别为user,group和others。网上的解释很多,但是这里我声明一下~最好理解的方式还是英文原意最合适:
-
u - user who owns the file
-
g - the group to which the user belongs
-
o - others
-
a - (u+g+o) anyone
权限
观察文件的权限可以使用命令: ls -al
这里的第二列使我们关心的,即文件权限。
第一个字母代表文件类型:
- 当为[ d ]则是目录
- 当为[ - ]则是文件
- 若是[ l ]则表示为链接文件
- 若是[ b ]则表示可随机存取设备
- 若是[ c ]则表示一次性读取设备
之后每三个字母一组表示权限,从前往后分别为u、g、o的权限。
- r = 4
- w = 2
- x = 1
- - = 0
这个在chmod命令使用的时候会用到。
对于文件,含义是:
- r (read) : 可读
- w (write) : 可写
- x (excute) : 可以执行
对于目录而言,含义略有不同:
-
r : 可以读目录以及其中的文件
-
w : 对于目录而言,写的含义略微广一点,包含下面几条
创建新的文件与目录;
删已经存在的文件与目录(不论该文件的权限为何!)
将已存在的文件或目录进行更名;
搬移该目录内的文件、目录位置。 -
x : 因为对于一个目录而言,我们不存在什么是否可以执行的问题,这里代表该目录能否“打开”
即用
cd
命令能否打开
权限修改
关于权限的修改问题,我们讨论下面三个命令:
- chown
- chgrp
- chmod
其中chmod用的是最多,下面一一介绍。
chgrp
同样,chgrp用于修改group。
格式为
chgrp [-r] new_group_name dir/file_name
其中-r
开关表示是否对子目录和子文件内容进行修改。
chown
chown顾名思义,是我们修改user的命令。
格式为
chown [-r] new_user_name1[:new_user_name2:...] dir/file_name
其中-r
开关表示是否对子目录和子文件内容进行修改。
与上面不同的是一个文件可以有多个owner,因此我们可以用:
进行分割。
chmod
chmod对权限的具体内容进行了修改。
格式为
chmod [-r] xyz dir/file_name
xyz都是0~7的数字,就是将rwx-的字符根据之前给出的权值对应,给出我们希望的权限目标。
有一些简单的方式用于微调:
命令 | 对象 | 操作 | 操作内容 | 文件 |
---|---|---|---|---|
chomod | u/g/o/a | +/-/= | rwx- | w文件名 |
直接给出几个具体的例子:
chmod u=rwx,go=rx .bashrc
chmod a+w .bashrc
一些常用的权限:
Setting | Numerical | Meaning |
---|---|---|
-rw------- | (600) | Only the owner has read and write permissions. |
-rw-r--r-- | (644) | Only the owner has read and write permissions; the group and others have read only. |
-rwx------ | (700) | Only the owner has read, write, and execute permissions. |
-rwxr-xr-x | (755) | The owner has read, write, and execute permissions; the group and others have only read and execute. |
-rwx--x--x | (711) | The owner has read, write, and execute permissions; the group and others have only execute. |
-rw-rw-rw- | (666) | Everyone can read and write to the file. (Becareful with these permissions.) |
-rwxrwxrwx | (777) | Everyone can read, write, and execute. (Again, this permissions setting can be hazardous.) |