Linux 权限管理(12个权限位)

一、权限的基本概述

我们可以把权限理解为操作系统对用户能够在系统中执行命令的功能限制。它主要用于约束用户对系统的操作,内容的访问等。

在linux服务器中有严格的权限等级,如果权限过高导致误操作会增加服务器的风险。所以linux系统中的各种权限及要给用户、服务等分配合理的权限十分重要。

 

二、9个普通权限位

<一>权限位介绍

1.权限位分类

[root@whb ~]# ll
total 59668
-rw-r--r-- 1 root root       86 Dec  9 21:54 123.txt
-rw-r--r-- 1 root root       86 Dec 10 18:54 456.txt

通过ll可以看到,除了第一个“-”指的是文件类型,后面的9位就是9位普通权限位,分别介绍下他们的意思

前三位:          属主的权限          owner

中间三位:         属组的权限          group

后三位:          其他人的权限       other

2.权限位上的权限

含义:

r            可读权限(readable)
w           可写权限(writable)
x            可执行权限(executable)
         -             无任何权限,只代表权限站位符

对应的数字权限:

r                4
w              2
x               1
-               0
3.当用户在操作文件时,系统中权限的判断过程
  • 1.判断该用户是否是此文件的属主,如果是,就按照属主的权限操作
  • 2.判断该用户所属组是否是此文件的属组,如果是,就按照属组的权限操作
  • 3.该用户对于此文件来说,就是陌生人,就使用其他的权限操作
案例:
[root@whb ~]# ll
-rwxr-xr--. 1 root test 13 Dec 15 00:54 pass6.txt
1.root用户对此文件的可以操作什么
          root是这个的属主 可读 可写 可执行
2.dev用户对此文件可以操作什么,已知,dev属于test组
          dev用户属于test组,该文件的属组是test dev用户拥有此文件的属组的权限 可读 可执行
3.oldboy用户对此文件有什么操作权限
          oldboy用户对于此文件来说,是陌生人,拥有陌生人的权限 可读

<二>权限设置

1.chmod设置权限
选项 含义
 -R       递归设置权限
  u       属主的权限
  g        属组的权限                                                               
  o       其它的人的权限
  a       所有人的权限
 +       添加权限
  -       移除权限
 =       覆盖权限
 r       读的权限
 w       写的权限
 x       执行的权限
 -       权限占位符,无权限
实例:
根据字母设置权限
[root@whb ~]# touch  test.txt
[root@whb ~]# ll
total 0
-rw-r--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod  u+x  test.txt             #赋予属主x权限
[root@whb ~]# ll
total 0
-rwxr--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod  g+w  test.txt             #赋予属组w权限
[root@whb ~]# ll
total 0
-rwxrw-r--. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod  o+x  test.txt             #赋予其他人x权限
[root@whb ~]# ll
total 0
-rwxrw-r-x. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod  o-x  test.txt             #取消其他人x权限     
[root@whb ~]# ll
total 0
-rwxrw-r--. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod  a+x  test.txt             #给所有人添加x权限 
[root@whb ~]# ll
total 0
-rwxrwxr-x. 1 root root 0 Dec 17 17:21 test.txt   
[root@whb ~]# chmod  -x  test.txt              #给所有人减去x权限 
[root@whb ~]# ll 
total 0
-rw-rw-r--. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod   a=r  test.txt            #给所有人权限设置成r--(只读权限) 
[root@whb ~]# ll
total 0
-r--r--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod u=rwx  test.txt            #给属主权限设置成rwx(读写执行) 
[root@whb ~]# ll
total 0
-rwxr--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod  g=x  test.txt             #给属组权限设置成--x(只有执行权限)
[root@whb ~]# ll
total 0
-rwx--xr--. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod  o=-  test.txt             #给其他人权限设置成---(无任何权限)
[root@whb ~]# ll
total 0
-rwx--x---. 1 root root 0 Dec 17 17:21 test.txt
根据数字设置权限
[root@whb ~]# ll
total 0
-rwx--x---. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod   644 test.txt 
[root@whb ~]# ll
total 0
-rw-r--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod  000  test.txt 
[root@whb ~]# ll
total 0
----------. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod  777  test.txt 
[root@whb ~]# ll
total 0
-rwxrwxrwx. 1 root root 0 Dec 17 17:21 test.txt
[root@whb ~]# chmod 700  test.txt 
[root@whb ~]# ll
total 0
-rwx------. 1 root root 0 Dec 17 17:21 test.txt


#递归设置权限 
[root@whb ~]# mkdir  test
[root@whb ~]# 
[root@whb ~]# touch  test/{1..5}.txt
[root@whb ~]# 
[root@whb ~]# 
[root@whb ~]# ll test
total 0
-rw-r--r--. 1 root root 0 Dec 17 17:42 1.txt
-rw-r--r--. 1 root root 0 Dec 17 17:42 2.txt
-rw-r--r--. 1 root root 0 Dec 17 17:42 3.txt
-rw-r--r--. 1 root root 0 Dec 17 17:42 4.txt
-rw-r--r--. 1 root root 0 Dec 17 17:42 5.txt
[root@whb ~]# ll -d test
drwxr-xr-x. 2 root root 71 Dec 17 17:42 test
[root@whb ~]# chmod -R 700 test
[root@whb ~]# ll -d test
drwx------. 2 root root 71 Dec 17 17:42 test
[root@whb ~]# ll test
total 0
-rwx------. 1 root root 0 Dec 17 17:42 1.txt
-rwx------. 1 root root 0 Dec 17 17:42 2.txt
-rwx------. 1 root root 0 Dec 17 17:42 3.txt
-rwx------. 1 root root 0 Dec 17 17:42 4.txt
-rwx------. 1 root root 0 Dec 17 17:42 5.txt
[root@whb ~]# touch  test/test.log
[root@whb ~]# ll test
total 0
-rwx------. 1 root root 0 Dec 17 17:42 1.txt
-rwx------. 1 root root 0 Dec 17 17:42 2.txt
-rwx------. 1 root root 0 Dec 17 17:42 3.txt
-rwx------. 1 root root 0 Dec 17 17:42 4.txt
-rwx------. 1 root root 0 Dec 17 17:42 5.txt
-rw-r--r--. 1 root root 0 Dec 17 17:43 test.log

三、权限的作用

<一>权限对文件的影响

选项 含义
  r       可读,是否能够读取文件内容
  w       可写,是否可以编辑、修改文件内容
  x        可执行,是否可以执行这个文件                                                               
1.文件只有r权限时,只能够查看文件内容和拷贝文件,其它操作是不被允许的,删除、移动要看上级目录的权限
[test@whb /opt]$ ll
total 4
-rw-r--r--. 1 root root 9 Dec 17 18:07 test.txt
[test@whb /opt]$ cat test.txt 
hostname
[test@whb /opt]$ head  test.txt 
hostname
[test@whb /opt]$ cp  test.txt  /tmp/
[test@whb /opt]$ vim  test.txt 
[test@whb /opt]$ echo  123 >> test.txt
-bash: test.txt: Permission denied
[test@whb /opt]$ ./test.txt
-bash: ./test.txt: Permission denied

2.文件只要w权限时,不能查看文件内容;文件不能执行,不能复制;vim编辑文件时会提示权限不足,强制保存退出会覆盖文件原来的内容,但可以通过追加和重定向向文件内写入内容

[test@whb /opt]$ cat test.txt 
cat: test.txt: Permission denied
[test@whb /opt]$ ./test.txt
-bash: ./test.txt: Permission denied
[test@whb /opt]$ vim  test.txt 
[test@whb /opt]$ echo  hostname >> test.txt
[test@whb /opt]$ echo  hostname > test.txt
[test@whb /opt]$ cp  test.txt   /mnt/
cp: cannot open ‘test.txt’ for reading: Permission denied

[root@whb ~]# cat /opt/test.txt 
pwd
[root@whb ~]# cat /opt/test.txt 
pwd
hostname
[root@whb ~]# cat /opt/test.txt 
hostname
3.只有x权限时,没有任何作用,无法对文件进行任何操作。

[test@whb /opt]$ ll
total 4
-rw-r----x. 1 root root 9 Dec 17 18:13 test.txt
[test@whb /opt]$ cat test.txt 
cat: test.txt: Permission denied
[test@whb /opt]$ vim  test.txt
[test@whb /opt]$ cp  test.txt   /tmp/
cp: cannot open ‘test.txt’ for reading: Permission denied
[test@whb /opt]$ echo  123 >> test.txt 
-bash: test.txt: Permission denied
[test@whb /opt]$ echo  123 > test.txt 
-bash:  test.txt: Permission denied
[test@whb /opt]$ ./test.txt 
bash: ./test.txt: Permission denied
4.有wx权限时,跟仅有w权限用处相同

[test@whb /opt]$ ll
total 4
-rw-r---wx. 1 root root 9 Dec 17 18:13 test.txt
[test@whb /opt]$ ./test.txt 
bash: ./test.txt: Permission denied
[test@whb /opt]$ echo  pwd >> test.txt 
[test@whb /opt]$ vim test.txt
[test@whb /opt]$ cat test.txt
cat: test.txt: Permission denied
[test@whb /opt]$ cp  test.txt  /tmp/
cp: cannot open ‘test.txt’ for reading: Permission denied
5.有rw权限时,可以正常读取、编辑此文件,但文件不能执行

[test@whb /opt]$ ll
total 4
-rw-r--rw-. 1 root root 9 Dec 17 18:21 test.txt
[test@whb /opt]$ cat test.txt 
hostname
[test@whb /opt]$ cp  test.txt  /tmp/
[test@whb /opt]$ ./test.txt
-bash: ./test.txt: Permission denied
[test@whb /opt]$ echo  pwd >> test.txt 
[test@whb /opt]$ vim test.txt
总结:
       r权限可以正常读取文件内容,可以执行查看文件的命令,不能修改或者执行文件,可以拷贝
       只有w权限,不能读取文件,可以追加或者重定向,使用vim命令时,会提示权限不足,强制修改保存时,会覆盖源文件的内容,不可以执行该文件,w权限需要r权限的配合。
       只有x权限时,什么操作都不可以,跟w权限配合时,等同于只有w权限的作用,x权限需要跟r权限配合使用。可以正常读取文件,执行文件,但是不能修改文件。

<二>权限对目录的影响

选项 含义
  r       表示可以显示目录下的文件列表和属性信息
  w       表示可以在目录下,创建、删除、修改、移动文件
  x        可以进入此目录  
1.目录只有r权限时,可以显示目录的文件列表,但是会提示权限不足,查看目录下的文件属性信息,会提示权限不足,属性信息被?所显示,无法进入目录,无法创建新文件,删除文件,移动文件
[test@whb /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test  test   9 Dec 17 18:41 passwd
drwxr-xr--. 2 root  root  86 Dec 17 18:50 test
-rw-r---w-. 1 root  root   9 Dec 17 18:33 test.txt
[test@whb /opt]$ ls  test
ls: cannot access test/old1.txt: Permission denied
ls: cannot access test/old2.txt: Permission denied
ls: cannot access test/old3.txt: Permission denied
ls: cannot access test/old4.txt: Permission denied
ls: cannot access test/old5.txt: Permission denied
old1.txt  old2.txt  old3.txt  old4.txt  old5.txt
[test@whb /opt]$ ls -l test
ls: cannot access test/old1.txt: Permission denied
ls: cannot access test/old2.txt: Permission denied
ls: cannot access test/old3.txt: Permission denied
ls: cannot access test/old4.txt: Permission denied
ls: cannot access test/old5.txt: Permission denied
total 0
-????????? ? ? ? ?            ? old1.txt
-????????? ? ? ? ?            ? old2.txt
-????????? ? ? ? ?            ? old3.txt
-????????? ? ? ? ?            ? old4.txt
-????????? ? ? ? ?            ? old5.txt
[test@whb /opt]$ cd  test
-bash: cd: test: Permission denied
[test@whb /opt]$ rm -f  test/old1.txt
rm: cannot remove ‘test/old1.txt’: Permission denied
[test@whb /opt]$ touch  test/new1.txt
touch: cannot touch ‘test/new1.txt’: Permission denied
[test@whb /opt]$ mv  test/old1.txt   /tmp/
mv: cannot stat ‘test/old1.txt’: Permission denied
2.只有w权限时,任何操作都做不了。因为无法进入目录,所以无法进行任何操作。
[test@whb /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test  test   9 Dec 17 18:41 passwd
drwxr-x-w-. 2 root  root  86 Dec 17 18:50 test
-rw-r---w-. 1 root  root   9 Dec 17 18:33 test.txt
[test@whb /opt]$ ls  test
ls: cannot open directory test: Permission denied
[test@whb /opt]$ cd test
-bash: cd: test: Permission denied
[test@whb /opt]$ touch  test/new1.txt
touch: cannot touch ‘test/new1.txt’: Permission denied
[test@whb /opt]$ rm -f  test/old1.txt
rm: cannot remove ‘test/old1.txt’: Permission denied
[test@whb /opt]$ mv test/old1.txt  /tmp/
mv: cannot stat ‘test/old1.txt’: Permission denied
3.只有x权限时,只能进入目录当中,其它的操作都不能操作
[test@whb /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test  test   9 Dec 17 18:41 passwd
drwxr-x--x. 2 root  root  86 Dec 17 18:50 test
-rw-r---w-. 1 root  root   9 Dec 17 18:33 test.txt
[test@whb /opt]$ ls  test
ls: cannot open directory test: Permission denied
[test@whb /opt]$ touch  test/new1.txt
touch: cannot touch ‘test/new1.txt’: Permission denied
[test@whb /opt]$ rm -f test/old1.txt
rm: cannot remove ‘test/old1.txt’: Permission denied
[test@whb /opt]$ mv test/old1.txt /tmp/
mv: cannot move ‘test/old1.txt’ to ‘/tmp/old1.txt’: Permission denied
[test@whb /opt]$ cd test
[test@whb /opt/test]$ ls
ls: cannot open directory .: Permission denied
4.有rw权限时,跟拥有单独的r权限是一个作用。因为无法进入目录,对目录下的文件无法操作。
[test@whb /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test  test   9 Dec 17 18:41 passwd
drwxr-xrw-. 2 root  root  86 Dec 17 18:50 test
-rw-r---w-. 1 root  root   9 Dec 17 18:33 test.txt
[test@whb /opt]$ ls  test
ls: cannot access test/old1.txt: Permission denied
ls: cannot access test/old2.txt: Permission denied
ls: cannot access test/old3.txt: Permission denied
ls: cannot access test/old4.txt: Permission denied
ls: cannot access test/old5.txt: Permission denied
old1.txt  old2.txt  old3.txt  old4.txt  old5.txt
[test@whb /opt]$ ll test
ls: cannot access test/old1.txt: Permission denied
ls: cannot access test/old2.txt: Permission denied
ls: cannot access test/old3.txt: Permission denied
ls: cannot access test/old4.txt: Permission denied
ls: cannot access test/old5.txt: Permission denied
total 0
-????????? ? ? ? ?            ? old1.txt
-????????? ? ? ? ?            ? old2.txt
-????????? ? ? ? ?            ? old3.txt
-????????? ? ? ? ?            ? old4.txt
-????????? ? ? ? ?            ? old5.txt
[test@whb /opt]$ touch  test/new.txt
touch: cannot touch ‘test/new.txt’: Permission denied
[test@whb /opt]$ rm -f test/old1.txt 
rm: cannot remove ‘test/old1.txt’: Permission denied
[test@whb /opt]$ mv test/old1.txt /tmp/
mv: cannot stat ‘test/old1.txt’: Permission denied
5.有rx权限时,可以正常的显示目录下的列表信息,属性信息,可以进入目录,不可以删除,创建,移动目录下的内容
[test@whb /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test  test   9 Dec 17 18:41 passwd
drwxr-xr-x. 2 root  root  86 Dec 17 18:50 test
-rw-r---w-. 1 root  root   9 Dec 17 18:33 test.txt
[test@whb /opt]$ ls test
old1.txt  old2.txt  old3.txt  old4.txt  old5.txt
[test@whb /opt]$ ll test
total 0
-rw-r--r--. 1 root root 0 Dec 17 18:50 old1.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old2.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old3.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old4.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old5.txt
[test@whb /opt]$ touch  test/new.txt
touch: cannot touch ‘test/new.txt’: Permission denied
[test@whb /opt]$ cd test
[test@whb /opt/test]$ ls
old1.txt  old2.txt  old3.txt  old4.txt  old5.txt
[test@whb /opt/test]$ ll
total 0
-rw-r--r--. 1 root root 0 Dec 17 18:50 old1.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old2.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old3.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old4.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old5.txt
6.有wx权限时,可以进入目录,可以创建、删除、移动文件,但是查看不了文件列表,移动到其他目录时,是需要看目标目录的权限,如果没有wx,是移动不进去的。
[test@whb /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test  test   9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root  root  86 Dec 17 18:50 test
-rw-r---w-. 1 root  root   9 Dec 17 18:33 test.txt
[test@whb /opt]$ ls  test
ls: cannot open directory test: Permission denied
[test@whb /opt]$ ll test
ls: cannot open directory test: Permission denied
[test@whb /opt]$ touch  test/new1.txt
[test@whb /opt]$ ls test
ls: cannot open directory test: Permission denied
[test@whb /opt]$ rm -f  test/old1.txt
[test@whb /opt]$ mv  test/old2.txt  /root
mv: cannot stat ‘/root/old2.txt’: Permission denied
[test@whb /opt]$ mv  test/old2.txt  /tmp
[test@whb /opt]$ rm -f test/*
总结:
       只有r权限时,只能查看目录下的文件列表,但是会提示权限不足,无法查看文件的属性信息,需要x权限的配合才能正常的显示文件列表信息。
       只有w权限的时候,什么都操作不了。rw权限跟单独的r权限作用是一样的,w权限需要x权限的配合,可以正常创建文件,删除,移动文件,移动文件需要看目标目录的权限,但是不能显示目录下的文件列表信息。
       只有x权限时,只能进入目录,但是什么操作不了。

四、属主属组设置

<一> 修改属组、属组
chown      修改文件或目录的属主、属组
选项:
                       -R           #递归设置文件目录属性
charpa      只可以修改属组
       只有root用户可以任意修改属主和属组,普通用户只能把属主、属组修改成自己,并且普通用户无法修改其他用户的文件。

[root@whb ~]# cd  /opt/
[root@whb /opt]# ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test  test   9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root  root  70 Dec 17 19:04 test
-rw-r---w-. 1 root  root   9 Dec 17 18:33 test.txt
[root@whb /opt]# id test
uid=1000(test) gid=1000(test) groups=1000(test)
[root@whb /opt]# chown  test.test  123
[root@whb /opt]# ll
total 12
-rw-rw--w-. 1 test test 10 Dec 17 18:36 123
-rw-r---w-. 1 test test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root root 70 Dec 17 19:04 test
-rw-r---w-. 1 root root  9 Dec 17 18:33 test.txt
[root@whb /opt]# chown  yuwei  passwd 
[root@whb /opt]# chown  yuwei  passwd 
[root@whb /opt]# ll
total 12
-rw-rw--w-. 1 test  test 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root  root 70 Dec 17 19:04 test
-rw-r---w-. 1 root  root  9 Dec 17 18:33 test.txt
[root@whb /opt]# chown  .yuwei  123
[root@whb /opt]# ll
total 12
-rw-rw--w-. 1 test  yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test   9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root  root  70 Dec 17 19:04 test
-rw-r---w-. 1 root  root   9 Dec 17 18:33 test.txt
[root@whb /opt]# chown  root:root 123
[root@whb /opt]# ll
total 12
-rw-rw--w-. 1 root  root 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root  root 70 Dec 17 19:04 test
-rw-r---w-. 1 root  root  9 Dec 17 18:33 test.txt
[root@whb /opt]# chgrp   test 123     #使用chgrp修改属组
[root@whb /opt]# ll
total 12
-rw-rw--w-. 1 root  test 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root  root 70 Dec 17 19:04 test
-rw-r---w-. 1 root  root  9 Dec 17 18:33 test.txt
递归设置属主、属组

[root@whb /opt]# chown  -R  test.test  test			#递归设置
[root@whb /opt]# ll
total 12
-rw-rw--w-. 1 root  test 10 Dec 17 18:36 123
-rw-r---w-. 1 yuwei test  9 Dec 17 18:41 passwd
drwxr-x-wx. 2 test  test 70 Dec 17 19:04 test
-rw-r---w-. 1 root  root  9 Dec 17 18:33 test.txt
[root@whb /opt]# ll test
total 0
-rw-rw-r--. 1 test test 0 Dec 17 19:03 new1.txt
-rw-r--r--. 1 test test 0 Dec 17 18:50 old3.txt
-rw-r--r--. 1 test test 0 Dec 17 18:50 old4.txt
-rw-r--r--. 1 test test 0 Dec 17 18:50 old5.txt
<二> umask权限控制
umask的计算
       umask是个权限控制的命令,默认的值是022(普通用户的umask值默认为002),系统默认创建目录的权限是怎么计算来的,由目录的最大权限777,减去umask的默认权限,得到的结果就是新创建目录的权限。系统默认创建文件的权限是怎么计算的,由文件的最大权限666,减去umask的默认权限,得到的结果就是新创建文件的权限。当文件的权限为出现奇数时,会在奇数权限位上面+1。
例如:umask值为002,所对应的文件和目录创建缺省权限分别为664和775。
[root@whb ~]# umask 
0022
[root@whb ~]# umask   033
[root@whb ~]# umask 
0033
[root@whb ~]# mkdir  data
[root@whb ~]# ll
total 0
drwxr--r--. 2 root root 6 Dec 18 17:06 data
drwxr-xr-x. 2 root root 6 Dec 18 16:58 test
-rw-r--r--. 1 root root 0 Dec 18 16:58 test.log
[root@whb ~]# touch  test.txt
[root@whb ~]# ll
total 0
drwxr--r--. 2 root root 6 Dec 18 17:06 data
drwxr-xr-x. 2 root root 6 Dec 18 16:58 test
-rw-r--r--. 1 root root 0 Dec 18 16:58 test.log
-rw-r--r--. 1 root root 0 Dec 18 17:06 test.txt

五、3个特殊权限

在 Linux 系统中,文件和目录其实有12个权限位,常见的只有9普通权限位,还有3个特殊的权限
1. setuid
当运行一个命令(二进制文件)的时候,相当于是这个命令的所有者在执行此命令。setuid主要作用在属主的x权限位。
当命令的属主有x位权限时,设置setuid后,x权限位显示的是 s
当命令的属主有x位权限时,设置setuid后,x权限位显示的是 S
对应的数字权限是  4
#此权限需要慎用
[root@whb ~]# ll /usr/bin/passwd      #passwd默认设置了setuid
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd
[root@whb ~]# stat  /usr/bin/passwd    
  File: ‘/usr/bin/passwd’
  Size: 27832     	Blocks: 56         IO Block: 4096   regular file
Device: 803h/2051d	Inode: 100960630   Links: 1
Access: (4755/-rwsr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:passwd_exec_t:s0
Access: 2019-12-16 18:28:46.025128480 +0800
Modify: 2014-06-10 14:27:56.000000000 +0800
Change: 2019-11-26 20:24:06.355108144 +0800
 Birth: -
 
 #root用户设置setuid 权限
[root@whb ~]# ll  /usr/bin/rm
-rwxr-xr-x. 1 root root 62952 Oct 31  2018 /usr/bin/rm
[root@whb ~]# chmod  u+s  /usr/bin/rm
[root@whb ~]# ll  /usr/bin/rm
-rwsr-xr-x. 1 root root 62952 Oct 31  2018 /usr/bin/rm
[root@whb ~]# stat /usr/bin/rm
  File: ‘/usr/bin/rm’
  Size: 62952     	Blocks: 128        IO Block: 4096   regular file
Device: 803h/2051d	Inode: 100686169   Links: 1
Access: (4755/-rwsr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:bin_t:s0
Access: 2019-12-18 03:41:02.098907636 +0800
Modify: 2018-10-31 03:16:01.000000000 +0800
Change: 2019-12-18 17:43:52.450121986 +0800

#普通用户测试
[test@whb ~]$ rm -rf /opt/
rm: cannot remove ‘/opt/’: Permission denied
[test@whb ~]$ rm -rf /opt/
[test@whb ~]$ ll /opt/
ls: cannot access /opt/: No such file or directory

#如果普通对这个命令文件没有执行权限时,也会报错
[root@whb ~]# mkdir  /opt
[root@whb ~]# 
[root@whb ~]# 
[root@whb ~]# chmod o-x  /usr/bin/rm
[root@whb ~]# ll  /usr/bin/rm
-rwsr-xr--. 1 root root 62952 Oct 31  2018 /usr/bin/rm

#普通用户测试
[test@whb ~]$ rm -rf /opt/
rm: cannot remove ‘/opt/’: Permission denied

#当一个文件属主没有执行权限时,设置setuid之后就是大S,有执行权限就是小s
[root@whb ~]# ll
total 0
drwxr--r--. 2 root root 6 Dec 18 17:06 data
drwxr-xr-x. 2 root root 6 Dec 18 16:58 test
-rw-r--r--. 1 root root 0 Dec 18 16:58 test.log
-rw-r--r--. 1 root root 0 Dec 18 17:06 test.txt
[root@whb ~]# chmod u+s  test.log 
[root@whb ~]# ll
total 0
drwxr--r--. 2 root root 6 Dec 18 17:06 data
drwxr-xr-x. 2 root root 6 Dec 18 16:58 test
-rwSr--r--. 1 root root 0 Dec 18 16:58 test.log
-rw-r--r--. 1 root root 0 Dec 18 17:06 test.txt

#普通用户对文件没有执行权限时,是执行不了,即使拥有setuid的权限,也不能执行,
[test@whb ~]$ ll 
total 4
-rwSrw-r--. 1 test test 4 Dec 18 17:53 123.txt
[test@whb ~]$ ./123.txt
-bash: ./123.txt: Permission denied
[test@whb ~]$ ll
total 4
-rwxrw-r--. 1 test test 4 Dec 18 17:53 123.txt
[test@whb ~]$ ./123.txt
/home/test

#root用户对文件没有执行权限时,是执行不了,即使拥有setuid的权限,root也是不能执行的
[root@whb ~]# chmod -x  /usr/bin/rm
[root@whb ~]# ll  /usr/bin/rm
-rwSr--r--. 1 root root 62952 Oct 31  2018 /usr/bin/rm
[root@whb ~]# mkdir /opt
[root@whb ~]# rm -rf /opt
-bash: /bin/rm: Permission denied
2. setgid
让一个目录下的所有新创建的目录或者文件的默认组都是该目录的组,而不是创建用户的组
让一个目录成为共享目录
主要作用于属组的x权限位
数字权限位 2
实例:创建2个用户,创建一个目录,让其属主及属组有任何权限,其它人没有权限

#创建两个测试用户
[root@whb ~]# useradd   test01
[root@whb ~]# useradd   test02
[root@whb ~]# echo '1'  |passwd  --stdin  test01
Changing password for user test01.
passwd: all authentication tokens updated successfully.
[root@whb ~]# echo '1'  |passwd  --stdin  test02
Changing password for user test02.
passwd: all authentication tokens updated successfully.

#创建一个共享目录
[root@whb ~]# mkdir   /opt/gongxiang

#创建一个共享组
[root@whb ~]# groupadd  gongxiang

#将共享,目录设置为共享组
[root@whb ~]# chgrp   gongxiang  /opt/gongxiang/
[root@whb ~]# ll /opt/
total 0
drwxr-xr-x. 2 root gongxiang 6 Dec 18 18:17 gongxiang

#让属主属组都要任何权限,其它用户没有权限
[root@whb ~]# chmod 770  /opt/gongxiang/

#将两个用户加入共享组
[root@whb ~]# usermod  -G gongxiang  test01
[root@whb ~]# usermod  -G gongxiang  test02

#给共享目录设置setgid
[root@whb ~]# chmod  g+s /opt/gongxiang/
[test01@whb /opt]$ stat gongxiang/
  File: ‘gongxiang/’
  Size: 86        	Blocks: 0          IO Block: 4096   directory
Device: 803h/2051d	Inode: 67753409    Links: 4
Access: (2770/drwxrws---)  Uid: (    0/    root)   Gid: ( 2026/gongxiang)
Context: unconfined_u:object_r:usr_t:s0
Access: 2019-12-18 18:48:13.316985658 +0800
Modify: 2019-12-18 18:48:11.411985725 +0800
Change: 2019-12-18 18:48:11.411985725 +0800
 Birth: -


#test01测试
[test01@whb /opt/gongxiang]$ touch  test01.txt
[test01@whb /opt/gongxiang]$ ll
total 0
-rw-rw-r--. 1 test01 gongxiang 0 Dec 18 18:43 test01.txt
[test01@whb /opt/gongxiang]$ mkdir  test01
[test01@whb /opt/gongxiang]$ ll
total 0
drwxrwsr-x. 2 test01 gongxiang 6 Dec 18 18:43 test01
-rw-rw-r--. 1 test01 gongxiang 0 Dec 18 18:43 test01.txt
[test01@whb /opt/gongxiang]$ ll  test02
total 0
-rw-rw-r--. 1 test02 gongxiang 0 Dec 18 18:45 test02.log
[test01@whb /opt/gongxiang]$ rm -f test02/test02.log 
[test01@whb /opt/gongxiang]$ ll  test02
total 0

#test02用户测试
[test02@whb /opt/gongxiang]$ touch  test02.txt
[test02@whb /opt/gongxiang]$ mkdir test02
[test02@whb /opt/gongxiang]$ ll
total 0
drwxrwsr-x. 2 test01 gongxiang 6 Dec 18 18:43 test01
-rw-rw-r--. 1 test01 gongxiang 0 Dec 18 18:43 test01.txt
drwxrwsr-x. 2 test02 gongxiang 6 Dec 18 18:44 test02
-rw-rw-r--. 1 test02 gongxiang 0 Dec 18 18:43 test02.txt
[test02@whb /opt/gongxiang]$ touch  test02/test02.log
[test02@whb /opt/gongxiang]$ ll test02
total 0
-rw-rw-r--. 1 test02 gongxiang 0 Dec 18 18:45 test02.log
3. sticky
粘滞位,

设置了sticky后,所有用户除了root和属主可以管理自己的文件或者目录,其它人没有管理的权限。
主要作用在其他人的x位,t表示
数字权限位 1
[root@whb ~]# mkdir -m 777   /test
[root@whb ~]# ll -d /test
drwxrwxrwx. 2 root root 6 Dec 18 18:58 /test
[root@whb ~]# chmod o+t /test/
[root@whb ~]# ll -d /test
drwxrwxrwt. 2 root root 6 Dec 18 19:00 /test
[root@whb ~]# stat  /test
  File: ‘/test’
  Size: 6         	Blocks: 0          IO Block: 4096   directory
Device: 803h/2051d	Inode: 1083835     Links: 2
Access: (1777/drwxrwxrwt)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:default_t:s0
Access: 2019-12-18 19:00:13.822960217 +0800
Modify: 2019-12-18 19:00:33.146959534 +0800
Change: 2019-12-18 19:01:09.490958251 +0800
 Birth: -

#test01用户测试
[test01@whb ~]$ touch /test/test01.txt
[test01@whb ~]$ ll /test/
total 0
-rw-rw-r--. 1 test01 test01 0 Dec 18 19:01 test01.txt
-rw-rw-r--. 1 test02 test02 0 Dec 18 19:01 test02.txt
[test01@whb ~]$ rm -f  /test/test02.txt 
rm: cannot remove ‘/test/test02.txt’: Operation not permitted

#test02用户测试
[test02@whb ~]$ touch /test/test02.txt
[test02@whb ~]$ ll /test/
total 0
-rw-rw-r--. 1 test01 test01 0 Dec 18 19:01 test01.txt
-rw-rw-r--. 1 test02 test02 0 Dec 18 19:01 test02.txt
[test02@whb ~]$ rm -f  /test/test01.txt 
rm: cannot remove ‘/test/test01.txt’: Operation not permitted
[test02@whb ~]$ rm -f  /test/test02.txt 

六、特殊属性

特殊属性不受权限的控制
选项 含义
  a       表示只能追加,不能删除,移动,修改,给重要的文件,日志
  i       什么都不能操作,不能删除,移动,追加,重定向,给重要的配置文件加上特殊属性
#测试a属性
[root@whb ~]# lsattr  test.txt 
-----a---------- test.txt
[root@whb ~]# cat test.txt
hostname
[root@whb ~]# ./test.txt 
qls
[root@whb ~]# cp test.txt  /tmp/
[root@whb ~]# rm -f  test.txt 
rm: cannot remove ‘test.txt’: Operation not permitted
[root@whb ~]# mv test.txt  /opt/
mv: cannot move ‘test.txt’ to ‘/opt/test.txt’: Operation not permitted
[root@whb ~]# vim  test.txt 
[root@whb ~]# echo pwd >> test.txt
[root@whb ~]# cat test.txt
hostname
pwd
[root@whb ~]# echo pwd > test.txt
-bash: test.txt: Operation not permitted

#测试i属性
[root@whb ~]# lsattr  test.txt
-----a---------- test.txt
[root@whb ~]# chattr -a  test.txt
[root@whb ~]# lsattr  test.txt
---------------- test.txt
[root@whb ~]# chattr  +i  test.txt
[root@whb ~]# cat test.txt
hostname
pwd
[root@whb ~]# ./test.txt
qls
/root
[root@whb ~]# cp ./test.txt  /opt/
[root@whb ~]# mv ./test.txt  /mnt/
mv: overwrite ‘/mnt/test.txt’? y
mv: cannot move ‘./test.txt’ to ‘/mnt/test.txt’: Operation not permitted
[root@whb ~]# rm -f  test.txt
rm: cannot remove ‘test.txt’: Operation not permitted
[root@whb ~]# echo 123 >test.txt
-bash: test.txt: Permission denied
[root@whb ~]# echo 123 >>test.txt
-bash: test.txt: Permission denied
.
点赞
  1. SEO Service说道:
    Awesome post! Keep up the great work! :)
  2. 尽豪奢说道:
    Firefox Windows 10
    很棒的帖子!!继续努力!!

回复 尽豪奢 取消回复