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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值