linux基本权限

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

忽略第一位 (第一位代表文件格式类型 - 代表普通文件) 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. 如上下如图所示其实我们在更改或者说是覆盖权限的时候我们其实都是用 数字代表权限 r = 4 ,w = 2 , x = 1, 只要记住这几个数字所有的方式就是轻松快速的更改。

权限对应表

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

  print("你是属主")

elif Permission  in the Group:

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

else:

  print("你是其他人")
  

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

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

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

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

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

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


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

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


方法一

修改权限:
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

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

[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

注:但是这些权限只针对普通用户,对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

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

[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

方法二

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]# 

常见的组合
默认目录是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]# 

目录: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

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]# 

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]$ 

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 ~]$ 

实战案例二:对目录有 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权限。
  

在这里插入图片描述

3.总结

在这里插入图片描述


4.实验

在这里插入图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值