您的位置:首页技术文章

linux中权限管理命令详解(chmod/chown/chgrp/unmask)

【字号: 日期:2023-10-30 13:46:59浏览:56作者:馨心

Linux操作系统对多用户的管理,是非常繁琐的,所以用组的概念来管理用户就变得简单,每个用户可以在一个独立的组,每个组也可以有零个用户或者多个用户。本文给大家介绍linux中权限管理命令详解(chmod/chown/chgrp/unmask),具体内容如下:

chmod

解释

命令名称:chmod 命令英文原意:change the permissions mode of a file 命令所在路径:/bin/chmod 执行权限:所有用户功能描述:改变文件或目录权限

语法

chmod [{ugoa}{+-=}{rwx}] [文件或目录] chmod [mode=421] [文件或目录] -R 递归修改 # 第一种修改方式 chmod [{ugoa}{+-=}{rwx}] [文件或目录]ugoa: u:所有者 g:所属组 o:其他人 a:所有人+-=: +:针对文件或目录增加某个权限 -:针对文件或目录减少某个权限 =:赋予文件或目录全新的权限,以此刻的权限为准 # 第二种修改方式 chmod [mode=421] [文件或目录]rwx: r:4 w:2 x:1rwxrw-r-- 权限:764(4+2+1=7/4+2=6/4)示例

# 第一种增加权限 chmod g+x test.txt [root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt-rw-r--r-- 1 root root 11 Nov 28 15:39 test.txt[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod g+x test.txt[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt-rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt# 第二种增加权限chmod 777 test.txt[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt-rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test.txt[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt-rwxrwxrwx 1 root root 11 Nov 28 15:39 test.txt权限特别注意

# 在/tmp下新建文件夹test[root@izm5e2q95pbpe1hh0kkwoiz tmp]# mkdir test# 在/tmp/test文件夹下新建test.txt[root@izm5e2q95pbpe1hh0kkwoiz tmp]# touch test/test.txt# 查看test文件下的文件[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l testtotal 0-rw-r--r-- 1 root root 0 Nov 28 17:54 test.txt# 查看/tmp/test文件夹的权限[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld testdrwxr-xr-x 2 root root 4096 Nov 28 17:54 test# 赋予/tmp/test文件夹全部的权限[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld testdrwxrwxrwx 2 root root 4096 Nov 28 17:54 test[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test/test.txt-rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt# 新增加一个普通用户并修改密码[root@izm5e2q95pbpe1hh0kkwoiz tmp]# useradd eternity[root@izm5e2q95pbpe1hh0kkwoiz tmp]# passwd eternity# 使用eternity帐号,密码123456,登录服务器# 查看当前目录[eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ pwd/home/eternity# 进入/tmp目录[eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ cd /tmp# 查看/tmp/test目录的权限,拥有全部权限[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -ld testdrwxrwxrwx 2 root root 4096 Nov 28 17:54 test# /tmp/test目录下存在test.txt,拥有读权限[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txt-rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt# 删除/tmp/test下的test.txt文件[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ rm test/test.txtrm: remove write-protected regular empty file ‘test/test.txt’? y# 删除成功,此时/tmp/test目录下test.txt已经没有了[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txtls: cannot access test/test.txt: No such file or directory

只有管理员拥有rw读写权限,所属组和其他人只有读权限,但是此时普通用户却删除了只有r读权限的文件,为什么???? 文件目录权限总结

代表字符 权限 对文件的含义 对目录的含义 r 读权限 可以查看文件内容 可以列出目录中的内容 w 写权限 可以修改文件内容 可以在目录中创建和删除文件 x 执行权限 可以执行文件 可以进入目录

分析

对于文件有写权限,仅仅代表可以修改文件的内容,而没有删除文件的权限

对于目录有写权限,可以在目录中创建和删除文件

因为上面的/tmp/test目录的权限为777 所以普通用户对于/tmp/test目录也具有创建文件和删除文件的权限所以,普通用户也能删除/tmp/test/test.txt文件但是普通用户无法编辑/tmp/test/test.txt文件,使用vim编辑文件的时候,会提示Waring: Changing a readonly file

chown

解释

命令名称:chown 命令英文原意:change file ownership 命令所在路径:/bin/chown 执行权限:所有用户功能描述:改变文件或目录的所有者

语法

chown [用户] [文件或目录]

在linux中只有root能改变文件所有者,即便是创建者都不可以

示例

# 改变文件所有者(将test.txt的所有者由eternity更改为root)chown root /tmp/test/test.txt[root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd/root[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt-rw-r--r-- 1 eternity eternity 7 Nov 28 18:15 /tmp/test/test.txt[root@izm5e2q95pbpe1hh0kkwoiz ~]# chown root /tmp/test/test.txt[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt-rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txtchgrp

解释

命令名称:chgrp命令英文原意:change file group ownership命令所在路径:/bin/chgrp执行权限:所有用户功能描述:改变文件或目录的所属组

语法

chgrp [用户组] [文件或目录]

示例

# 改变文件所属组(将test.txt的所属组由eternity更改为eternityz)chgrp eternityz /tmp/test/test.txt# 当前目录[root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd/root# 查看详细信息[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt-rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txt# 增加eternityz组[root@izm5e2q95pbpe1hh0kkwoiz ~]# groupadd eternityz# 改变所属组[root@izm5e2q95pbpe1hh0kkwoiz ~]# chgrp eternityz /tmp/test/test.txt[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt-rw-r--r-- 1 root eternityz 7 Nov 28 18:15 /tmp/test/test.txtumask

解释

命令名称:umask 命令英文原意the user file-creation mask 命令所在路径:shell内置命令执行权限:所有用户功能描述:显示/设置文件的缺省权限

语法

umask [-S] -S 以rwx形式显示新建文件缺省权限(大写的S)

示例

# 查看文件的缺省权限umask -S# 查看umaskumask[root@izm5e2q95pbpe1hh0kkwoiz ~]# umask00220022中0 特殊权限022 ----w--w-# 通过所有权限777和022权限进行异或操作,得到缺省权限777 rwx rwx rwx022 --- -w- -w-================目录 rwx r-x r-x文件 rwx r-- r--# 更改umask值,进而改变缺省权限umask 077# 更改umask值之后,缺省权限变为777 rwx rwx rwx077 --- rwx rwx================目录 rwx --- ---文件 rw- --- ---# 以下实验符合更改缺省权限的设置[root@izm5e2q95pbpe1hh0kkwoiz ~]# umask 077[root@izm5e2q95pbpe1hh0kkwoiz ~]# mkdir /tmp/lyf[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -ld /tmp/lyfdrwx------ 2 root root 4096 Nov 29 10:55 /tmp/lyf[root@izm5e2q95pbpe1hh0kkwoiz ~]# touch /tmp/lyf/lyf[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/lyf/lyf-rw------- 1 root root 0 Nov 29 10:56 /tmp/lyf/lyf

在linux中只有root能改变文件所有者,即便是创建者都不可以文件的创建者为默认的所有者,此时默认的所属组也是文件创建者 linux中文件夹的缺省权限时rwxr-xr-x,文件的缺省权限是rw-r--r--,新建文件不具备可执行权限

到此这篇关于linux中权限管理命令详解(chmod/chown/chgrp/unmask)的文章就介绍到这了,更多相关linux 权限管理命令内容请搜索优爱好网以前的文章或继续浏览下面的相关文章希望大家以后多多支持优爱好网!

标签: Linux系统
相关文章: