当前位置:   article > 正文

linux基本权限_descend into write-protected directory

descend into write-protected directory

linux的基本权限

1. 权限修改命令chmod

2. 属主属组修改命令chwon

3. 基本权限设置案例

4. 实验


1.linux中的文件或目录的权限关联着用户以及组。

我们可以通过ls -l 命令来查看当前目录的文件权限:

[root@hjh ~]# ls -l
total 48
-rw-r--r--  1 root root 35159 Aug 29 14:09 man.txt

  • 1
  • 2
  • 3
  • 4

忽略第一位 (第一位代表文件格式类型 - 代表普通文件) r w - 前三位代表 属主(User),中三位 r - - 代表属组(Group), 后三位 r - - 代表其他用户(Other) ,第一个 root 代表文件的属主 , 第二个 root 代表文件的属组, r是 read的意思也就是 可读 ,w 代表write的意思就是可写,x 是Executable的意思也就是可执行

我们可以通过id 命令来查看当前用户的的信息来判断自己当前的基本信息

[root@hjh ~]# id 
uid=0(root) gid=0(root) groups=0(root)
  • 1
  • 2

权限解析

  1. 如上下如图所示其实我们在更改或者说是覆盖权限的时候我们其实都是用 数字代表权限 r = 4 ,w = 2 , x = 1, 只要记住这几个数字所有的方式就是轻松快速的更改。

权限对应表

  1. 我们要做的就是判断自己是属于哪一类:
if Permission == User:

  print("你是属主")

elif Permission  in the Group:

  print("你是属组的成员")

else:

  print("你是其他人")
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

4. 如果我们想让一个用户获得一个文件你想要的权限
  • 你可以让用户加入到那个某个拥有此文件你想要权限的组中(当然前提是存在这样的组)

  • 你可以修改文件本身所属主或者所属组(用chown命令),修改文件对外的权限(用chmod命令)

  1. 我们新开一个用户:
[hjh@hjh ~]$ id
uid=1000(hjh) gid=1000(hjh) groups=1000(hjh)

  • 1
  • 2
  • 3
	`	使用usermod命令的 -G选项将 `hjh` 加入到root组中:
  • 1

[root@hjh ~]# usermod -G root hjh
[root@hjh ~]# id hjh
uid=1000(hjh) gid=1000(hjh) groups=1000(hjh),0(root)

  • 1
  • 2
  • 3
  • 4
  • 5
		如上图可以看到hjh的额外组(groups)中增加了root ,也就是代表现在`hjh`获得了man.txt的属组的权限。
  • 1


1.权限修改命令chmod

方法一

方法二

chmod

[root@hjh test]# mkdir dir
[root@hjh test]# ls
dir
hjh test]# touch dir/file
[root@hjh test]# ll -d dir
drwxr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# ll -d dir/file
-rw-r--r-- 1 root root 0 Sep 15 16:58 dir/file

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

从上面的结果可以看出在新建一个文件时,文件夹所有一般都具有执行权限,而文件一般都没有执行权限,将上面的环境作为下面实验的环境。


方法一

修改权限:
chmod      ~~~~      [ugoa]      ~~~~      [+ - =]      ~~~~      [rwx]      ~~~~      filename
我们上面的环境做个实验,这里我们选择用户权限来做实验,其他同理。(特别说明=表示覆盖。

[root@hjh test]# ll -d dir
drwxr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod u=rx dir
[root@hjh test]# ll -d dir
dr-xr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod u+w dir
[root@hjh test]# ll -d dir
drwxr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod  u-w dir
[root@hjh test]# ll -d dir
dr-xr-xr-x 2 root root 18 Sep 15 16:58 dir
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这里要特殊说明的是修改权限是可以同时指定多用户的,下面我做出示例。

[root@hjh test]# ll -d dir
dr-xr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod  ug=rwx dir
[root@hjh test]# ll -d dir
drwxrwxr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod u=rwx,g=rx dir
[root@hjh test]# ll -d dir
drwxr-xr-x 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod u=rwx,g=rwx,o= dir
[root@hjh test]# ll -d dir
drwxrwx--- 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# chmod a=rwx dir
[root@hjh test]# ll -d dir
drwxrwxrwx 2 root root 18 Sep 15 16:58 dir
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

注:但是这些权限只针对普通用户,对root用户无效,下面示例:

[root@hjh test]#  chmod a= dir
[root@hjh test]# ll -d dir
d--------- 2 root root 18 Sep 15 16:58 dir
[root@hjh test]# ls dir/
file
[root@hjh test]# touch dir/test
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

对所有用户进行操作权限时你甚至不需声明,示例:

[root@hjh test]# ll -d dir
d--------- 2 root root 30 Sep 15 17:52 dir
[root@hjh test]# chmod a=rx dir
[root@hjh test]# ll -d dir
dr-xr-xr-x 2 root root 30 Sep 15 17:52 dir
[root@hjh test]# chmod = dir
[root@hjh test]# ll -d dir
d--------- 2 root root 30 Sep 15 17:52 dir
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

方法二

chmod nnn filename
第一个n: U
第二个n: G
第三个n: O
接下来我们做一个实验:
所属用户权限为 r-- 所属组为rx- 其他用户没权限 ---

[root@hjh test]# ll -d dir/file
-rw-r--r-- 1 root root 0 Sep 15 16:58 dir/file
[root@hjh test]# chmod 450 dir/file
[root@hjh test]# ll -d dir/file
-r--r-x--- 1 root root 0 Sep 15 16:58 dir/file
[root@hjh test]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

常见的组合
默认目录是755
文件是644

[root@hjh test]# mkdir test 
[root@hjh test]# ll -d test
drwxr-xr-x 2 root root 6 Sep 15 19:48 test
[root@hjh test]# touch test/test.txt
[root@hjh test]# ll -d test/test.txt
-rw-r--r-- 1 root root 0 Sep 15 19:49 test/test.txt
[root@hjh test]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

目录:755,750,700
文件 :644,640,600


chmod -R 选项的意思 递归的相=修改目录的权限

[root@hjh test]# ll -d dir
drwxrwxrwx 2 root root 30 Sep 15 17:52 dir
[root@hjh test]# ll -d dir/file
-rwxrwxrwx 1 root root 0 Sep 15 16:58 dir/file
[root@hjh test]# chmod -R = dir/
[root@hjh test]# ll -d dir
d--------- 2 root root 30 Sep 15 17:52 dir
[root@hjh test]# ll -d dir/file
---------- 1 root root 0 Sep 15 16:58 dir/file
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.属主属组修改命令chown

chown [user] . | : [group] [- R] filename

chown

[root@hjh test]# ll -d dir
d--------- 2 root root 30 Sep 15 17:52 dir
[root@hjh test]# chown hjh dir
[root@hjh test]# ll -d dir
d--------- 2 hjh root 30 Sep 15 17:52 dir
[root@hjh test]# chown .dba dir
[root@hjh test]# ll -d dir
d--------- 2 hjh dba 30 Sep 15 17:52 dir
[root@hjh test]# ll -d dir/
file  test  
[root@hjh test]# ll -d dir/file
---------- 1 root root 0 Sep 15 16:58 dir/file
[root@hjh test]# chown -R hjh.hjh dir/
[root@hjh test]# ll -d dir dir/file 
d--------- 2 hjh hjh 30 Sep 15 17:52 dir
---------- 1 hjh hjh  0 Sep 15 16:58 dir/file
[root@hjh test]# chown -R root:root dir/ 
[root@hjh test]# ll -d dir/ dir/file
d--------- 2 root root 30 Sep 15 17:52 dir/
---------- 1 root root  0 Sep 15 16:58 dir/file
[root@hjh test]# 

[root@hjh test]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3.基础权限设置案例

1.文件权限案例

2.目录权限案例

3.总结

![基本权限案例](https://img-blog.csdnimg.cn/20190916204132789.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2h1YW5qaW5naHVp,size_16,color_FFFFFF,

1.文件权限使用案例:

//默认文件其他用户仅有读权限
[root@hjh test]# echo "date" > date.txt
[root@hjh test]# ll -d date.txt 
-rw-r--r-- 1 root root 5 Sep 16 21:10 date.txt
//测试下权限
[hjh@hjh test]$  cat date.txt 
date
[hjh@hjh test]$ echo "test" > date.txt 
-bash: date.txt: Permission denied
[hjh@hjh test]$ /tmp/test/test/date.txt
-bash: /tmp/test/test/date.txt: Permission denied
//增加执行权限并测试
[root@hjh test]# chmod o+x date.txt 
[root@hjh test]# ll -d date.txt 
-rw-r--r-x 1 root root 5 Sep 16 21:11 date.txt
[hjh@hjh test]$ /tmp/test/test/date.txt 
Mon Sep 16 21:22:33 CST 2019
//增加写权限并测试
[root@hjh test]# chmod  o+w date.txt 
[root@hjh test]# ll -d date.txt 
-rw-r--rwx 1 root root 5 Sep 16 21:11 date.txt
[hjh@hjh test]$ echo "test" > date.txt 
[hjh@hjh test]$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

rwx对文件的影响

2.目录权限案例:

实战案例一:对目录没有 w 权限 ,对文件有 r w x

//搭建环境
[root@hjh test]# pwd
/tmp/test
[root@hjh test]# ll -d  /tmp/test test.txt 
-rw-r--r-- 1 root root  0 Sep 16 22:05 test.txt
drwxr-xr-x 2 root root 22 Sep 16 22:05 /tmp/test
[root@hjh test]# chmod 777 test.txt 
[root@hjh test]# ll -d test.txt 
-rwxrwxrwx 1 root root 0 Sep 16 22:05 test.txt
[root@hjh test]# echo "test" > test.txt
换普通用户实验
[hjh@hjh ~]$ cat /tmp/test/test.txt 
test
[hjh@hjh ~]$ rm -r /tmp/test/
rm: descend into write-protected directory ‘/tmp/test/’? y
rm: cannot remove ‘/tmp/test/test.txt’: Permission denied
[hjh@hjh ~]$ echo  "test" >> /tmp/test/test.txt 
[hjh@hjh ~]$ /tmp/test/test.txt 
[hjh@hjh ~]$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

实战案例二:对目录有 w, 对文件没有任何权限

//搭建实验环境
[root@hjh test]# chmod  o=w /tmp/test
[root@hjh test]# chmod 000 test.txt 
[root@hjh test]# ll -d /tmp/test/ test.txt 
---------- 1 root root 10 Sep 16 22:10 test.txt
drwxr-x-w- 2 root root 22 Sep 16 22:05 /tmp/test/


//用普通用户做实验
[hjh@hjh ~]$ cat /tmp/test/test.txt
cat: /tmp/test/test.txt: Permission denied
[hjh@hjh ~]$ cat /tmp/test/
cat: /tmp/test/: Permission denied
[hjh@hjh ~]$ cd /tmp/test/
-bash: cd: /tmp/test/: Permission denied
[hjh@hjh ~]$ touch /tmp/test/hjh.txt
touch: cannot touch ‘/tmp/test/hjh.txt’: Permission denied

// 注:这里你会发现你的写权限都不好使了
//我们切换回root加上x权限
[root@hjh test]# chmod  o=wx /tmp/test
//我们再切回普通用户
[hjh@hjh ~]$ touch /tmp/test/hjh.txt
[hjh@hjh ~]$ 
//用root用户查看文件是否新建成功
[root@hjh test]# ls
hjh.txt  test.txt
[hjh@hjh ~]$ rm -rf  /tmp/test/hjh.txt
[hjh@hjh ~]$ 

[root@hjh test]# chmod  o=rw  /tmp/test

[hjh@hjh test]$ ls  /tmp/test/
ls: cannot access /tmp/test/test.txt: Permission denied
test.txt


结论:从这里可以看出想要在一个目录中查看或者操作文件必须首先要进入这个目录,也即是要有X权限。
  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

在这里插入图片描述

3.总结

在这里插入图片描述


4.实验

在这里插入图片描述


  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/640310
推荐阅读
相关标签
  

闽ICP备14008679号