基于Linux下的权限管理
文件属性查看:
ls -l file
如上图所示在桌面建立了一个文件,然后用你ls -l file查看文件属性。
alias命令就是查看命令的简写,比如ll和ls -l效果相同。
- rw-r--r-- 1 root root 0 Apr 2 08:32 file
-代表普通文件,rw-r--r--代表文件的具体权限,1是指cat能看到的被系统记录的次数,root指文件拥有者,root指文件所在组,0表示文件大小,Apr 2 08:32代表最后一次更改的时间,file表示文件。
例如:以下例子对每一块进行详细解释。
- rw-rw-r-- 1 kiosk kiosk 0 Oct 2 17:05 file
[1] [2] [3] [4] [5] [6] [7] [8]
[1]文件类型
- 普通文件。
d 目录。
s socket套接字。
l 软链接,快捷方式。
p 管道。
c 字符设备,终端ps。
b 块设备,比如我们插入U盘之后,就会多了/dev/sdb也就是所谓的块设备。
[2] 读写权限
rw-|rw-|r--
@ $ *
@ 文件拥有者对文件能做的动作
$ 文件所在组的组成员能对文件做的动作
* 其他人对与文件能做的动作优先级,九位权限三类人。
[3] 文件内容被系统记录的次数,1是指能看到的被系统记录的次数
[4] 文件拥有者
[5] 文件所在组
[6] 文件大小
[7] 文件最后一次被更改的时间
[8] 文件名称
查看目录属性:
ls -ld directoy
d rwxr-xr-x 3 kiosk kiosk 33 Aug 27 09:47 directory
[1] [2] [3] [4] [5] [6] [7] [8]
[1] 类型(同文件)
[2] 权限
[3] 子目录个数
[4] 目录拥有者
[5] 目录所在组
[6] 目录中内容的属性的大小
[7] 目录中文件增加或减少或被更名的时间
[8] 目录名称
对于图片而言,来分别看一下,d表示这是个目录,rwxr-xr-x代表目录的权限,westos代表目录拥有者,westos代表目录所在组,88代表内容属性大小,Apr 2 05:28表示最后操作目录实践,666表示目录名称。
权限的识别:
用户权限(u) 组成员权限(g) 其他用户权限(o)
权限种类
r
r权限针对文件,表示可以查看文件内容
r权限针对目录,表示可以ls 查看目录中存在的文件名称
w
w权限针对文件,表示可以更改文件的内容
w权限针对目录,表示是否可以删除目录中的子文件或者子目录
x
x权限对于文件,表示是否可以开启文件当中记录的程序
x权限对于目录,表示是否可以进入目录中
chmod ugo+-=rwx 改权限
我们同样可以用监控命令来观察更改权限效果:
u=rwx=7 |g=rwx=7|o=rwx=7
所用二进制计算办法来进行,满权限就是rwx等于4+2+1=7
u=rw-=6 g=r--=4 o=r--=4
r=4,w=2,x=1,-=0
7=rwx,6=rw-,5=r-x,4=r--,3=-wx,2=-w-,1=--x,0=---
下图呈现了我们更改权限以及效果的呈现。我们可以分别对ugo用户操作,也可以单个赋予权限,赋权限可以用我们的rwx,也可以通过二进制计算结果进行赋予权限。
文件的默认权限:
解释一下文件默认权限,满权限为777减去系统预留权限022为755,但我们查看文件时如下图为712,为啥不是755呢?意思就是除了系统预留权限还有内核保留权限755-712=43。
umask 显示系统预留权限值
umask 077 临时修改umask值
上图所示我们用umask命令将文件的默认权限更改成了077,所谓临时就是当我们关闭终端再重新打开时umask还是原来的022。
永久修改:
vim /etc/bashrc 71行是普通用户的更改,73是超级用户的更改
vim /etc/profile 60行是普通用户的更改,62是超级用户的更改
source /etc/bashrc 刷新bash配置
source /etc/profile 刷新系统配置
我们可以在永久修改权限前后建立文件目录来查看文件目录权限的更改。
我们首先使用vim命令更改超级用户如下图更改为077,如果不调用source命令则umask值不会发生变化,具体呈现如下图。
特殊权限:
stickyid 强制位
o+t 指针对目录,当一个目录上有t权限时,这个目录中的文件只能 被文件拥有者删除
chmod o+t directroy,chmod 1777 directory这两个命令效果一样
我们来验证就是首先在超级用户下建立一个目录然后赋予满权限,随后分别切换两个用户当然也必须在一开始所建立的目录下操作建立文件,我们可以清楚地看到在westos用户下也可以删除student建立的文件,这显然是不合理的。
随后我们给所建立目录赋予权限chmod o+t /westos/然后重复上面切换用户建立文件,最后我们发现在westos用户下已经不能删除student的文件了,这就是我们想要的效果。
sgid 粘制位
g+s 针对目录,在目录中创建的文件都自动归属到目录所在组,针对 二进制文件,文件内记录的程序在执行时和执行者的组身份没有 关系,而是以二进制文件的所有组的身份执行的
chmod g+s file|directory,chmod 2777 file|directory同效果命令
从下图可以看到第一次我们在student用户下在westos中建立studentfile时用显示文件属性可以看到文件所有人和文件所在组均为student,之后我们给目录赋予chmod g+s /westos/权限,然后切换到student用户下建立文件查看属性发现已经将文件所在组自动归属到Linux下。
suid 冒险位
u+s 针对文件,文件记录动作在执行时是一文件所有人身份执行的,与是谁发起的无关
chmod u+s file,chmod 4777 file同效果命令
u+s命令的操作展示:
[[email protected] Desktop]# su - student
Last login: Wed Apr 4 22:08:24 EDT 2018 on pts/0
[[email protected] ~]$ ls -ld /bin/touch
-rwsr-xr-x. 1 root root 62432 Jan 24 2014 /bin/touch
[[email protected] ~]$ touch file
[[email protected] ~]$ ll file
-rw-rw-r-- 1 student student 0 Apr 4 22:31 file
[[email protected] ~]$ logout
[[email protected] Desktop]# chmod 4775 /bin/touch 添加u+s权限
[[email protected] Desktop]# ls -ld /bin/touch 查看权限
-rwsrwxr-x. 1 root root 62432 Jan 24 2014 /bin/touch
[[email protected] Desktop]# su - student
Last login: Wed Apr 4 22:31:40 EDT 2018 on pts/1
[[email protected] ~]$ touch file1
[[email protected] ~]$ ll
total 0
-rw-rw-r-- 1 student student 0 Apr 4 22:31 file 我们可以看到file1文件的用户 所有者已经变成了root。
-rw-rw-r-- 1 root student 0 Apr 4 22:32 file1
首先我们将/bin/rm文件所有人和文件所在组均更改为student,然后赋予权限chmod -ug+s /bin/rm这时作为超级用户也无法随便删除掉普通用户的文件了。
权限列表:
acl 指定特定的用户对特定的文件拥有特殊权力
-rw-rwx---+ 1 root root 0 Mar 28 02:39 file
权限列表开启,那么这个文件的普通权限也在权限列表中识别,ls -l 看到的权限是不准确的
查看
权限列表的操作:
[[email protected] Desktop]# ls -l /mnt/westos
-rw-rw-r--+ 1 root root 13 Apr 4 21:45 /mnt/westos
有加号权限列表开启
#内容如下:
file: mnt/file 文件名称
owner: root 文件的拥有者
group: root 文件的组
user::rw- 文件拥有者的权限
user:westos:rw- 特殊指定用户的权限
group::--- 文件组的权限
group:student:rwx 特殊指定组的权限
mask::rwx 权限最大值
other::--- 其他人的权限
getfacl /mnt/file 查看权限列表
[[email protected] Desktop]# getfacl /mnt/westos 查看westos的权限列表
getfacl: Removing leading '/' from absolute path names
file: mnt/westos
owner: root
group: root
user::rw-
user:student:rw-
group::r--
mask::rw-
other::r--
下图是一开始给student用户赋予读写权限,然后分别切换到student用户和westos用户,我们可以从下图看出来westos用户是没有写的权限。
设定
setfacl -m u:lee:rwx /mnt/file 设定lee用户对file文件可以读写执行
setfacl -m g:student:rwx /mnt/file 设定student组对文件可以读写执行
setfacl -x u:lee /mnt/file 在权限列表中删除用户lee的信息
setfacl -x g:student /mnt/file 在权限列表中删除student组的信息
setfacl -b /mnt/file 关闭权限列表那么,"+"消失
权限列表的相关操作:
[[email protected] Desktop]# useradd hello
[[email protected] Desktop]# ll /mnt/westos
-rw-rw-r--+ 1 root root 13 Apr 4 21:45 /mnt/westos
[[email protected] Desktop]# setfacl -m u:hello:0 /mnt/westos 更改hello用户权限为空
[[email protected] Desktop]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
file: mnt/westos
owner: root
group: root
user::rw-
user:student:rw-
user:hello:---
group::r--
mask::rw-
other::r--
[[email protected] Desktop]# setfacl -m g:student:rwx /mnt/westos 更改student组的权限为rwx
[[email protected] Desktop]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
file: mnt/westos
owner: root
group: root
user::rw-
user:student:rw-
user:hello:---
group::r--
group:student:rwx
mask::rwx
other::r--
[[email protected] Desktop]# setfacl -x g:student /mnt/westos 删除组student
[[email protected] Desktop]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
file: mnt/westos
owner: root
group: root
user::rw-
user:student:rw-
user:hello:---
group::r--
mask::rw-
other::r--
[[email protected] Desktop]# setfacl -x u:hello /mnt/westos 删除用户hello
[[email protected] Desktop]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
file: mnt/westos
owner: root
group: root
user::rw-
user:student:rw-
group::r--
mask::rw-
other::r--
[[email protected] Desktop]# setfacl -x u:student /mnt/westos 删除用户student然后查看
[[email protected] Desktop]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
file: mnt/westos
owner: root
group: root
user::rw-
group::r--
mask::r--
other::r--
[[email protected] Desktop]# ll /mnt/westos
-rw-r--r--+ 1 root root 13 Apr 4 21:45 /mnt/westos
[[email protected] Desktop]# setfacl -b /mnt/westos
[[email protected] Desktop]# ll /mnt/westos
-rw-r--r-- 1 root root 13 Apr 4 21:45 /mnt/westos 加号消失,关闭权限列表。
mask值是能够赋予用户权限的最大值,当设定acl列表后,如果用chmod命令缩小文件的权限,那么mask值会被更改。
chmod 600 /mnt/file 做这样的动作mask值会发生改变
在更改前
file: mnt/file
owner: root
group: root
user::rw-
user:lee:rwx
group::---
mask::rwx
other::---
更改后
file: mnt/file
owner: root
group: root
user::rw-
user:lee:rwx effective:--- 此位置表示有效的权力
group::---
mask::---
other::--
恢复
setfacl -m m:rwx /mnt/file 用此命令可以恢复mask的值
mask代表能赋予用户的最大权力。具体操作:
[[email protected] Desktop]# ll /mnt/westos
-rw-r--r-- 1 root root 13 Apr 4 21:45 /mnt/westos
[[email protected] Desktop]# setfacl -m u:student:rwx /mnt/westos 给学生用户最大权限然后进行查看。
[[email protected] Desktop]# ll /mnt/westos
-rw-rwxr--+ 1 root root 13 Apr 4 21:45 /mnt/westos
[[email protected] Desktop]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
# file: mnt/westos
# owner: root
# group: root
user::rw-
user:student:rwx
group::r--
mask::rwx
other::r--
[[email protected] Desktop]# chmod 644 /mnt/westos 更改权限为644
[[email protected] Desktop]# ll /mnt/westos
-rw-r--r--+ 1 root root 13 Apr 4 21:45 /mnt/westos
[[email protected] Desktop]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
# file: mnt/westos
# owner: root
# group: root
user::rw-
user:student:rwx #effective:r-- 有效的权力,因为之前已经被改变了所以有效权力为644
group::r--
mask::r--
other::r--
[[email protected] Desktop]# setfacl -m m:rwx /mnt/westos 将mask值还原
[[email protected] Desktop]# ll /mnt/westos
-rw-rwxr--+ 1 root root 13 Apr 4 21:45 /mnt/westos
[[email protected] Desktop]# getfacl /mnt/westos 查看权限
getfacl: Removing leading '/' from absolute path names
# file: mnt/westos
# owner: root
# group: root
user::rw-
user:student:rwx
group::r--
mask::rwx
other::r--
acl列表的默认权限:
权限针对与目录生效
当对目录设定普通的权限列表后,是不能取定特定用户对在目录中新建的文件由指定权限的。如果需要权限自动添加到新建文件上那么要设定目录的默认权限。
setfacl -m d:u:student:rwx /mnt/westos 设定在westos目录中
新建文件对student用户有rwx权限,对已经存在的文件无效,对目录本身无效.
具体的效果呈现:
[[email protected] Desktop]# mkdir /mnt/test
[[email protected] Desktop]# touch /mnt/test/file{1..3} 建立目录以及目录下面的文件
[[email protected] Desktop]# ls -ld /mnt/test 查看目录权限
drwxr-xr-x 2 root root 42 Apr 4 23:26 /mnt/test
[[email protected] Desktop]# ls -l /mnt/test/ 查看文件权限
total 0
-rw-r--r-- 1 root root 0 Apr 4 23:26 file1
-rw-r--r-- 1 root root 0 Apr 4 23:26 file2
-rw-r--r-- 1 root root 0 Apr 4 23:26 file3
[[email protected] Desktop]# setfacl -Rm u:student:rwx /mnt/test/
给目录以及目录里面文件权限对student用户可写。
[[email protected] Desktop]# ls -l /mnt/test/
total 0
-rw-rwxr--+ 1 root root 0 Apr 4 23:26 file1
-rw-rwxr--+ 1 root root 0 Apr 4 23:26 file2
-rw-rwxr--+ 1 root root 0 Apr 4 23:26 file3 文件权限已经加成功
[[email protected] Desktop]# ls -ld /mnt/test
drwxrwxr-x+ 2 root root 42 Apr 4 23:26 /mnt/test 目录权限已经加成功
[[email protected] Desktop]# touch /mnt/test/newfile
[[email protected] Desktop]# ls -l /mnt/test/
total 0
-rw-rwxr--+ 1 root root 0 Apr 4 23:26 file1
-rw-rwxr--+ 1 root root 0 Apr 4 23:26 file2
-rw-rwxr--+ 1 root root 0 Apr 4 23:26 file3
-rw-r--r-- 1 root root 0 Apr 4 23:28 newfile 新文件没有权限
[[email protected] Desktop]# rm -fr /mnt/test/newfile
[[email protected] Desktop]# setfacl -m d:u:student:rwx /mnt/test/
给新建立的文件增加对student用户可写。
[[email protected] Desktop]# getfacl /mnt/test
getfacl: Removing leading '/' from absolute path names
# file: mnt/test
# owner: root
# group: root
user::rwx
user:student:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:student:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[[email protected] Desktop]# touch /mnt/test/newfile
[[email protected] Desktop]# ll /mnt/test/
total 0
-rw-rwxr--+ 1 root root 0 Apr 4 23:26 file1
-rw-rwxr--+ 1 root root 0 Apr 4 23:26 file2
-rw-rwxr--+ 1 root root 0 Apr 4 23:26 file3
-rw-rw-r--+ 1 root root 0 Apr 4 23:30 newfile 新文件有了权限
[[email protected] Desktop]# getfacl /mnt/test/newfile
getfacl: Removing leading '/' from absolute path names
# file: mnt/test/newfile
# owner: root
# group: root
user::rw-
user:student:rwx #effective:rw-
group::r-x #effective:r--
mask::rw-
other::r--