--linux--文件权限
=======
①文件的访问权限分为:读(r)、写(w)、执行(x)
②文件的被访问对象分为:文件拥有者、用户组、其他人
③用户组、用户、密码信息分别存储在目录/etc下的group、passwd、shadow文件中
【2. 关于/和/root目录】
①/目录为根目录,root为超级管理员的根目录
②/目录和root目录的访问权限如下所示:
①文件:权限x意味着该文件为可执行的
②目录:权限x意味着可以可以访问该目录下的内容
③备注:如果想对他人开放某个目录的话,请记住,开放该目录的x属性。因为如果不能访问该目录,那么自然也就无法进入目录下的文件了。
我们来看下面的例子
[paul@localhost /]$ ls -ld testdir/
drwxrwxrwx 2 paul root 4096 11月 18 15:48 testdir/
[paul@localhost /]$ chmod -R 754 testdir/
[paul@localhost /]$ ls -ld testdir/
drwxr-xr-- 2 paul root 4096 11月 18 15:48 testdir/
[paul@localhost /]$ su - oracle
Password:
[oracle@localhost ~]$ cd /
[oracle@localhost /]$ ls -ld testdir/
drwxr-xr-- 2 paul root 4096 11月 18 15:48 testdir/
[oracle@localhost /]$ cd testdir/
-bash: cd: testdir/: 权限不够
[paul@localhost ~]$ cd /
[paul@localhost /]$ chmod -R 755 testdir/
[paul@localhost /]$ su - oracle
Password:
[oracle@localhost ~]$ ls -ld /testdir/
drwxr-xr-x 2 paul root 4096 11月 18 15:48 /testdir/
[oracle@localhost ~]$ cd /testdir/
[oracle@localhost testdir]$
【4. 关于权限w的理解】
①要判断用户对某个文件、目录是否有权删除,应该看用户对其父目录的操作权限,而不是其本身
②权限w对于文件来说意味着修改(注意不是删除),对于目录来说则意味:
a.建立新的文件与目录
b.删除已经存在的文件与目录(不论文件是属于谁的)
c.将已存在的文件或目录进行重命名
d.移动该目录内的文件、目录的位置
例如下面的例子:
[root@localhost /]# ls -ld /
drwxr-xr-x 25 root root 4096 11月 18 15:20 /
[root@localhost /]# ls -ld testdir/ t1.txt t2.txt
-rwxrwxr-x 1 root root 0 11月 18 15:20 t1.txt
-rw-r--r-- 1 paul root 0 11月 18 15:20 t2.txt
drwxrwxrwx 2 paul root 4096 11月 18 15:48 testdir/
我们看到对于/目录,它的权限属性设置是用户组和其他人只能读和执行,而不具备写的权利。而/目录下的testdir目录具备777权限。现在我们尝试在paul用户下删除testdir目录
[paul@localhost ~]$ cd /
[paul@localhost /]$ ls -ld testdir/ t1.txt t2.txt
-rwxrwxr-x 1 root root 0 11月 18 15:20 t1.txt
-rw-r--r-- 1 paul root 0 11月 18 15:20 t2.txt
drwxrwxrwx 2 paul root 4096 11月 18 15:48 testdir/
[paul@localhost /]$ rm -rf testdir/
rm: 无法删除目录‘testdir/’: 权限不够
[paul@localhost /]$
我们看到删除失败了。为什么?这是因为我们为testdir目录设置的权限属性,是指定testdir目录下的文件的,而不是testdir目录本身。如果我们要删除testdir目录,应该参考用户对其父目录/的权限属性。而从前面中我们已经知道/目录对其它用户的权限为只读,所以当然不能删除testdir了。下面我们试一下能否删除testdir目录下的文件t3.txt
[paul@localhost testdir]$ ls -ld t3.txt
-rw-r--r-- 1 root root 0 11月 18 17:06 t3.txt
[paul@localhost testdir]$ rm -f t3.txt
我们看到这个文件是属于root的,但是我们依然可以在paul用户下删除它,原因就是因为我们对testdir这目录设置的权限属性是777,即其它用户也可以对这个目录下的文件进行写操作。
【5. 文件权限的修改】
①文件权限属性的修改,包括数字表示和字符表示两种
②数字格式的命令格式如:chmod [-R] xyz 文件,其中xyz代表了3个领域的权限
③字符格式的命令格式如:chmod [-R] <a|u|g|o><+|-|=><rwx> 文件名,
a. 其中a|u|g|o分别代表了所有用户,拥有者,用户组,其它人
b. 其中+|-|=分别代表了增加权限,去除权限,赋予权限
c. 其中rwx分别代表了read,write,execute权限
d. 在+和-状态下,只要是没有指定到的项目,其属性不会改变。=状态下,没有指定到项目一律不支持
我们来看看下面两种情况的例子:
上面的例子我们首先将目录testdir设置成为所有用户都为可读可写,不能执行的权限。接下来的chmod命令使用a+w表示为所有用户(a)增加(+)执行权限(x)。同理go-w则表示对于用户组(g)和其他用户(o),去除(-)他们的写权限(w)。
【1. 基本要点】
①文件的访问权限分为:读(r)、写(w)、执行(x)
②文件的被访问对象分为:文件拥有者、用户组、其他人
③用户组、用户、密码信息分别存储在目录/etc下的group、passwd、shadow文件中
【2. 关于/和/root目录】
①/目录为根目录,root为超级管理员的根目录
②/目录和root目录的访问权限如下所示:
[root@localhost ~]# ls -ld /
drwxr-xr-x 25 root root 4096 11月 18 15:20 /
[root@localhost ~]# ls -ld /root
drwxr-x--- 13 root root 4096 11月 18 15:20 /root
drwxr-xr-x 25 root root 4096 11月 18 15:20 /
[root@localhost ~]# ls -ld /root
drwxr-x--- 13 root root 4096 11月 18 15:20 /root
可以看到这两个目录的用户和用户组都为root所有,/目录下允许用户组和其他用户具有读和执行的权限,而root目录十分重要,所以除了用户和所在的用户组之外,其他用户一律无权查看。
【3. 关于权限x在文件和目录下的不同作用】
①文件:权限x意味着该文件为可执行的
②目录:权限x意味着可以可以访问该目录下的内容
③备注:如果想对他人开放某个目录的话,请记住,开放该目录的x属性。因为如果不能访问该目录,那么自然也就无法进入目录下的文件了。
我们来看下面的例子
[paul@localhost /]$ ls -ld testdir/
drwxrwxrwx 2 paul root 4096 11月 18 15:48 testdir/
[paul@localhost /]$ chmod -R 754 testdir/
[paul@localhost /]$ ls -ld testdir/
drwxr-xr-- 2 paul root 4096 11月 18 15:48 testdir/
[paul@localhost /]$ su - oracle
Password:
[oracle@localhost ~]$ cd /
[oracle@localhost /]$ ls -ld testdir/
drwxr-xr-- 2 paul root 4096 11月 18 15:48 testdir/
[oracle@localhost /]$ cd testdir/
-bash: cd: testdir/: 权限不够
我们看到,在将paul用户下的目录testdir访问权限设置为754之后,其它用户对该目录的访问权限只有一个读(r),当我们切换到oracle用户后访问该目录会出现权限不够的错误。此时我们再增加一个执行权限(x)给其它用户,看看结果如何:
[paul@localhost ~]$ cd /
[paul@localhost /]$ chmod -R 755 testdir/
[paul@localhost /]$ su - oracle
Password:
[oracle@localhost ~]$ ls -ld /testdir/
drwxr-xr-x 2 paul root 4096 11月 18 15:48 /testdir/
[oracle@localhost ~]$ cd /testdir/
[oracle@localhost testdir]$【4. 关于权限w的理解】
①要判断用户对某个文件、目录是否有权删除,应该看用户对其父目录的操作权限,而不是其本身
②权限w对于文件来说意味着修改(注意不是删除),对于目录来说则意味:
a.建立新的文件与目录
b.删除已经存在的文件与目录(不论文件是属于谁的)
c.将已存在的文件或目录进行重命名
d.移动该目录内的文件、目录的位置
例如下面的例子:
[root@localhost /]# ls -ld /
drwxr-xr-x 25 root root 4096 11月 18 15:20 /
[root@localhost /]# ls -ld testdir/ t1.txt t2.txt
-rwxrwxr-x 1 root root 0 11月 18 15:20 t1.txt
-rw-r--r-- 1 paul root 0 11月 18 15:20 t2.txt
drwxrwxrwx 2 paul root 4096 11月 18 15:48 testdir/我们看到对于/目录,它的权限属性设置是用户组和其他人只能读和执行,而不具备写的权利。而/目录下的testdir目录具备777权限。现在我们尝试在paul用户下删除testdir目录
[paul@localhost ~]$ cd /
[paul@localhost /]$ ls -ld testdir/ t1.txt t2.txt
-rwxrwxr-x 1 root root 0 11月 18 15:20 t1.txt
-rw-r--r-- 1 paul root 0 11月 18 15:20 t2.txt
drwxrwxrwx 2 paul root 4096 11月 18 15:48 testdir/
[paul@localhost /]$ rm -rf testdir/
rm: 无法删除目录‘testdir/’: 权限不够
[paul@localhost /]$我们看到删除失败了。为什么?这是因为我们为testdir目录设置的权限属性,是指定testdir目录下的文件的,而不是testdir目录本身。如果我们要删除testdir目录,应该参考用户对其父目录/的权限属性。而从前面中我们已经知道/目录对其它用户的权限为只读,所以当然不能删除testdir了。下面我们试一下能否删除testdir目录下的文件t3.txt
[paul@localhost testdir]$ ls -ld t3.txt
-rw-r--r-- 1 root root 0 11月 18 17:06 t3.txt
[paul@localhost testdir]$ rm -f t3.txt我们看到这个文件是属于root的,但是我们依然可以在paul用户下删除它,原因就是因为我们对testdir这目录设置的权限属性是777,即其它用户也可以对这个目录下的文件进行写操作。
【5. 文件权限的修改】
①文件权限属性的修改,包括数字表示和字符表示两种
②数字格式的命令格式如:chmod [-R] xyz 文件,其中xyz代表了3个领域的权限
③字符格式的命令格式如:chmod [-R] <a|u|g|o><+|-|=><rwx> 文件名,
a. 其中a|u|g|o分别代表了所有用户,拥有者,用户组,其它人
b. 其中+|-|=分别代表了增加权限,去除权限,赋予权限
c. 其中rwx分别代表了read,write,execute权限
d. 在+和-状态下,只要是没有指定到的项目,其属性不会改变。=状态下,没有指定到项目一律不支持
我们来看看下面两种情况的例子:
[root@localhost /]# chmod 666 testdir
[root@localhost /]# ls -ld testdir
drw-rw-rw- 2 paul root 4096 11月 20 14:51 testdir
[root@localhost /]# chmod a+x testdir
[root@localhost /]# ls -ld testdir
drwxrwxrwx 2 paul root 4096 11月 20 14:51 testdir
[root@localhost /]# chmod go-w testdir
[root@localhost /]# ls -ld testdir
drwxr-x-r-x 2 paul root 4096 11月 20 14:51 testdir
[root@localhost /]# ls -ld testdir
drw-rw-rw- 2 paul root 4096 11月 20 14:51 testdir
[root@localhost /]# chmod a+x testdir
[root@localhost /]# ls -ld testdir
drwxrwxrwx 2 paul root 4096 11月 20 14:51 testdir
[root@localhost /]# chmod go-w testdir
[root@localhost /]# ls -ld testdir
drwxr-x-r-x 2 paul root 4096 11月 20 14:51 testdir
上面的例子我们首先将目录testdir设置成为所有用户都为可读可写,不能执行的权限。接下来的chmod命令使用a+w表示为所有用户(a)增加(+)执行权限(x)。同理go-w则表示对于用户组(g)和其他用户(o),去除(-)他们的写权限(w)。

浙公网安备 33010602011771号