linux文件权限(unit6)

来源:互联网 发布:数据分析师教程 编辑:程序博客网 时间:2024/06/03 10:51
#

文件权限

#

1.文件属性的查看

ls -l filename
-|rw-r–r–.|1| root| root| 46 |Oct 1 05:03 |filename
— ————————— — ———— ———— ——-
1 /// 2 /// 3 / 4 //// 5 /// 6 ///// 7 /////////// 8

1.”-“:文件类型
- ##普通文件
d ##目录
c ##字符设备
s ##套接字
p ##管道
b ##快设备
l ##连接

2.”rw-r–r–”:文件读写权限
rw-|r–|r–
* $ @

*所有人的权限 u
$所有组的权限 g
@其他人的权限 o

3.”1”:
对文件:文件内容被系统记录的次数
对目录:目录中文件属性的字节数 不是文件的字节数

4.”root”:文件所有人

5.”root”:文件所有组

6.”46”:文件内容的大小 (字节数)

7.”Oct 1 05:03”:文件最后一次被修改的时间

8.”filename”:文件名字 还包含文件路径

2.文件所有人所有组的管理

chown username file|dir ##更改文件的所有人
chown username:groupname file|dir ##更改所有人所有组
chown -R username dir ##更改目录本身及里面所有内容的所有人
chgrp -R groupname dir ##更改目录本身及里面所有内容的所有组

[root@localhost /]# cd /mnt
[root@localhost mnt]# chown student file
[root@localhost mnt]# ls -l file
-rw-r–r–. 1 student root 11 Apr 7 08:18 file
[root@localhost mnt]# mkdir book
[root@localhost mnt]# ll -d book
drwxr-xr-x. 2 root root 6 Apr 7 08:22 book
[root@localhost mnt]# chown student book
[root@localhost mnt]# ll -d book
drwxr-xr-x. 2 student root 6 Apr 7 08:22 book
[root@localhost mnt]# chown student:student book
[root@localhost mnt]# ll -d book
drwxr-xr-x. 2 student student 6 Apr 7 08:22 book

[root@localhost mnt]# mkdir note
[root@localhost mnt]# touch note/file2
[root@localhost mnt]# ll -d note
drwxr-xr-x. 2 root root 18 Apr 7 08:26 note
[root@localhost mnt]# ll note/file2
-rw-r–r–. 1 root root 0 Apr 7 08:26 note/file2
[root@localhost mnt]# chown -R student note
[root@localhost mnt]# ll -d note
drwxr-xr-x. 2 student root 18 Apr 7 08:26 note
[root@localhost mnt]# ll note/file2
-rw-r–r–. 1 student root 0 Apr 7 08:26 note/file2

[root@localhost mnt]# chgrp -R student note
[root@localhost mnt]# ll -d note
drwxr-xr-x. 2 student student 18 Apr 7 08:26 note
[root@localhost mnt]# ll note/file2
-rw-r–r–. 1 student student 0 Apr 7 08:26 note/file2

监控命令
watch -n 1 ls -lR /mnt

3.文件普通权限

rw-|r–|r–
u g o
u:文件所有人对文件可以读写
g:文件组成员对文件可读
o:其他人对文件可读
u优先匹配,g次优先,o当u,g不匹配时匹配
1.r
对文件:可以查看文件中的字符
对目录:可以查看目录中文件的信息

2.w
对文件:可以更改文件内字符
对目录:可以在目录中添加删除文件

3.x
对文件:可以运行文件内记录的程序动作
对目录:可以进入目录中

4.字符方式修改该文件权限
chmod [-R]

4.系统默认权限的设定

从系统存在角度来说,开放权力越大,系统存在意义越高
从系统安全角度来说,开放权力越少,系统安全性越高
所以系统设定新建文件或目录会去掉一些权限
设定方式
umask ##查看系统保留权限默认为022
umask 077 ##修改该系统保留权限为077,此设定为临时设定,只当前shell中生效

永久设定方式:
vim /etc/bashrc ##shell
70 if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then
71 umask 002 ##普通用户umask
72 else
73 umask 077 ##超级用户umask
74 fi

vim /etc/profile ##系统
59 if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then
60 umask 002 ##普通用户umask
61 else
62 umask 077 ##超级用户umask
63 fi

以上两个文件umask设定值必须保持一致
source /etc/bashrc
source /etc/profile
让设定立即生效,source ,(重启过程)

5.特殊权限

1.suid ##冒险位
之针对二进制可执行文件,
文件内记录的程序产生的进程的所有人为文件所有人
和进程发起人身份无关

设定方式:
chmod u+s file
suid=4
chmod 4xxx file

2.sgid ##强制位
对文件:只针对二进制可执行文件,
任何人运行二进制文件
程序时程序产生的进程的所有组都是文件的所有组
和程序发起人组的身份无关
对目录:当目录有sgid权限后,目录中新建的所有文件的所有组
都自动归属到目录的所有组之中,和文件建立者所在的组无关

设定方式:

chmod g+s file|dir
sgid=2
chmod 2xxx file|dir

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

设定方式:
chmod o+t direcotry
t=1
chmod 1777 direcotry

0 0
原创粉丝点击