文件的权限位

    r, readable

        文件:文本查看工具

        目录:ls

    w, writeable

        文件:可修改

        目录:可在目录下创建、删除文件

    x,exec

        文件:可执行

        目录:cd 或 ls -l

文件的属主或属组

[root@izpo45bh60h6bsz ~]# ls -l-rw-r--r--  1 root      root             27 Jul 31 20:04 grep.txtrw- 属主的权限r-- 属组的权限r-- 其他用户的权限左root 文件的属主右root 文件的属组

进程的安全上下文

    用户: 文件 文件

    centos:  /bin/cat /path/to/somefile

    1)用户对文件的操作(x)

用户名同文件的属主

    用户名同文件的属主,应用属主的权限,不在检查后续的权限(例二中说明)。属主有x,则可执行,运行为一个进程,进程的名字为进程发起者的名字

如果用户名不同文件的属主

    用户名同文件的属组,应用属组的权限,不在检查后续的权限。属组有x,则可执行,运行为一个进程,进程的名字为进程发起者的名字

非属主或属组

    应用其他用户的权限,有x,则可执行,运行为一个进程,进程的名字为进程发起者的名字

   

2) 进程对文件的操作(rw)

 进程的发起者(进程名)同文件的属主,应用属主的权限,不在检查后续的权限

 进程的发起者(进程名)同文件的属组,应用属组的权限,不在检查后续的权限

应用其他权限

准备............................##确认命令的路径[root@izpo45bh60h6bsz ~]# which --skip-alias cat  /usr/bin/cat##复制命令[root@izpo45bh60h6bsz ~]# cp -p /bin/cat /tmp/cat ##same as --preserve=mode,ownership,timestamps[root@izpo45bh60h6bsz ~]# ls -l /tmp/cat-rwxr-xr-x 1 root root 54080 Aug  5 11:28 /tmp/cat1、创建用户[root@izpo45bh60h6bsz ~]# useradd centos2、修改属主和属组[root@izpo45bh60h6bsz ~]# chown centos.centos /tmp/cat [root@izpo45bh60h6bsz ~]# ls -l /tmp/cat-rwxr-xr-x 1 centos centos 54080 Aug  5 11:29 /tmp/cat#############注意 chmod所有用户可用,chown,chgrp仅root可用#################例一:1)应用属主的x权限》》》用户名同属主,属主有x权限,可运行为一个进程centos用户下[centos@izpo45bh60h6bsz ~]$ chmod 700 /tmp/cat[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/cat -rwx------ 1 centos centos 54080 Aug  5 11:29 /tmp/cat[centos@izpo45bh60h6bsz ~]$ ls -l /etc/fstab-rw-r--r-- 1 root root 358 Jun 11 05:05 /etc/fstab[centos@izpo45bh60h6bsz ~]$ /tmp/cat /etc/fstab  ##进程的发起者非属主非属组,应用/etc/fstab文件的其他用户的权限## /etc/fstab# Created by anaconda on Fri Feb 24 02:58:22 2017#例二:2)应用属组的x权限》》》用户名同属主同属组,属主无x权限,仅属组有x权限,不能执行centos用户下[centos@izpo45bh60h6bsz ~]$ chmod 670 /tmp/cat[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/cat      ##用户匹配到的为属主的权限,没有执行权限位x权限-rw-rwx--- 1 centos centos 54080 Aug  5 11:29 /tmp/cat[centos@izpo45bh60h6bsz ~]$ ls -l /etc/fstab-rw-r--r-- 1 root root 358 Jun 11 05:05 /etc/fstab[centos@izpo45bh60h6bsz ~]$ /tmp/cat /etc/fstab  ##进程不能发起-bash: /tmp/cat: Permission denied###由以下的过程分析,首个匹配到centos时,应用首个匹配到的用户的权限。后面就算有同用户名的属组也不应用其权限#############################》》》用户名不同属主同属组,仅属组有x权限,能执行###### 修改属主为root回到root用户[root@izpo45bh60h6bsz ~]# chown root /tmp/cat [root@izpo45bh60h6bsz ~]# ls -l /tmp/cat   ##用户为root,应用属主的权限,没有任何权限----rwx--- 1 root centos 54080 Aug  5 11:29 /tmp/cat[root@izpo45bh60h6bsz ~]# /tmp/cat  /etc/fstab   ###嘿嘿,好奇怪## /etc/fstab# Created by anaconda on Fri Feb 24 02:58:22 2017#######root用户没有权限,执行。依然有执行权限###########回到centos用户[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/cat    ##用户为centos,第一个匹配到的是属组,应用属组的权限,属组有x权限,可执行为一个进程----rwx--- 1 root centos 54080 Aug  5 11:29 /tmp/cat[centos@izpo45bh60h6bsz ~]$ ls -l /etc/fstab-rw-r--r-- 1 root root 358 Jun 11 05:05 /etc/fstab[centos@izpo45bh60h6bsz ~]$ /tmp/cat /etc/fstab  ##能够执行为进程,进程名为用户名,此时应用/etc/fstab文件其他用户的权限## /etc/fstab# Created by anaconda on Fri Feb 24 02:58:22 2017#######比对来看,第一个被匹配到的用户类别,应用对应类的权限###########》》》用户名同属主同属组,仅其他有x权限,不能执行root用户下[root@izpo45bh60h6bsz ~]# chown centos:centos /tmp/cat[root@izpo45bh60h6bsz ~]# chmod 667 /tmp/catcentos用户下[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/cat   ##匹配到第一个同用户名的是属主,应用属主的权限,无x权限,不能运行为进程-rw-rw-rwx 1 centos centos 54080 Aug  5 11:29 /tmp/cat[centos@izpo45bh60h6bsz ~]$ ls -l /etc/fstab-rw-r--r-- 1 root root 358 Jun 11 05:05 /etc/fstab[centos@izpo45bh60h6bsz ~]$ /tmp/cat /etc/fstab  ##不能运行为一个进程,权限拒绝-bash: /tmp/cat: Permission denied例三3)应用其他用户的权限》》》用户名不同属主不同属组,其他有x权限,能执行root用户下[root@izpo45bh60h6bsz ~]# chown root.root /tmp/cat[root@izpo45bh60h6bsz ~]# chmod 755 /tmp/catcentos用户下[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/cat      ##匹配不到,应用其他权限,其他有x权限,可以运行为一个进程-rwxr-xr-x 1 root root 54080 Aug  5 11:29 /tmp/cat[centos@izpo45bh60h6bsz ~]$ /tmp/cat /etc/fstab  ##进程名为用户名,进程名不能匹配到/etc/fstab属主或属组的权限,应用其他权限,可读## /etc/fstab# Created by anaconda on Fri Feb 24 02:58:22 2017.....

特殊权限位

    

SUID

    文件对文件有x权限,运行为进程后,进程名是进程发起者的名字。

    当存在SUID权限后,运行为进程,其进程名是文件的属主

SGID

    任何人创建文件和目录时,其属组为创建者的基本组

    一旦某目录被设定了SGID权限,则对此目录有写权限的用户,在此目录或子目录中创建的文件为目录的基本组

Sticky 

    限制公共场景的限制 ,对于一个多人可写的目录,如果设置了sticky,则每个用户仅能删除自己的文件。避免别人删除不属于自己的文件,设置了sticky权限

   

 SUID

1、在root中修改文件的属主和属组 user.user user:user[root@izpo45bh60h6bsz ~]# chown root.centos /tmp/cat[root@izpo45bh60h6bsz ~]# ls -l /tmp/cat-rw-rw-rwx 1 root centos 54080 Aug  5 11:29 /tmp/cat2、在centos中修改权限属组必须有x[root@izpo45bh60h6bsz ~]# su - centosLast login: Sat Aug  5 11:33:41 CST 2017 on pts/1Welcome 10003 your home /home/centos[centos@izpo45bh60h6bsz ~]$ chmod 755 /tmp/cat[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/cat     ##用户centos匹配到属组的权限,能执行为一个进程,进程的名字为进程发起者的名字-rwxr-xr-x 1 root centos 54080 Aug  5 11:29 /tmp/cat[centos@izpo45bh60h6bsz ~]$ ls -l /etc/shadow  ---------- 1 root root 2895 Aug  5 11:31 /etc/shadow[centos@izpo45bh60h6bsz ~]$ /tmp/cat /etc/shadow  ##进程名不能匹配到/etc/shadow属主或属组,应用其他用户的权限,其他用户没有任何权限。/tmp/cat: /etc/shadow: Permission denied3、在root用户中给此用户授权suid权限[centos@izpo45bh60h6bsz ~]$ chmod u+s /tmp/catchmod: changing permissions of ‘/tmp/cat’: Operation not permitted[root@izpo45bh60h6bsz ~]# chmod u+s /tmp/cat[root@izpo45bh60h6bsz ~]# ls -l /tmp/cat-rwsr-xr-x 1 root centos 54080 Aug  5 11:29 /tmp/cat4、在centos用户中运行cat /etc/shadow[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/cat  ##确认权限-rwsr-xr-x 1 root centos 54080 Aug  5 11:29 /tmp/cat[centos@izpo45bh60h6bsz ~]$ ls -l /etc/shadow ##确认centos用户对文件没有权限,centos用户发起的进程对文件也不可能有权限---------- 1 root root 2895 Aug  5 11:31 /etc/shadow [centos@izpo45bh60h6bsz ~]$ /tmp/cat /etc/shadow ##执行时,进程的属主为root,root对任何文件都能操作.............test1:!!:17327:0:99999:7:100:100:test2:!!:17327:0:99999:7:100:100:test3:!!:17327:0:99999:7:100:100:test4:!!:17327:0:99999:7:100:100:..............

问题1:用户为自己设置密码时,需要修改shadow文件,shadow文件对任何用户都没有写权限,故而,用户执行passwd命令后,其进程名不是进程发起者,而是........

[root@izpo45bh60h6bsz ~]# fgrep "test1" /etc/shadow ##纯文本字符组成的PATTERN,对目标文本逐行匹配,打印匹配到的字符所在的行test1:!!:17327:0:99999:7:100:100:test18:$6...........[root@izpo45bh60h6bsz ~]# su - test1 [test1@izpo45bh60h6bsz ~]$ passwd  ##用户没有密码时,不能修改Changing password for user test1.Changing password for test1.(current) UNIX password: passwd: Authentication token manipulation error[root@izpo45bh60h6bsz ~]# echo "123" | passwd --stdin test1Changing password for user test1.passwd: all authentication tokens updated successfully.[root@izpo45bh60h6bsz ~]# fgrep 'test1' /etc/shadowtest1:$6$tMqP7HCh$Idl82b1AqXsAssE57D2jWQNrMgPRtZ7RP/OoSTNMHzG1fEruYW49f6QZfe314ETLlYwWu5YtUJu8Rx./Uceif/:17383:0:99999:7:::test18:$6...........尝试修改密码,从而修改shadow文件##生成密码,普通用户修改密码必须满足密码复杂度机制,长,随机,周期长,数字、字母、特殊字符3种[root@izpo45bh60h6bsz ~]# tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 6 | xargsFjG1LO##修改密码[test1@izpo45bh60h6bsz ~]$ passwdChanging password for user test1.Changing password for test1.(current) UNIX password: New password: Retype new password: passwd: all authentication tokens updated successfully.[test1@izpo45bh60h6bsz ~]$ 查看shadow文件[root@izpo45bh60h6bsz ~]# fgrep 'test1' /etc/shadowtest1:$6$KvgQbaJC$AxpTwktyH1kDldxoMXorPwL/2VHEutGaZq/RXXL8xLPtgStH23MDfHPlo5ZtFKRJTjKv/kmduyeBmPd1xiyV60:17383:0:99999:7:::test18:$6...........###########密码部分改变的原因##############[test1@izpo45bh60h6bsz ~]$ ls -l /bin/passwd       ##test1用户能执行此文件-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /bin/passwd[test1@izpo45bh60h6bsz ~]$ ls -l /etc/shadow       ##执行后的进程名为root,故而能改shadow文件---------- 1 root root 2985 Aug  5 13:56 /etc/shadow

SGID

1、准备目录[root@izpo45bh60h6bsz ~]# cd /tmp[root@izpo45bh60h6bsz tmp]# mkdir test[root@izpo45bh60h6bsz tmp]# ls -ld test  ##root用户创建,属组、主为rootdrwxr-xr-x 2 root root 4096 Aug  5 14:54 test[root@izpo45bh60h6bsz tmp]# groupadd mygrp #添加用户[root@izpo45bh60h6bsz tmp]# groupadd distro[root@izpo45bh60h6bsz tmp]# useradd centos[root@izpo45bh60h6bsz tmp]# chown .mygrp test [root@izpo45bh60h6bsz tmp]# ls -ld testdrwxr-xr-x 2 root mygrp 4096 Aug  5 14:54 test》》》任何人创建文件和目录时,其属组为创建者的基本组1、mygrp用户组内的用户对此目录有w权限[root@izpo45bh60h6bsz tmp]# chmod g+w test[root@izpo45bh60h6bsz tmp]# ls -ld testdrwxrwxr-x 2 root mygrp 4096 Aug  5 14:54 test2、添加distro、centos用户到mygrp组内,让distro用户拥有mygrp组的权限[root@izpo45bh60h6bsz tmp]# su - centos[centos@izpo45bh60h6bsz ~]$ mkdir /tmp/test/a.centosmkdir: cannot create directory ‘/tmp/test/a.centos’: Permission denied[centos@izpo45bh60h6bsz ~]$ touch /tmp/test/a.centostouch: cannot touch ‘/tmp/test/a.centos’: Permission denied[root@izpo45bh60h6bsz tmp]# gpasswd -a distro mygrp[root@izpo45bh60h6bsz tmp]# gpasswd -a centos mygrp3、让mygrp、distro用户分别在此目录中创建文件[root@izpo45bh60h6bsz tmp]# su - centos [centos@izpo45bh60h6bsz ~]$ touch /tmp/test/a.centos[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/test/a.centos-rw-rw-r-- 1 centos centos 0 Aug  5 15:12 /tmp/test/a.centos[root@izpo45bh60h6bsz tmp]# su - distro[distro@izpo45bh60h6bsz ~]$ touch /tmp/test/a.distro[distro@izpo45bh60h6bsz ~]$ ls -l /tmp/test/a.distro-rw-rw-r-- 1 distro distro 0 Aug  5 15:12 /tmp/test/a.distro》》》一旦某目录被设定了SGID权限,则对此目录有写权限的用户,在此目录或子目录中创建的文件为目录的基本组root用户中[root@izpo45bh60h6bsz tmp]# ls -ld testdrwxr-xr-x 2 root mygrp 4096 Aug  5 14:54 test[root@izpo45bh60h6bsz tmp]# chmod g+s /tmp/test[root@izpo45bh60h6bsz tmp]# ls -ld /tmp/testdrwxrwsr-x 2 root mygrp 4096 Aug  5 15:12 /tmp/test  ##小写s1、让mygrp、distro用户分别在此目录中创建文件[root@izpo45bh60h6bsz tmp]# su - distro[distro@izpo45bh60h6bsz ~]$ touch /tmp/test/b.distro[distro@izpo45bh60h6bsz ~]$ ls -l /tmp/test/b.distro-rw-rw-r-- 1 distro mygrp 0 Aug  5 15:16 /tmp/test/b.distro[root@izpo45bh60h6bsz tmp]# su - centos[centos@izpo45bh60h6bsz ~]$ touch /tmp/test/b.centos[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/test/b.centos-rw-rw-r-- 1 centos mygrp 0 Aug  5 15:16 /tmp/test/b.centos

Sticky

##centos用户对目录有写权限,可以删除任意文件[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/testtotal 0-rw-rw-r-- 1 centos centos 0 Aug  5 15:12 a.centos-rw-rw-r-- 1 distro distro 0 Aug  5 15:12 a.distro-rw-rw-r-- 1 centos mygrp  0 Aug  5 15:16 b.centos-rw-rw-r-- 1 distro mygrp  0 Aug  5 15:16 b.distro##删除a.distro文件[centos@izpo45bh60h6bsz ~]$ rm /tmp/test/a.distrorm: remove write-protected regular empty file ‘/tmp/test/a.distro’? y[centos@izpo45bh60h6bsz ~]$ ls -l /tmp/testtotal 0-rw-rw-r-- 1 centos centos 0 Aug  5 15:12 a.centos-rw-rw-r-- 1 centos mygrp  0 Aug  5 15:16 b.centos-rw-rw-r-- 1 distro mygrp  0 Aug  5 15:16 b.distro##避免别人删除不属于自己的文件,设置了sticky权限[root@izpo45bh60h6bsz tmp]# chmod o+t /tmp/test[root@izpo45bh60h6bsz tmp]# su - centos[centos@izpo45bh60h6bsz ~]$ rm /tmp/test/b.distro rm: cannot remove ‘/tmp/test/b.distro’: Operation not permitted

SGID,SUID,STICKY

sst 三位二进制用八进制表示

    000 0    001 1    010 2    011 3    100 4    101 5     110 6    111 7    1777 --> sticky + rwxrwxrwx4777 --> suid + rwxrwxrwx

特殊权限位映射

    SUID占据属主的执行权限位

    SGID占据属组的执行权限位

    STICKY占据其他的执行权限位

    分别用s,s,t表示,有x权限时,用小宝,没有s权限时,用大写

SUID权限位1)有xdrwxrwxr-x 2 centos centos 4096 Aug  5 15:11 a.centosdrwxrwxr-x 2 centos centos 4096 Aug  5 15:11 a.centos# chmod u+s a.centosdrwsrwxr-x 2 centos centos 4096 Aug  5 15:11 a.centos2)无x# chmod u-x a.centosdrwSrwxr-x 2 centos centos 4096 Aug  5 15:11 a.centosSGID权限位1)有x-rwxrwxr-- 1 distro distro    0 Aug  5 15:04 a.distro# chmod g+s a.distro-rw-rwsr-- 1 distro distro    0 Aug  5 15:04 a.distro2)无x# chmod g-x a.distro-rw-rwSr-- 1 distro distro    0 Aug  5 15:04 a.distroSTICKY权限位1)无x-rw-rw-r-- 1 mygrp  mygrp     0 Aug  5 15:03 a.mygrpchmod o+t a.mygrp -rw-rw-r-T 1 mygrp mygrp 0 Aug  5 15:03 a.mygrp2)有x# chmod o+x a.mygrp -rw-rw-r-t 1 mygrp mygrp 0 Aug  5 15:03 a.mygrp