linux权限体系(五)文件权限修改命令chown、usermod、chgrp和chmod

一、文件权限命令合集

  1. 更改文件所有者和属组:chown、usermod
  2. 更改为文件属组:chgrp
  3. 更改文件的权限:chmod
  4. 更改文件的扩展属性:lsattr、chattr

二、更改文件所有者:chown

chown命令不仅能更改文件的属主,也能更改文件的属组。

NAME
chown - change file owner and group

SYNOPSIS
chown [OPTION]… [OWNER][:[GROUP]] FILE…
chown [OPTION]… --reference=RFILE FILE…

DESCRIPTION
This manual page documents the GNU version of chown. chown
changes the user and/or group ownership of each given file.
If only an owner (a user name or numeric user ID) is given,
that user is made the owner of each given file, and the files’
group is not changed. If the owner is followed by a colon and
a group name (or numeric group ID), with no spaces between
them, the group ownership of the files is changed as well. If
a colon but no group name follows the user name, that user is
made the owner of the files and the group of the files is
changed to that user’s login group. If the colon and group
are given, but the owner is omitted, only the group of the
files is changed; in this case, chown performs the same func‐
tion as chgrp. If only a colon is given, or if the entire op‐
erand is empty, neither the owner nor the group is changed.

常见的使用方法:

  1. chown tyson file 或 chown UID file 更改了文件的属主
  2. chown tyson:mygrp file 或 chown tyson:GID file 更改文件的属主和属组
  3. chown tyson: file 更改文件的属主,同时也将文件的属组改成新的属主的基本组
  4. chown :mygrp file 与chgrp命令功能相同
  5. chown : file 什么都不变
    需要注意的是,修改操作不允许平级的操作。也就是普通用户不能擅自将属主或者属组修改成其他平级的属主或者属组。

常见的选项:

  • -R
  • –recursive 对目录及目录下的文件进行递归修改
[tyson@tysonscloud learnchown]$ ll
total 0
-rw-rw-r-- 1 tyson tyson 0 Jan 30 10:45 test1
-rw-rw-r-- 1 tyson tyson 0 Jan 30 10:45 test2
[tyson@tysonscloud learnchown]$ cd ..
[tyson@tysonscloud ~]$ su root
Password: 
[root@tysonscloud tyson]# chown -R gentoo: learnchown/
[root@tysonscloud tyson]# ll
total 12
drwxrwxr-x 2 gentoo gentoo 4096 Jan 30 10:45 learnchown
drwxrwxr-x 2 tyson  tyson  4096 Jan 27 16:29 learnStat
drwxrwxr-x 2 tyson  tyson  4096 Jan 30 10:05 learnUmask
[root@tysonscloud tyson]# cd learnchown/
[root@tysonscloud learnchown]# ll
total 0
-rw-rw-r-- 1 gentoo gentoo 0 Jan 30 10:45 test1
-rw-rw-r-- 1 gentoo gentoo 0 Jan 30 10:45 test2
  • –reference=RFILE 以某文件为模板对指定文件进行权限修改
drwxrwxr-x 2 gentoo gentoo 4096 Jan 30 10:45 learnchown
drwxrwxr-x 2 tyson  tyson  4096 Jan 27 16:29 learnStat
drwxrwxr-x 2 tyson  tyson  4096 Jan 30 10:05 learnUmask
[root@tysonscloud tyson]# chown --reference=learnStat/ learnchown/
[root@tysonscloud tyson]# ll
total 12
drwxrwxr-x 2 tyson tyson 4096 Jan 30 10:45 learnchown
drwxrwxr-x 2 tyson tyson 4096 Jan 27 16:29 learnStat
drwxrwxr-x 2 tyson tyson 4096 Jan 30 10:05 learnUmask

三、usermod简单案例

将用户加入到某个组中
usermod -a -G username groupname

四、更改文件属组:chgrp

语法格式:chgrp [options] 组名 文件/目录
常用到的选项主要是-R,递归修改属组。但是chown命令已经可以完成修改属组的需求了,所以chgrp这个命令使用的频率较少。

五、更改文件权限:chmod

几种常用的格式

  1. 语法格式1: chmod 7000 file
  2. 语法格式2: chmod u=rw-,g=rw-,o=rwx file
  3. 语法格式3: chmod u-w file

你可能感兴趣的:(linux基础)