您的位置:首页 > 运维架构 > Linux

Linux中文件权限

2019-01-22 00:01 447 查看

Linux中文件权限

文件属性查看:

ls -l file

- rw-r--r-- 1 root root 0 Dec 31 09:55 file
1   2       3  4    5   6      7         8

1.文件类型
-:普通文件
d:目录
c:字符设备
s:套接文件
p:管道
b:块设备
l:链接

2.文件读写权限
rw-r–r--
1 2 3
1拥有者
2所属组
3其他人

3.对文件:文件内容被系统记录的次数
对目录:目录下子目录的数量,包括本目录的路径和返回上层目录的路径

4.文件的拥有者

5.文件的所属组

6.文件的内容大小

7.文件最后一次被修改的时间

8.文件的名字

修改文件拥有者和所属组

1.chown 可以改变文件的拥有者
若系统中之前存在一个文件file,通过ll查看文件属性得知该文件的拥有者和所属组为超级用户root

chown student file

将文件file的拥有者改为普通用户student

chgrp studnet file

将文件file的所属组改为普通用户studnet

2.若系统中之前存在一个目录westos,通过ll查看目录属性得知该目录的拥有者和所属组位超级用户root

chown student westos/

将目录westos的拥有者改为普通用户student

chgrp student westos/

将目录westos的所属组改为普通用户student
上述所有操作可以通过ll进行查看

3.若系统中之前存在的一个目录westos或者文件file的拥有者和所属组都是root用户,如果要同时改变文件或目录的拥有者和所属组为普通用户student,可以通过一条指令直接进行,而不像上述命令来分开执行,比较方便。

chown studnet.student westos/ | file

通过这样一条指令可以直接改变目录westos/或者文件file的拥有者和所属组为student

4.还有一种情况,如果westos目录的拥有者和所属组为root,在westos目录下存在linux目录,拥有者和所属组也是root,如果想要在改变westos目录的拥有者和所属组同时也改变westos子目录linux的拥有者和所属组,就可以在执行上述命令时在前面加上-R,类似于递归,否则只是改变了westos目录,他的子目录linux不会改变
注意,改变后的拥有者或所属组用户必须存在

文件拥有者和所属组的改变

chown username file|dir

chgrp groupname file|dir

chown username.groupname file|dir

chown -R username file|dir (递归)

chgrp -R groupname file|dir (递归)

修改文件的普通权限

  1. u拥有者
  2. g所属组
  3. o其他人

r
对文件:可以查看文件的字符内容
对目录:可以查看目录中文件/目录的信息
w
对文件:可以更改文件中的内容
对目录:可以在目录中增删改查
x
对文件:可以运行文件中记录的程序动作(例如写了一个脚本,要通过sh执行这个脚本时就要给脚本文件加上x)
对目录:可以进入目录

chmod可以更改文件普通权限

chmod u-x file

让文件file的拥有者没有可执行的权限

chmod u-w,g+x file

让文件file的拥有者没有写的权限,所属组加上可执行权限

chmod ugo-r file

让文件file的拥有者、所属组及其他人没有读的权限

改变文件普通权限还有另外一种方式
r=4 w=2 x=1
rwx=7 rw-=6 r-x=5 -wx=3 r–=4 -w-=2 --x=1 - - -=0

chmod 744 file

系统默认权限的设定

系统设定新文件或目录时会去掉一些权限
设定方式
umask(普通用户和超级用户) #查看系统减掉的权限
umask xxx 修改系统减掉xxx,此设定是临时设定,只在当前shell生效

永久设定

vim /etc/bashrc

70 if [ $UID -gt 199 ] &&[ “

id-gn
” = “
id-un
” ]; then
71 umask 002 #普通用户的umask
72 else
73 umask 022 #超级用户的umask
74 fi

59 if [ $UID -gt 199 ] && [ “

id -gn
” = “
id -un
” ]; then
60 umask 002
61 else
62 umask 022
63 fi
以上两个文件的umask值必须保持一致
配置改完后执行下面的两条指令,让系统重新加载配置文件

source /etc/bashrc

source /etc/profile

acl的定义

acl=access control

[root@localhost Desktop]# ll
total 4
-rw-rwxr--+ 1 root root 114 Jan 16 02:36 file
^(当出现+说明开启了acl访问控制列表)
-rw-r--r--  1 root root   0 Jan 16 02:34 file~

指定特殊的用户对特殊的文件有特殊的权限
被指定的用户将会被放置到acl列表中,可以将acl列表看作绿色通道,里面的用户对特殊文件会拥有比较特殊的权限

[root@localhost Desktop]ll
-rw-r--r-- 1 root root 114 Jan 16 02:36 file
-rw-r--r-- 1 root root   0 Jan 16 02:34 file~
[root@localhost Desktop]# setfacl -m u:student:rwx file(如果要对一个目录开启acl访问控制列表,需要在u前面加上d:)
[root@localhost Desktop]# ll
total 4
-rw-rwxr--+ 1 root root 114 Jan 16 02:36 file
-rw-r--r--  1 root root   0 Jan 16 02:34 file~

注意:当文件/目录有权限列表时,ls -l看到的权限是假的,唯一的用途就是查看有没有开启acl访问控制列表
mask用来标示实际能够赋予用户最大权限

[root@localhost Desktop]# getfacl file(这条指令可以查看正确的权限)
  • #file: file #目录/文件的名称
  • #owner: root #目录/文件的拥有者
  • #group: root #目录/文件的所属组
  • user::rw- #拥有者所拥有的权限
  • user:student:rwx #acl列表中(特殊用户)的权限
  • group::r-- #特殊组的权限
  • mask::rwx #权限掩码
  • other::r-- #其他人的权限

删除列表中的用户或组

setfacl -x u:student file

关闭列表

setfacl -b file

通过ll查看文件属性,可以看到文件file的加号消失了,说明关闭了acl访问控制列表
可能:使用chmod改变为文件普通权限的时候可能会破坏acl mask

特殊权限位

1.suid ##冒险位
只针对二进制可执行文件
文件内记录的程序产生的进程的拥有者为文件的拥有者
和进程的发起人没关系

[root@desktop0 mnt]# chmod u+s /usr/bin/touch == [root@desktop0 mnt]# chmod 4755 /usr/bin/touch

[root@desktop0 mnt]# ll /usr/bin/touch
-rwsr-xr-x. 1 root root 62432 Jan 25  2014 /usr/bin/touch
[root@desktop0 mnt]# su - student
Last login: Mon Dec 31 16:23:23 CST 2018 on pts/0
[student@desktop0 ~]$ ll
total 0
-rw-------. 1 student student 0 Dec 31 16:23 file
[student@desktop0 ~]$ touch file2
[student@desktop0 ~]$ ll
total 0
-rw-------. 1 student student 0 Dec 31 16:23 file
-rw-------. 1 root    student 0 Dec 31 16:25 file

2.sgid ##强制位
对文件:只针对二进制可执行文件,
任何人运行二进制文件程序时程序产生的进程的所属组都是文件的所有组
和程序发起人组的身份无关

[root@desktop0 mnt]# chmod g+s /usr/bin/touch ^C
[root@desktop0 mnt]# chmod 2755 /usr/bin/touch
[root@desktop0 mnt]# ll /usr/bin/touch
-rwxr-sr-x. 1 root root 62432 Jan 25  2014 /usr/bin/touch
[root@desktop0 mnt]# su - student
Last login: Mon Dec 31 16:25:36 CST 2018 on pts/0
[student@desktop0 ~]$ ls
file  file2
[student@desktop0 ~]$ touch file3
[student@desktop0 ~]$ ll
total 0
-rw-------. 1 student student 0 Dec 31 16:23 file
-rw-------. 1 root    student 0 Dec 31 16:25 file2
-rw-------. 1 student root    0 Dec 31 16:43 file3

对目录:当目录有sgid权限后,目录中新建的所有文件的所有组
都自动归属到目录的所有组之中,和文件建立者所在的组无关

[root@desktop0 mnt]# chmod g+s /westos/
[root@desktop0 mnt]# ll -d /westos/
drwxrwsrwx. 3 root root 19 Dec 31 16:46 /westos/
[root@desktop0 mnt]# su - studnet
su: user studnet does not exist
[root@desktop0 mnt]# su - student
Last login: Mon Dec 31 16:45:59 CST 2018 on pts/0
[student@desktop0 ~]$ cd /westos/
[student@desktop0 westos]$ ls
stuent
[student@desktop0 westos]$ ll
total 0
drwx------. 2 student student 6 Dec 31 16:46 stuent
[student@desktop0 westos]$ touch ww
[student@desktop0 westos]$ ll
total 0
drwx------. 2 student student 6 Dec 31 16:46 stuent
-rw-------. 1 student root    0 Dec 31 16:47 ww
[student@desktop0 westos]$ mkdir ee
[student@desktop0 westos]$ ll
total 0
drwx--S---. 2 student root    6 Dec 31 16:47 ee
drwx------. 2 student student 6 Dec 31 16:46 stuent
-rw-------. 1 student root    0 Dec 31 16:47 ww

设定方式:

chmod g+s file|dir

sgid=2

chmod 2xxx file|dir

3.sticky ##粘制位
t权限:
只针对于目录,当一个目录上有t权限,那么目录中的文件只能被文件的拥有者删除

设定方式:

chmod o+t direcotry

t=1

chmod 1777 direcotry

drwxrwxrwt. 2 root root 6 Dec 31 17:04 /pub/
[root@desktop0 mnt]# su - westos
Last login: Mon Dec 31 17:03:45 CST 2018 on pts/0
[westos@desktop0 ~]$ cd /pub/
[westos@desktop0 pub]$ touch westos
[westos@desktop0 pub]$ ll
total 0
-rw-------. 1 westos root 0 Dec 31 17:06 westos
[westos@desktop0 pub]$ su - student
Password:
Last login: Mon Dec 31 17:03:26 CST 2018 on pts/0
[student@desktop0 ~]$ cd /pub/
[student@desktop0 pub]$ ls
westos
[student@desktop0 pub]$ rm -rf westos
rm: cannot remove ‘westos’: Operation not permitted
[student@desktop0 pub]$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: