Home >System Tutorial >LINUX >Detailed explanation of shell operating principle and Linux permissions
Linux, strictly speaking, is the core of an operating system. In other words, ordinary users cannot call the core directly. In order to communicate with the core, we need to go through the core's "shell" program, also known as the shell. Shell is a command line interpreter that receives commands entered by the user and converts them into operation codes executed by the system. Shell can call different system functions to complete different operations depending on the type of command. Therefore, we can use Shell to execute a variety of commands and operations to control and manage the system. In short, the Shell acts as a bridge between the user and the underlying kernel.
How to understand? Why can't we use kernel directly?
“
From a technical perspective, the simplest definition of Shell: command interpreter (command Interpreter) mainly includes:
”
将使用者的命令翻译给核心(kernel)处理。 同时,将核心的处理结果翻译给使用者。
“
Note: shell is the collective name for all shell programs, and bash is a specific shell. For example: centos 7 shell: bash
”
shell是做命令行解释的 对系统有危害性的命令会被阻止从而保护操作系统 其好处为子进程出现任何问题都不会影响父进程shell
2.1 The concept of permissions
Command: su [username]
Function: Switch users.
“
For example, to switch from the root user to the ordinary user, use su user. To switch from the ordinary user user to the root user, use suroot (root can be omitted). At this time, the system will prompt you to enter the password of the root user.
”
2.2 Permission Management
“
Permissions essentially determine whether someone can do something.
”
“
Files are affected by people and files are affected by their own characteristics (thing attributes), so file permissions = people and things attributes
”
File permission attributes: r (read), w (write), x (execution permission)
Person (not a specific person, but a role): owner, group, other (other)
“
Use the character corresponding to the first column of the multi-column attribute displayed by the command ls -l (ll) to distinguish its file type.
”
“
There is no need to list other, because the one who is not the owner or the group to which it belongs is other
”
Help understand – owner and group
“
Let’s take a simple example. Within a company, two project teams have to complete the same code task. They compete with each other, but the company only provides them with one server. They complete the The code needs to be submitted. This is the code you wrote. It must be only for you and your team leader to see it, and not for your competitor group to see it. This gives rise to the concept of belonging groups.
”
file type
i. Read (r/4): For files, Read has the permission to read the file content; for directories, it has the permission to browse the directory information
ii. Write (w/2): For files, Write has the permission to modify the file content; for directories, it has the permission to delete files in the moved directory
iii. Execution (x/1): execute For files, it has the permission to execute the file; for directories, it has the permission to enter the directory
iv. “—” means that you do not have the permission
2.3 Representation method of file permission value
Character representation method
Linux means | illustrate | Linux | illustrate |
---|---|---|---|
r – – | Read only | – w – | Writable only |
– – x | Executable only | r w – | Readable and writable |
– w x | Writable and executable | r – x | Readable and executable |
r w x | Readable, writable and executable | – – – | No permission |
How to represent octal values
Permission symbol (read, write and execute) | Octal | binary |
---|---|---|
#r | 4 | 1 0 0 |
w | 2 | 0 1 0 |
x | 1 | 0 0 1 |
r w | 6 | 1 1 0 |
r x | 5 | 1 0 1 |
w x | 3 | 0 1 1 |
r w x | 7 | 1 1 1 |
– – – | 0 | 0 0 0 |
权限更改
$ chmod 777 text.c $ chmod 000 text.c $ chmod 640 text.c
2.4 文件访问权限的相关设置方法
chmod
R -> 递归修改目录文件的权限
说明:只有文件的拥有者和 root 才可以改变文件的权限
① 用户表示符 +/-= 权限字符
示例:
“
注意:chmod 可以给拥有者,所属组,其他用户同时修改权限,中间用逗号隔开
”
“
如果要修改不是自己的文件的时候需要 sudo 临时权限提升或者直接切成 root 身份
”
chown
功能:修改文件的拥有者
格式:chown [参数] 用户名 文件名
实例:
$ sudo chown root test.c// 修改拥有者 $ sudo chown lighthouse test.c// 修改拥有者 $ sudo chown :lighthouse test.c// 修改所属组 $ sudo chown lighthouse:lighthouse test.c// 可以将拥有者、所属组同时修改 $ sudo chown root:root test.c// 可以将拥有者、所属组同时修改
chgrp
功能:修改文件或目录的所属组
格式:chgrp [参数] 用户组名 文件名
常用选项:-R 递归修改文件或目录的所属组
实例:
$ sudo chgrp root text.c $ sudo chgrp lighthouse text.c
2.4 修改文件的掩码
umask
功能:查看或修改文件掩码
语法:umask 权限值
新建文件夹默认权限 = 0666
新建目录默认权限 = 0777
“
但是我们观察到,新建的文件和目录并不是默认的起始权限,这里是什么原因呢?
”
“
原因就是创建文件或目录的时候还要受到 umask 的影响。假设默认权限是 mask,则实际创建的出来的文件权限是:umask & ~umask
”
说明:将现有的存取权限减去权限掩码后,即可产生建立文件时预设权限。超级用户默认掩码值为 0022,普通用户默认为 0002。
2.5 file 指令
file
功能说明:辨识文件类型。
语法:file [选项] 文件或目录…
常用选项:
-c 详细显示指令执行过程,便于排错或分析程序执行的情形。
-z 尝试去解读压缩文件的内容。
面试题:进入一个目录要什么权限?
x r w
新发现:
“
就是只要用户具有目录的写权限, 用户就可以删除目录中的文件, 而不论这个用户是否有这个文件的写权限.我创建的一个文件, 凭什么被你一个外人可以删掉
”
结论
如果目录本身对 other 具有 w 权限,other 可以删掉任何目录下的东西
如果目录本身对 other 没有 w 权限,other 则不可以删除
“
我们的需求:other 可以在特定的目录下创建文件并写入,但是不想让任何人删除掉自己的文件
”
这里为了解决这个不科学的问题,Linux 引入了粘滞位的概念
粘滞位
语法:chmod +t 目录名
功能:给目录加上粘滞位
注意
“
Can only be set for directories, generally restricting other permissions. For directories with the sticky bit set, only the owner of the file and the root user can delete it, and others cannot delete it
”
Example:
“
There will be a lot of temporary data when there are multiple people or the system, and all temporary files are placed in the
/tmp
directory of the system. All permissions need to be released, but you only want the owner of the file to delete his or her own file. This requires setting the sticky bit”
The above is the detailed content of Detailed explanation of shell operating principle and Linux permissions. For more information, please follow other related articles on the PHP Chinese website!