Home > System Tutorial > LINUX > body text

Detailed explanation of shell operating principle and Linux permissions

王林
Release: 2024-02-09 14:39:13
forward
659 people have browsed it

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)处理。
同时,将核心的处理结果翻译给使用者。
Copy after login
shell 运行原理和 Linux 权限详解

Note: shell is the collective name for all shell programs, and bash is a specific shell. For example: centos 7 shell: bash

  • Compared with the Windows GUI, when we operate Windows, we do not directly operate the Windows kernel, but click through the graphical interface to complete our operations (for example, to enter the D drive, we usually double-click the D drive letter. Or run an application) .
  • The shell has the same function for Linux, mainly parsing our instructions and parsing the instructions to the Linux kernel. The feedback results are run through the kernel and parsed to the user through the shell.
  • Shell operating principle
shell是做命令行解释的
对系统有危害性的命令会被阻止从而保护操作系统
其好处为子进程出现任何问题都不会影响父进程shell
Copy after login
  • Help understanding:
  • If you are a boring and shy programmer, then shell is like a matchmaker
  • The operating system kernel is the pretty flower in your village that makes your heart beat.
  • You have fallen in love with Xiaohua, but are embarrassed to confess directly, then let your family find a matchmaker to help you propose marriage
  • You communicate directly with the matchmaker about everything, and the matchmaker conveys your thoughts to Xiaohua
  • And we found that the matchmaker’s surname was Wang, so we called her Wang Po, which corresponds to the bash we often use.
  1. Linux permissions

2.1 The concept of permissions

  • There are two types of users under Linux: super user (root) and ordinary user.
  • Super user: can do anything under the linux system without restrictions
  • Ordinary users: Do limited things under linux.
  • The command prompt of the super user is "#", and the command prompt of the ordinary user is "$".
shell 运行原理和 Linux 权限详解
  • User switching command

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.

shell 运行原理和 Linux 权限详解
  • Switch to root: Use su -, we can switch to the root user (enter the root account password at this time, the password will not be echoed on the screen)
  • Switch back to a normal user: Use su – username to switch back to a normal user (you do not need to enter a password at this time, but we do not recommend switching back like this, because when using the su – command, more bash processes will be created)
  • It is recommended to switch back to the normal user: enter exit or hit Ctrl d to return to the normal user

2.2 Permission Management

  • What are permissions

Permissions essentially determine whether someone can do something.

  • For people: For example, I am the owner of this house, so I can enter and exit the house freely
  • Targeting things: I want to eat a computer, I want to play League of Legends on bread, and watch a movie. You can eat bread, but computers cannot. You can eat bread, but you cannot eat computers; this includes two attributes.

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)

  • Classification of file visitors (person)
  • The owner of the file and file directory: u—User (Chinese civilian legal issues)
  • The user of the group to which the owner of the file and file directory belongs: g—Group (not much to say)
  • Other users: o—Others (foreigners)
  • File Types and Access Permissions (Thing Properties)
shell 运行原理和 Linux 权限详解

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.

shell 运行原理和 Linux 权限详解

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.

shell 运行原理和 Linux 权限详解

file type

  • d: Folder
  • -: Ordinary file
  • l: Soft link (similar to Windows shortcut)
  • b: Block device files (such as hard disk, optical drive, etc.)
  • p: pipe file
  • c: character device file (such as screen and other serial devices)
  • s: socket file
shell 运行原理和 Linux 权限详解
  • Basic permissions

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

shell 运行原理和 Linux 权限详解

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
shell 运行原理和 Linux 权限详解

权限更改

$ chmod 777 text.c
$ chmod 000 text.c
$ chmod 640 text.c
Copy after login
shell 运行原理和 Linux 权限详解

2.4 文件访问权限的相关设置方法

chmod

  • 功能:设置文件的访问权限
  • 格式:chmod [参数] 权限 文件名
  • 常用选项:

R -> 递归修改目录文件的权限

说明:只有文件的拥有者和 root 才可以改变文件的权限

  • chmod 命令权限值得格式

① 用户表示符 +/-= 权限字符

  • +: 向权限范围增加权限代号所表示的权限
  • -: 向权限范围取消权限代号所表示的权限
  • =: 向权限范围赋予权限代号所表示的权限
    用户符号:
  • u:拥有者
  • g:拥有者同组用
  • o:其它用户
  • a:所有用户

示例:

shell 运行原理和 Linux 权限详解

注意:chmod 可以给拥有者,所属组,其他用户同时修改权限,中间用逗号隔开

如果要修改不是自己的文件的时候需要 sudo 临时权限提升或者直接切成 root 身份

  • sudo chmod 用户表示符 +/-= 权限字符 文件名

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// 可以将拥有者、所属组同时修改
Copy after login
shell 运行原理和 Linux 权限详解

chgrp

功能:修改文件或目录的所属组
格式:chgrp [参数] 用户组名 文件名
常用选项:-R 递归修改文件或目录的所属组

实例:

$ sudo chgrp root text.c
$ sudo chgrp lighthouse text.c
Copy after login
shell 运行原理和 Linux 权限详解

2.4 修改文件的掩码

umask

功能:查看或修改文件掩码

语法:umask 权限值

shell 运行原理和 Linux 权限详解

新建文件夹默认权限 = 0666

新建目录默认权限 = 0777

shell 运行原理和 Linux 权限详解

但是我们观察到,新建的文件和目录并不是默认的起始权限,这里是什么原因呢?

原因就是创建文件或目录的时候还要受到 umask 的影响。假设默认权限是 mask,则实际创建的出来的文件权限是:umask & ~umask

shell 运行原理和 Linux 权限详解
  • 我们也可以通过修改文件的 umask 码值来修改文件的权限:
shell 运行原理和 Linux 权限详解

说明:将现有的存取权限减去权限掩码后,即可产生建立文件时预设权限。超级用户默认掩码值为 0022,普通用户默认为 0002。

2.5 file 指令

file

功能说明:辨识文件类型。

语法:file [选项] 文件或目录…

常用选项:

-c 详细显示指令执行过程,便于排错或分析程序执行的情形。

-z 尝试去解读压缩文件的内容。

shell 运行原理和 Linux 权限详解
  1. 目录权限

面试题:进入一个目录要什么权限?

  • 可读权限: 如果目录没有可读权限,则无法用 ls 等命令查看目录中的文件内容.
  • 可写权限: 如果目录没有可写权限,则无法在目录中创建文件, 也无法在目录中删除文件.
  • 可执行权限:如果没有目录可执行权限,则无法 cd 到目录中
x
r
w
Copy after login
  1. 粘滞位

新发现:

就是只要用户具有目录的写权限, 用户就可以删除目录中的文件, 而不论这个用户是否有这个文件的写权限.我创建的一个文件, 凭什么被你一个外人可以删掉

shell 运行原理和 Linux 权限详解

结论

如果目录本身对 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:

shell 运行原理和 Linux 权限详解

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

  1. Summarize
  • The executable permissions of a directory indicate whether you can execute commands in the directory.
  • If the directory does not have – )
  • And if the directory has -x permissions but not -r permissions, the user can execute commands and can cd into the directory. But because there is no read permission to the directory
  • Therefore, even if you can execute the ls command in the directory, you still do not have permission to read the documents in the directory.

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!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!