Linux基础入门之文件特殊权限

摘要

大家都知道文件和目录的权限最常见的有三个.可读(r)..可写(w)..可执行(x)..它们的级别分别是4..2..1..我们有时也会发现有些文件所属主的权限上带有一个s的标志位.目录的所属组上也带有s标志位.很多人不理解这是为什么….下面我们举例来看一下…

[[email protected] /tmp/facl]$ ls -l `which passwd`
-rwsr-xr-x. 1 root root 30768 2月  22 2012 /usr/bin/passwd
[[email protected] /tmp/facl]$ id dwhdtest
uid=500(dwhdtest) gid=500(dwhdtest) 组=500(dwhdtest)
[[email protected] /tmp/facl]$ passwd dwhdtest
更改用户 dwhdtest 的密码 。
新的 密码:
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[[email protected] /tmp/facl]$ su - dwhdtest
[[email protected] ~]$ passwd
更改用户 dwhdtest 的密码 。
为 dwhdtest 更改 STRESS 密码。
(当前)UNIX 密码:
新的 密码:
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[[email protected] ~]$ 
[[email protected] ~]$ ls -l /etc/shadow
---------- 1 root root 1035 5月  25 20:34 /etc/shadow
[[email protected] ~]$ 
[[email protected] ~]$ cat /etc/shadow
cat: /etc/shadow: 权限不够
[[email protected] ~]$ ls -l `which cat`
-rwxr-xr-x 1 root root 48568 10月 15 2014 /bin/cat
[[email protected] ~]$ ls -l `which passwd`
-rwsr-xr-x. 1 root root 30768 2月  22 2012 /usr/bin/passwd
[[email protected] ~]$ 
Linux基础入门之文件特殊权限
Linux基础入门之文件特殊权限
Linux基础入门之文件特殊权限

特殊权限

mode

安全上下文(就是进程的安全背景):

1、进程以某一用户身份运行:进程是发起此进程的用户代理,其他用户身份进程发起者。

2、权限匹配模式

(1):进程的属主,是否与被访问的文件属主相同

(2):进程的属主所属于的组当中,是否有一个与被访问的文件属组相同

(3):以other的权限进行访问

suid:Set UID

前提:此类文件为可执行的命令或者脚本

当某用户尝试执行具有suid权限的命令时,不是以这个命令的发起者作为进程的用户身份,而是以这个文件的属主当作进程身份.

chmod u[+|-]s FILE...

属主原本有执行权限时用ls -l查看显示为小s

属主原本没有执行权限时用ls -l查看显示为大S

sgid: Set GID

前提:此类文件为可执行的命令或者脚本

chmod g[+|-]s FILE...

常用方法:如果将目录的属组设置sgid权限,所有用户在此目录中创建的文件属组不再是用户的基本组而是目录的属组

sticky:粘滞位

chmod o[+|-]t FILE...

赋予文件粘滞位,只要一个目录有了粘滞位,那么都可以在这个目录创建文件,修改文件,删除自己创建的文件,但是不能删除别人的文件

suidsgidstick

二进制 八进制
000 0 ---
001 1 --sticky
010 2 -sgid-
011 3 -sgidsticky
100 4 suid--
101 5 suid-sticky
110 6 suidsgid-
111 7 suidsgidsticky

说到这里,我们应该不难发现 我们在给文件设置权限的时候其实是可以给4位的

栗子: chmod 1644 FILE...

           chmod 2644 FILE...  等

由此可见我们的umask为什么是4位了

 


1、给文件添加suid权限方法

[[email protected] /tmp/facl]$ ls -l testfile
-rwxr-xr-x 1 root root 0 5月  25 20:20 testfile
[[email protected] /tmp/facl]$ chmod u+s testfile
[[email protected] /tmp/facl]$ ls -l testfile
-rwsr-xr-x 1 root root 0 5月  25 20:20 testfile
[[email protected] /tmp/facl]$

Linux基础入门之文件特殊权限

2、当文件原本有可执行权限时执行chmod u+s,用ls-l显示为小s,当文件原本没有可执行权限时,执行chmod u+s用ls-l显示为大S

[[email protected] /tmp/facl]$ ls -l testfile
-rwxr-xr-x 1 root root 0 5月  25 20:20 testfile
[[email protected] /tmp/facl]$ chmod u+s testfile
[[email protected] /tmp/facl]$ ls -l testfile
-rwsr-xr-x 1 root root 0 5月  25 20:20 testfile
[[email protected] /tmp/facl]$
[[email protected] /tmp/facl]$ chmod u-s testfile
[[email protected] /tmp/facl]$ chmod 644 testfile
[[email protected] /tmp/facl]$ ls -l testfile
-rw-r--r-- 1 root root 0 5月  25 20:20 testfile
[[email protected] /tmp/facl]$ chmod u+s testfile
[[email protected] /tmp/facl]$ ls -l testfile
-rwSr--r-- 1 root root 0 5月  25 20:20 testfile
[[email protected] /tmp/facl]$ ls -l testfile 

Linux基础入门之文件特殊权限
3、sgid的主要作用

[[email protected] /tmp/facl]# mkdir testfacl
[[email protected] /tmp/facl]# useradd gentoo
[[email protected] /tmp/facl]# passwd gentoo
更改用户 gentoo 的密码 。
新的 密码:
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[[email protected] /tmp/facl]# ls -ld /tmp/facl/testfacl/
drwxr-xr-x 2 root root 4096 5月  26 14:38 /tmp/facl/testfacl/
[[email protected] /tmp/facl]# chmod o+w -R /tmp/facl/testfacl
[[email protected] /tmp/facl]# ls -ld /tmp/facl/testfacl/
drwxr-xrwx 2 root root 4096 5月  26 14:38 /tmp/facl/testfacl/
[[email protected] /tmp/facl]# chmod g+s /tmp/facl/testfacl/
[[email protected] /tmp/facl]# ls -ld /tmp/facl/testfacl/
drwxr-srwx 2 root root 4096 5月  26 14:41 /tmp/facl/testfacl/
[[email protected] /tmp/facl]# 
[[email protected] /tmp/facl]# su - gentoo
[[email protected] ~]$ cd /tmp/facl/testfacl/
[[email protected] /tmp/facl/testfacl]$ touch testfaclgroup
touch: 无法创建"testfaclgroup": 权限不够
[[email protected] /tmp/facl/testfacl]$ touch testfaclgroup
[[email protected] /tmp/facl/testfacl]$ ls -l
总用量 0
-rw-rw-r-- 1 gentoo gentoo 0 5月  26 14:41 testfaclgroup
[[email protected] /tmp/facl/testfacl]$ touch testfaclgroup_2
[[email protected] /tmp/facl/testfacl]$ ls -l
总用量 0
-rw-rw-r-- 1 gentoo gentoo 0 5月  26 14:41 testfaclgroup
-rw-rw-r-- 1 gentoo root   0 5月  26 14:41 testfaclgroup_2
[[email protected] /tmp/facl/testfacl]$ 

Linux基础入门之文件特殊权限

[[email protected] /tmp/facl]# groupadd slackware
[[email protected] /tmp/facl]# mkdir -pv /tmp/facl/a_facltest
mkdir: 已创建目录 "/tmp/facl/a_facltest"
[[email protected] /tmp/facl]# ls -ld /tmp/facl/a_facltest
drwxr-xr-x 2 root root 4096 5月  26 14:53 /tmp/facl/a_facltest
[[email protected] /tmp/facl]# chown :slackware /tmp/facl/a_facltest
[[email protected] /tmp/facl]# ls -d /tmp/facl/a_facltest
/tmp/facl/a_facltest
[[email protected] /tmp/facl]# ls -ld /tmp/facl/a_facltest
drwxr-xr-x 2 root slackware 4096 5月  26 14:53 /tmp/facl/a_facltest
[[email protected] /tmp/facl]# chmod g+w /tmp/facl/a_facltest
[[email protected] /tmp/facl]# ls -ld /tmp/facl/a_facltest
drwxrwxr-x 2 root slackware 4096 5月  26 14:53 /tmp/facl/a_facltest
[[email protected] /tmp/facl]# id gentoo
uid=501(gentoo) gid=501(gentoo) 组=501(gentoo)
[[email protected] /tmp/facl]# usermod -a -G slackware gentoo
[[email protected] /tmp/facl]# id gentoo
uid=501(gentoo) gid=501(gentoo) 组=501(gentoo),502(slackware)
[[email protected] /tmp/facl]# chmod g+s /tmp/facl/a_facltest
[[email protected] /tmp/facl]# ls -ld /tmp/facl/a_facltest
drwxrwsr-x 2 root slackware 4096 5月  26 14:57 /tmp/facl/a_facltest
[[email protected] /tmp/facl]# 
[[email protected] /tmp/facl]# su - gentoo
[[email protected] ~]$ cd /tmp/facl/
[[email protected] /tmp/facl]$ id
uid=501(gentoo) gid=501(gentoo) 组=501(gentoo),502(slackware)
[[email protected] /tmp/facl]$ ls -ld
drwxr-xr-x 4 root root 4096 5月  26 14:53 .
[[email protected] /tmp/facl]$ ls -l
总用量 8
drwxrwxr-x 2 root slackware 4096 5月  26 14:53 a_facltest
drwxr-srwx 2 root root      4096 5月  26 14:41 testfacl
[[email protected] /tmp/facl]$ touch /tmp/facl/a_facltest/sgidtest_a
[[email protected] /tmp/facl]$ ls -l /tmp/facl/a_facltest/sgidtest_a
-rw-rw-r-- 1 gentoo gentoo 0 5月  26 14:57 /tmp/facl/a_facltest/sgidtest_a
[[email protected] /tmp/facl]$ ls -ld /tmp/facl/a_facltest
drwxrwxr-x 2 root slackware 4096 5月  26 14:57 /tmp/facl/a_facltest
[[email protected] /tmp/facl]$
[[email protected] /tmp/facl]$
[[email protected] /tmp/facl]$
[[email protected] /tmp/facl]$
[[email protected] /tmp/facl]$ touch /tmp/facl/a_facltest/sgidtest_b
[[email protected] /tmp/facl]$ ls -l /tmp/facl/a_facltest/sgidtest_b
-rw-rw-r-- 1 gentoo slackware 0 5月  26 15:00 /tmp/facl/a_facltest/sgidtest_b
[[email protected] /tmp/facl]$ 

Linux基础入门之文件特殊权限
4、sticky演示

[[email protected] /tmp/facl]# mkdir -pv /tmp/facl/b_test             #创建一个/tmp/facl/b_test
mkdir: 已创建目录 "/tmp/facl/b_test"
[[email protected] /tmp/facl]# chown :slackware /tmp/facl/b_test      #将/tmp/facl/b_test 属组改成slackware
[[email protected] /tmp/facl]# chmod g+s /tmp/facl/b_test             #给/tmp/facl/b_test sgid权限
[[email protected] /tmp/facl]# ls -ld /tmp/facl/b_test                #查看/tmp/facl/b_test 目录的权限
drwxr-sr-x 2 root slackware 4096 5月  26 17:24 /tmp/facl/b_test
[[email protected] /tmp/facl]# chmod g+w /tmp/facl/b_test             #给/tmp/facl/b_test 添加属组写权限
[[email protected] /tmp/facl]# ls -ld /tmp/facl/b_test                #查看/tmp/facl/b_test目录的权限
drwxrwsr-x 2 root slackware 4096 5月  26 17:24 /tmp/facl/b_test
[[email protected] /tmp/facl]# chmod o+t /tmp/facl/b_test             #给/tmp/facl/b_test目录添加stick
[[email protected] /tmp/facl]# ls -ld /tmp/facl/b_test                #查看/tmp/facl/b_test 的权限
drwxrwsr-t 2 root slackware 4096 5月  26 17:24 /tmp/facl/b_test
[[email protected] /tmp/facl]# id gentoo                              #查看gentoo用的附加组是slackware
uid=501(gentoo) gid=501(gentoo) 组=501(gentoo),502(slackware)
[[email protected] /tmp/facl]# su - gentoo                            #切换到gentoo用户
[[email protected] ~]$ cd /tmp/facl/b_test/                         #进入/tmp/facl/b_test目录
[[email protected] /tmp/facl/b_test]$ touch test_b                  #创建个test_b的文件
[[email protected] /tmp/facl/b_test]$ ls -l test_b                  #查看test_b的权限
-rw-rw-r-- 1 gentoo slackware 0 5月  26 17:27 test_b
[[email protected] /tmp/facl/b_test]$ ls -ld .                      #查看/tmp/facl/b_test目录的权限 得知自己有在这个目录的写权限
drwxrwsr-t 2 root slackware 4096 5月  26 17:27 .
[[email protected] /tmp/facl/b_test]$ exit                          #回到root用户
logout
[[email protected] /tmp/facl]# useradd redhat                         #添加个redhat用户
[[email protected] /tmp/facl]# usermod -a -G slackware redhat         #给redhat用户添加slackware附加组
[[email protected] /tmp/facl]# id redhat                              #查看redhat用户信息 得知附加组是slackware
uid=502(redhat) gid=503(redhat) 组=503(redhat),502(slackware)
[[email protected] /tmp/facl]# su - redhat                            #切换到redhat用户
[[email protected] ~]$ cd /tmp/facl/b_test/                         #进入/tmp/facl/b_test目录
[[email protected] /tmp/facl/b_test]$ touch test_c                  #创建个test_c文件
[[email protected] /tmp/facl/b_test]$ ls -l test_c                  #查看test_c文件权限
-rw-rw-r-- 1 redhat slackware 0 5月  26 17:30 test_c
[[email protected] /tmp/facl/b_test]$ ls -l                         #查看test_b和test_c的权限,两文件权限区别就是属主不一样
总用量 0
-rw-rw-r-- 1 gentoo slackware 0 5月  26 17:27 test_b
-rw-rw-r-- 1 redhat slackware 0 5月  26 17:30 test_c
[[email protected] /tmp/facl/b_test]$ ls -ld .
drwxrwsr-t 2 root slackware 4096 5月  26 17:30 .
[[email protected] /tmp/facl/b_test]$ rm -rf test_c                 #删除自己创建的test_c的文件
You are going to execute "/bin/rm -rf test_c",please confirm (yes or no):yes   #没有返回信息 删除成功
[[email protected] /tmp/facl/b_test]$ ls -l
总用量 0
-rw-rw-r-- 1 gentoo slackware 0 5月  26 17:27 test_b
[[email protected] /tmp/facl/b_test]$ rm -rf test_b                  #删除gentoo用户创建的test_b的文件
You are going to execute "/bin/rm -rf test_b",please confirm (yes or no):yes
/bin/rm: 无法删除"test_b": 不允许的操作                                #返回信息说不允许删除
[[email protected] /tmp/facl/b_test]$ echo -e "test exit test_b \n`date`" >> test_b #往gentoo用户创建的test_b文件内写入内容
[[email protected] /tmp/facl/b_test]$ cat test_b                     #查看发现redhat用户写入成功
test exit test_b
2015年 05月 26日 星期二 17:32:10 CST
[[email protected] /tmp/facl/b_test]$ 

Linux基础入门之文件特殊权限
Linux基础入门之文件特殊权限


  • 本文由 发表于 2015年5月26日17:16:09
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: