Linux权限总结

news/2024/5/3 3:46:07/文章来源:https://blog.csdn.net/m0_55283616/article/details/126249927

放弃不难,但坚持很酷,加油!希望此文对您有所帮助!

目录

  • shell运行原理---外壳程序
  • Linux权限的概念
    • 如何修改文件权限?
  • 常见权限问题(面试题)
    • 1.目录权限
    • 2.umask
    • 3.写权限的作用
    • 4.如何创建一个共享目录(粘滞位)
  • 关于权限的总结

shell运行原理—外壳程序

在这里插入图片描述

Linux权限的概念

Linux下有两种用户:超级用户(root)、普通用户。
超级用户:可以在linux系统做任何事情,不受限制。
普通用户:在linux下做有限的事情
超级用户的命令提示符是“#“,普通用户的命令提示符是”$"。
命令:su [用户名]
功能:切换用户。
例如,要从root用户切换到普通用户user,则使用su user。要从普通用户user切换到root用户则使用su root(root可以管理),此时系统会提示输入root用户的口令。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

如何修改文件权限?

改人
在这里插入图片描述

改属性 (ugo ± ,八进制方案)
在这里插入图片描述
在这里插入图片描述

常见权限问题(面试题)

1.目录权限

目录权限(在目录下操作,看的是此目录的相关权限)
进入一个目录,需要什么权限? x权限(易错)
查看目录下的文件列表:r
要在目录下创建文件或者目录:在这里插入图片描述

可执行权限:如果目录没有可执行权限,则无法cd到目录中。
可读权限:如果目录没有可读权限,则无法使用ls等命令查看目录中的文件内容。
可写权限:如果目录没有可写权限,则无法在目录中创建文件,也无法在目录中删除文件。

2.umask

umask:权限掩码
凡是在umask中出现的权限,都不应该在最终权限中出现!
在这里插入图片描述

为何我们创建一个目录或者文件,默认权限是你所看到的样子?(如上图所示)
1.目录起始权限是777,普通文件起始权限是666
2.最终权限 = 起始权限&(~umask)
在这里插入图片描述
虽然说默认的umask是0002,但是我们可以自己手动改,得到我们所需要的。
比如如果我们想让权限都为空,可以设置为umask 0777

在这里插入图片描述

3.写权限的作用

[jyf@VM-12-14-centos ~]$ cd lesson4
[jyf@VM-12-14-centos lesson4]$ ll
total 8
drwxrwxr-x 2 jyf jyf 4096 Sep 16 20:18 dir
d--------- 2 jyf jyf 4096 Sep 18 19:12 director
-rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
---------- 1 jyf jyf    0 Sep 18 19:11 test.cpp
[jyf@VM-12-14-centos lesson4]$ umask
0002
[jyf@VM-12-14-centos lesson4]$ chmod u-w dir //将目录dir的拥有者的写权限去掉
[jyf@VM-12-14-centos lesson4]$ ll
total 8
dr-xrwxr-x 2 jyf jyf 4096 Sep 16 20:18 dir
d--------- 2 jyf jyf 4096 Sep 18 19:12 director
-rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
---------- 1 jyf jyf    0 Sep 18 19:11 test.cpp
[jyf@VM-12-14-centos lesson4]$ cd dir
[jyf@VM-12-14-centos dir]$ touch other
touch: cannot touch ‘other’: Permission denied //文件所在的目录没有写权限,无法创建文件
[jyf@VM-12-14-centos dir]$ cd ..
[jyf@VM-12-14-centos lesson4]$ chmod u+w dir
[jyf@VM-12-14-centos lesson4]$ ll
total 8
drwxrwxr-x 2 jyf jyf 4096 Sep 16 20:18 dir
d--------- 2 jyf jyf 4096 Sep 18 19:12 director
-rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
---------- 1 jyf jyf    0 Sep 18 19:11 test.cpp
[jyf@VM-12-14-centos lesson4]$ cd dir
[jyf@VM-12-14-centos dir]$ touch test.c
[jyf@VM-12-14-centos dir]$ ll
total 0
-rw-rw-r-- 1 jyf jyf 0 Sep 27 19:43 test.c
[jyf@VM-12-14-centos dir]$ touch file.txt
[jyf@VM-12-14-centos dir]$ ll
total 0
-rw-rw-r-- 1 jyf jyf 0 Sep 27 19:51 file.txt
-rw-rw-r-- 1 jyf jyf 0 Sep 27 19:43 test.c
[jyf@VM-12-14-centos dir]$ cd ..
[jyf@VM-12-14-centos lesson4]$ chmod u-w dir
[jyf@VM-12-14-centos lesson4]$ cd dir
[jyf@VM-12-14-centos dir]$ rm test.c
rm: cannot remove ‘test.c’: Permission denied //文件所在的目录没有写权限,无法删除文件
[jyf@VM-12-14-centos dir]$ rm -f test.c
rm: cannot remove ‘test.c’: Permission denied
[jyf@VM-12-14-centos dir]$ cd ..
[jyf@VM-12-14-centos lesson4]$ rm dir
rm: cannot remove ‘dir’: Is a directory
[jyf@VM-12-14-centos lesson4]$ rm -rf dir //目录没有写权限,无法删除目录
rm: cannot remove ‘dir/test.c’: Permission denied
rm: cannot remove ‘dir/file.txt’: Permission denied
[jyf@VM-12-14-centos lesson4]$ ll
total 8
dr-xrwxr-x 2 jyf jyf 4096 Sep 27 19:51 dir
d--------- 2 jyf jyf 4096 Sep 18 19:12 director
-rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
---------- 1 jyf jyf    0 Sep 18 19:11 test.cpp
[jyf@VM-12-14-centos lesson4]$ 

4.如何创建一个共享目录(粘滞位)

可执行权限:如果目录没有可执行权限,则无法cd到目录中。
可读权限:如果目录没有可读权限,则无法使用ls等命令查看目录中的文件内容。
可写权限:如果目录没有可写权限,则无法在目录中创建文件,也无法在目录中删除文件。

于是问题来了,换句话说,就是只要用户拥有目录的写权限,用户就可以删除目录中的文件,而不论这个用户是否有这个文件的写权限。
这好像不太科学啊,就像我张三创建的一个文件,凭什么可以被你李四删除?我们用下面的过程印证一下。

[jyf@VM-12-14-centos lesson4]$ cd /
[jyf@VM-12-14-centos /]$ ll
total 76
lrwxrwxrwx.   1 root root     7 Mar  7  2019 bin -> usr/bin
dr-xr-xr-x.   5 root root  4096 Jul 28 11:37 boot
drwxr-xr-x    2 root root  4096 Nov  5  2019 data
drwxr-xr-x   19 root root  3020 Aug 26 08:05 dev
drwxr-xr-x.  95 root root 12288 Sep 27 22:23 etc
drwxr-xr-x.   5 root root  4096 Sep 27 20:57 home
lrwxrwxrwx.   1 root root     7 Mar  7  2019 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Mar  7  2019 lib64 -> usr/lib64
drwx------.   2 root root 16384 Mar  7  2019 lost+found
drwxr-xr-x.   2 root root  4096 Apr 11  2018 media
drwxr-xr-x.   2 root root  4096 Apr 11  2018 mnt
drwxrwxrwx    2 root root  4096 Sep 27 22:34 mytemp
drwxr-xr-x.   4 root root  4096 Aug 26 08:03 opt
dr-xr-xr-x  102 root root     0 Aug 26 08:05 proc
dr-xr-x---.   7 root root  4096 Sep 27 22:24 root
drwxr-xr-x   25 root root   880 Aug 26 08:06 run
lrwxrwxrwx.   1 root root     8 Mar  7  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x   13 root root     0 Sep 27 20:28 sys
drwxrwxrwt.   8 root root  4096 Sep 28 03:24 tmp
drwxr-xr-x.  14 root root  4096 Jan  8  2021 usr
drwxr-xr-x.  20 root root  4096 Jan  8  2021 var
[jyf@VM-12-14-centos /]$ cd mytemp
[jyf@VM-12-14-centos mytemp]$ ll
total 0
-rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf1
-rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf2
-rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf3
-rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj1
-rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj2
-rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj3
[jyf@VM-12-14-centos mytemp]$ su
Password: 
[root@VM-12-14-centos mytemp]# su yfj
[yfj@VM-12-14-centos mytemp]$ whoami
yfj
[yfj@VM-12-14-centos mytemp]$ cat jyf1
cat: jyf1: Permission denied
[yfj@VM-12-14-centos mytemp]$ ./jyf2
bash: ./jyf2: Permission denied
[yfj@VM-12-14-centos mytemp]$ echo "hello world">jyf1
bash: jyf1: Permission denied
[yfj@VM-12-14-centos mytemp]$ rm jyf1
rm: remove write-protected regular empty file ‘jyf1’? y
[yfj@VM-12-14-centos mytemp]$ ll
total 0
-rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf2
-rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf3
-rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj1
-rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj2
-rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj3
[yfj@VM-12-14-centos mytemp]$ ll ../mytemp -d
drwxrwxrwx 2 root root 4096 Sep 28 20:39 ../mytemp

从上述可以看出用户yfj在目录myemp中对用户jyf的文件不能读,不能写,不能执行这个文件,但是,它却可以删除,因为这个文件所在的目录的给所有人都开发了写权限,如果这个作为共享目录,不就乱套了吗?

共享目录需要满足的条件(共享目录一般由超级管理员root在其根目录下创建,为多个用户所共享)

1.当多个用户共享一个目录,需要在该目录下,进行读写,创建删除文件
2.但是自己只能删除自己的,而不能删除别人的(w:可以互删,但是不满足条件)

解决方法:给目录设置粘滞位,一般由谁(root)设置谁才能取消。
当一个目录设置为“粘滞位”(用chmod + t),则该目录下的文件只能由
1.超级管理员删除 2.该目录的所有者删除 3.该文件所有者删除

[root@VM-12-14-centos mytemp]# chmod +t mytemp //**给mytemp设置粘滞位**
[root@VM-12-14-centos /]# ll
total 76
lrwxrwxrwx.   1 root root     7 Mar  7  2019 bin -> usr/bin
dr-xr-xr-x.   5 root root  4096 Jul 28 11:37 boot
drwxr-xr-x    2 root root  4096 Nov  5  2019 data
drwxr-xr-x   19 root root  3020 Aug 26 08:05 dev
drwxr-xr-x.  95 root root 12288 Sep 27 22:23 etc
drwxr-xr-x.   5 root root  4096 Sep 27 20:57 home
lrwxrwxrwx.   1 root root     7 Mar  7  2019 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Mar  7  2019 lib64 -> usr/lib64
drwx------.   2 root root 16384 Mar  7  2019 lost+found
drwxr-xr-x.   2 root root  4096 Apr 11  2018 media
drwxr-xr-x.   2 root root  4096 Apr 11  2018 mnt
drwxrwxrwt    2 root root  4096 Sep 28 20:39 mytemp
drwxr-xr-x.   4 root root  4096 Aug 26 08:03 opt
dr-xr-xr-x  113 root root     0 Aug 26 08:05 proc
dr-xr-x---.   7 root root  4096 Sep 27 22:24 root
drwxr-xr-x   25 root root   880 Aug 26 08:06 run
lrwxrwxrwx.   1 root root     8 Mar  7  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x   13 root root     0 Sep 27 20:28 sys
drwxrwxrwt.   8 root root  4096 Sep 28 03:24 tmp
drwxr-xr-x.  14 root root  4096 Jan  8  2021 usr
drwxr-xr-x.  20 root root  4096 Jan  8  2021 var
[jyf@VM-12-14-centos mytemp]$ rm yfj1
rm: remove write-protected regular empty file ‘yfj1’? y
rm: cannot remove ‘yfj1’: Operation not permitted
[jyf@VM-12-14-centos mytemp]$ touch jyf1
[jyf@VM-12-14-centos mytemp]$ ll
total 0
-rw-rw-r-- 1 jyf jyf 0 Sep 28 20:45 jyf1
-rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf2
-rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf3
-rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj1
-rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj2
-rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj3

关于权限的总结

目录的可执行权限是表示你可否在目录下执行命令
如果目录没有x权限,则无法对目录执行任何命令,甚至无法cd进入目录,即使目录仍然有 -r读权限(这个地方很容易犯错,认为有读权限就可以进入目录读取目录下的文件)
而如果目录具有x权限,但没有 r 权限,则用户可以执行命令,可以cd进入目录。但由于没有目录的读权限。所在目录下,即使可以执行ls命令,但仍然没有权限读出目录下的文档。

[jyf@VM-12-14-centos lesson4]$ ll
total 4
drwxrwxr-x 2 jyf jyf 4096 Sep 28 21:56 dir
-rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
[jyf@VM-12-14-centos lesson4]$ chmod u-r dir
[jyf@VM-12-14-centos lesson4]$ ls
dir  test.c
[jyf@VM-12-14-centos lesson4]$ cd dir
[jyf@VM-12-14-centos dir]$ ll
ls: cannot open directory .: Permission denied

linux权限的总结就到这里了,我们下次再见!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_15718.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

机器学习在分子模拟中的应用

文章目录一、背景二、分子动力学模拟介绍简史基本步骤应用发展方向三、AlphaFold**前言****Alphafold2做了什么改进****Alphafold2会议具体细节:****Alphafold为什么强?****学术问题:**四、TorchMD五、Deep Potential**阶段一:对已…

TCP/IP网络编程:P3->地址族与数据序列

本系列文章为《TCP/IP网络编程----尹圣雨》学习笔记,前面的系列文章链接如下 TCP/IP网络编程:P1->理解网络编程和套接字 TCP/IP网络编程:P2->套接字类型与协议设置 文章目录前言一、分配给套接字的IP地址与端口号1.1 网…

CSP2022 J/S 游寄

9.18 A.m. 自己学校考,但只能睡到7点不到,就很无语。 来了好多同学,关系也不错,聊了一会天就去考试了。 至于考试没什么好说的,J也就那样。 P.m. 上午对了一下答案,貌似 \(92\) ? 中午机房太吵了,没怎么睡就去考试了。 考什么大家应该都有数,宇宙射线?秒表?做尼玛呢…

卷积神经网络的应用实例,卷积神经网络可解释性

神经网络激活函数与损失函数的作用 谷歌人工智能写作项目:神经网络伪原创 深度学习之损失函数与激活函数的选择 深度学习之损失函数与激活函数的选择在深度神经网络(DNN)反向传播算法(BP)中,我们对DNN的前向反向传播算法的使用做…

单片机原理与应用以及C51编程技术——硬件体系结构梳理

文章目录一、单片机的结构原理1.1 主要性能和特点1.2 内部框图1.3 CPU1.3.1 运算器1.3.2 控制器1.4 几个主要的特殊功能寄存器SFR说明1.4.1 程序指针PC1.4.2 累加器A1.4.3 寄存器B1.4.4 数据指针DPTR1.4.5 程序状态字PSW介绍例子1.4.6 堆栈指针SP堆栈的介绍堆栈的作用堆栈操作的…

Verilog学习笔记

sky视频笔记:数字逻辑回顾&Hello World_哔哩哔哩_bilibili 一、数电基础 1.组合逻辑 电路逻辑输出值只和当前的输入有关比如:AND/OR/XOR/NAND/NOR/MUX/Adder/Multiplier 2.时序逻辑 电路逻辑输出值跟当前的输入和电路的当前状态有关保存当前状态的…

用Python生成Hilbert矩阵

代码放在了最后,前面是解题思路 目录 1.什么是Hilbert矩阵矩阵: 2.找规律 1.第一种思路:先从值出发(找规律) 2.第二种思路:先从下标索引出发(找规律) 三、代码展示 四、输出展…

WPF 界面打不开提示 System.ArithmeticException Overflow or underflow in the arithmetic operation 异常

本文告诉大家如何解决界面打不开,抛出 System.ArithmeticException: Overflow or underflow in the arithmetic operation 异常的修复方法本文告诉大家如何解决界面打不开,抛出 System.ArithmeticException: Overflow or underflow in the arithmetic operation 异常的修复方…

某IOT设备漏洞分析

申明:本文章所分享内容仅用于网络安全技术讨论,切勿用于违法途径,所有渗透都需获取授权,违者后果自行承担,与本文及作者无关,请谨记守法. 设备名称: DLINK DIR-818l 固件包: d-link DIR818L_FW105b01 A1 环…

Rust学习笔记:简单练习

最近一个月比较闲,忙碌大半年终于有自己短暂的休息时间。如果不写大的程序,偶尔写写一些小东西,其实用起来也很自在,前提是如果没有太多生活压力。看电视变成奢侈的事情。有时候,我会羡慕老外享受福利待遇非常好&#…

javaweb|JSTL的下载、配置与原理,解决uri导入时报错的问题

今天在配置jstl时,发现在引入uri时出现了问题,地址直接报红。在尝试了几种方法后,最后成功解决了。 JSTL一、下载1、https://tomcat.apache.org/2、找到taglib标准库3、下载前2个jar包二、配置1、放入web-inf的lib文件夹里2、将Jar包放入tomc…

windows下 解决PHP-CGI 进程崩溃502

PHP是世界上最好的语言,但需要PHP解析器;Apachephp,需要通过mod_php.so和php相连;nginxphp 需要转发给 cgi程序 关于FastCGI: 全称 FastCGI Process Manager,是一种进程管理器,管理 cgi,市面上…

1474_AURIX TC275 WDT的运行模式

全部学习汇总: GreyZhang/g_TC275: happy hacking for TC275! (github.com) 1. 前面的内容中其实已经看到了,这个看门狗的时钟其实是固定的,SPB的时钟。这样,后面了解时钟树的时候需要注意一下。其实,在功能安全的失效…

ES6--》一文搞懂JS中的Promise

目录 Promise Promise使用 Promise封装Ajax请求 Promise封装读取文件 Promise.prototype.then 方法 Promise多文件读取 Promise.prototype.catch() Promise.prototype.finally() Promise.all() Promise.race() Promise.allSettled() Pomise.any() Promise.resolve…

微信小程序开发入门与实战(Behaviors使用)

作者 : SYFStrive 博客首页 : HomePage 📜: 微信小程序 📌:个人社区(欢迎大佬们加入) 👉:社区链接🔗 📌:觉得文章不错可以点点关注 &#x1f4…

【原创】基于JavaWeb的社区疫情防控管理系统(疫情防控管理系统毕业设计)

项目介绍:后端采用JspServlet。前端使用的是Bootstrap的一个网站模板。开发一个在线的社区疫情防控管理系统。从角色的划分,包括用户、社管员、管理员。功能模块上包括了社区公告发布、高风险地区记录、地区感染信息管理、社区出入登记管理、行程信息管理…

关于maven生命周期的理解

晚上有点无聊,看到了一些东西引发了自己的思路,就想将maven的一些东西总结总结,有从网上抄的,也有自己的思路。 一、生命周期是指什么(lifecycle) Maven的生命周期就是对所有的构建过程进行抽象和统一。包…

Posix与System V IPC

Posix与System V IPC一、Posix IPC1.概述2.IPC名字3.px_ipc_name函数3.创建与打开IPC通道4.IPC权限二、System V IPC1.概述2.key_t键和ftok函数3.ipc_perm结构4.创建与打开IPC通道5.IPC权限6.标识符重用7.ipcs和ipcrm程序8.内核限制一、Posix IPC 1.概述 三种类型的IPC合称为…

Redux的基本使用过程详解

文章目录Redux的使用过程Redux测试项目的搭建Redux的基本使用步骤Redux目录的结构划分React的三大原则Redux的使用过程 Redux测试项目的搭建 1.创建一个新的项目文件夹:learn-redux # 执行初始化操作 npm init -y或yarn init -y # 安装redux:npm install redux --save或yarn …

自定义View 布局过程(Layout)

目录一、作用二、layout过程详解2.1单一View的layout过程具体使用具体流程源码分析总结2.2ViewGroup的layout过程具体使用具体流程源码分析总结三、细节问题:getWidth() ( getHeight())与 getMeasuredWidth() (getMeasuredHeight(…