linux下文件权限管理

一.权限概述

(一)权限

用户使用软件资源的权利

(二)设置权限的目的

让用户可以有操作文件的权利

(三)权限分类

普通权限

高级权限,特殊需求

默认权限

权限是设置在文件上的,不是用户上的

二.普通权限

(一)rwx

1.r读权限 --4

目录:可以使用ls列出目录下的文件

文件:可以使用如cat命令查看文件内容

2.w写权限 --2

目录:可以在该目录下创建,删除,移动文件等操作

文件:可以修改文件内容

3.x执行权限 --1

目录:可以进入目录下

文件:文件可执行,如命令,脚本文件

4.没有权限 --0

(二)UGO

1.UGO指什么

u:文件创建者,拥有者

G:同一组的成员

O:其他用户

2.不同身份用户的权限
[root@localhost ~]# ll install.log
-rw-r--r--. 1 root root 53013 Mar  4 15:39 install.log
  U  G  O  

(三)文件权限修改(chmod)

1.chmod命令用法
chmod [选项] 文件名
常见选项:
-R, --recursive		递归更改目录和目录里文件的权限
2.权限修改方式

字母修改

u:表示文件拥有者
g:表示文件属组用户
o:表示其他人,即不是文件的创建者,也不在文件属组里
a:表示所有人

案例如下
[root@localhost tmp]# chmod o+x,g+w,o+w file
[root@localhost tmp]# ll file
-rw-rw-rwx. 1 root root 0 Mar  8 20:44 file
[root@localhost tmp]# 

数字修改

r–4;w–2;x–1

[root@localhost tmp]# chmod 644 file
[root@localhost tmp]# ll file
-rw-r--r--. 1 root root 0 Mar  8 20:44 file
[root@localhost tmp]# 

三.高级权限

1.冒险位

冒险位,指文件操作者(用户)临时拥有文件拥有者的权限
冒险位,一般针对的是命令或者脚本文件
冒险位,用字母表示是s或S;数字表示是4
冒险位的设置:'chmod u+s 文件名'或者chmod 4xxx 文件名'

案例如下
[inst01@localhost /]$ ll /bin/mkdir
-rwxr-xr-x. 1 root root 50056 Mar 23  2017 /bin/mkdir
[inst01@localhost /]$ exit
logout
[root@localhost tmp]# chmod u+s /bin/mkdir
[root@localhost tmp]# ll /bin/mkdir 
-rwsr-xr-x. 1 root root 50056 Mar 23  2017 /bin/mkdir
[root@localhost tmp]# 
[root@localhost tmp]# chmod u-s /bin/mkdir 
[root@localhost tmp]# ll /bin/mk
mkdir   mknod   mktemp  
[root@localhost tmp]# ll /bin/mkdir 
-rwxr-xr-x. 1 root root 50056 Mar 23  2017 /bin/mkdir
[root@localhost tmp]# 

2.强制位

强制位,一般针对的是目录
如果一个目录拥有强制位,那么任何用户在该目录里所创建的任何文件的==属组都会继承该目录的属组。
强制位,用字母表示是s或S;数字表示是2
强制位的设置:'chmod g+s 文件名'或者'chmod 2xxx 文件名'

案例如下
[root@localhost tmp]# ll
total 4
-rw-r--r--. 1 user01 admin    0 Mar  8 20:44 file
drwxr-xr-x. 2 root   root  4096 Mar  8 21:21 test
[root@localhost tmp]# chmod g+s test/
[root@localhost tmp]# ll -d test
drwxr-sr-x. 2 root root 4096 Mar  8 21:21 test
[root@localhost tmp]# 
[root@localhost tmp]# chmod 2763 /tmp/test
[root@localhost tmp]# ll -d test/
drwxrwS-wx. 2 root root 4096 Mar  8 21:21 test/
[root@localhost tmp]# su - inst01
[inst01@localhost ~]$ touch /tmp/test/file
[inst01@localhost ~]$ ll /tmp/test/file
-rw-rw-r--. 1 inst01 root 0 Mar  8 21:25 /tmp/test/file
[inst01@localhost ~]$ 

3.粘滞位

粘滞位,一般针对的是公共目录
如果一个公共目录拥有粘滞位,那么该目录下的文件,只有root和文件的创建者可以删除,其他人只能自己管理自己。(A用户不能删除B用户创建的文件)
粘滞位,用字母表示是t或T;数字表示是1
粘滞位的设置:'chmod o+t 文件名'或者'chmod 1xxx 文件名'

案例如下
[root@localhost tmp]# chmod g-s test/
[root@localhost tmp]# ll
total 4
-rw-r--r--. 1 user01 admin    0 Mar  8 20:44 file
drwxrwxrwx. 2 root   root  4096 Mar  8 21:27 test
[root@localhost tmp]# 
[root@localhost tmp]# chmod o+t test/
[root@localhost tmp]# ll
total 4
-rw-r--r--. 1 user01 admin    0 Mar  8 20:44 file
drwxrwxrwt. 2 root   root  4096 Mar  8 21:27 test
[root@localhost tmp]# 
[root@localhost tmp]# touch test/file1
[root@localhost tmp]# su - inst01
[inst01@localhost ~]$ touch /tmp/test/file2
[inst01@localhost ~]$ exit
logout
[root@localhost tmp]# su - user01
[user01@localhost ~]$ touch /tmp/test/file3
[user01@localhost ~]$ ll -d /tmp/test/
drwxrwxrwt. 2 root root 4096 Mar  8 21:29 /tmp/test/
[user01@localhost ~]$ ll /tmp/test/
total 0
-rw-r--r--. 1 root   root   0 Mar  8 21:28 file1
-rw-rw-r--. 1 inst01 inst01 0 Mar  8 21:29 file2
-rw-rw-r--. 1 user01 user01 0 Mar  8 21:29 file3
[user01@localhost ~]$ 
[user01@localhost test]$ ll
total 0
-rw-r--r--. 1 root   root   0 Mar  8 21:28 file1
-rw-rw-r--. 1 inst01 inst01 0 Mar  8 21:29 file2
[user01@localhost test]$ rm -rf file2
rm: cannot remove `file2': Operation not permitted
[user01@localhost test]$ 

四.默认权限

五.文件的属主和数组

(一)如何查看文件的属主和数组

ls -l命令可以查看
[root@localhost tmp]# ls -l
total 0
-rw-r--r--. 1 root root 0 Mar  8 20:44 file
[root@localhost tmp]# 

(二)如何修改文件的属主和数组

1.chown命令

既可以修改属主,也可以修改属组

只修改文件的属主
# chown 用户名	 文件名


修改文件的属主和属组
# chown 用户名.组名  文件名
# chown 用户名:组名  文件名
# chown 用户名.	文件名	//没有指定组名,默认是用户的主组
只修改文件的属组
# chown .组名	文件名
# chown :组名 文件名

可以加-R选项,表示递归修改

案例如下
只修改属主
[root@localhost tmp]# chown inst01 file
[root@localhost tmp]# ll file
-rw-r--r--. 1 inst01 admin 0 Mar  8 20:44 file
[root@localhost tmp]# 

修改属主和属组
[root@localhost tmp]# chown inst01.inst01 file
[root@localhost tmp]# ll file
-rw-r--r--. 1 inst01 inst01 0 Mar  8 20:44 file
[root@localhost tmp]# 
[root@localhost tmp]# chown user01:user01 file
[root@localhost tmp]# ll file
-rw-r--r--. 1 user01 user01 0 Mar  8 20:44 file
[root@localhost tmp]# 

只修改属组
[root@localhost tmp]# chown .admin file
[root@localhost tmp]# ll file
-rw-r--r--. 1 user01 admin 0 Mar  8 20:44 file
[root@localhost tmp]# 

2.chgrp命令

只能修改属组

# chgrp 组名 文件名

案例如下
[root@localhost tmp]# ls -l
total 0
-rw-r--r--. 1 root root 0 Mar  8 20:44 file
[root@localhost tmp]# 
[root@localhost tmp]# chgrp admin file
[root@localhost tmp]# ll file
-rw-r--r--. 1 root admin 0 Mar  8 20:44 file
[root@localhost tmp]# 

猜你喜欢

转载自blog.csdn.net/sxlong_work/article/details/88932521