Detailed explanation of Linux operating system (most detailed)

Linux operating system

1. Basic introduction to linux operating system

Linux is a Unix-like operating system that is free to use and spread freely. So what is a unix-like operating system? It was developed in 1969 by Ken Thompson, the father of Unix, at the Bell Labs of AT&T (American Telephone & Telegraph, now the second largest operator in the United States, with the first largest operator being Verizon Wireless). At that time, it was completely Out of curiosity or interest.

​ In July 1974, Unix first came into contact with the outside world. When Unix reached the fifth edition, it was provided to universities for teaching purposes with the agreement "for educational purposes only" and became a model in operating system courses at that time. Textbook. Various universities and companies began to make various improvements and extensions to Unix through Unix source code. As a result, Unix became widely popular. In 1979, the V7 version of Unix was released, and the license began to prohibit universities from using Unix source code, including learning in classes. By 1980, Unix began to develop out of control, with various variants emerging.

The Linux operating system includes, but is not limited to, all the functions of Unix. It is open source and free. You can obtain its source code without paying any fee, and you can modify it according to your own needs. The Linux operating system was written by Linus Benedict Torvalds when he was in college and invited everyone to improve it. Since then, the Linux operating system has become the preferred operating system in the server field.

The well-known operating systems such as Centos, redhat, and ubuntu are all developed based on the Linux kernel (kernel) and form their own products. So in this course, we use the ubuntu operating system to explain the basic use of the Linux operating system.


2. Installation of Linux operating system

​ Refer to the video for the installation of the ubuntu operating system and the installation of link tools. The video address is:


3. Linux directory structure

The directory structure of Linux is very different from that of Windows. Windows has the concept of drive letters, such as C drive, D drive, etc. The directory structure of Linux is similar to an upside-down tree, with the root directory at the top: as shown in the figure below

Insert image description here

directory name effect
/bin Store the most frequently used commands
/boot Store some core files when starting the Linux operating system
/etc Store system management and configuration files
/dev Used to store device files
/home Directory of stored users. Each user has his own directory.
/opt The location where additional installed optional application packages are placed. Under normal circumstances, we can install tomcat, etc. here.
/usr Used to store system applications, the more important directory /usr/local is the local system administrator software installation directory
/was It is used to store files that need to change data during operation. It is also an overflow area for some large files, such as log files of various services (system startup logs, etc.).

4. Basic operations of Linux system

1. Change directory
  • cd /test: Switch to the /test directory for operation.
  • cd -: Switch back to the last edited directory.
  • cd ~: Switch back to the current user's home directory.
  • cd ...: Switch to the directory one level above the current directory.
2. View file content
  • cat: View the entire contents of a file
  • head: View the first 10 lines of the file content by default
  • tail: View the last 10 lines of the file content by default
  • more: View the file contents in pages. Use the space bar to page down, but you can only page down.
  • less: View the file contents in pages. Use the space bar to page down. You can also use PgUp and PgDown to page up and down.
3. View the contents of the folder
  • ls(list): View the contents of a folder

  • ls -a: View the contents of a folder, including hidden files

  • ls -l (can be abbreviated as ll): View detailed information about the contents of a folder

Insert image description here

查看到文件夹的详细信息后,会看到有七栏,那么我们分别来解释一下是什么意思:

​ The first column: There are 10 symbols. The first symbol represents the attributes of the file, - represents the file, d is the directory, and l is the connection file; the following 9 are divided into three groups, each group is rwx, and r is Readable, w is writable, x is executable, the first group represents the permissions of the current user, the second group is the permissions of the user group; the third group is the permissions of other users.

​ The second column: It is the number of files or folders. If it is a file, it is 1. If it is a directory, it is the number of folders in the directory, including the current directory and the upper-level directory.

​ The third column: The user to whom the current file or folder belongs.

​ The fourth column: the user group to which the current file or folder belongs.

​ Column 5: The size of the current file or folder, in KB.

​Column 6: Creation time.

​ Column 7: The name of the file or folder or link.

4. Relative path, absolute path

To understand the relative path, you must first understand the two concepts of . and.... One dot represents the current directory, which is the directory where we are currently operating; two dots represent the upper-level directory.

Relative path, the so-called relative must have a reference object, for example, two classmates A and his deskmate B, and they are sitting together at this moment. If someone asks A now: "Where is B?", and A replies: "B is beside me.", then A's answer at this time is to use himself as a reference, and B is beside him.

​ The absolute path is a top-level address, and we all know this address. For example, "Universe", "Earth", "China", "Beijing", etc. can be selected as our top-level address. We also use the above mentioned Take the two deskmates A and B mentioned above as an example. A has another answer: "He is in the Xth row and the One way to answer is to use the earth as the top-level directory and then search down layer by layer.

​ Then in the Linux operating system, the reference object selected by the relative path is the directory where we are currently performing the operation; the top-level directory selected by the absolute path is our root (/) directory.

5. Creation of folders and files
  • touch test: Create a file named test.
  • mkdir test: Create a folder named test.
  • mkdir -p /a/b: Create directory a, and then create directory b under directory a.
6. Deletion of files and folders
  • rm test: Delete the file named test or the empty folder, and you will be asked whether you want to delete it.
  • rm -f test: Delete the file named test or the empty folder without prompt.
  • rm -rf test: Delete the file or folder named test. -r means recursion.
7. Copy of files or folders
  • cp ./test /data: Copy the test file in the current directory to the /data directory.
  • cp /test/a.txt /datas: Copy the a.txt file in the /test directory to the /datas directory.
  • cp -r /test /datas: Copy the /test directory to the /datas directory. -r means recursion, regardless of whether the /test folder is empty or not.
  • cp -r /test/* /datas/: Copy all contents in the /test directory to the /datas directory.
8.Move files and folders
  • mv test /datas: Move the test file or folder in the current directory to the /datas directory.
  • mv /test/* /datas: Move all contents in the /test directory to the /datas directory.
9. Repeat commands for files and folders
  • mv test test1: Rename the test directory or file to test1.

5. Uploading and downloading of files and folders

​ We can directly use the tools provided by xshell to upload and download files.

Insert image description here


6. Use of vi and vim

​ vi is the text editor that comes with the Linux operating system. Its function is very powerful. We must master its use to lay the foundation for us to modify various configurations or write script files. Its shortcut key commands are as follows:

Order meaning command mode
Esc (emphasis) Enter command line mode from edit mode edit mode
i (emphasis) Insert before current character edit mode
a Insert after current character edit mode
A Insert at the end of the current line edit mode
I Insert at the beginning of the current edit mode
o (emphasis) Go to the next line of the current line for editing edit mode
O (emphasis) Go to the previous line of the current line for editing edit mode
yy (emphasis) Copy a line edit mode
p (emphasis) Paste the line copied by yy edit mode
dd (emphasis) Delete a line edit mode
h (emphasis) Move cursor left edit mode
l (emphasis) Move cursor right edit mode
j (emphasis) Cursor moves up edit mode
k (key point) Cursor moves down edit mode
G Move cursor to end of file command line mode
gg Move the cursor to the beginning of the file command line mode
q Exit file command line mode
:wq (emphasis) Save and exit command line mode
:set nu! (emphasis) Set or cancel line number command line mode
u (emphasis) Undo last operation command line mode
w The cursor jumps to the first letter of the next word command line mode
b The cursor jumps to the first letter of the previous word command line mode
$ The cursor jumps to the end of the line command line mode
0 The cursor jumps to the beginning of the line command line mode
:/public Find the word public command line mode
n Jump to next found content command line mode
N Find the last found content command line mode
:/public\c Case-insensitive search command line mode
:%s/abc/QQ Replace the first abc of the current line with QQ command line mode
:%s/abc/QQ/g Replace all abc in the entire line with QQ command line mode
:%s/abc/QQ/gi Replace all abc in the full text with qq, ignoring case, command line mode
:5,12s/abc/QQ/gi Replace all abc in lines 5 to 12 with QQ, ignoring case. command line mode
ctrl + j Implement newline operation in edit mode edit mode
Number+G Jump directly to the corresponding line command line mode

7. Content filtering

  • grep abc test.txt: Find all lines containing abc in the test.txt file

8. Pipeline Operation

** Pipeline operation is to use the result of the previous operation as the data source of the next operation, using the "|" symbol. **For example:

  • ll | grep abc: Find the folder or file whose name contains abc in the current folder.

Exercise: 1. View the contents of lines 5 to 8 of the /etc/passwd file. (head -8 passwod | tail -4)

2. Check the contents of the 10th to last line to the 3rd to last line of the /etc/passwd file.

9. Process management

1. View all processes
  • ps -ef: View detailed information about all running processes in the current operating system. What we need to focus on is the "process number"
  • ps -ef | grep tomcat: View information about the tomcat process.
2. Close the process (kill the process)

Before closing the process, we need to check the pid of the process that needs to be killed, and then close the process through the kill command. Use the kill -l command to view the methods that can be used to kill the process, as shown in the following figure:

Insert image description here

Then the way to kill the process is as follows:

  • kill -kill 5678 or kill -9 5678: Kill the process with process number 5678.

​ 为什么可以使用 -kill 或者 -9 呢?我们通过kill -l 可以看到杀死进程的方式有64种,所有的方式都形如SIG***,我们将SIG去掉后的单词,就可以作为kill后的参数;又或者我们直接使用该命令对应的变好,例如 SIGKILL 对应的编号为9,所以我们也可以使用 -9 作为参数。

九.服务管理

  • systemctl: 查看所有的服务。
  • systemctl status 服务名: 查看服务状态
  • systemctl stop 服务名: 关闭服务
  • systemctl restart 服务名: 重启服务。

关闭防火墙命令: systemctl stop firewalld

十.软件的解压与压缩

​ 在linux操作系统中我们最常见的两种文件是tar.gz和zip文件,那么针对这两种文件我们有不同的压缩和解压缩的方式。

tar.gz文件的解压、压缩,在实际工作中参数v不需要加,可能会导致机器卡死:

  • tar -zxvf jdk-8u60-linux-x64.tar.gz: 将jdk-8u60-linux-x64.tar.gz文件解压到当前目录。
  • tar -zxvf jdk-8u60-linux-x64.tar.gz -C /test: 将jdk-8u60-linux-x64.tar.gz解压到 /test目录下。
  • tar -zcvf hello.tar.gz jdk1.8.0_60: 将 jdk1.8.0_60文件夹压缩成hello.tar.gz文件。

zip文件的压缩与解压,在使用zip解压缩文件需要安装zip软件:

  • unzip apache-tomcat-8.5.2-src.zip: 将apache-tomcat-8.5.2-src.zip文件解压到的当前目录。
  • unzip apache-tomcat-8.5.2-src.zip -d /test: 将apache-tomcat-8.5.2-src.zip文件解压到 /test目录下。
  • zip -r apache.zip apache-tomcat-8.5.2-src: 将apache-tomcat-8.5.2-src文件压缩成apache.zip文件。

注意:在使用unzip或者zip命令的时候,会报错,原因是因为没有zip的这个软件,需要使用yum install zip这个命令进行安装。

十一.用户、用户组的管理

1.用户相关的操作
  • passwd qf: 修改qf这个用户的密码。
  • useradd test: 添加test用户。
  • cat /etc/passwd: 查看系统中拥有多少用户。
  • su - test: 切换到test用户。
  • userdel test: 删除test用户。
  • userdel -r test: 删除test用户,并删除其家目录。
2.用户组相关操作
  • groupadd test: 添加名为test的用户组。
  • cat /etc/group: 查看所有的用户组。
  • groupdel test: 删除test用户组。
  • useradd -m test -g test: 创建一个test用户,并指定其用户组为test。
  • usermod test -g abc: 将test用户的组改为abc.

十二. yum的方式安装软件

12.1 配置yum源

/etc/yum.repo.d/Centos-Base.repo 进行备份:

mv CentOS-Base.repo CentOS-Base.repo.bak

下载阿里的yum源:http://mirrors.aliyun.com/repo/Centos-7.repo, 然后将文件上传到/etc/yum.repo.d/, 命名为:Centos-Base.repo

12.2 安装软件

安装 unzip: yum install -y unzip

十三. 文件权限的管理

​ 前面我们已经讲过,对于一个文件或者文件夹,我们用户和用户组可以有rwx的权限。那么我们可以通过chmod命令修改文件的读写和执行权限。

  • chmod u+w profile: 将profile文件所属的用户加入写权限。
  • chmod 775 profile: 将profile文件所属的文件拥有rwx权限,用户组拥有rwx权限,其他用户拥有r-x权限。

写权限:a) 对于文件夹,写权限表示是否可以在文件夹下新建文件(文件夹),以及删除删除。

​ b) 对于文件来说,就是无法写入内容。

执行权限:a) 对于文件,表示进入文件夹的权限;就算拥有了写权限,那么也是无法进入的。

​ b) 对于文件来讲,一般都是shell脚本(.sh结尾的文件),是否可以执行。

读权限:对于文件来说,是无法查看文件内容的。

13.1 文件夹的授权

apache-tomcat-8.5.59 整个文件夹按照递归的方式,将所有权授予 test 用户。

chown -R test apache-tomcat-8.5.59

十四. 软件的安装

1.jdk的安装

​ a. 解压 tar -zxvf jdk-8u60-linux-x64.tar.gz

​ b.进入到jdk的家目录,拷贝其家目录的路径。

​ c.配置环境变量,需要修改/etc/profile文件,加上如下两行内容

export JAVA_HOME=/datas/jdk1.8.0_60

export PATH=$JAVA_HOME/bin:$PATH

​ d.刷新环境变量:source /ect/profile

2.tomcat的安装

​ a.解压:tar -zxvf apache-tomcat-8.5.42.tar.gz

​ b.进入到/datas/apache-tomcat-8.5.42/bin/

​ c. ./startup.sh

3.mysql的安装

第一步,安装wget:

yum install -y wget   #安装wget

第二步,安装mysql专有yum源:

wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

yum localinstall -y mysql57-community-release-el7-7.noarch.rpm

第三步,导入密钥

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

第四步,安装mysql:

yum install -y mysql-community-server

第五步,启动mysql:

systemctl status mysqld
systemctl start mysqld

第六步,进入mysql:

mysql -uroot -p  #会提示输入密码,/var/log/mysqld.log文件,中记录着mysql的临时密码。

第七步,重置为自己的密码:

alter user 'root'@'localhost' identified by 'Ab123456cd!!';

第八步(可选),如果想让其他机器的用户访问该myql,需要授权:

grant all on *.* to 'root'@'%' identified by 'Ab123456cd!!';  #授权
flush privileges; 

第八步,开启自动启动:

systemctl enable mysqld

十五. 磁盘扩容

​ 磁盘扩容使用到LVM(Logic Volume Manager)技术.

​ a. 在虚拟机上增加一块磁盘。

​ b. 对磁盘进行分区:fdisk /dev/sdb​

root@ubuntu:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x927bf0e2.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): 

Created a new partition 1 of type 'Linux' and of size 10 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

​ c. 对磁盘格式化:mkfs -t ext4 /dev/sdb1,可以通过mount命令查看文件的格式。

root@ubuntu:~# mkfs -t ext4 /dev/sdb1
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 2621184 4k blocks and 655360 inodes
Filesystem UUID: e7746867-675b-4c59-9fe1-7e0011e4fc8a
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

​ d. 创建物理卷:pvcreate /dev/sdb1

root@ubuntu:~# pvcreate /dev/sdb1
  WARNING: Device for PV iM84L6-hkJb-P91v-J48U-gGeD-uBhp-Xf6tGv not found or rejected by a filter.
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.

​ e. 查看所有的物理卷:pvscan

root@ubuntu:~# pvscan
  PV /dev/sda3   VG ubuntu-vg       lvm2 [<19.00 GiB / <15.00 GiB free]
  PV /dev/sdb1                      lvm2 [<10.00 GiB]
  Total: 2 [<29.00 GiB] / in use: 1 [<19.00 GiB] / in no VG: 1 [<10.00 GiB]

​ f. 查看卷组:vgdisplay

root@ubuntu:~# vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       1024 / 4.00 GiB
  Free  PE / Size       3839 / <15.00 GiB
  VG UUID               41dd8K-nWqX-rrVd-seqy-dW6H-ypLL-8Sd8vw

​ g. 将 /dev/sdb1加入到 ubuntu-vg这个组:vgextend ubuntu-vg /dev/sdb1

root@ubuntu:~# vgextend ubuntu-vg /dev/sdb1
  Volume group "ubuntu-vg" successfully extended

​ h.再次查看卷组信息:vgdisplay

root@ubuntu:~# vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               28.99 GiB
  PE Size               4.00 MiB
  Total PE              7422
  Alloc PE / Size       1024 / 4.00 GiB
  Free  PE / Size       6398 / 24.99 GiB
  VG UUID               41dd8K-nWqX-rrVd-seqy-dW6H-ypLL-8Sd8vw

​ i. 查看物理卷信息:pvscan

root@ubuntu:~# pvscan
  PV /dev/sda3   VG ubuntu-vg       lvm2 [<19.00 GiB / <15.00 GiB free]
  PV /dev/sdb1   VG ubuntu-vg       lvm2 [<10.00 GiB / <10.00 GiB free]
  Total: 2 [28.99 GiB] / in use: 2 [28.99 GiB] / in no VG: 0 [0   ]

​ k. 扩展逻辑卷:lvextend -L +5G /dev/ubuntu-vg/ubuntu-lv

root@ubuntu:~# lvextend -L +5G /dev/ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from 4.00 GiB (1024 extents) to 9.00 GiB (2304 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

​ l. 刷新磁盘:resize2fs /dev/ubuntu-vg/ubuntu-lv

root@ubuntu:~# resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 2359296 (4k) blocks long.

十六. nginx安装

第一步,一些准备工作

yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel

第二步,下载解压,下载地址:http://nginx.org/en/download.html,然后上传到linux上,然后解压

第三步,进入到解压目录,开始安装

./configure 
make
make install

第四步,查看nginx安装地址

whereis nginx

第五步,进入到sbin目录下,启动 nginx

./nginx

十七. 前后端分离项目部署流程

17.1 服务端

先根据实际的情况修改响应的配置:

  1. 数据库的地址,用户名、密码等信息;
  2. jwt的过期日期

将spring-boot项目打包,打包的方式,执行maven命令:package,会在target下生产一个jar包,将jar包上传到 Linux,然后启动springboot项目,启动的命令如下:

java -jarmall-manager-backend.jar  --server.port=8080
17.2 前端部署

第一步,安装nginx

第二步,修改响应的配置信息,例如修改 @/utils/request.js 文件中服务端的请求地址,以及请求token过期的跳转地址。

第二步,前端项目进行打包,打包的命令参考 package.json 文件,命令如下:

npm run build:prod

执行完如上命令之后,会在项目下产生一个 dist 目录。

The third step is to upload the contents of the dist directory to htmlthe directory under the nginx installation directory. You can access it by starting nginx.

17. Other commands

  • du -h: View the size of the folder, which will be displayed in a tree structure.
  • du -h --max-depth=1: Only view the size of one layer of folders (h is human, which is viewed in a human-readable way)
  • ll -h: View the file size.
  • lvmdiskscan: Detect various types of storage devices.
  • pwd: View the absolute path where the current operation is located.
  • su - root: Switch to the root user.
  • echo “hello world” > test.txt: Write “hello world” into the test.txt file, overwriting the existing content in the file.
  • echo "How are you?" >> test.txt: Append "How are you?" to the end of the test.txt file.
  • netstat -tnpl View ports.
  • systemctl stauts firewalld: View the status of the firewall.
  • systemctl disable firewalld: Disable the firewall at boot.
  • find / -name 'mysql': Recursively search for files or folders containing mysql in the / directory.
  • find / -name '*mysql*': Recursively search for files or folders whose names contain mysql in the / directory.
  • top View system resource consumption

Guess you like

Origin blog.csdn.net/asdf12388999/article/details/128833863