Linux下的权限管理命令

  • chmod

命令名称:chmod
命令英文原意:change the permissions mode of a file
命令所在路径:/bin/chmod
执行权限:管理员(root)和文件的所有者
语法:chmod 【{ugoa}{+-=}{rwx}】【文件或目录】
-R ——– 递归修改
功能描述:改变文件或目录权限
例:给所有者增加执行权限
命令:chomd u+x 文件名

[root@localhost tmp]# ls -l test
-rw-r--r--. 1 root root 0 38 22:55 test
[root@localhost tmp]# chmod u+x test
[root@localhost tmp]# ls -l test
-rwxr--r--. 1 root root 0 38 22:55 test

例:给所属组增加w权限,给其他人取消r权限
命令:chmod g+w o-r 文件名

[root@localhost tmp]# ls -l test
-rwxr--r--. 1 root root 0 38 22:55 test
[root@localhost tmp]# chmod g+w,o-r test
[root@localhost tmp]# ls -l test
-rwxrw----. 1 root root 0 38 22:55 test

例:将所属组的权限改为rwx
命令:chmod g=rwx 文件名

[root@localhost tmp]# ls -l test
-rwxrw----. 1 root root 0 38 22:55 test
[root@localhost tmp]# chmod g=rwx test
[root@localhost tmp]# ls -l test
-rwxrwx---. 1 root root 0 38 22:55 test

也可使用数字的方式修改权限

权限的数字表示
r —– 4
w —- 2
x —– 1
例:
rwxrw-r- -
7 6 4
例:chmod 777 test
修改目录test及其目录下文件为所有用户有全部权限

[root@localhost tmp]# ls -l test
-rwxrwx---. 1 root root 0 38 22:55 test
[root@localhost tmp]# chmod 777 test
[root@localhost tmp]# ls -l test
-rwxrwxrwx. 1 root root 0 38 22:55 test

文件目录权限总结
这里写图片描述
file对应的rwx操作的具体表现:
r —– cat / more / head / tail / less
w —- vim
x —– script command
directory对应的rwx操作的具体表现:
r —– ls
w —- touch / mkdir / rmdir / rm
x —– cd

  • chown

命令名称:chown
命令英文原意:change file ownership
命令所在路径:/bin/chown
执行权限:管理员(root)
语法:chmod 【用户】【文件或目录】
功能描述:改变文件或目录的所有者

[root@localhost tmp]# useradd ly
[root@localhost tmp]# su ly
[ly@localhost tmp]$ ls -l test
-rwxrwxrwx. 1 root root 0 38 22:55 test
[ly@localhost tmp]$ chown liuyun test
chown: 正在更改"test" 的所有者: 不允许的操作
[ly@localhost tmp]$ su root
[root@localhost tmp]# chown liuyun test
[root@localhost tmp]# ls -l test
-rwxrwxrwx. 1 liuyun root 0 38 22:55 test`
[root@localhost tmp]# su liuyun
[liuyun@localhost tmp]$ chown root test
chown: 正在更改"test" 的所有者: 不允许的操作
[liuyun@localhost tmp]$ ls -l test
-rwxrwxrwx. 1 liuyun root 0 38 22:55 test

一开始test文件的所有者为root,其他人ly无法改变其所有者,但root可以修改其所有者,将其改为liuyun后,再在所有者liuyun用户下修改test的所有者,还是无法修改。由此,文件的所有者只有root可以改变

`


  • chgrp

命令名称:chgrp
命令英文原意:change file group ownership
命令所在路径:/bin/chgrp
执行权限:管理员(root)和文件的所有者
语法:chmod 【用户组】【文件或目录】
功能描述:改变文件或目录所属组

[liuyun@localhost tmp]$ ls -l test
-rwxrwxrwx. 1 liuyun root 0 38 22:55 test
[liuyun@localhost tmp]$ chgrp liuyun test
[liuyun@localhost tmp]$ ls -l test
-rwxrwxrwx. 1 liuyun liuyun 0 38 22:55 test
[liuyun@localhost tmp]$ su root 
[root@localhost tmp]# chgrp root test
[root@localhost tmp]# ls -l test
-rwxrwxrwx. 1 liuyun root 0 38 22:55 test
[root@localhost tmp]# su ly
[ly@localhost tmp]$ chgrp liuyun test
chgrp: 正在更改"test" 的所属组: 不允许的操作
[ly@localhost tmp]$ ls -l test

文件test的所有者liuyun和管理员root都可以修改其所属组,但其他人ly不能进行修改

添加用户:useradd 用户名
添加组:groupadd 组名
所属组是文件创建者的缺省组

  • umask

命令名称:umask
命令英文原意:the user file-creation mask
命令所在路径:shell内置命令
执行权限:所有用户
语法:umask【-S】
-S —- 以rwx形式显示新建文件默认权限
功能描述:显示、设置文件的默认权限

[ly@localhost tmp]$ umask -S
u=rwx,g=rwx,o=rx

mkdir创建的文件的默认权限为rw-r-xr-x
touch创建的文件的默认权限为rw-r- -r- -
linux规定新建文件不具有可执行(x)权限

[root@localhost tmp]# mkdir test
[root@localhost tmp]# ls -dl test
drwxr-xr-x. 2 root root 6 38 23:19 test
[root@localhost tmp]# touch test2
[root@localhost tmp]# ls -l test2
-rw-r--r--. 1 root root 0 38 23:20 test2

设置文件的默认权限
设我们要设置的默认权限为:rwxrw-rw- ,即用数字表示为766,777-766=011,则设置命令如下:

[root@localhost tmp]# umask 011
[root@localhost tmp]# mkdir test3
[root@localhost tmp]# ls -dl test3
drwxrw-rw-. 2 root root 6 38 23:27 test3

可以修改默认的权限,但不建议修改文件的缺省权限

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值