linux系统中文件、目录的权限

1、linux系统中一共有七种文件类型,参考:https://www.cnblogs.com/songgj/p/8890710.html

linux一共有7种文件类型,分别如下:

            -:普通文件

           d:目录文件
           l: 软链接(类似Windows的快捷方式)
          (下面四种是特殊文件)
           b:块设备文件(例如硬盘、光驱等)
           p:管道文件
           c:字符设备文件(例如猫等串口设备)
           s:套接口文件/数据接口文件(例如启动一个MySql服务器时会产生一个mysql.sock文件)

 

2、创建测试数据,linux中可以使用ll或者ls -l或者 ls -h获取文件的基本信息

[root@linuxprobe test]# touch a.txt b.txt;mkdir test1 test2
[root@linuxprobe test]# ln -s a.txt a.link;ln -s b.txt b.link
[root@linuxprobe test]# ll
total 0
lrwxrwxrwx. 1 root root 5 Oct 19 23:13 a.link -> a.txt
-rw-r--r--. 1 root root 0 Oct 19 23:13 a.txt
lrwxrwxrwx. 1 root root 5 Oct 19 23:13 b.link -> b.txt
-rw-r--r--. 1 root root 0 Oct 19 23:13 b.txt
drwxr-xr-x. 2 root root 6 Oct 19 23:13 test1
drwxr-xr-x. 2 root root 6 Oct 19 23:13 test2

文件列表信息分为:文件类型、权限、链接数、所属用户、所属用户组、文件大小、最后修改时间、文件名。

其中文件的权限从 文件信息的左侧的第二个字符开始,依次向后每三个字符为一组,共三组,分别代表所有者、所属组、其他人的权限。

以a.txt文件为例:

所有者(u表示)权限:rw-

所属组(g表示)权限:r--

其他人(o表示)权限:r--

在权限中r表示可读,位于三个字符最左侧,用4表示;

在权限中w表示可写,位于三个字符最中间,用2表示;

在权限中x表示可执行,位于三个字符的最右侧,用1表示。

文件权限对应关系

       

3、r可读,表示可以打开

[root@linuxprobe test]# echo -e "abcd\nefgh" > a.txt  ## 向 a.txt中写入部分数据测试
[root@linuxprobe test]# ll a.txt ## 查看权限,所有者,所属组,其他人均有读的权限
-rw-r--r--. 1 root root 10 Oct 19 23:27 a.txt
[root@linuxprobe test]# chmod o-r a.txt  ## 使用chmod 命令减去其他人读的权限,o表示其他(other)
[root@linuxprobe test]# ll a.txt  ##重新查看权限,其他已经没有读的权限
-rw-r-----. 1 root root 10 Oct 19 23:27 a.txt
[root@linuxprobe test]# whoami  
root
[root@linuxprobe test]# cat a.txt  ## 所有这有读的权限
abcd
efgh
[root@linuxprobe test]# su - linuxprobe  ## 切换至普通用户
Last login: Mon Oct 19 23:26:09 CST 2020 on pts/1
[linuxprobe@linuxprobe ~]$ cd /home/test/
[linuxprobe@linuxprobe test]$ ll a.txt  ## 再次查看权限
-rw-r-----. 1 root root 10 Oct 19 23:27 a.txt
[linuxprobe@linuxprobe test]$ whoami
linuxprobe
[linuxprobe@linuxprobe test]$ cat a.txt  ## 没有可读的权限
cat: a.txt: Permission denied

 

4、w表示写的权限,可以对文件进行修改,增减

[root@linuxprobe test]# ll a.txt  ## 查看当前权限
-rw-r-----. 1 root root 10 Oct 19 23:27 a.txt
[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# echo -e "xxxx\nyyyy\nzzzz" >> a.txt  ## 向a.txt文件中追加部分内容
[root@linuxprobe test]# cat a.txt
abcd
efgh
xxxx
yyyy
zzzz
[root@linuxprobe test]# su - linuxprobe ## 切换至普通用户
Last login: Mon Oct 19 23:33:24 CST 2020 on pts/1
[linuxprobe@linuxprobe ~]$ cd /home/test/
[linuxprobe@linuxprobe test]$ ll a.txt ##再次查看权限
-rw-r-----. 1 root root 25 Oct 19 23:33 a.txt
[linuxprobe@linuxprobe test]$ whoami
linuxprobe
[linuxprobe@linuxprobe test]$ echo -e "xxxx\nyyyy\nzzzz" >> a.txt  ## 没有写的权限,追加失败
-bash: a.txt: Permission denied

 

5、x 表示可执行的权限

[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# ll test.sh  ## 查看权限
-rw-rw-r--. 1 linuxprobe linuxprobe 36 Oct 19 23:38 test.sh
[root@linuxprobe test]# cat test.sh
#!/bin/bash
echo "hello world!"
pwd
[root@linuxprobe test]# bash test.sh ## 利用bash命令直接执行
hello world!
/home/test
[root@linuxprobe test]# ./test.sh  ## 没有执行权限
bash: ./test.sh: Permission denied
[root@linuxprobe test]# chmod u+x test.sh ## 给所有至赋予执行权限,能够直接执行
[root@linuxprobe test]# ./test.sh
hello world!
/home/test

 

6、权限的增减,以a.txt为例

[root@linuxprobe test]# ll a.txt  ## 查看权限
-rw-r-----. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod u+x a.txt ## 所有者增加执行权限
[root@linuxprobe test]# ll a.txt
-rwxr-----. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod g+x a.txt  ## 所属组增加执行权限
[root@linuxprobe test]# ll a.txt
-rwxr-x---. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod o+x a.txt  ##其他人增加执行权限
[root@linuxprobe test]# ll a.txt
-rwxr-x--x. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod o-x a.txt  ## 其他人减去执行权限
[root@linuxprobe test]# ll a.txt
-rwxr-x---. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod u-r a.txt  ## 所有者减去读的权限
[root@linuxprobe test]# ll a.txt
--wxr-x---. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod a=x a.txt  ## 所有人均为执行权限(a=all)
[root@linuxprobe test]# ll a.txt
---x--x--x. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod a=rw a.txt  ## 所有人为读和写的权限
[root@linuxprobe test]# ll a.txt
-rw-rw-rw-. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod 700 a.txt  ##所有者权限为7,所属组和其他人均为0(7表示r+w+x,其中r=4,w=2,x=1)
[root@linuxprobe test]# ll a.txt
-rwx------. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod 777 a.txt  ## 所有者、所属组和其他人均有读写执行的权限
[root@linuxprobe test]# ll a.txt
-rwxrwxrwx. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod 007 a.txt  ## 同上
[root@linuxprobe test]# ll a.txt
-------rwx. 1 root root 25 Oct 19 23:33 a.txt
[root@linuxprobe test]# chmod 755 a.txt  ## 同上,其中5表示读和执行的权限(5表示r+x,其中r=4,x=1)
[root@linuxprobe test]# ll a.txt
-rwxr-xr-x. 1 root root 25 Oct 19 23:33 a.txt

##目录的权限与文件类似

 

posted @ 2020-10-19 23:54  小鲨鱼2018  阅读(330)  评论(0编辑  收藏  举报