Linux permission management
How to view and read permission information
File directory permission information--
| rw-r--r-- |. | 1 | root | root | 0 | Oct 15 09:51 | westosfile1-1
2 3 4 5 6 7 8 9
1. 文件类型 -表示普通文件,d表示目录,s表示套接字socket,l表示软链接,b表示块设备,c表示字符设备,p表示管道
2. 权限信息,rw- 表示用户权限u,r--表示用户组权限g,最后三位表示其他人权限o
3. . 表示系统的SELinux开启,及安全上下文
4. 1 对于文件来说是系统的硬链接个数,即文件内容被系统记录的个数。对于目录来说,是目录下的子目录个数。
5. root 文件拥有者
6. root 文件拥有组
7. 0 对于文件来说,是文件的大小。对于目录来说,是目录的子文件中元数据大小,即属性个数。
8. 时间戳,代表文件最后一次被修改的时间。
9. 文件或者目录名称
10.软连接相当于是快捷方式,多个节点都指向源文件的路径;硬链接相当于复制,每个文件都是源文件,只有全部删除,硬链接数才为0。
Set file owner and owning group information
##设置文件拥有者
chown zhy /mnt/westosfile1
chown -R zhy /mnt/westosfile3
##设置文件拥有组
chgrp zhy /mnt/westosfile2
chgrp -R zhy /mnt/westosfile3
#同时设置文件拥有者和拥有组
chown -R zhy.zhy /mnt/test
chown -R root:root /mnt/test
Types and functions of ordinary permissions
- The user's identity to the file
-u file owner
-g file all groups
-o other members - Permission level
-user>group>other
3. Permission type
--means that the permission is not enabled
-r Read permission, for files, is to read the file content. For the directory, it is to view the file under the directory
-w write permission, for the file, it is to modify the file content. For directories, delete and modify files in the directory.
-x execute permission, the file name can be used to call the internal program of the file. For directories, you can enter the directory.
How to set common permissions
- Copy permissions by command
chmod --reference=/mnt/test /mnt/westosfile1
- Modify
chmod+ugo+ -=rwx+file or directory by character mode
chmod+a=-+rwx+file or directory
- Modify
r=4 w=2 r=1
digitally chmod 777 /mnt/westosfile1
System default permission setting
为了系统的安全,我们需要保留系统的一部分权力,那么如何保留权利?
>umask ##表示系统保留权限
>umask+权限值 ## 可以临时设定权限值
>永久设定权限值
需要修改俩个文件,一个是/etc/bashrc,shell配置文件,另一个是/etc/profile系统配置文件
vim /etc/bashrc
vim /etc/profile
File permissions: 777-umask-111
Directory permissions: 777-umask
The initial permissions of the file can be seen in the figure=777-umask-111=777-022-111=644 that is rw- r--r
--Initial permissions of the directory=777-umask=777-022=755 i.e. rwxr-xr-x
Special permissions in the system
- stickyid ##针对目录,可以在目录下创建删除文件,但是只有root和文件所有者可以删除自己的文件。
- 命令:chmod o+t /mnt/test/
- 或者:chmod 1777 /mnt/test
- suid ##只针对可执行的二进制文件(c程序)
- ##使用suid,不论文件是谁发起,都以文件拥有者身份去执行
- 命令:chmod u+s /bin/cat
- sgid ##设置了sgid权限后,针对二进制文件,该命令发起的程序是以该命令所有组的身份去执行,和谁发起的无关
- ##针对目录,目录中新出现的文件会自动复制上层目录的所有组身份。
- 命令: chmod g+s /bin/cat chmod g+s /mnt/test/
For binary files For
directories:
facl permission list
-
Traditional permissions have only three identities (user, group, other), and there are three collocation permissions (rwx). There is no way to set specific permission requirements for a certain user or a certain group. Use ACL (File Access Control List, Access Control List) mechanism.
-
facl permission list matching order: file owner>specified file owner>file all group>specified file group>others
-
facl command:
- getfacl ##查看权限列表 - setfacl ##设置权限列表 - ##参数 -m 设定权限 - -x 删除指定用户 - -b 关闭列表功能
For files
指定组和用户并设置权限。
删除指定用户和组。
关闭facl列表
For catalog
setfacl -m u:zhy:rw /mnt/test ##只针对目录,设置目录的特殊所有者
setfacl -Rm u:zhy:rw /mnt/test ##针对目录,同时设置目录下文件的指定所有者,但不指定新出现文件的权限
setfacl -Rm d:zhy:rw /mnt/test ##针对目录中新出现的文件
attr permissions
There are some very important files in the system, we need to set it to be unavailable to any user, then we need to use this permission.
- chattr -+i 文件或者目录 ##针对文件,设置此权限后,文件不可以被添加和删除;
- ##针对目录,设置此权限后,不可以操作目录下的任何文件
- chattr -+a 文件或目录 ##针对文件,不可以删除和修改文件内容
- ##针对目录,可以在目录下添加文件,但是不可以删除文件
- lsattr 文件或者-d 目录 ##查看文件或者目录设置的权限