Linux系统——文件权限
作用: 普通用户执行命令期间,会临时获得程序的属主用户对系统的操作权限。作用: 在目录下创建文件时,文件会自动继承目录的属组。作用: 避免普通用户间相互删除文件的。作用:影响新建的文件、目录的默认权限。优势:针对单个用户、用户组设置权限。作用: 修改文件属主、属组。作用: 修改文件的属组。
·
Linux系统——文件权限
一、文件常规权限
1、常规权限
- r 读 4
- w 写 2
- x 执行 1
// 查看文件权限
[root@martin-host ~]# ls -l /tmp/file01
-rw-rw-r-- 1 demon IT 0 10月 9 09:18 /tmp/file01
demon:属主/所有者,用户名
IT: 属组/所属组,用户组名
权限9位,每3位一组
第1组:属主的权限
第2组:属组的权限
第3组:其他用户的权限
// 查看目录权限
[root@martin-host ~]# ls -ld /etc/
drwxr-xr-x. 152 root root 12288 10月 9 09:02 /etc/
2、权限的含义
2.1 针对文件
- r 只能查看文件内容(cat/head/tail/more/less/grep)
- w 修改文件内容(vim)
- x 执行权限
2.2 针对目录
- r 查看目录下的文件
- w 修改目录下的文件(touch/mkdir/rm/mv/)
- x 可切换目录(cd)
注意:一个用户想要正常查看目录下的文件,必须同时拥有rx权限
二、修改权限的命令
1、chown
作用: 修改文件属主、属组
# chown 用户名.用户组名 文件
[root@martin-host ~]# chown martin.jishu /test/1.txt
[root@martin-host ~]# ls -l /test/1.txt
-rw-r--r-- 1 martin jishu 0 10月 9 09:50 /test/1.txt
[root@martin-host ~]# chown demon /test/1.txt
[root@martin-host ~]# ls -l /test/1.txt
-rw-r--r-- 1 demon jishu 0 10月 9 09:50 /test/1.txt
2、chgrp
作用: 修改文件的属组
# chgrp 用户组名 文件
[root@martin-host ~]# chgrp IT /test/2.txt
[root@martin-host ~]# ls -l /test/2.txt
-rw-r--r-- 1 root IT 0 10月 9 09:50 /test/2.txt
3、chmod
作用: 修改权限
# chmod {augo}{+-=}{rwx} 文件
[root@martin-host ~]# ls -l /test/3.txt
-rw-r--r-- 1 root root 0 10月 9 09:50 /test/3.txt
[root@martin-host ~]# chmod g+w /test/3.txt
[root@martin-host ~]# ls -l /test/3.txt
-rw-rw-r-- 1 root root 0 10月 9 09:50 /test/3.txt
[root@martin-host ~]# chmod o-r /test/3.txt
[root@martin-host ~]# ls -l /test/3.txt
-rw-rw---- 1 root root 0 10月 9 09:50 /test/3.txt
[root@martin-host ~]# chmod u+x,g-w /test/3.txt
[root@martin-host ~]# ls -l /test/3.txt
-rwxr----- 1 root root 0 10月 9 09:50 /test/3.txt
[root@martin-host ~]# chmod a+x /test/3.txt
[root@martin-host ~]# ls -l /test/3.txt
-rwxr-x--x 1 root root 0 10月 9 09:50 /test/3.txt
# chmod nnn 文件
[root@martin-host ~]# chmod 600 /test/4.txt
[root@martin-host ~]# ls -l /test/4.txt
-rw------- 1 root root 0 10月 9 09:50 /test/4.txt
三、facl 文件访问控制列表
优势:针对单个用户、用户组设置权限
1、针对单个用户
# setfacl -m u:用户名:权限 文件
[root@martin-host ~]# setfacl -m u:martin:rw /opt/work/file02
[root@martin-host ~]# ls -l /opt/work/file02
-rw-rw-r--+ 1 root root 4 10月 9 11:39 /opt/work/file02
[root@martin-host ~]# getfacl /opt/work/file02
getfacl: Removing leading '/' from absolute path names
# file: opt/work/file02
# owner: root
# group: root
user::rw-
user:martin:rw-
group::r--
mask::rw-
other::r--
2、针对单个用户组
# setfacl -m g:用户组:权限 文件
[root@martin-host ~]# setfacl -m g:IT:rx /opt/work/file02
[root@martin-host ~]# getfacl /opt/work/file02
getfacl: Removing leading '/' from absolute path names
# file: opt/work/file02
# owner: root
# group: root
user::rw-
user:martin:rw-
group::r--
group:IT:r-x
mask::rwx
other::r--
3、删除facl权限
[root@martin-host ~]# setfacl -x u:martin /opt/work/file02
[root@martin-host ~]# getfacl /opt/work/file02
getfacl: Removing leading '/' from absolute path names
# file: opt/work/file02
# owner: root
# group: root
user::rw-
group::r--
group:IT:r-x
mask::r-x
other::r--
四、特殊权限
suid 4、sgid 2、sticky bit 1
1、suid
针对可执行程序设置
作用: 普通用户执行命令期间,会临时获得程序的属主用户对系统的操作权限
[root@martin-host ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856 4月 1 2020 /usr/bin/passwd
// 添加suid权限
[root@martin-host ~]# chmod u+s /usr/bin/passwd
// 删除suid权限
[root@martin-host ~]# chmod u-s /usr/bin/passwd
2、sgid
针对目录设置
作用: 在目录下创建文件时,文件会自动继承目录的属组
[root@martin-host ~]# chmod g+s /opt/work/
[root@martin-host ~]# ls -ldh /opt/work/
drwxr-sr-x 2 root IT 20 10月 9 14:27 /opt/work/
3、sticky bit
针对目录设置
作用: 避免普通用户间相互删除文件的
[root@martin-host ~]# chmod o+t /opt/test/
[root@martin-host ~]#
[root@martin-host ~]# ls -ldh /opt/test/
drwxrwxr-t 2 root IT 51 10月 9 14:36 /opt/test/
五、权限其他
1、umask
权限反掩码
作用:影响新建的文件、目录的默认权限
- 针对root用户
[root@martin-host ~]# whoami
root
[root@martin-host ~]# umask
0022
文件的默认权限:666-022=644
目录的默认权限:777-022=755
- 针对普通用户
[martin@martin-host ~]$ whoami
martin
[martin@martin-host ~]$
[martin@martin-host ~]$ umask
0002
[martin@martin-host ~]$
2、文件隐藏属性
- 查看隐藏属性
[root@martin-host ~]# lsattr /opt/work/file01
---------------- /opt/work/file01
- a append追加
只允许向文件中追加内容 , 不能被删除
针对重要日志文件
[root@martin-host ~]# chattr +a /opt/work/file01
[root@martin-host ~]# lsattr /opt/work/file01
-----a---------- /opt/work/file01
[root@martin-host ~]# echo "192.168.10.1" >> /opt/work/file01
[root@martin-host ~]#
[root@martin-host ~]# cat /opt/work/file01
abc
abababa
192.168.10.1
[root@martin-host ~]#
[root@martin-host ~]# rm /opt/work/file01
rm: 无法删除"/opt/work/file01": 不允许的操作
- i
文件内容不允许修改、不能被删除
针对重要模板文件
[root@martin-host ~]# chattr +i /opt/work/file01
[root@martin-host ~]# lsattr /opt/work/file01
----i----------- /opt/work/file01
3、共同选项
- -R 递归修改文件权限信息
[root@martin-host ~]# chown -R martin.IT /opt/test/
更多推荐
所有评论(0)