linux文件以及权限管理

linux文件权限与管理者

linux 权限类型分为三类 ,r(read) 可读 ,w(write) 可写 ,x(eXecute) 可执行 。
r 对文件而言,具有读取文件内容的权限;对目录来说,具有浏览目录的权限。
w 对文件而言,具有新增,修改,删除文件内容的权限;对目录来说,具有新建,删除,修改,移动目录内文件的权限。
x 对文件而言,具有执行文件的权限;对目录了来说该用户具有进入目录的权限。

linux文件的管理者分为三种类型,分别是所有者 ,所在组, 其他组 。

  • 所有者 ,一般为文件的创建者,创建者自然成为文件所有者 ,可以更改所有者 。
  • 所在组 , 当某用户创建文件后,该用户所在组默认成为文件所在组,可以更改所在组。
  • 其他组, 除了所有者和所在组的用户外的用户为其他组
[root@localhost yuanwanli]# useradd zhangsan    # 创建用户zhangsan 
[root@localhost yuanwanli]# su zhangsan       
[zhangsan@localhost ~]$ touch test.txt  # 使用zhangsan用户创建文件test.txt 
[zhangsan@localhost ~]$ ls -l
total 0
-rw-rw-r--. 1 zhangsan zhangsan 0 Aug 23 20:14 test.txt   #  所有者 ,所在组分别为zhangsan, zhangsan . 

ls -l 内容解释
在这里插入图片描述

  1. 标识文件类型 , - 普通文件,d 目录, l 软连接, c 字符设备(键盘,鼠标),b块文件
  2. 9个连续字符, 每三个分别表示所有者,所在组, 其他组的权限。譬如rw- rw- r–表示所有者有rw权限,没有x权限,所在组成员有rw权限,没有x权限 , 其他组只有r权限。
  3. 数字, 如果是目录表示子目录个数,如果是文件表示软连接个数
  4. 文件所有者
  5. 文件所在组
  6. 数字,表示文件大小,如果是目录为4096
  7. 文件创建日期
  8. 文件名称以及后缀

修改权限 chmod 指令

方法一 :通过+ - = 变更权限

“=” 表示给权限赋值

[zhangsan@localhost ~]$ chmod  u=rwx,g=rwx,o=rw test.txt 
[zhangsan@localhost ~]$ ls -l 
total 12
-rw-r--r--. 1 root     root     9792 Aug 23 22:23 ok.txt
-rwxrwxrw-. 1 zhangsan zhangsan    0 Aug 23 20:14 test.txt

“+ -”分别表示给增加减少权限 , 例如给test.txt文件的所有者、所在组取消执行权限,其他组增加啊执行权限。

[zhangsan@localhost ~]$ chmod u-x,g-x,o+x  test.txt 
[zhangsan@localhost ~]$ ls -l
total 12
-rw-r--r--. 1 root     root     10251 Aug 23 22:26 ok.txt
-rw-rw-rwx. 1 zhangsan zhangsan     0 Aug 23 20:14 test.txt
方法二 : 通过数字变更权限

数字规则为 r=4, w=2, x=1 ,rwx = 7, rw = 6, r= 4
例如设置test.txt文件的所有者权限为rwx, 所在组为rw,其他组为r 。

[zhangsan@localhost ~]$ chmod 764 test.txt 
[zhangsan@localhost ~]$ ls -l 
total 28
-rw-r--r--. 1 root     root     24786 Aug 24 00:01 ok.txt
-rwxrw-r--. 1 zhangsan zhangsan     0 Aug 23 20:14 test.txt

修改文件所有者、所在组

分别使用chown , chgrp 命令。用法如下

[root@localhost zhangsan]# chown root test.txt  # test.txt的所有者修改为zhangsan 
[root@localhost zhangsan]# ll 
total 28
-rw-r--r--. 1 root root     26316 Aug 24 00:11 ok.txt
-rwxrw-r--. 1 root zhangsan     0 Aug 23 20:14 test.txt
[root@localhost zhangsan]# chown zhangsan:root test.txt   # 同时修改所有者与所在组
[root@localhost zhangsan]# ll
total 28
-rw-r--r--. 1 root     root 26316 Aug 24 00:11 ok.txt
-rwxrw-r--. 1 zhangsan root     0 Aug 23 20:14 test.txt
[root@localhost zhangsan]# chgrp zhangsan test.txt 
[root@localhost zhangsan]# ll
total 28
-rw-r--r--. 1 root     root     26622 Aug 24 00:13 ok.txt
-rwxrw-r--. 1 zhangsan zhangsan     0 Aug 23 20:14 test.txt

-R 处理该目录和该目录下所有文件。

[root@localhost home]# chown -R root:root /home 
[root@localhost home]# ll 
-rwxr-xr-x.  1 root root 227024 Aug 16 08:42 vmware-uninstall-tools.pl
lrwxrwxrwx.  1 root root     48 Aug 16 08:46 vmware-user -> /lib/vmware-tools
drwx------.  4 root root   4096 Aug 20 05:11 xingdian
drwx------. 30 root root   4096 Aug 23 20:10 yuanwanli
drwx------.  4 root root   4096 Aug 24 00:11 zhangsan

文件颜色

Linux中文件颜色所代表的文件类型 ,熟悉颜色可以帮助我们更快的认识文件

颜色 类型
绿色 可执行文件,可执行的程序
红色 压缩文件或者包文件
蓝色 目录
白色 一般性文件,如文本文件,配置文件,源码文件等
浅蓝色 链接文件,主要是使用ln命令建立的文件
红色闪烁 表示链接的文件有问题
黄色 设备文件
灰色 表示其他文件

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43705953/article/details/108135733