Linux文件权限

阅读目录
1.概述
2.修改文件所属用户、所属组
#1.使用chown命令改变文件/目录的所属用户
#2.使用chgrp改变文件/目录的所属组
#3.使用chmod命令修改文件/目录的权限
3.扩展权限
#3.1.默认权限
#3.2特殊权限
4.查看和修改文件属性命令lsattr,chattr
#前言:我们知道,无论什么东西,涉及到安全性的,比如文件、文件夹、磁盘(就如window系统的磁盘,我们就可以通过bitlocker技术将磁盘给加密锁起来)、服务器,等都需要设置权限管理,以保证安全性,接下来让我们来探讨以下Linux的文件权限

1.概述
权限是操作系统用来限制对资源访问的机制,权限一般分为读、写、执行。

系统中的每个文件都拥有特定的权限、所属用户及所属组,通过这样的机制来限制哪些用户、哪些组可以对特定文件进行什么样操作。

#Linux的权限是基于UGO模型进行控制

1.U代表User(用户),G代表Group(组),O代表Other(其他)
2.每一个文件的权限基于UGO进行设置
3.权限三个一组(rwx),对应UGO分别设置

#查看权限

[root@ctos3 ~]# ls -ld test
drwxr-xr-- 2 root root 6 Mar  9 01:37 test
#讲解:第一个d是文件类型,后面9位3个为一组

#文件权限说明

linux文件或目录的权限位是由9个权限位来控制,每三位一组,
它们分别是文件属主(Owner)的读、写、执行,用户组(Group)的读、写、执行以及(Other)其它用户的读、写、执行

其中
r(read)读权限, 可以读取文件内容,可以列出目录内容 用数字表示为4
w(write)写权限, 可以修改文件内容,可以在目录中创建删除文件 用数字表示为2
x(excute)执行权限,可以作为命令执行,可以访问目录内容 用数字表示为1

  • 没有权限, 用数字表示为0

2.修改文件所属用户、所属组

#1.使用chown命令改变文件/目录的所属用户
修改格式:chown 用户 文件名/目录名
#例子,将test.txt的所属用户从root更改为demo用户
[root@ctos3 ~]# ls -l test.txt 
-rw-r--r-- 1 root root 0 Mar  9 01:36 test.txt

[root@ctos3 ~]# chown  demo test.txt  #更改

[root@ctos3 ~]# ls -l test.txt 
-rw-r--r-- 1 demo root 0 Mar  9 01:36 test.txt

#参数介绍
1.-R 参数递归的修改目录下的所有文件的所属用户
#例子:将/test目录下的所有文件和用户所属用户修改成demo

[root@ctos3 ~]# chown -R demo /test/
[root@ctos3 ~]# ls -l /test/
drwxr-xr-x 3 demo root 16 Mar 9 01:55 aa

2.加个;也可以快速改回所属用户和所属组

#2.使用chgrp改变文件/目录的所属组
命令格式:chgrp 用户 文件/目录名
#例子:

[root@ctos3 ~]# chgrp  demo /test/
[root@ctos3 ~]# ls -ld /test/
drwxr-xr-x 3 demo demo 16 Mar  9 01:55 /test/
#注意点:一般都是用chown修改用户和组的了 格式chown -R 用户.组 + 文件

#3.使用chmod命令修改文件/目录的权限

 命令格式:chmod +模式 +文件

模式为如下格式:
1.u、g、o、分别代表用户、组和其他
2.a可以代指ugo
3.+、-代表加入或删除对应权限
4.r、w、x代表三种权限

#修改例子:

chmod u+rw test.txt  #在user用户那里添加rw权限
chmod g-x test.txt    #在group组那里去掉x权限
chmod go+r test.txt  #在组和其他用户添加r权限
chmod u=rx test.txt  #将用户权限设置为rx权限

#提示:chmod命令也支持以数字方式修改

-r=4  (2的2次方)
-w=2(2的1次方)
-x=1 (2的0次方)
使用数字表示权限时,每组权限分别对应数字之和:
rw=4+2=6
rwx=4+2+1=7
r-x=4+1=5

#例子:

chmod 664 test.txt  == rw-rw-r
chmod 775 test.txt  == rwxrwxr-x

3.扩展权限
#3.1.默认权限

每一个终端都拥有一个umask属性,来确定新建文件、文件夹的默认权限
umask使用数字权限方式表示,如:022

目录的默认权限是777-umask    就是755
文件的默认权限是:666-umask  就是644

一般,普通用户的默认umask是0222,root用户的默认umask是0222

也就是说,对于普通用户来讲:
新建文件的权限是:666-002=664
新建目录的权限是:777-002=775

#例子:

#创建目录,默认权限为755
[root@ctos3 ~]# ls -ld /test1/
drwxr-xr-x 2 root root 6 Mar  9 02:09 /test1/

#创建文件,默认权限为644
[root@ctos3 ~]# touch 2.txt
[root@ctos3 ~]# ls -l 2.txt 
-rw-r--r-- 1 root root 0 Mar  9 02:11 2.txt
复制代码
#可以使用umask查看设置的umask值

[root@ctos3 ~]# umask
0022

#如果想要创建的文件权限多少,可以自己定义

[root@ctos3 ~]# umask 035  #设置默认umask为035,创建出来的文件默认权限为642
[root@ctos3 ~]# touch fil035
[root@ctos3 ~]# ls -l fil035 
-rw-r---w- 1 root root 0 Mar  9 02:25 fil035
#注意为什么是642,而不是631呢,因为是奇数的话就会加1,从之前默认权限6就会加到7,用7-3就是4,7-5就是2,0为偶数所以还是6,所以为642

[root@ctos3 ~]# umask 022  #设置022,创建文件的权限为644
[root@ctos3 ~]# touch fil022
[root@ctos3 ~]# ls -l fil022 
-rw-r--r-- 1 root root 0 Mar  9 02:25 fil022

#3.2特殊权限

linux系统基本权限位为9位权限,但还有额外3位权限位,共12位权限
  suid      用户对应的权限位
  sgid      用户组对应的权限位
  sticky    其他用户对应的权限位(只能是删除自己,不能删除别人的。。但是有个例外,就是文件所有者就可以删除)

与普通权限一样,特殊权限也可以使用数字方式表示
-SUID=4
-SGID-2
-Sticky=1

#设置特殊权限

#1.设置suid针对用户的)
命令格式:chmod 4755 file 或者 chmod u+s file

#例子:

[root@ctos3 ~]# which passwd
/usr/bin/passwd
[root@ctos3 ~]# ls -l `which passwd`
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd

[root@ctos3 ~]# chmod 4644 test.txt #设置test文件的s为权限
[root@ctos3 ~]# ls -l test.txt 
-rwSr--r-- 1 demo root 0 Mar  9 01:36 test.txt

#2.设置sgid(针对组的)
命令格式:chmod 2755 file 或者 chmod g+s file

#例子:

[root@ctos3 ~]# chmod 2755 /test
[root@ctos3 ~]# ls -ld /test
drwxr-sr-x 3 demo demo 16 Mar  9 01:55 /test

#3.设置sticky(可以将自己文件保护起来)
命令格式:chmod o+t file

#例子:

[root@ctos3 ~]# touch 3.txt
[root@ctos3 ~]# chmod o+t 3.txt 
[root@ctos3 ~]# ls -ld 3.txt 
-rw-r--r-T 1 root root 0 Mar  9 02:38 3.txt

#查找系统中设置了suid的文件

[root@ctos3 ~]# find /usr/bin/ -type f -perm 4755 -exec ls -l {} \;
-rwsr-xr-x. 1 root root 32008 Apr 11  2018 /usr/bin/fusermount
-rwsr-xr-x. 1 root root 64240 Nov  5  2016 /usr/bin/chage
-rwsr-xr-x. 1 root root 78216 Nov  5  2016 /usr/bin/gpasswd
-rwsr-xr-x. 1 root root 41776 Nov  5  2016 /usr/bin/newgrp
-rwsr-xr-x. 1 root root 44320 Apr 11  2018 /usr/bin/mount
-rwsr-xr-x. 1 root root 32184 Apr 11  2018 /usr/bin/su
-rwsr-xr-x. 1 root root 32048 Apr 11  2018 /usr/bin/umount
-rwsr-xr-x. 1 root root 27680 Apr 10  2018 /usr/bin/pkexec
-rwsr-xr-x. 1 root root 57576 Apr 10  2018 /usr/bin/crontab
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd

#有关suid和sgid总结

1.suid是针对命令和二进制程序的
2.suid作用是让普通用户以root(或其他)的用户角色运行只有root(或其他)账号才能运行的程序或命令,或程序命令对应本来没有权限操作的文件等
3.sgid与suid不同的是,sgid既可以针对文件也可以针对目录设置
4.sgid是针对用户组权限位的

4.查看和修改文件属性命令lsattr,chattr
#使用lsattr命令显示文件属性,使用chattr命令修改文件属性
#例子:

root@ctos3 ~]# mkdir attribute
[root@ctos3 ~]# cd attribute/
[root@ctos3 attribute]# echo "file attribution" > attribution.txt
[root@ctos3 attribute]# lsattr
---------------- ./attribution.txt
#根据上面操作。使用lsattr查看没有赋予任何属性,下面就使用chattr来为文件添加属性

[root@ctos3 attribute]# chattr +i attribution.txt 
[root@ctos3 attribute]# lsattr 
----i----------- ./attribution.txt

#提示:添加i属性到文件之后,即使是root用户也不能修改、删除文件,可以加a权限,但是添加了也不能删除文件,知道将这两个权限删除,才能删除修改文件

[root@ctos3 attribute]# chmod 655 attribution.txt 
chmod: changing permissions of ‘attribution.txt’: Operation not permitted
[root@ctos3 attribute]# rm attribution.txt 
rm: remove regular file ‘attribution.txt’? y
rm: cannot remove ‘attribution.txt’: Operation not permitted
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mumu_wangwei

主修"红尘道--红尘练心"

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值