Linux系统权限

1 权限描述    1

1.1 权限描述    1

1.2 文件权限对应表    1

1.3 三种角色    1

1.4 文件和用户以及组之间的关系    1

2 修改权限命令chmod    1

2.1 第一种方法    1

2.2 第二种方法chmod nnn [-R] filename    1

3 修改文件所属 chown    1

4 基础权限设置案例    1

4.1 文件权限试验案例    1

4.2 目录权限实验案例    1

 

  1. 权限描述

    在Linux中权限是非常重要要的,因为Linux一切皆文件,合理的分配每个文件,每个用户的权限是保证整个系统安全运行的必备操作

  1. 权限描述

/root/dir的权限是所属用户root读写执行,所属组root读执行,其他用户读执行

/root/dir/file的权限是所属用户root读写,所属组root读,其他用户读

[root@oldboy ~]# ll -d dir/

drwxr-xr-x 2 root root 18 8月 16 15:32 dir/

[root@oldboy ~]# ll dir/file

-rw-r--r-- 1 root root 0 8月 16 15:32 dir/file    

 

 

  1. 文件权限对应表

字母

含义

对应权限

rread

读取权限

4

wwrite

写入权限

2

xexecute

执行权限

1

-(没有权限)

没有权限

0

 

  1. 三种角色

 

User(u):

属主用户(文件所有者)

Group(g):

属组用户(包含组成员)

Other(o):

匿名用户(其他人)

 

  1. 文件和用户以及组之间的关系

  2. 判断用户是否为文件的所有者,如果是,按所有者的权限进行访问
  3. 判断是否为文件的所有组成员,如果是,就按组的权限去访问
  4. 不满足上面条件的用户就是文件的其他的人,按其他人的权限去访问
  5. 修改权限命令chmod

chmod[ugoa][+-=][rwx][-R] filename #修改文件目录权限rwx

    -R:级联修改,递归修改

  1. 第一种方法

增加权限+

 

[root@oldboy dir]# ll

-rw-r--r-- 1 root root 0 8月 16 15:32 file    #增加其他人的执行权限

[root@oldboy dir]# chmod o+x /root/dir/file

[root@oldboy dir]# ll

-rw-r--r-x 1 root root 0 8月 16 15:32 file

去掉权限-

[root@oldboy dir]# ll

-rw-r--r-x 1 root root 0 8月 16 15:32 file

[root@oldboy dir]# chmod g-r /root/dir/file     #去掉所属组的读权限

[root@oldboy dir]# ll

-rw----r-x 1 root root 0 8月 16 15:32 file

 

指定权限=

[root@oldboy dir]# ll

-rw----r-x 1 root root 0 8月 16 15:32 file

[root@oldboy dir]# chmod ugo=rwx /root/dir/file         #指定所有用户拥有读写执行权限

[root@oldboy dir]# ll

-rwxrwxrwx 1 root root 0 8月 16 15:32 file

  1. 第二种方法chmod nnn [-R] filename

第一个n:U

第一个n:G

第一个n:O

读取权限

4

写入权限

2

执行权限

1

没有权限

0

 

常用几种组合

默认目录755

默认文件644

目录:755 750 700

文件:644 640 600

用数字修改权限

-rwxrwxrwx 1 root root 0 8月 16 15:32 file

[root@oldboy dir]# chmod 644 /root/dir/file     #指定拥有者权限为读写,所属组和其他人权限为读权限

[root@oldboy dir]# ll

-rw-r--r-- 1 root root 0 8月 16 15:32 file

 

  1. 修改文件所属 chown

chown[user][.|:][group][-R]filename

-rw-r--r-- 1 root root 0 8月 16 15:32 file

[root@oldboy dir]# chown oldboy.music file     #修改file文件属于oldboy用户属于music组

[root@oldboy dir]# ll

-rw-r--r-- 1 oldboy music 0 8月 16 15:32 file

 

  1. 基础权限设置案例

权限

对文件的影响

对目录的影响

读取权限(r

具有读取阅读文件内容的权限

具有浏览目录和子目录的权限

写入权限(w

具有新增修改文件内容的权限

具有增加和删除目录内文件

执行(x

具有执行文件的权限

具有访问目录内容(取决于目录中文件的权限)

 

  1. 文件权限试验案例

 

文件权限

结果

读取权限(r

文件只有r权限: 具有读取阅读文件内容权限

1.能使用查看类命令 catheadtaillessmore

2.不能移动、不能编辑,不能删除

写入权限(w

如果文件只有w权限: 具有新增、修改文件内容的权限

1.使用vim编辑,会提示权限拒绝, 但可强制保存,会覆盖之前文件内容

2.使用echo命令重定向或追加重定向技术可以往文件内写入数据

3.使用cat命令读取文件, 将读取到的文件输出交给仅有w权限文件的输入

4.不能复制、不能移动、不能删除,(删除需要看上级目录w的权限)

执行权限(x

文件只有x权限,具有执行文件的权限。

//注意: 普通用户需要有r权限,管理员不需要

1.不能执行、查看、编辑、复制、移动、删除

rw权限

可以查看和编辑文件内容

rx权限

只能查看和执行文件、不能编辑、复制、移动、删除

rx权限

允许浏览目录内文件以及子目录、并允许在目录内新建文件, 不允许创建、删除文件和目录

 

  1. 默认文件其他用户仅有读权限

-rwxrwxr-- 1 root root 0 8月 16 15:32 file

[oldboy@oldboy dir]$ cat file

date

[oldboy@oldboy dir]$ echo hello > file

-bash: file: 权限不够

只能查看不能修改

2、//测试读权限(无法执行或删除)

[root@oldboy ~]# su - oldboy

[oldboy@oldboy ~]$ cat /tmp/date.txt

date

[oldboy@oldboy ~]$ echo "test" >/tmp/date.txt

-bash: /tmp/date.txt: Permission denied

[oldboy@oldboy ~]$ /tmp/date.txt

-bash: /tmp/date.txt: Permission denied

3、增加执行权限测试执行权限

[root@oldboy ~]# chmod o+x /tmp/date.txt

[root@oldboy ~]# ll /tmp/date.txt

-rw-r--r-x. 1 root root 5 Aug 16 06:37 /tmp/date.txt

[oldboy@oldboy ~]$ /tmp/date.txt

Thu Aug 16 06:40:56 CST 2018

4、增加w写权限测试写权限

[root@oldboy ~]# chmod o+w /tmp/date.txt

[root@oldboy ~]# ll /tmp/date.txt

-rw-r--rwx 1 root root 5 Aug 16 06:38 /tmp/date.txt

[oldboy@oldboy ~]$ echo "test" >/tmp/date.txt

 

  1. 目录权限实验案例

目录权限

执行结果

只有r权限

1.能使用ls命令浏览目录及子目录, 同时会提示权限拒绝

2.能使用ls -l命令浏览目录及子目录, 会带问号,同时只能看到文件名

总结: 目录只有r权限,仅仅只能浏览内的文件名,无其他操作权限

写入权限(w

如果目录只有w权限:具有增加、删除或修改目录内文件名权限(需要x配合)

//注意:如果目录有w权限, 可以在目录创建文件, 可以删除目录中的文件(跟文件权限无关)

不能进入目录、不能复制目录、不能删除目录、不能移动目录

执行权限(x

目录只有x权限

1.只能进入目录

2.不能浏览、复制、移动、删除

  

 

1:、对目录没有 w,对文件有 rwx

[root@oldboy ~]# mkdir /test

[root@oldboy ~]# echo "test" > /test/test.txt

[root@oldboy ~]# chmod 777 /test/test.txt

[root@oldboy ~]# ll -d /test

drwxr-xr-x. 2 root root 22 Aug 16 06:52 /test

[root@oldboy ~]# ll /test/test.txt

-rwxrwxrwx. 1 root root 5 Aug 16 06:52 /test/test.txt

 

普通用户验证权限

[oldboy@oldboy ~]$ cat /test/test.txt

test

[oldboy@oldboy ~]$ rm -f /test/test.txt

2:、对目录有 w,对文件没有任何权限

[root@oldboy ~]# chmod 777 /test/

[root@oldboy ~]# chmod 000 /test/test.txt

[root@oldboy ~]# ll -d /test

drwxrwxrwx. 2 root root 22 Aug 16 06:52 /test

[root@oldboy ~]# ll -d /test/test.txt

----------. 1 root root 5 Aug 16 06:52 /test/test.txt

 

/普通用户验证权限

[oldboy@oldboy ~]$ cat /test/test.txt

cat: /test/test.txt: Permission denied

[oldboy@oldboy ~]$ rm -f /test/test.txt

[oldboy@oldboy ~]$ touch /test/test1.txt

 

3:、对目录没有 x,对文件有任何权限

[root@oldboy ~]# chmod 766 /test/

[root@oldboy ~]# chmod 777 /test/test.txt

[root@oldboy ~]# ll -d /test/

drwxrw-rw-. 2 root root 22 Aug 16 06:58 /test/

[root@oldboy ~]# ll /test/test.txt

-rwxrwxrwx. 1 root root 5 Aug 16 06:58 /test/test.txt

普通用户验证权限

[oldboy@oldboy ~]$ cd /test

-bash: cd: /test: Permission denied

[oldboy@oldboy ~]$ cat /test/test.txt

cat: /test/test.txt: Permission denied

[oldboy@oldboy ~]$ rm -f /test/test.txt

rm: cannot remove '/test/test.txt': Permission denied

 

原文地址:https://www.cnblogs.com/majinhai/p/9534078.html