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]$

四.默認權限

umask值的設定

五.文件的屬主和數組

(一)如何查看文件的屬主和數組

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]#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM