- 对于同一个文件,针对不同用户设置不同的权限
权限管理操作
一、查看权限
- 查看文件权限
[root@localhost ~]# ls -l /etc/passwd
-rw-r--r-- 1 root root 2360 9月 22 21:22 /etc/passwd
root 属主/所有者、用户名
root 属组/所属组、用户组名
- rw- 文件属主的权限
- r- 文件属组的权限
- r-- 其他用户的权限
文件属主、属组的变化都会对文件权限的含义有影响
- 查看目录权限
[root@localhost ~]# ls -ld /etc
drwxr-xr-x. 147 root root 8192 10月 11 08:20 /etc
- 权限的含义
r 读 w 写 x 执行
- 针对文件
- r权限
- 查看文件内容 cat/head/tall/more/less/grep
- w权限
- 修改文件内容 vim
- x权限
- 针对脚本文件
- r权限
- 针对目录
- r权限
- 查看目录下的文件
- ls 目录名称
- w权限
- 操作目录下的文件(创建、删除、重用名)
- x权限
- 可以使用cd切换进目录
任意一个用户可以正常查看目录下的文件,必须具备rx权限,缺一不可
- 可以使用cd切换进目录
- r权限
二、修改权限的指令
1、修改属主、属组
# chown 用户名[.用户组名] 文件名称
[root@localhost ~]# chown userA /opt/linux/1.txt
[root@localhost ~]# chown userA.jishu /opt/linux/2.txt
2、修改属组
[root@localhost ~]# chgrp nobody /opt/linux/3.txt
3、修改文件权限
方法1)
# chmod {augo}{+-=}{rwx} 文件名
- a all 所有
- u user 属主
- g group 属组
- o other 其他用户
[root@localhost ~]# chmod a+x /opt/linux/1.txt
[root@localhost ~]# chmod u-w /opt/linux/2.txt
[root@localhost ~]# chmod u-w,g+x,o-r /opt/linux/3.txt
方法2)
# chmod nnn 文件名称
[root@localhost ~]# chmod 600 /opt/linux/4.txt
[root@localhost ~]# ls -l /opt/linux/4.txt
-rw------- 1 userA nobody 0 Dec 20 10:49 /opt/linux/4.txt
[root@localhost ~]# chmod 765 /opt/linux/5.txt
[root@localhost ~]# ls -l /opt/linux/5.txt
-rwxrw-r-x 1 userA jishu 0 Dec 20 10:49 /opt/linux/5.txt
三、facl 文件访问控制列表
-
设置权限的一种方法
-
针对单个用户、用户组设置权限
1、设置facl权限
针对单个用户设置
# setfacl -m u:用户名:权限 文件名称
针对单个用户组设置
# setfacl -m g:用户组:权限 文件名称
[root@localhost ~]# setfacl -m u:user4:r /opt/linux/file01.txt
2、查看facl权限
[root@localhost ~]# getfacl /opt/linux/file01.txt
getfacl: Removing leading '/' from absolute path names
# file: opt/linux/file01.txt
# owner: user1
# group: user2
user::rw-
user:user4:r--
group::r-x
mask::r-x
other::rwx
3、删除facl权限
[root@localhost ~]# setfacl -x u:user4 /opt/linux/file01.txt
# setfacl -x g:用户组 文件名称
四、suid, sgid, sticky bit
1、suid 4
- 作用
- 当普通用户运行文件时,会临时获取到文件属主对操作系统的权限
- 针对可执行文件进行设置(脚本、命令文件)
- 查看suid权限
[root@localhost ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd
- 设置suid权限
# chmod u+s 文件名称
2、sgid 2
- 应用场景
- 针对目录设置
- 作用
- 一旦目录拥有sgid权限后,在该目录创建文件时,所有文件会自动继承目录的属组信息
[root@localhost ~]# chmod g+s /opt/data/
[root@localhost ~]# ls -ldh /opt/data/
drwxr-sr-x 2 root jishu 32 Dec 20 14:05 /opt/data/
3、sticky bit 1
- 应用场景
- 针对目录设置
- 作用
- 防止误删除文件
- 只有文件属主、目录属主、root用户可删除该目录下的文件
[root@localhost ~]# chmod o+t /webdata/
[root@localhost ~]# ls -ldh /webdata/
drwxrwxr-t 2 root jishu 76 Dec 20 14:17 /webdata/
[root@localhost ~]# chmod 2770 /opt/test1/
[root@localhost ~]#
[root@localhost ~]# ls -ldh /opt/test1/
drwxrws--- 2 root root 6 Dec 20 16:20 /opt/test1/
五、权限其他操作
1、递归修改权限
- R选项
[root@localhost ~]# chown -R martin /opt/test2/
[root@localhost ~]# ls -ldh /opt/test2/
drwxr-xr-x 2 martin root 137 Dec 20 16:23 /opt/test2/
[root@localhost ~]# ls -l /opt/test2/
total 0
-rw-r--r-- 1 martin root 0 Dec 20 16:23 10.txt
-rw-r--r-- 1 martin root 0 Dec 20 16:23 1.txt
-rw-r--r-- 1 martin root 0 Dec 20 16:23 2.txt
-rw-r--r-- 1 martin root 0 Dec 20 16:23 3.txt
2、复制文件保留权限
[root@localhost ~]# ls -l /opt/file03.txt
-rw------- 1 martin jishu 0 Dec 20 16:27 /opt/file03.txt
[root@localhost ~]# cp -a /opt/file03.txt /tmp/
[root@localhost ~]# ls -l /tmp/file03.txt
-rw------- 1 martin jishu 0 Dec 20 16:27 /tmp/file03.txt
[root@localhost ~]#
3、umask值
- umask值影响创建文件、目录的默认权限
针对root用户来说
[root@localhost ~]# umask
0022
- 第一个0,代表创建文件、目录时没有任意特殊权限
- 创建文件
- 666 - 022 = 644
- 创建目录
- 777 - 022 = 755
普通用户的umask值
[martin@localhost ~]$ umask
0002