前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >掌握Linux文件权限,看这篇就够了

掌握Linux文件权限,看这篇就够了

作者头像
老油条IT记
发布2020-09-04 16:57:35
1.9K0
发布2020-09-04 16:57:35
举报

作者:老油条IT记 公众号:老油条IT记

#前言 我们知道,无论什么东西,涉及到安全性的,比如文件、文件夹、磁盘(就如window系统的磁盘,我们就可以通过bitlocker技术将磁盘给加密锁起来)、服务器,等都需要设置权限管理,以保证安全性,接下来让我们来探讨以下Linux的文件权限。

1.权限概述

权限是操作系统用来限制对资源访问的机制,权限一般分为读、写、执行。系统中的每个文件都拥有特定的权限、所属用户及所属组,通过这样的机制来限制哪些用户、哪些组可以对特定文件进行什么样操作。

#Linux中权限基于UGO模型进行控制 u:代表user(用户) g:代表group(组) o:代表other(其他)

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

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

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

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

#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

#参数介绍 -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 用户.组 + 文件

#2.3.使用chmod命令修改文件/目录的权限 命令格式 chmod +模式 +文件

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

#示例 chmod u+rw test.txt #给所属用户权限位添加读写权限 chmod g+rw test.txt #给所属组权限位添加读写权限 chmod o+rw test.txt #给其他用户权限位添加读写权限 chmod u=rw test.txt #设置所属用户权限位的权限位读写 chmod a-x test.txt #所有权限为去掉执行权限

#修改权限2 命令chmod也支持以数字方式修改权限,三个权限分别由三个数字表示: r=4 w=2 x=1

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

语法: chmod 755 文件或文件夹名字

例: [root@centos7 ~]# touch test.txt [root@centos7 ~]# chmod 755 test.txt

3.默认权限

每一个终端都拥有一个umask属性,来确定新建文件、文件夹的默认权限

/etc/profile文件可以看到设置的umask值 if [ $UID -gt 199 ] && [ "/usr/bin/id -gn" = "/usr/bin/id -un" ]; then umask 002 else umask 022 fi #注释:如果UID大于199并且用户的组名和用户名一样,umask值为002,否则就为022 #注释:gt在shell脚本中是大于,id -gn:显示组名,id -un:显示用户名

#UID小于199并且用户的组名和用户名一样 目录创建的默认权限为777-umask 就是755 文件创建的默认权限为777-umask 就是644

#例子: #root用户创建文件,权限为644,uid为0 [root@centos7 ~]# touch test.txt [root@centos7 ~]# ls -l test.txt -rw-r--r--. 1 root root 0 Jul 13 00:30 test.txt

#切换到普通用户创建文件,权限为664,uid大于199 [root@centos7 ~]# su - test Last login: Mon Jul 13 00:07:25 EDT 2020 on pts/0 [test@centos7 ~]$ touch test1.txt [test@centos7 ~] ls -l test1.txt -rw-rw-r--. 1 test test 0 Jul 13 00:31 test1.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

4.特殊权限

Linux系统中的基本权限位为9位权限,加上3位特殊权限位,共12位权限 文件的特殊权限:suid,sgid,sticky

suid:是针对二进制可执行程序上的,对目录设置无效 suid作用:让普通用户可以以root(或其他)的用户角色运行只有root才能运行的程序或命令 suid数字表示为4,在文件所有者权限的第三位为小写的s,就代表拥有suid属性

sgid:既可以针对文件也可以针对目录设置 sgid作用:在设置了sgid权限的目录下建立文件时,新创建的文件的所属组会继承上级目录的所属组 sgid数字表示为2,在文件所属组权限的第三位为小写的s,就代表拥有sgid属性

sticky:设置sticky可以将自己的文件保护起来 sticky数字表示为1,在其他用户权限位的第三位为小写t #示例 [root@centos7 ~]# chmod o+t /data/ [root@centos7 ~]# ls -ld /data/ drwxr-xr-t. 2 root root 6 Jul 13 03:20 /data/

#查找系统中的有特殊权限位文件

[root@centos7 ~]# find /usr/bin -type f -perm 4755 -exec ls -l {} ; -rwsr-xr-x. 1 root root 32096 Oct 30 2018 /usr/bin/fusermount -rwsr-xr-x. 1 root root 73888 Aug 8 2019 /usr/bin/chage -rwsr-xr-x. 1 root root 78408 Aug 8 2019 /usr/bin/gpasswd -rwsr-xr-x. 1 root root 41936 Aug 8 2019 /usr/bin/newgrp -rwsr-xr-x. 1 root root 44264 Apr 1 00:51 /usr/bin/mount -rwsr-xr-x. 1 root root 32128 Apr 1 00:51 /usr/bin/su -rwsr-xr-x. 1 root root 31984 Apr 1 00:51 /usr/bin/umount -rwsr-xr-x. 1 root root 57656 Aug 8 2019 /usr/bin/crontab -rwsr-xr-x. 1 root root 23576 Apr 1 00:07 /usr/bin/pkexec -rwsr-xr-x. 1 root root 27856 Mar 31 23:57 /usr/bin/passwd

[root@centos7 ~]# find /usr/bin -type f -perm 2755 -exec ls -l {} ; -rwxr-sr-x. 1 root tty 19544 Apr 1 00:51 /usr/bin/write

[root@centos7 ~]# ls -ld /tmp/ drwxrwxrwt. 12 root root 4096 Jul 13 08:14 /tmp/

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

#例子: [root@centos7 ~]# useradd user1 [root@centos7 ~]# su - user1 [user1@centos7 ~]$ less /etc/shadow /etc/shadow: Permission denied

[user1@centos7 ~]$ su - root [root@centos7 ~]# chmod u+s /usr/bin/less #添加suid权限

[root@centos7 ~]# su - user1 Last login: Mon Jul 13 08:19:26 EDT 2020 on pts/0 [user1@centos7 ~]$ less /etc/shadow #可以查看 [root@centos7 ~]# ll /usr/bin/less -rwsr-xr-x. 1 root root 158240 Jul 30 2015 /usr/bin/less [root@centos7 ~]# chmod 4755 /usr/bin/less #相当于u+s

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

#例子: [root@centos7 ~]# mkdir test [root@centos7 ~]# ls -ld test drwxr-xr-x. 2 root root 6 Jul 13 08:28 test

[root@centos7 ~]# chmod g+s test #设置sgid权限 [root@centos7 ~]# ls -ld test drwxr-sr-x. 2 root root 6 Jul 13 08:28 test

[root@centos7 ~]# chgrp test1 test/ [root@centos7 ~]# ls -ld test/ drwxr-sr-x. 2 root test1 20 Jul 13 08:29 test/

[root@centos7 ~]# touch test/aa.txt #创建新文件的所属组会继承上级目录的 [root@centos7 ~]# ls -l test/aa.txt -rw-r--r--. 1 root test1 0 Jul 13 08:29 test/aa.txt

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

5.查看和修改文件属性命令lsattr,chattr

lsattr:显示文件属性 chattr:修改文件属性

参数: -a:只能追加内容, -i:不能被修改 +a :(append)只能追加内容,如echo “111” >> test.txt +i :(Immutable:不可改变) 系统不允许对这个文件进行任何的修改 -a:移除a参数 -i:移除i参数

#例子: 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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 老油条IT记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.权限概述
  • 2.修改文件所属用户、所属组
  • 3.默认权限
  • 4.特殊权限
  • 5.查看和修改文件属性命令lsattr,chattr
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档