玩转并理解linux中的文件/目录的rwx权限

玩转并理解linux中的文件/目录的rwx权限linux是一个相对安全的系统,其中的权限更是无处不在。在本文中,我们来谈谈linux中的文件/目录的rwx权限。为了简便起见,我们仅仅以文件owner的rwx为例。一.文件的rwx权限分别是什么意思?1.r权限:可读权限,验证如下:[taoge@localhostlearn_c]$ls-ltotal0[t

大家好,又见面了,我是你们的朋友全栈君。

 

        linux是一个相对安全的系统, 其中的权限更是无处不在。 在本文中, 我们来谈谈linux中的文件/目录的rwx权限。 为了简便起见, 我们仅仅以文件owner的rwx为例。

 

        一. 文件的rwx权限分别是什么意思?

         1. r权限:可读权限, 验证如下:

 

 [taoge@localhost learn_c]$ ls -l

total 0

[taoge@localhost learn_c]$ echo hello > a.txt

[taoge@localhost learn_c]$ ls -l

total 4

-rw-rw-r– 1 taoge taoge 6 May  6 03:51 a.txt

[taoge@localhost learn_c]$ chmod 000 a.txt 

[taoge@localhost learn_c]$ ls -l

total 4

———- 1 taoge taoge 6 May  6 03:51 a.txt

[taoge@localhost learn_c]$ cat a.txt 

cat: a.txt: Permission denied

[taoge@localhost learn_c]$ chmod u+r a.txt 

[taoge@localhost learn_c]$ ls -l

total 4

-r——– 1 taoge taoge 6 May  6 03:51 a.txt

[taoge@localhost learn_c]$ cat a.txt

hello

[taoge@localhost learn_c]$ 

 

 

 

         2. w权限: 可写权限, 验证如下:

 

 [taoge@localhost learn_c]$ ls -l

total 0

[taoge@localhost learn_c]$ touch a.txt

[taoge@localhost learn_c]$ ls -l

total 0

-rw-rw-r– 1 taoge taoge 0 May  6 03:56 a.txt

[taoge@localhost learn_c]$ chmod 000 a.txt

[taoge@localhost learn_c]$ ls -l

total 0

———- 1 taoge taoge 0 May  6 03:56 a.txt

[taoge@localhost learn_c]$ chmod u+w a.txt

[taoge@localhost learn_c]$ ls -l

total 0

–w——- 1 taoge taoge 0 May  6 03:56 a.txt

[taoge@localhost learn_c]$ echo hello > a.txt

[taoge@localhost learn_c]$ cat a.txt

cat: a.txt: Permission denied

[taoge@localhost learn_c]$ chmod u+r a.txt

[taoge@localhost learn_c]$ cat a.txt

hello

[taoge@localhost learn_c]$ 

 

 

 

     3. x权限:可执行权限, 验证如下:

 

 [taoge@localhost learn_c]$ ls -l

total 4

-rw-rw-r– 1 taoge taoge 65 May  6 04:02 test.c

[taoge@localhost learn_c]$ cat test.c 

#include <stdio.h>

int main()

{

printf(“good\n”);

return 0;

}

[taoge@localhost learn_c]$ gcc test.c 

[taoge@localhost learn_c]$ ls -l

total 12

-rwxrwxr-x 1 taoge taoge 4638 May  6 04:04 a.out

-rw-rw-r– 1 taoge taoge   65 May  6 04:02 test.c

[taoge@localhost learn_c]$ ./a.out 

good

[taoge@localhost learn_c]$ chmod 000 a.out 

[taoge@localhost learn_c]$ ./a.out

bash: ./a.out: Permission denied

[taoge@localhost learn_c]$ chmod u+x a.out 

[taoge@localhost learn_c]$ ./a.out 

good

[taoge@localhost learn_c]$ 

 
 
        

       二. 目录的rwx权限分别是什么意思?

        1. r权限:可读权限(可列举查看目录下的内容), 验证如下:

 

[taoge@localhost learn_c]$ ls -l

total 0 
[taoge@localhost learn_c]$ mkdir test

[taoge@localhost learn_c]$ ls -l

total 4

drwxrwxr-x 2 taoge taoge 4096 May  6 04:07 test

[taoge@localhost learn_c]$ touch ./test/a.txt

[taoge@localhost learn_c]$ ls ./test/

a.txt

[taoge@localhost learn_c]$ chmod u-r test/

[taoge@localhost learn_c]$ ls ./test/

ls: cannot open directory ./test/: Permission denied

[taoge@localhost learn_c]$ 

 

 

      2. w权限:可写权限(可以往目录中写东东, 比如文件), 验证如下:

 

 [taoge@localhost learn_c]$ ls -l

total 0

[taoge@localhost learn_c]$ mkdir test

[taoge@localhost learn_c]$ ls -l

total 4

drwxrwxr-x 2 taoge taoge 4096 May  6 04:13 test

[taoge@localhost learn_c]$ touch ./test/a.txt

[taoge@localhost learn_c]$ chmod u-w test

[taoge@localhost learn_c]$ touch ./test/b.txt

touch: cannot touch `./test/b.txt’: Permission denied

[taoge@localhost learn_c]$ 

 

 

     3. x权限: 可执行权限(可以cd进去), 验证如下:

 

 

 [taoge@localhost learn_c]$ ls -l

total 0

[taoge@localhost learn_c]$ mkdir test

[taoge@localhost learn_c]$ ls -l

total 4

drwxrwxr-x 2 taoge taoge 4096 May  6 04:17 test

[taoge@localhost learn_c]$ cd test/

[taoge@localhost test]$ cd –

/home/taoge/Desktop/learn_c

[taoge@localhost learn_c]$ chmod u-x test/

[taoge@localhost learn_c]$ cd test/

bash: cd: test/: Permission denied

[taoge@localhost learn_c]$ 
 
      

       好,最后我们再来看一个问题:在某目录test中创建一个文件或者删除一个文件, 需要test目录具备什么权限呢? 答曰:需要目录test具备wx权限, 验证如下:

 

 

 [taoge@localhost learn_c]$ ls -l

total 0

[taoge@localhost learn_c]$ mkdir test

[taoge@localhost learn_c]$ touch ./test/a.txt ./test/b.txt ./test/c.txt ./test/d.txt

[taoge@localhost learn_c]$ ls -l

total 4

drwxrwxr-x 2 taoge taoge 4096 May  6 04:33 test

[taoge@localhost learn_c]$ chmod u-r test/

[taoge@localhost learn_c]$ touch ./test/e.txt

[taoge@localhost learn_c]$ chmod u-w test/

[taoge@localhost learn_c]$ touch ./test/f.txt

touch: cannot touch `./test/f.txt’: Permission denied

[taoge@localhost learn_c]$ rm ./test/a.txt

rm: cannot remove `./test/a.txt’: Permission denied

[taoge@localhost learn_c]$ chmod u+w test/

[taoge@localhost learn_c]$ chmod u-x test/

[taoge@localhost learn_c]$ touch ./test/f.txt

touch: cannot touch `./test/f.txt’: Permission denied

[taoge@localhost learn_c]$ rm ./test/a.txt

rm: cannot remove `./test/a.txt’: Permission denied

[taoge@localhost learn_c]$ chmod u+x test/

[taoge@localhost learn_c]$ 

 

 

       因此, 如果某一目录test删除不掉, 很可能是因为其中有不可删除的文件, 从本质上来讲, 就是test自己没有wx权限了。

 

 

       好, 本文先闲谈到这里。

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139708.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java string简单例子_javaStringBuilder类的详解及简单实例

    java string简单例子_javaStringBuilder类的详解及简单实例javaStringBuilder类的详解及简单实例实现代码:publicclassStringBuilderTest{/***@paramargs*/publicstaticvoidmain(String[]args){StringBuildersb=newStringBuilder();//追加字符串sb.append(“java”);//sb=”java”…

    2022年7月17日
    18
  • WebService 实例应用

    WebService 实例应用两个工程分别部署在两台电脑上:webservice_client客户端  webservice_server:服务器端先说服务器导入jar包改写xml文件:cxfcom.rainspnsor.webservice.CXFNonSpringServiceImpl0cxf/services/*然后创建类:1util中:

    2022年7月21日
    14
  • git每次push和pull都要输入密码

    git每次push和pull都要输入密码

    2022年2月18日
    39
  • 动态代理

    动态代理

    2021年7月20日
    53
  • networkmanager和network区别_network graph

    networkmanager和network区别_network graph一、NetworkManager做了什么:NetworkManager确保网络连接正常。当检测到系统中没有网络配置但有网络设备时,NetworkManager会创建临时连接以提供连接。通过不同的工具(GUI,nmtui,nmcli)提供管理。NetworkManager可以配置网络别名,IP地址,静态路由,DNS信息和VPN连接,以及许多特定于连接的参数。重新启动后保持设备状态,并接管重新启动期间设置为受管模式的接口。未明确设置为不受管理但由用户或其他网络服务手动控制的设备。可以和netwo

    2022年9月1日
    2
  • android自定义toast样式_android设置对话框宽度

    android自定义toast样式_android设置对话框宽度在一般的android开发中我们一般弹出一些提示信息,例如已打开蓝牙,wifi之类的提示,我们都是会选择Toast进行弹出。今天我们的客户提出们应用弹出提示太小,用户不注意的情况下,容易被忽略掉,要弹出的宽度填充整个屏幕,首先想到是不是需要自定义Toast,经过自己的一番研究后,发现不需要自定Toast,用现有的Toast就可以轻松实现了。publicvoidshowToast(Cont

    2022年9月13日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号