十、修改权限命令

前言

如何对一个文件/目录的权限进行管理呢?
 Linux/Unix 的文件调用权限分为三级 : 文件拥有者、群组、其他。
chmod用于管理文件或目录的权限,文件或目录权限的控制分别以读取(r)、写入(w)、执行(x)3种来表示可读可写可执行。

修改权限命令

chmod 修改文件、目录权限

  • chomd u+x/tmp/testfile
  • chomd 755 /tmp/testfile

chown 更改属主、属组

chgrp 可以单独更改属组,不常用

权限范围

权限范围:
u,User    即文件或目录的拥有者
g,Group   即文件或目录的所属群组
o,Other    除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围
a,All     即全部的用户,包含拥有者,所属群组以及其他用户

练习

1 给a.txt文件的拥有者赋予可执行权限

@SC02ZRC4KMD6N ~ % ls -l a.txt
-rw-r--r--  1 user1  staff  14  3 29 14:43 a.txt
user1@SC02ZRC4KMD6N ~ % chmod u+x a.txt
user1@SC02ZRC4KMD6N ~ % ls -l a.txt    
-rwxr--r--  1 user1  staff  14  3 29 14:43 a.txt

2 给属组减少可读权限

user1@SC02ZRC4KMD6N ~ % chmod g-r a.txt
user1@SC02ZRC4KMD6N ~ % ls -l a.txt    
-rwx---r--  1 user1  staff  14  3 29 14:43 a.txt

3 给其他用户设置只有只写权限

user1@SC02ZRC4KMD6N ~ % chmod o=w a.txt
user1@SC02ZRC4KMD6N ~ % ls -l a.txt    
-rwx----w-  1 user1  staff  14  3 29 14:43 a.txt

4 给所有用户增加可读权限

user1@SC02ZRC4KMD6N ~ % chmod a+r a.txt
user1@SC02ZRC4KMD6N ~ % ls -l a.txt    
-rwxr--rw-  1 user1  staff  14  3 29 14:43 a.txt

5 用数字给其他用户设置读写权限,自己和属组为只读权限

user1@SC02ZRC4KMD6N ~ % chmod 446 a.txt 
user1@SC02ZRC4KMD6N ~ % ls -l a.txt    
-r--r--rw-  1 user1  staff  14  3 29 14:43 a.txt

6 创建一个文件的默认权限为666减去umask值

user1@SC02ZRC4KMD6N ~ % touch e.txt
user1@SC02ZRC4KMD6N ~ % ls -l e.txt
-rw-r--r--  1 user1  staff  0  3 29 18:15 e.txt
user1@SC02ZRC4KMD6N ~ % umask
022

猜你喜欢

转载自blog.51cto.com/12936780/2483236