文件权限

一、文件权限
1.文件权限存在的意义
系统最底层安全设定方法之一
保证文件可以被可用的用户做相应操作
socket :程序对外的接口
2.文件权限的查看
(1)查看一个文件的权限:ls -ld(-ld表示目录本身而不是目录里面的内容)
格式:ls -l file

[root@localhost Desktop]# ls -l passwd
-rw-r–r--. 1 root root 2089 Oct 8 09:33 passwd
[root@localhost Desktop]#

(2)查看一个目录的权限
格式:ls -ld dir

[root@localhost Desktop]# ls -ld a
drwxr-xr-x. 2 root root 18 Oct 8 07:49 a
[root@localhost Desktop]#

ll file = ls ld 与ls -l 结果一致
ll -d dir

3.文件权限的读取

  • |rw-rw-r–| 1 |kiosk | kiosk | 0 | Jul 21 09:18 | file
    [1] [2] [3] [4] [ 5] [6] [7] [8]

[1]
文件的类型

  • ##空文件,或者文本 普通文件
    d ##目录
    l ##软链接 (快捷方式)
    s ##socket 套接字
    b ##block 块设备
    c ##字符设备 ls -l /dev/pts/0
    p
    [2]
    文件的权限(rw 读写)
    rw-|rw-|r–
    1 2 3
    1.[u] 文件拥有者对文件能做什么操作
    2.[g] 文件所有组对文件能做什么操作
    3.[o] 其他人对文件能做什么操作

[3]
对文件:(| 1 |)文件硬链接个数(文件内容被记录的次数)
对目录:目录中子目录的个数

[4]
文件的所有人

[5]
文件所有组

[6]
对文件:文件大小
对目录:目录中子文件元数据(matedate可以理解为文件的属性)大小
元数据:- |rw-rw-r–| 1 |kiosk | kiosk | 6| Jul 21 09:18 | file =6+文件的字符数
[1] [2] [3] [4] [5] [6] [7] [8]

文件的内容被修改的时间

[8]
文件的名称

例如:文件passwd
-rw-r–r--. 1 root root 2089 Oct 8 09:33 passwd

文件类型:普通文件
文件所有者权限:读写
文件所有组权限:读写
其他人对文件权限:读
文件内容被记录次数:1次
文件所有人:root
文件所有组:root
文件大小:2089字节
文件内容被修改时间:Oct 8 09:33
文件名称:passwd

4、如何改变文件的所有人和所有组(chown|chgrp)
(1)更改文件所有人
格式:chown username file|dir (unser的file的文件必须存在)

[root@localhost Desktop]# chown student passwd
[root@localhost Desktop]# ls -l passwd
-rw-r–r--. 1 student root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

(2)修改文件所有人的所有组
chown user.group file|dir

[root@localhost Desktop]# chown root.root passwd
[root@localhost Desktop]# ls -l passwd
-rw-r–r--. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

(3)修改目录的所有人
格式:chown -R user.group dir

[root@localhost Desktop]# chown -R student.root a
[root@localhost Desktop]# ls -ld a
drwxr-xr-x. 2 student root 18 Oct 8 07:49 a
[root@localhost Desktop]#

(4)修改文件所有组

格式:chgrp group file|dir

[root@localhost Desktop]# chgrp student a
[root@localhost Desktop]# ls -ld a
drwxr-xr-x. 2 student student 18 Oct 8 07:49 a

(5)chgrp -R group dir(将用户一并改变)

[root@localhost Desktop]# chgrp -R root a
[root@localhost Desktop]# ls -ld passwd
-rw-r–r--. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

5、如何改变文件的权限
(1).对权限的理解
r
对文件:是否可以查看文件中的内容 —>cat file
对目录:是否可以查看目录中有什么子文件或者子目录 —> ls dir
w
对文件:是否可以改变文件里面记录的字符
对目录:是否可以对目录中子目录或子文件的元数据进行更改
x
对文件:是否可以通过文件名称调用文件内记录的程序
对目录:是否可以进入目录

(2).更改方式
chmod <u|g|o><+|-|=><r|w|x> file|dir
chmod u+x /mnt/file1

[root@localhost Desktop]# ls -l passwd
-rw-r–r--. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]# chmod u+x passwd
[root@localhost Desktop]# ls -l passwd
-rwxr–r--. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

chmod g-r /mnt/file2

[root@localhost Desktop]# chmod g-r passwd
[root@localhost Desktop]# ls -l passwd
-rwx—r–. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

chmod ug-r /mnt/file3

[root@localhost Desktop]# chmod g-r passwd
[root@localhost Desktop]# ls -l passwd
-rwx—r–. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

chmod u-r,g+x /mnt/file4

[root@localhost Desktop]# chmod u-r,g+x passwd
[root@localhost Desktop]# ls -l passwd
–wx-wxrwx. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

chmod -r /mnt/file5

[root@localhost Desktop]# chmod -r passwd
[root@localhost Desktop]# ls -l passwd
–wx-wx-wx. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

chmod o=r-x /mnt/file6

[root@localhost Desktop]# ls -l passwd
–wx-wx-wx. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]# chmod o=r-x passwd
[root@localhost Desktop]# ls -l passwd
–wx-wxr–. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

(3)通过数字修改文件权限
rwx
210
r=4
w=2
x=1

r-x|r–|--x
5 4 1

chmod 541 /mnt/file1

7=rwx
6=rw-
5=r-x
4=r–
3=-wx
2=-w-
1=–x
0=—

[root@localhost Desktop]# chmod 777 passwd
[root@localhost Desktop]# ls -l passwd
-rwxrwxrwx. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]# chmod 521 passwd
[root@localhost Desktop]# ls -l passwd
-r-x-w—x. 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]#

6.umask
umask 系统建立文件是默认保留的权力
umask 077 ##临时设定系统预留权限为077

永久更改umask
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

vim /etc/bashrc ##shell配置文件
70 if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then
71 umask 002
72 else
73 umask 077
74 fi

source /etc/profile #让更改立即生效
source /etc/bashrc

7.特殊权限
(1).sticky 粘制位
作用:
只针对目录生效,当一个目录上有sticky权限时
在这个目录中的文件智能被文件的所有者删除

设定方式:
chmod o+t dir
chmod 1xxx dir

[root@localhost Desktop]# touch b/e
[root@localhost Desktop]# chmod 1777 b
[root@localhost Desktop]# ls -ld b
drwxrwxrwt. 2 root root 14 Oct 10 08:03 b
[root@localhost Desktop]# rm -fr b/c
[root@localhost Desktop]# ls b
e
[root@localhost Desktop]# rm -fr b/e
[root@localhost Desktop]# ls b
[root@localhost Desktop]#

(2).sgid ##强制位(只针对组)
作用
对文件: 只针对与二进制可执行文件
当文件上有sgid时任何人执行此文件产成的进程都属于文件的的组
对目录:
当目录上有sgid权限时任何人在此目录中建立的文件都属于目录的所有组

设定方式
chmod g+s file|dir
chmod 2xxx file|dir

在root账户建立的文件一般来说目录所有人和目录所有组都属于root,但给文件加入强制位后,目录所有人为root,但所有组不变

[root@localhost Desktop]# mkdir mm
[root@localhost Desktop]# chown student mm
[root@localhost Desktop]# chgrp student mm
[root@localhost Desktop]# ll mm
total 0
[root@localhost Desktop]# ll
total 36
-rw-r–r--. 1 root root 0 Oct 9 10:02 1111
drwxrwxrwt. 2 student root 18 Oct 8 07:49 a
drwxrwxrwt. 2 root root 6 Oct 10 08:05 b
drwxr-xr-x. 2 root root 6 Oct 8 07:54 c
-rw-r–r--. 1 root root 0 Oct 8 07:44 file1~
-rw-r–r--. 1 root root 6 Oct 8 07:44 file3
drwxr-xr-x. 2 student student 6 Oct 10 08:13 mm
-rwxrwxrwt. 1 root root 2089 Oct 10 07:23 passwd
drwxr-xr-x. 3 root root 14 Oct 8 08:51 r
-rwxrwxrwx. 1 root root 0 Oct 9 09:57 test
-rw-r–r--. 1 root root 16059 Oct 8 09:44 test~
drwxr-xr-x. 2 root root 6 Sep 30 07:23 Untitled Folder
-rw-r–r--. 1 root root 3568 Sep 27 02:05 zuoye~
-rw-r–r--. 1 root root 1948 Sep 29 09:16 zuoye1~
drwxr-xr-x. 2 root root 4096 Oct 9 21:25 截图2
[root@localhost Desktop]# chmod 2777 mm
[root@localhost Desktop]# ls -ld mm
drwxrwsrwx. 2 student student 6 Oct 10 08:13 mm
[root@localhost Desktop]# cd mm
[root@localhost mm]# touch nn
[root@localhost mm]# ls -l nn
-rw-r–r--. 1 root student 0 Oct 10 08:15 nn
[root@localhost mm]#

(3).suid ##冒险位(与sgid相反)
只针对与2进制可执行文件
当文件上有suid时任何人执行这个文件中记的程序产生的进程都属于文件的所有人

设定方式
chmod u+s file
chmod 4xxx file

8、acl权限列表
1.作用
让特定的用户对特定的文件拥有特定权限

2.acl列表查看
-rw-rwxr–+ 1 root root 0 Jul 21 15:45 file
^
acl开启
getfacl file ##查看acl开启的文件的权限
# file: file ##文件名称
# owner: root ##文件拥有者
# group: root ##文件拥有组
user::rw- ##文件拥有人的权限
user:kiosk:rwx ##指定用户的权限
group::r-- ##文件拥有组的权力
mask::rwx ##能赋予用户的最大权力伐值
other::r-- ##其他人的权限

[root@localhost Desktop]# getfacl passwd
#file: passwd
owner: root
group: root
#flags: --t
user::rwx
group::rwx
other::rwx

3.acl列表的管理

setfacl -m u:username:rwx file ##设定username对file拥有rwx权限

(1)setfacl -m d:u:student:rwx /mnt/westos

[root@localhost Desktop]# getfacl passwd
#file: passwd
#owner: root
group: root
flags: --t
user::rwx
group::rwx
other::rwx

(2)[root@localhost Desktop]# setfacl -m u:root:r passwd ##设置root对passwd拥有r权限

[root@localhost Desktop]# getfacl passwd
file: passwd
owner: root
group: root
flags: --t
user::rwx
user:root:r–
group::rwx
mask::rwx
other::rwx
[root@localhost Desktop]#

(3)setfacl -m g:group:rwx file ##设定group组成员对file拥有rwx权限

[root@localhost Desktop]# setfacl -m g:root:r passwd
[root@localhost Desktop]# getfacl passwd
#file: passwd
#owner: root
#group: root
flags: --t
user::rwx
user:root:r–
group::rwx
group:root:r–
mask::rwx
other::rwx
[root@localhost Desktop]#

(4)setfacl -x u:username file ##从acl列表中删除username

[root@localhost Desktop]# setfacl -x u:root passwd
[root@localhost Desktop]# ls -l passwd
-rwxrwxrwt+ 1 root root 2089 Oct 10 07:23 passwd
[root@localhost Desktop]# getfacl passwd
#file: passwd
#owner: root
#group: root
#flags: --t
user::rwx
group::rwx
group:root:r–
mask::rwx
other::rwx

(5)setfacl -b file ##关闭file上的acl列表

4.mask值
在权限列表中mask标示能生效的权力值
当用chmod减小开启acl的文件权限时mask值会发生改变
chmod g-w westos
如果要恢复mask值
setfacl -m m:rw westos

5.acl的默认权限设定
acl默认权限只针对目录设定
“acl权限只针对设定完成之后新建立的文件或目录生效,而已经存在的文件是不会继承默认权限”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值