Linux 的文件属性主要包含两部分:ownership 和 permission

一个文件或文件夹的属性包括:1.归属于某个 user 和 user group,2.在此 user 和 user group 下的读/写/执行权限及其他 user 的相关权限。分别通过 chown 和 chmod 来进行设置。

参考连接:
https://www.computerhope.com/unix/uchmod.htm
https://www.computerhope.com/unix/uchown.htm

chown

chown 修改文件或文件夹的 ownership。

ownership

Linux 是为多用户模式设计的,所以对于一个文件的操作对于不同的用户是不同的,这就是 permission。

主要有三种 permissions:

  • User permissions. These permissions apply to a single user who has special access to the file. This user is called the owner.
  • Group permissions. These apply to a single group of users who have access to the file. This group is the owning group.
  • Other permissions. These apply to every other user on the system. These users are known as others, or the world.

当一个文件创建的时候,当前登录用户就是这个文件的所有者,用户群就是当前用户所在的群。其他用户就是 other。

基本语法

chown user file
chown user:group file
chown -R user:group directory
  • 使用 -R 来设置一个文件夹及其内所有文件的权限。
  • 如果不需要设置 group,可以省略掉。

chmod

chmod 用来设置文件或文件夹的权限。

基本语法

chmod options permissions file

permission 有两种设置模式:符号和数字

符号:

  • 使用 u/g/o/a 表示用户/用户群/其他的用户/所有用户
  • 使用 r/w/x 设置读/写/执行权限

例如:

chmod u=rwx,g=rx,o=r myfile

表示 user 权限是:读写执行;group user 权限是:读执行;other 用户权限是:读

数字:

  • 使用 0~7 表示权限信息,转换成二进制模式三位数就代表:读/写/执行,例如 7 -> 111,表示:读写执行权限

    例如:
    chmod 754 myfile

表示 user 权限是:111读写执行;group user 权限是:101读执行;other 用户权限是:100读

option
常用的选项就是 -R 表示对文件夹及其内所有文件的设置。

权限的设置方法

可以使用运算符:+/-/= 来进行设置:

  • = 表示进行总体设置,如:u=rwx
  • + 表示增加某个权限,如:g+x
  • - 表示取消某个权限,如:o-w

如果运算符前没有指定用户,则默认代表全部用户 a。

查看文件权限设置

可以使用一下命令查看文件或文件夹的权限设置情况:

ls -l file

以上就是文件权限设置的简单应用。

标签:chown, chmod

你的评论