linux

Linux

image-20240706201444747

Pre-knowledge 前置知识

Hardware: A general term for various physical devices composed of electronic, mechanical and optoelectronic components in a computer system.

硬件:计算机系统中,电子,机械和光电元件等组成的各种物理装置的总称。

image-20240706201627741

Software: The interface and bridge between the user and the computer hardware, through which the user communicates with the computer.

软件:用户和计算机硬件之间的接口和桥梁,用户通过软件与计算机进行交流。

Computer = Hardware Software

计算机 = 硬件 + 软件

Operating system: the bridge between hardware and software. Arrange hardware work.

操作系统: 硬件和软件之间的桥梁。安排硬件干活。

常见操作系统:

window

linux

macOS

Android

ios

harmonyOS

Linux System Core

linux系统:系统内核 和 系统级应用程序

Linux system: system kernel and system-level applications

Kernel: 调度CPU、调度内存、调度文件系统、调度网络通讯、调度IO等。

Kernel: scheduling CPU, scheduling memory, scheduling file system, scheduling network communication, scheduling IO, etc.

System level Application: 出厂自带程序,可供用户快速上手操作系统。

System level Application: factory self-contained program for users to quickly get started with the operating system.

image-20240706202610741

image-20240706202649542

Where to Find core code of Linux?

Linux Release Version = Application + linux kernel

image-20240706202852326

What is Virtural Machine?

通过虚拟化技术,在电脑内,虚拟出计算机硬件,并给虚拟的硬件安装操作系统。

Through virtualization technology, in the computer, split a part of hardware, and install the operating system on the virtual hardware.

VMware

download address:

https://www.vmware.com/cn/products/workstation-pro.html

在mac中,使用VMware Fusion Pro

for Mac, download VMware Fusion

https://support.broadcom.com/group/ecx/productfiles?subFamily=VMware Fusion&displayGroup=VMware Fusion 13 Pro for Personal Use&release=13.5&os=&servicePk=520294&language=EN

free licence key

4C21U-2KK9Q-M8130-4V2QH-CF810 —> works for me

Reference link:

https://gist.github.com/PurpleVibe32/30a802c3c8ec902e1487024cdea26251

download Centos

https://www.centos.org/download/

image-20240706214109112

we need use thrid-part tool to connect linux System and control it.

FinalShell

https://www.hostbuf.com/t/988.html

打开linux系统,在终端查看ip地址

Open the Linux system and check the ip address at the terminal.

1
ifconfig

image-20240707213510648

在finalshell中配置

Configure in finalshell

image-20240707213640592

Virtual machine snapshot 虚拟机快照

虚拟机快照可以帮助我们保存当前linux的状态

Virtual machine snapshots can help us save the current state of Linux System.

然后我们可以随时通过snapshot 回到该状态

Then we can go back to that state at any time by snapshot.

image-20240707214914553

linux和win目录架构

Linux的目录结构是一个树型结构

Linux directory structure is a tree structure

Windows 系统可以拥有多个盘符,如C盘、D盘、E盘 Linux没有盘符这个概念,只有一个根目录/,所有文件都在它下面

The Windows system can have multiple disk characters, such as C disk, D disk, E disk. Linux does not have the concept of disk letter, only one root directory/, and all files are under it.

image-20240707215229740

Linux Basic Command

  1. 目录切换相关命令(cd/pwd)

Directory switch related commands (cd/pwd)

  1. 相对路径、绝对路径和特路径符

Relative, Absolute, and Special Path

  1. 创建目录命令(mkdir)

Create Directory Commands (mkdir)

4.1 文件操作命令part1(touch、cat、more)

File operation command part1(touch, cat, more)

4.2 文件操作命令part2(cp、mv、rm) 查找命令(which、find)

file operation command part2(cp, mv, rm) find command (which, find)

  1. grep、wc和管道符

grep, wc, and pipelining

  1. echo、tail和重定向符

echo, tail, and redirects

  1. vim编辑器

vim editor

命令通用格式

Command Common Format

1
command [-options] [parameter]

command:命令本身

The command itself

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

Some options of the [optional, non-required] command can control the behavior details of the command through the option parameter: [optional, non-required] command parameters, most of which are used to point to the target of the command, etc.

ls

1
ls [-a -1 -h] [Linux path]

列出目录下的内容

List the contents of current directory

-a 表示:all的意思,即列出全部文件(包含隐藏的文件/文件夹)

-a means: all, that is, list all files (including hidden files/folders)

-l 表示:以列表(竖向排列)的形式展示内容,并展示更多信息

-l means: display the content in the form of a list (arranged vertically) and show more information

1
2
3
ls -a
ls -al
ls -l

image-20240707220232501

1
ls -lh

增加单位显示

Increase the unit display

cd && pwd

Cd: 更改当前所在的工作目录。

CD command, change the current working directory

pwd: Print Work Directory。

image-20240707220852535

relevant path && absolute path

绝对路径:以根目录为起点,描述路径的一种写法,路径描述以/开头

Absolute path: a way to describe the path starting from the root directory. The path description starts/

相对路径:以当前目录为起点,描述路径的一种写法,路径描述无需以/开头

Relative path: A method of describing a path starting from the current directory. The path description does not need to start/.

1
cd

默认在home 目录

Default in the home directory

1
cd /.

在根目录

In the root directory

特殊符号 special sign

1
. 当前目录
1
.. 上一级
1
~ home目录

mkdir —> create folder

Make Directory

1
mkdir [-p] linux路径

添加-p —> 创建多个层级

Add-p —> Create multiple levels

1
mkdir -p itcase/good

touch —> 创建文件(create file)

Eg:

1
touch test.txt

ls -l 查改文件格式信息

image-20240721200127361

d开头的就是 folder 文件夹

-开头的就是file

cat

1
cat filename

查看文件内容

image-20240721200256064

more

1
more filename

more也是查看文件内容

But more supports page turning, cat can’t

空格翻页 SPACE page turning

q退出查询 q exit query

小结 Summary

image-20240721200753613

cp 复制文件夹/文件 Copy Folder/File

copy

1
2
cp [-r] 参数1 参数2
cp [-r] para1 para2

Para1: 被复制的文件/文件夹。 Para1: Copied files/folders.

Para2:要复制到去的地方。 Para2: Where to copy to go.

Mv 移动文件 Move files

move

1
2
mv [-r] 参数1 参数2
mv [-r] para1 para2

也可以当做是改名

image-20240721201711904

rm 删除文件 delete

1
2
rm [-r -f] 参数1 参数2 参数3 ... 参数N
rm [-r -f] parameter 1 parameter 2 parameter 3... parameter N

-r 删除文件夹 -r delete folder

-f 强制删除 -f Force Delete

通配符 Wildcard *

用来做模糊匹配 Used for fuzzy matching

image-20240721202024239

1
rm -r test*

删除所有test开头的文件/文件夹

Delete all files/folders beginning with test

1
rm -r *test*

切换root用户 Switch the root user

1
su root

image-20240721202302453

exit退出,切回原用户

exit, switch back to the original user

which 查看命令存放的文件地址

which —> View the file address where the command is stored

Eg:

1
which cd

image-20240721203004877

find 查询文件

find —> query file

按照文件名查找

1
2
find 起始路径 -name "被查出文件"
find start path-name "found file"

Eg:

从根目录开始查找 所有test的文件

Find all test files from the root directory

1
find / -name "test"

image-20240721203254396

other

1
find / -name "test*"

根目录开始 查找test开头的文件

Start from the root directory and find the file at the beginning of the test.

按照size大小查找 Find by size

1
find / -size +100M

查看大于100MB的文件 View files larger than 100MB

1
find / -size -10k

查看小于10KB的文件 View files smaller than 10KB

image-20240721203649697

Grep 从文件中过滤文件行 Filtering file lines from a file

1
2
grep [-n] 关键字 文件路径
grep [-n] keyword filepath

-n 结果中显示匹配的行的行号

-n shows the line number of the matched line in the result

关键字: 过滤的关键字

Keywords: filtered keywords

文件路径:过滤内容的文件路径

file path: file path to filter content

image-20240721204546472

wc 用来做数量统计

wc is used for quantitative statistics.

1
2
wc [-c -m -l -w] 文件路径
wc [-c -m -l -w] File Path

-c 统计bytes数量

-m 统计字符数量

-l 统计行数

-w 统计单词数量

Eg:

1
wc helloword

2行 11个单词 51个字节数

2 lines 11 words 51 bytes

image-20240721221743132

Pipeline symbol 管道符 |

grep + |

Eg:

1
cat helloworld | grep ithe

image-20240721222000896

Eg2:

cat helloword的输出 作为 wc的输入

The output of cat helloword as the input of wc

1
cat helloworld | wc

​ 2 11 51

Eg3:

1
ls /usr/bin | grep gtf

gtf

Eg4:

1
ls -l /usr/bin | wc -l

统计行数 Count the number of rows

1
cat helloworld | grep ithe | grep stock

ithe stock code is 0003.

echo 输出内容 —> Output Content

1
echo hello world

image-20240721222947872

重定向符号 >>>

Redirect symbols > and >>

> 是将左侧命令覆盖写入 文件

> is to overwrite the left command to the file.

>> 是将左侧命令追加写入文件

>> is to append the left command to the file

1
echo "hello" test.txt

image-20240721223256873

1
2
echo `ls` >> test.txt
cat test.txt
1
2
3
hello
hello world
Desktop Documents Downloads helloworld Music Pictures Public Templates test.txt Videos

image-20240721223545706

Tail 查看文件尾部内容 Viewing the contents at the end of a file

1
tail [-f -num] linux路径

-f 持续追踪

-num 查看尾部多少行,默认为10行

1
tail -1 test.txt
1
Desktop Documents Downloads helloworld Music Pictures Public Templates test.txt Videos

Vim Editor

vi/vim是visual interface的简称,是Linux中最经典的文本编辑器

VI is short for visual interface and is the most classic text editor in Linux.

三种工作模式

Three working modes

  1. Command mode

  2. Insert mode

  3. last line mode

image-20240726161606192

i 进入 insert mode

esc 退出 last line mode

0 进入当前行的末尾

$ 进入当前行的开始

yy复制当前行

p粘贴当前行

root

what is root

Root 拥有最大的操作系统权限

Root has maximum operating system privileges

how to switch user account

切换root

switch to root

su —> switch user

1
su - root

-表示切换user后,是否需要重新加载环境变量

-Indicates whether environment variables need to be reloaded after user switching

Exit 退出切换

1
exit

root —> 其它任意用户不需要密码

Root —> any other user does not need a password

配置sudo 命令 将该用户的该命令赋予root的权限

Configure the sudo command to give this command to root for this user

eg:

1
sudo mkdir test

user and user group

linux可以

  1. 配置多个用户

    1. Configure multiple users
  2. 配置多个用户组
    2. Configure multiple user groups

image-20240726163927937

创建用户组

create usergroup

1
sudo groupadd group1

删除用户组

delete usergroup

1
sudo groupdel group2

创建用户

create user

1
useradd [-g -d] username1

-g 指定用户的组,不指定,会创建一个同名group并把 user分入group

-d 指定用户HOME路径, 不指定,默认: /home/用户名

删除用户

delete user

1
userdel [-r] username2

-r 删除用户的HOME目录

不使用-r,删除用户时,HOME目录会保存

查看用户所属的组

Check the group to which the user belongs

1
id username3

image-20240726165814815

修改用户所属组

Modify the group to which a user belongs

1
usermod -aG groupname username

image-20240726165945249**

查看系统中有哪些用户

See which users are in the system

1
getent passwd

查看系统有哪些group

See what groups the system has.

1
getent group

image-20240726170523938

查看权限控制

View Permission Control

1
ls -l

image-20240726170953589

上面可以分为两部分: 文件控制权限 + 文件所属用户

The above can be divided into two parts.

File control permissions + users to which the file belongs

cotrol permission

image-20240726171317449

用户 用户组 其它用户

rwx

r: read

针对文件可以查看文件内容

w: write

针对文件表示可以修改此文件

x: excute

将文件作为程序执行

d 表示 该文件是否是 文件夹

d indicates whether the file is a folder

**修改权限 **—> chmod

modify control permissions

1
chmod [-R] 权限 文件

chmod [-R] permission file

-R 针对该文件夹内所有文件都修改权限

modify permissions for all files in this folder

eg:

1
chmod rwx --x --x test1

本人可以任意,其他人只能执行

I can do whatever I want, and others can only do it.

tips

r = 4

w = 2

x = 1

r-x ==>5

1
2
3
751 rwx rw- --x
777 rwx rwx rwx
752 rwx r-x -w-
1
chmod 777 test1

chown 修改用户组 Chown modify the user group

Can only be used in root user

1
chown [-R] [用户] [:] [用户组] 文件
1
chown [-R] [user] [:] [user group] file

Eg:

1
2
3
4
chown root hello.txt 将hello.txt所属用户修改为root
chown :root hello.txt 将hello.txt所属用户组修改为root
chown root:group1 hello.txt将hello。txt所属用户修改为root,用户组修改为group
chown -R root test 将文件夹test的所属用户修改为root并对文件夹内全部内容应用同样规则
1
2
3
4
5
6
7
8
chown root hello.txt 
Modify the user of hello.txt to root
chown:root hello.txt
Modify the user group of hello.txt to root
chown root:group1 hello.txt
The user of txt is modified to root, and the user group is modified to group.
Chown -R root test
modifies the user of the folder test to root and applies the same rules to everything in the folder.

实用tips

ctl + c 强制停止运行程序

ctl + c force stop running program

crl + d 退出程序,比如python交互界面

crl+d exits the program, such as the python interactive interface

history 查看历史命令

history View History Command

!+command —> 在history中从下往上找,找到第一个最近的同命令

ctrl + L 清理屏幕

1
clear 也行

ctrl+ L —> Clean up the screen

操作系统安装好软件

Operating system installed software

win

exe, msi file

mac

Dmg. Pkg file

linux

Yum

yum:RPM包软件管理器,用于自动化安装配置Linux软件,并可以自动解决依赖问题。

yum:RPM package software manager, used to automate the installation and configuration of Linux software, and can automatically resolve dependency issues.

1
yum [-y] [install | remove | search] softwareName

-y 自动确定,无需手动确定

install 安装

remove 删除

search 搜索

Eg: 安装 wget

1
sudo yum -y install wget

Ceotos 和 Ubuntu 是使用不同的包管理工具 Ceotos and Ubuntu are using different package management tools

Centos

yum

Ubuntu

apt

两者语法是一样的 The syntax is the same for both

查看系统是centos还是unbuntu

1
cat /etc/os-release

systemctl command

Linux系统很多软件(内置或第三方)均支持使用systemctl命令控制:启动、停止、开机自启

Many software (built-in or third-party) of Linux system supports the use of systemctl command control: start, stop, boot self-start

1
systemctl start|stop|status|enable|disable 服务名

系统内置服务

NetworkManager 主网络服务

network 副网络服务

firewalld 防火墙服务

sshd,ssh服务 远程登录服务

Eg:

firewalld

查看防火墙状态

1
systemctl status firewalld

关闭防火墙

1
systemctl stop firewalld

关闭开机启动firewall

1
systemctl disable firewalld

打开

1
systemctl enable firewalld

chrony

网络时间协议(NTP)客户端和服务器软件,用于在Linux系统中同步计算机时钟。它特别设计来在系统中维持精确的时间。

Network Time Protocol (NTP) client and server software for synchronizing computer clocks in Linux systems. It is specifically designed to maintain accurate time in the system.

1
sudo yum install chrony

httpd

安装apache服务器软件可以通过httpd服务名,配合systemctl进行控制

1
sudo yum install -y httpd

类似windows中的快捷方式

Similar to shortcuts in Windows

1
ln -s 参数1 参数2

-s: create softlink

参数1: 被链接的文件

参数2:要链接去的地方 (快捷文件)

Eg:

1
ln -s /etc/yum/conf ~/yum.conf

日期 和 时间

date 命令

1
date [-d] [+格式化字符串]

-d按照给定的字符串显示日期,一般用于日期计算

%Y年

%y 年份后两位数字

%M 月份

%d 日

%H 小时

%M 分钟

%S 秒

%s 自1970-01-01 00:00:00 UTC 到现在的秒数

Eg:

1
date +%Y-%m-%d

image-20240727174116024

ip地址 和 主机名

ip address and host name

每一台联网的电脑都会有一个地址,用于和其它计算机进行通讯

Every networked computer will have an address for communicating with other computers.

IP地址主要有2个版本,V4版本和V6版本(V6很少用,课程暂不涉及)

There are mainly 2 versions of IP address, V4 version and V6 version (V6 is rarely used and the course is not involved for the time being)

IPV4版本的地址格式是:a.b.c.d,其中abcd表示0~255的数字,如192.168.88.101就是一个标准的IP地址

The address format of IPV4 version is: a. B. c.d, where abcd represents a number from 0 to 255, for example, 192.168.88.101 is a standard IP address

1
ifconfig

可以通过命令:ifconfig,查看本机的ip地址,如无法使用ifconfig命令,可以安装:yum -y install net-tools

image-20240727174501260

特殊ip地址

127.0.0.1

指代本机

0.0.0.0

指代本机

在端口绑定中用来确定绑定关系

在一些IP地址限制中,表示所有IP的意思,如放行规则设置为0.0.0.0,表示允许任意IP访问

主机名 host name

1
hostname 

修改主机名

1
hostnamectl set-hostname xxx

域名解析 Domain name resolution

因为ip地址不好记,所以创建domain name,然后通过 domianresolver 解析域名。

Because the ip address is difficult to remember, create a domain name and then resolve the domain name through domianresolver.

DNS就是用来 提供ip的东西。

DNS is what is used to provide IP.

image-20240727175016341

  1. 先查看本机的记录(私人地址本)
  2. 再联网去DNS服务查询

配置固定IP Configure fixed IP

当前我们虚拟机的Linux操作系统,其IP地址是通过DHCP服务获取的。

At present, the IP address of the Linux operating system of our virtual machine is obtained through the DHCP service.

DHCP:动态获取IP地址,即每次重启设备后都会获取一次,可能导致P地址频繁变动

DHCP: Dynamic acquisition of IP address, that is, it will be obtained every time after restarting the device, which may lead to frequent changes in P address.

  1. 修改VMware

    在VMware Workstation(或Fusion)中配置Ip地址网关和网段(IP地址的范围)

  2. 修改Linux

    在Linux系统中手动修改配置文件,固定IP

网络传输

  1. ping查看是否联通

    Ping to see if it is Unicom

    1
    ping [-c num] ip或者域名

    -c num 只ping num次

    image-20240727180239670

  2. wget命令下载文件

    wget command to download files

    1
    wget [-b] url

    -b 后台下载 Background download

    Eg:

    wget download hadoop

    https://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-3.4.0/hadoop-3.4.0-aarch64.tar.gz

    1
    wget https://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-3.4.0/hadoop-3.4.0-aarch64.tar.gz

    image-20240727181028844

    后台下载

    1
    wget -b https://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-3.4.0/hadoop-3.4.0-aarch64.tar.gz

    查看进度

    1
    tail wget-log

    跟踪进度

    1
    tail -f wget-log
  3. curl命令发起网络请求

    use curl command initiates a network request.

curl 发送 http网络请求,用于: 下载文件, 获取信息等。

curl sends http network requests for: downloading files, obtaining information, etc.

1
curl [-O] url

-O: 用于下载文件,当url是下载链接时,可以使用该选项保存文件。

Eg:

查看你的ip地址

1
curl cip.cc

curl本质就跟浏览器打开网站一样

Curl is essentially like a browser opening a website.

端口 port

端口,是设备与外界通讯交流的出入口。端口可以分为:物理端口和虚拟端口两类。

The port is the entrance and exit for communication between the equipment and the outside world. Ports can be divided into two types: physical ports and virtual ports.

image-20240727181940717

两台计算机之间通讯 Communication between two computers

image-20240727182003077

计算机里面程序通讯 Application communication

image-20240727182050595

计算机程序之间的通讯,通过IP只能锁定计算机,但是无法锁定具体的程序。

Communication between computer programs can only lock the computer through IP, but cannot lock specific programs.

通过端口可以锁定计算机上具体的程序,确保程序之间进行沟通。

Through the port can lock the specific program on the computer, to ensure that the communication between the program.

Port Classification

Public port

• 公认端口:1~1023,通常用于一些系统内置或知名程序的预留使用,如SSH服务的22端口,HTTPS服务的443端口非特殊需要,不要占用这个范围的端口

Sign port

• 注册端口:1024~49151,通常可以随意使用,用于松散的绑定一些程序\服务

Dynamic port

• 动态端口:49152~65535,通常不会固定绑定程序,而是当程序对外进行网络链接时,用于临时使用。

查看端口使用情况 View Port Usage

nmap 查看ip的端口情况

nmap view ip port status

1
sudo yum -y install nmap
1
nmpa ip地址

eg:

查看本地端口

image-20240727182619594

Netstat 查看指定端口号

Netstat to view the specified port number

结合grep 查看指定端口

Eg: 查看22port

1
netstat -anp| grep 22

check process 查看进程

ps

1
ps [-e -f]

-e 显示全部进程

-e Show all processes

-f 完全格式化显示

-f fully formatted display

image-20240727184242690

从左到右分别是:

•UD:进程所属的用户D

• PD:进程的进程号口

• PPD:进程的父口(启动此进程的其它进程)

•C:此进程的CPU占用率(百分比)

• STIME:进程的启动时间

•TTY:启动此进程的终端序号,如显示?,表示非终端启动

•TME:讲程上用内时间

check 指定progress information

Eg: 查看tail command的information

1
ps -ef | grep tail

kill progress

1
kill [-9] pidId

-9: 强制关闭 Force Close

Eg:

image-20240727184759784

查看资源占用情况 View resource usage

1
top

image-20240727184955403

前五行信息

image-20240727185036669