The most complete tutorial in Linux history

1. Introduction to Linux

1.Hardware and software

Computers as we know them are composed of hardware and software.

Hardware: a general term for various physical devices in a computer system consisting of electronic, mechanical and optoelectronic components;

To put it simply, hardware is something that can be seen and touched.

 

Software: It is the interface and bridge between users and computer hardware. Users communicate with computers through software. The Linux we want to learn is a type of software.

A complete computer:

 2. Operating system

1.1 Concept

The operating system is a type of computer software. It mainly serves as a bridge between the user and the computer, scheduling and managing computer hardware to work. A computer without an operating system is just a pile of unusable plastic.

1.2 Function

When a computer has an operating system, it is equivalent to having a soul. The operating system can

a. Scheduling the CPU for work

b. Schedule memory for work

c. Schedule hard disk for data storage

d. Scheduling network cards for network communication

e. Schedule the speaker to make a sound and the printer to print the content

......

"The user uses the operating system, and the operating system arranges the hardware to do the work."

1.3 Common operating systems

a. PC operating system: Windows, Linux, MacOS

b. Mobile operating system: Android, IOS, HarmonyOS

3. Linux operating system

1.1 Birth

Linux founder: Linus Torvalds

Linux was born in 1991, when the author was in college

        Because the founder often browsed news and processed emails when he was in college, he found that the existing operating system was not easy to use, so he decided to write an operating system in protected mode. This was the Linux prototype. He was 21 years old at the time, and later became popular among netizens all over the world. With support, it is now compatible with a variety of hardware and has become one of the most mainstream server operating systems.

1.2 Linux kernel

The Linux system consists of the Linux system kernel and system-level applications

1) The kernel provides the core functions of the system, such as: scheduling CPU, scheduling memory, scheduling system files, scheduling IO, etc.

2) System-level applications can understand the programs that come with the factory, allowing users to quickly get started with the operating system, such as file manager, task manager, picture viewing, music playback, etc.

For example: when playing music, whether the user uses their own music player or a third-party player installed by themselves, the player program calls the relevant functions provided by the kernel, and the kernel schedules the hardware CPU for decoding, sound generation, etc.

1.3 Linux distribution

The kernel is free and open source, which means

1) Anyone can obtain and modify the kernel and integrate system-level programs by themselves

2) Provides a complete package of kernel + system-level programs, called a Linux distribution

Anyone can package Linux. There are many Linux distributions on the market. The commonly used ones are as follows:

Among them, CentOS is the most used in China, and ubuntu is very popular abroad.

Centor download link

Tsinghua University open source software mirror station Ubuntu download link

1.4 Linux application areas

Today, various Linux distributions are used in various occasions, from embedded devices to supercomputers, and have established a position in the server field. Usually servers use LAMP (Linux + Apache + MySQL + PHP) or LNMP (Linux + Nginx + MySQL + PHP) combination.

Linux is currently not only used in homes and businesses, but is also very popular in government.

  • The Brazilian federal government is world-famous for its support of Linux.
  • There are news reports that the Russian military's own Linux distribution has come to fruition as the GHost project.
  • India's Kerala Federation plans to promote the use of Linux in high schools across the federation.
  • In order to achieve technological independence, the People's Republic of China exclusively uses Linux in Loongson processors.
  • Some regions in Spain have developed their own Linux distributions and are widely used in the government and education fields, such as gnuLinEx in the Extremadura region and Guadalinex in the Andalusia region.
  • Portugal also uses its own Linux distribution, Caixa Mágica, for Magalh?es laptops and e-escola government software.
  • France and Germany are also beginning to gradually adopt Linux.

 4. Virtual machine

With the help of virtualization technology, we can simulate computer hardware through software in the system and install a real operating system on the virtual machine hardware. In this way, a complete computer can be virtualized in the computer for us to learn the Linux system.

2. Common Linux commands

 1. General format of commands

command [-option] [parameter]
command:命令本身
option:[可选,非必填]命令的一些选项,可以通过选项控制命令行为细节
parameter:[可选,非必填]命令的参数,多数用于命令的指向目标等

Example

示例1
ls -l/home/haha
意思是以列表的形式,显示/home/haha目录下的内容
ls是命令本身,-l是选项,/home/haha是参数

示例2
cp -r test1 test2
意思是复制test1成为test2
cp是命令本身,-r是选项,test1和test2是参数

2. ls command

Function: List the contents of the directory

Syntax format:

1.格式:ls [-a -l -h] [Linux路径]
        -a -l -h是可选的选项
            -a:表示all的意思,即列出全部文件(包括隐藏的文件/文件夹)
            -l:表示以列表(竖向排列)的形式展示内容,并展示更多信息
            -h:表示以易于阅读的形式,列出文件大小,如K,M,G;且必须搭配-l一起使用
            Linux路径是此命令可选的参数
            注:当不使用选项和参数,直接使用ls命令本体,表示:以平埔的形式列出当前工作目录下的内容
2.语法中的选项是可以组合使用的(表示同时拥有):
如-a和-l的组合(表示同时拥有-l和-a的功能)可以按照ls -a -l 或 ls -la 或 ls -al方式书写
3.语法中的选项组合后还可以和参数一起使用:
如ls -al / 表示以列表的形式展示根目录下的所有(包括隐藏的)文件/文件夹

当前目录?
Linux系统的命令行终端,在启动的时候,默认会加载:
当前登录用户的HOME目录作为当前工作目录,所以ls命令列出的是HOME目录的内容
HOME目录:每个Linux操作用户在Linux系统的的个人账户目录,路径在:/home/用户名
如下图中的Linux用户是tom,其HOME目录是:/home/tom

Example 1 (no parameters and options)

 Example 2 (with parameters and options) 

 

3. Directory switching related commands (cd/pwd)

The cd command comes from English: Change Directory

The pwd command comes from English: Print Work Directory

Syntax format:

1.语法:cd [Linux路径]
cd命令无需选项,只有参数,表示要切换到哪个目录下
cd命令直接执行,不写参数,表示回到用户的HOME目录
2.语法:pwd
作用:查看当前所在的工作目录
pwd命令,无选项,无参数,直接输入pwd即可

Example 1 (using root user)

Example 2 (using ordinary user zh)

4. Relative paths and absolute paths

Relative path: A way of describing a path starting from the root directory. The path description starts with /

Absolute path: Taking the current path as the starting point, a way of describing the path. The path does not need to start with /

Example 1 (absolute path)

If you are an ordinary user: cd /home/zh/Desktop

Example 2 (relative path)

special path character

. represents the current directory. For example, cd./Desktop represents switching to the Desktop directory under the current directory, which has the same effect as cd Desktop.

.. represents the upper-level directory, for example, cd.. switches to the upper-level directory, and cd../.. switches to the upper-level directory.

~ represents the HOME directory. For example, cd~ can switch to the HOME directory or cd~/Desktop can switch to the Desktop directory in HOME.

Example 3

5. Create directory (folder) mkdir

mkdir from English: Make Directory

Syntax format

mkdir [-p] Linux路径
-p选项可选,表示自动创建不存在的父目录,适用于创建连续的多层级目录
参数必填,表示Linux路径,既要创建的文件夹的路径,相对路径或绝对路径均可

Example 1

Example 2

 Note: Creating a folder requires modifying permissions. Please ensure that all operations are within the HOME directory and do not operate outside HOME. Because of permission issues, creation outside HOME cannot be successful.

 6. File operation commands 1 (touch, cat, more)

Syntax format

1. touch Linux路径
touch命令无选项,参数必填,表示要创建的文件路径。相对、绝对、特殊路径字符均可以使用
2. cat Linux路径
cat同样没有选项,只有必填参数,参数表示:被查看文件路径。相对、绝对、特殊路径字符均可以使用
3. more Linux路径
more同样没有选项,只有必填参数,参数表示:被查看文件路径。相对、绝对、特殊路径字符均可以使用

2和3的区别
cat是直接将内容全部显示出来
more支持翻页,如果文件内容过多,可以一页一页的展示

Example 1 (Use of touch and cat commands)

Example 2 (Usage of more command)

Use the more command to view the files in the path: /etc/services

During viewing, use the space to turn pages;

Exit via q to view

 7. File operation commands 2 (cp, mv, rm)

cp comes from the English word copy

mv comes from the English word move

Syntax format

1. cp命令复制文件\文件夹 
语法:cp [-r] 参数1 参数2
-r选项,可选,用于复制文件夹使用,表示递归
参数1,Linux路径,表示被复制的文件或文件夹
参数2,Linux路径,表示要复制去的地方

2. mv命令
用于移动文件\文件夹
语法:mv 参数1 参数2
参数1,Linux路径,表示被移动的文件或文件夹
参数2,Linux路径,表示要移动去的地方,如果目标不存在,则进行改名,确保目标存在

3. rm命令
用于删除文件\文件夹
语法:rm [-r -f] 参数1 参数2 ...参数N
-r选项,可选,用于删除文件\文件夹
-f表示force,强制删除(不会弹出提示确认信息)
注:普通用户删除内容不会弹出提示,只有root管理员用户删除内容会有提示,所以一般普通用户用不到-f选项
参数1、参数2...参数N表示要删除的文件或文件夹路径,用空格隔开

4.通配符" * "用来模糊匹配
*表示通配符,即匹配任意内容(包括空)
示例
text*,表示匹配人何以test开头的内容
*test,表示匹配任何以test结尾的内容
*test*,表示匹配任何包含test的内容

Example 1 (Usage of cp command)

Copy files

copy folder

 Example 2 (Usage of mv command)

Move file, move target exists

Move the file, the target does not exist, rename it

Example 3 (Usage of rm command)

Delete Files

Delete folder

Example 4

Delete all folders starting with test

Precautions

rm is a dangerous command, especially when you are the root (super administrator) user, please use it with caution! ! !

Please do not use the following commands under the root administrator user

rm -rf/

rm -rf/*

The effect is equivalent to formatting the C drive on Windows.

8. grep, wc, pipe character “|”

 grep function: filter file lines by keywords from the file

Syntax format

1. grep命令
语法:grep [-n] 关键字 文件路径
选项 -n,可选,表示在结果中显示匹配的行的行号
关键字,必填,表示过滤的关键字,带有空格或其他特殊符号,建议使用" "将关键字包围起来
文件路径,必填,表示过滤文件内容的文件路径,可作为内容输入端口

2. wc命令
语法:wc [-c -m -l -w] 文件路径
-c 统计bytes数量
-m 统计字符数量
-l 统计行数
-w 统计单词数量
文件路径,被统计的文件,可作为内容输入端口

3. 管道符|
含义:将管道符左边的结果,作为右边命令的输入

Example 1 (Usage of grep command)

Use the touch command to create haha.txt in the HOME directory, edit and save the following content through the graphical page

My name is haha.It comes from China. 

Xixi very handsome and smart.

 Example 2 (Usage of wc command)

 Example 3 (application of pipe character “|”)

 Example 4 (application of nested pipe character “|”)

First create a file named test.txt, edit and save the following content through the graphical page

Zhang San is a student,he is very smart.

Li Si is a student,he is the monitor.

9. which and find commands

1. which命令
前面的一系列命令,他们的本体其实就是一个个的二进制可执行程序,就像windows系统中的.exe文件,是一个意思,我们可以通过which命令,查看所使用的一系列的程序文件存放在哪里
语法:which 要查找的命令
2. find命令
在图形化中,我们可以通过系统提供的搜索功能,搜索指定的文件
同样,在linux系统中。我们可以同通过find命令去搜索指定的文件
*按照文件名搜索
语法:find 起始路径 -name "被查找文件名"
注:也可以搭配通配符使用
*按照文件大小搜索
语法:find 起始路径 -size + |-n[KMG]
+、-表示大于小于
n表示大小数字
KMG表示大小单位,k(小写字母)表示kb,M表示MB,G表示GB
查找小于10kB的文件:find / -size -10k
查找大于100MB的文件:find / -size +100M
查找大于1G的文件:find / -size +1G

为了示例演示,获取最大权限,可以在整个系统完成搜索我们可以切换到root用户
命令:su -root
输入密码:******

 Example 1

 Example 2

10. echo, tail, redirection character

Syntax format

1. echo:在命令行内输出指定内容
语法格式:echo 输出内容
无选项,只有一个参数,表示要输出的内容,复杂内容可以用""包围
注:若输出内容带有空格或\等特殊符号,建议使用双引号包围

2. 反引号`
问题提出:在命令行输入echo pwd输出的时pwd,但我们的本意是想输出当前工作目录
解决方案--->使用反引号(飘号),被`包围的内容会被当作命令去执行
echo `pwd`

3. 重定向符:>和>>
>,将左侧命令的结果,"覆盖"写入到符号右侧指定的文件中
>>,将左侧命令的结果,"追加"写入到符号右侧指定的文件中

4. tail:查看文件尾部内容,跟踪文件的最新更改
语法:tail [-f -num] Linux路径
选项,-f,表示持续跟踪
选项,-num,表示,查看尾部多少行,不填默认是10行

Example 1: Use of echo command

Example 2: Use of backtick `

Example 3: Redirector

Example 4: Use of tail command

11. vi/vim compiler

concept

vi/vim is the abbreviation of visual interface and is the most classic text editor in Linux.

Like the text editor in the graphical interface, vi is an excellent choice for compiling text files under the command line.

Vim is an enhanced version of vi. It is compatible with all instructions of vi. It can not only edit text, but also has the function of shell programming. It can identify the correctness of syntax using fonts of different colors, which greatly facilitates program design and editing.

Three working modes of vi/vim editor

1) Command mode

In command mode, the case editor typed is understood as a command, and different functions are executed driven by commands.

In this mode, you can text edit the file content

2) Insert mode

Also known as edit mode, insert mode

In this mode, you can freely edit the file content

3) Last line mode

Starting with:, usually used for saving and exiting files

Commonly used shortcut keys in command mode

model Order describe
command mode i Enter input mode at the current cursor position
command mode a Enter input mode after current cursor position
command mode I At the beginning of the current line, enter input mode
command mode A At the end of the current line, enter input mode
command mode o Enter input mode on the line below the current cursor
command mode O Enter input mode on the line above the current cursor
input mode esc In any case, enter esc to return to command mode

model Order describe
command mode 0 Move the cursor to the beginning of the current line
command mode $ Move the cursor to the end of the current line
command mode pgUp Page up
command mode PgDn Page down
command mode / Enter search mode
command mode n Continue searching down
command mode M Continue searching upward

Syntax format

语法格式:
vi 文件路径
vim 文件路径
vim兼容全部的vi功能,后续将全部使用vim命令

注:
如果文件路径表示的文件不存在,那么此命令会用于编辑新文件
如果文件路径表示的文件存在,那么此命令用于编辑已有文件

Example

Editing a file through the vi/vim command will open a new window. At this time, this window is the command mode window. The command mode is the entrance and exit of the vi editor;

Entering the vi editor will enter the command mode, and then enter keyboard commands through the command mode to enter the input mode;

Finally, the input mode needs to return to the command mode, and then the bottom line command mode can be entered through the command

step

1) Use vim hello.txt to edit a new file. After execution, you will enter the command mode.

 Press enter key

2) In command mode, press i on the keyboard to enter input mode

3) Enter in the input mode: Hello Linux

4) After the input is completed, press esc to return to command mode

5) In command mode, press the keyboard: enter bottom line mode

6) Enter: wq in bottom line mode, save the file and exit the vi editor

7) View file content

 content has been written

3. Linux users and permissions

1. Linux root user

1.1 Overview

1) Whether it is Windows, MacOS or Linux, multi-user management mode is used for permission management.

2) In the Linux system, the account with the greatest authority is called root (super administrator), and in the early days we always used the ordinary user zh

3) The root user has the greatest system operation permissions, while the permissions of ordinary users are blocked in many places.

For example, when we use an ordinary user to create a file/directory in the root directory, we will be prompted "Insufficient permissions to create the file/directory"

The permissions of ordinary users are generally unrestricted in their HOME directory.

Once out of the HOME directory, in most places, ordinary users only have read and execute permissions, but no modification permissions.

1.2 su and exit commands

su comes from the English word: Switch User

grammar

语法:su [-] [用户名]
-符号是可选的,表示是否在切换用户后加载环境变量(后续将会讲解),建议带上
参数:用户名,表示要切换的用户,用户名也可以省略,省略表示切换到root

After switching users, you can use the exit command to return to the previous user, or you can use the shortcut key: ctrl+d

Note:

1) When using a normal user, you need to enter a password to switch to other users, such as switching to the root user.

2) Use the root user to switch to other users without a password and you can switch directly (after all, you are a super administrator)

 ​​​​​

1.3 sudo command

When we know the root password, we can switch to root through the su command to obtain maximum permissions; however, it is not recommended to use the root user for a long time to avoid system damage. At this point we can use the sudo command to authorize ordinary users and temporarily execute it as root.

grammar:

语法:sudo 其它命令
在其他命令之前,带上sudo,即可为这一条命令临时赋予root授权
但并不是所有的用户,都有权利使用sudo,我们需要为普通用户配置sudo认证

 Configure sudo authentication for ordinary users:

a. Switch to the root user and execute the visudo command. It will automatically open through the vi editor: /etc/sudoers

b. Add at the end of the file: zh ALL=(ALL) NOPASSWD:ALL

c. Finally save through wq

d. Switch back to the normal user and create the haha ​​folder in the root directory for testing.

 

You can find operations that can only be performed by super users. By using the sudo command, ordinary users can also perform operations.

2. Users and user groups

2.1 Users and user groups

1) Multiple users and user groups can be configured in the Linux system (users can join multiple user groups)

2) There are two levels of permission management in Linux: permission control for users and permission control for user groups; for example, for a certain file, you can control the permissions of the user or the permissions of the user group.

2.2 User group management

1) Create user group

grammar:

groupadd 用户组名

 2) Delete user group

grammar

groupdel 用户组名

3) Create user

grammar:

useradd [-g -d] 用户名
选项:-g指定用户的组,不指定-g,会创建同名组并自动加入,指定-g需要组已经存在,如已存在同名组,必须使用-g
选项:-d指定用户HOME路径,不指定,HOME目录默认在:/home/用户名

4) Delete user

grammar:

语法:userdel [-r] 用户名
选项-r,删除用户的HOME目录,不使用-r,删除用户时,HOME目录保留

5) View the groups to which the user belongs

grammar:

id [用户名]
参数:用户名,被查看的用户,如果不提供则查看自身

6) Modify the group to which the user belongs

grammar:

usermod -aG 用户组 用户名,将指定用户加入指定组

3. View permission control information

3.1 Understand permission information

(1, represents the permission control information of files and folders; 2, represents the user to which the file and folder belongs; 3, represents the user group to which the file and folder belongs)

1) Parse serial number 1, permission details

Permission details are divided into 10 slots:

- represents a file

d means folder

I stands for soft connection

User permissions User group permissions Other user rights
-or d or i r or - w or - x or - r or - w or - x or - r or - w or - x or -

Note: "-" means no such permission

2) The meaning of rwx

 r (read), indicates read permission

w (write), indicating write permission

x (Execute permission), indicates execution permission

3) The meaning of rwr is slightly different for different files and folders.

rwx Target files for folders
r Can view file contents You can view the contents of the folder, such as the ls command
w This file can be modified You can operate within the folder: create, delete, rename, etc.
x This file can be executed as a program You can change the working directory to this folder, that is, cd to enter

4) Example: drwxr-xr-x

1) d, indicating that this is a folder

2) rwx, indicating that the user to whom the folder belongs has rwx permissions on the file, that is, read, write, and execute permissions;

3) rx (the first one) means that the user group to which the folder belongs has rx permissions on the file, that is, read and execute permissions; no write permissions;

4) rx (the last one) means that other users have rx permissions on the folder, that is, they have read and execute permissions; they have no write permissions;

4. chmod command

5. chown command

Guess you like

Origin blog.csdn.net/qq_61902168/article/details/128267522