Linux Development Foundation notes

0. Description

From Tencent video Linux development video, mainly for my own review

Linux 1. Overview

1.1 origin and development of Linux

unix is a time-sharing operating system (the difference between time-sharing system and real-time system) )
GNU(GNU is not Unix) aims to eliminate restrictions on software copying, distribution and modification
GPL (GNU General Public License) guarantees the freedom of GNU software to all users through the agreement
LGPL is more business friendly than GPL
BSD(Berkeley Software Distribution)license
Copyright and copyleft, the former is copyright, the latter is free and free
What is the difference between Linux kernel and distribution?
The distribution has more applications, and the kernel is the core program.
Extended reading:

1.2 structure and file system type of Linux

kernel: the following is the hardware. There are hardware drivers and system management
Runtime: rich library, system application
shell: user interaction, command line
Utilities: Applications

EXT3 and EXT4 are the default file system types of Linux
FAT32 and NT
SWAP type file systems are only used in Linux SWAP partitions

The principle of swap in Linux. The size of swap is generally twice that of memory

1.3 file attributes and directory structure of Linux

FHS(Filesystem Hierarchy standard)

Differences between / bin and / usr Blogs:
/sbin: executable that requires special permissions
/etc: configuration file, host name, network card address. It can be saved after shutdown
/var: put frequently changing data, such as logs and emails
/proc: virtual directory. After shutdown, there is no content. The port mapped in. Operating system kernel information.
/sys: virtual directory
/opt: third party software
/mnt: manual mount directory

1.3 installation of Linux system environment

The number of main partitions plus expansion partitions shall not exceed 4, the maximum number of main partitions shall not exceed 4, and the expansion partition shall not exceed 1. The logical partition is in the expansion partition (there is no limit on the number of logical partitions). Generally, partitions are formatted, and blank partitions are not formatted.

Difference between primary partition and extended partition:

1. Main partition

The primary partition is also called the boot partition. Windows system generally needs to be installed in this primary partition, so as to ensure automatic entry into the system after startup. In short, the primary partition is a disk partition that can boot the computer to read files,

A hard disk can create up to four primary partitions at the same time. After creating four primary partitions, you can no longer create extended partitions and logical partitions. In addition, the primary partition is independent, corresponding to the first partition on the disk. At present, most computers generally divide Disk C into primary partitions when partitioning.

2. Extended partition

Extended partition is a concept, which can't be seen in the hard disk and can't be used directly.

In addition to the primary partition, the remaining disk space is the extended partition. When a hard disk allocates all capacity to the primary partition, there is no expansion partition. Only when the capacity of the primary partition is less than the capacity of the hard disk, the remaining space belongs to the expansion partition. The expansion partition can continue to be expanded and divided into multiple logical partitions.

3. Logical partition: the extended partition cannot be used directly. It is used in the way of logical partition, so the extended partition can be divided into several logical partitions. Their relationship is an inclusive relationship, and all logical partitions are part of the extended partition. If the extended partition is no longer partitioned, the entire extended partition is a logical partition.

1.4 installation of Linux system environment

Virtual machine installation, 1 / 4 of the real memory, the cpu has several cores to write
Install using ISO image files

Recommendation: during installation, press F2 to cut off the BIOS interface immediately after restart. Use the + button in Boot to put the CD-ROM on the top and start from the CD-ROM to avoid starting from VMware Player. Then press F10 to save exit
After installation and shutdown, prompt to remove the disc. Do not check the link at startup in the equipment status.

2. Command overview

2.1 command format and help

Ordinary users are $, privileged users are $#

type pwd
type ls

Find commands in vim

# Look from top to bottom
/Find content
# Look from bottom to top
?Find content

3. Directory operation

3.1 path concept of directory

Relative path and absolute path

3.2 directory operation

# Lists the self information of the current entry
ls -d
# Create multi-level directories recursively
mkdir -p
# Directory copy command
cp -a # Copy all
cp -f # A forced copy, overwrite existing
# remove empty directories
rmdir -p # Recursively delete multiple empty directories 
# Delete non empty directory
rm -i# interactive
# Directory move rename

4. File operation

4.1 document display and filtering

# In a terminal
while :;do echo "hello LiLi." >> test.txt;sleep 1; done
# In another terminal
tail -f test.txt
# This is often the case when viewing log files

4.2 addition, deletion and copy of documents

# In the root directory, find the file with cc in the file name, and start from below the root directory
find / -name "cc"
# You can only find the executable file and find it in the PATH variable
which cc
#which can only find the executable file and find it in the PATH variable.
#whereis looks from the linux file database (/ var/lib/slocate/slocate.db), so it is possible to find the newly deleted or newly created file. Match all
#locate is the same as above, but the file name is a partial match.
#find is a direct search on the hard disk. It has powerful functions, but it consumes the hard disk and is generally not used.
# locate uses index technology to find
locate cc

4.3 basic editing of text editor

vim--->After entering directly
:q--->You can't exit directly
:w  File name--->Save exit
# copy
# Paste, paste lowercase below the current line, and paste uppercase above the current line
# Add line
# Delete line
# You can add numbers in front of commands
# /Search down? Yes, look up
# : s replaces the current line, g is global (all of the current line), and c is comfort
# % s full text replacement:
# Last line
# first line

vim multiline comment:

4.4 advanced editing of text editor

# Jump to line 13
# The line number is not displayed
:set nonu
# Line number display
:set nu
# Written in vimrc, the point here means to hide the file
vim ~/.vimrc
set nu
set tabstop=4
set autoindent #Auto indent
# Undo and fallback in command mode
 Lowercase u
# Multiple files are executed in parallel. The advantage is that the contents of multiple files can be transferred to each other
:args#View several file parameters
: first#Go to the first file
: last#Go to the last file
: next#Next file
: prev#Previous file

5. File compression and archiving

5.1 basic file archiving and compression

5.2 7z compression and archiving

Advantages of using 7z: high compression ratio; Rar format can be decompressed, but rar cannot be compressed

. o file: target file. It is equivalent to an obj file in windows c or cpp file corresponds to one O documents

. a file::: there are many o combined static library files, equivalent to those under windows lib file. Provide interfaces to external programs for static connection, i.e. STATIC mode. Multiple A can link to generate an exe executable

. so file::: it is a shared object, that is, a dynamic link file, and windows dll file, which is used for dynamic connection. Load only when used.

. bin file::: executable file under linux, equivalent to that under windows exe file.

6. Equipment mounting, software package upgrade and installation

6.1 mount and unload concept

The computer is automatically mounted in the directory / media
Mount a file to a directory

# View disk
sudo fdisk -l

What are the reading materials and equipment documents:

6.2 mounting and unloading equipment

# iso9660 file format, / dev/cdrom is the device file, / mnt is the directory (- t can be omitted sometimes) (this is the mounting of the optical disk device)
sudo mount -t iso9660 /dev/cdrom /mnt
# iso is a file, not a device. Put the file in the loopback device, and then loop and mount it in / media (mount the CD image file)
sudo mount -o loop -t iso9660 mycd.iso /media
# Because the CD-ROM is read-only, there will be a warning after mounting, but the u-disk will not
# When using - A to uninstall, it indicates that the device is busy and needs to wait for a while. It is not an error
# Uninstall device and directory equivalents

6.3 software package upgrade

# Update software list
sudo apt-get update
# Upgrade a software
sudo apt-get upgrade vim

7. Process management

7.1 process view

# $$is the current process. To view the specified process, just follow - p with the pid number
top -p $$

7.2 process control

Termination is normal exit, and suspension is interrupted exit

# View process number
pidof gedit

8. System management

8.1 simple system operation

Shutdown can specify a time before shutdown.
sudo is required for reboot and shutdown

8.2 planned tasks

man cront
# See Chapter 5 crontab
man 5 crontab
# Edit crontab
crontab -e
# After entering the crontab, / 1 is every 1 minute, without / is the fifth minute of each time
# m h dom mon dow command
*/1 6,8-10 * * * tar czf /home/iotek/dabao.tgz /home/iotek/*.deb
# Restart cron service
sudo service cron restart
# View date
# Set system time
sudo date -s "5:58:55"
# View help documentation for crontab
crontab -h
# delete
crontab -ri
# Looking back, does crontab still exist
crontab -l

8.3 memory status viewing

du -s /home/iotek
du -h /home/iotek/examples.desktop

8.4 disk partition

A partition is a write to a device file

Press m to view the help file form

Press n to add the partition
p is the main partition and e is the expansion partition
Set partition number (default)
Set sector (default)
Press p to print
Extended partitions cannot be used directly and need to be divided into logical partitions
Press n
Divide the logical partition by l -

Now I don't want to change the logical partition to linux, but change this id
Finally, linux is changed to fat32

The type of all partitions can be list ed in uppercase L (hexadecimal representation)

Press p again to see that the partition type is changed, but it is not finally written into the device file. Remember to enter w!!!!

syncing synced to disks
Finally, sudo fdisk -l check it out

9. Network connection and address viewing

9.1 network connection and address viewing

# Local address link
# Stop after sending 6 times
ping -c 6
# Domain name is OK

ifconfig only temporarily changes the network interface address

lo is a virtual network card, local loopback, take a circle on the network protocol, and then come back
The hardware address mac address is the ID card of the network card. The ip address can be changed, but the network card address cannot be changed

9.2 domain name resolution

The web page cannot be opened, qq can log in. It indicates that the network connection is normal and the domain name resolution is faulty

Note: not all IP addresses support reverse resolution

9.3 view of routing and network statistics

#Add a gateway
sudo route add default gw
# Delete gateway
sudo route del default gw

# View routing table
netstat -r

10. User management

10.1 user addition, deletion and viewing

# Add a user, but there is no directory and the creation is incomplete
sudo useradd bencai
# Delete this user
sudo userdel bencai
# Create a user with a directory
sudo useradd -m bencai
# Set password for user
sudo passwd bencai
# Login user
su bencai

10.2 user switching

# Ordinary users $Symbol root user#state
sudo -i
# Operate in the specified user
sudo -u bencai ls /home
# Commands that sudo can execute
sudo -l
# Error: user01 is not in sudoers folder
sudo bencai
# Exit the bencai environment and enter the root permission
sudo -i
# Add user01 to sudo group
gpasswd -a user01 sudo
# Change the ip address of lo
sudo ifconfig lo

User account configuration 10.3

sudo usermod -L user01
# Failed to switch from current user to user01
su user01
# But administrators can
sudo -i
su user01
passwd user01
# Unlock user
sudo usermod -U user01

less /etc/passwd# System users cannot log in in the user interface. Only bencai and user01 can log in
sudo less /etc/shawdow

11. User group management

11.1 adding, deleting and viewing user groups

sudo groupadd user02
tail /etc/group
sudo groupdel user02
# Specify id
sudo groupadd -g 1007 user02

11.2 user group account configuration

Effective group?
When groups view the user's group, it will pop up a lot. When the iotek user touch es a File file, only the iotek group can access the File. Now I want the bencai team to visit. First, there should be bencai in the group, and then turn bencai into an effective group (the first group is an effective group)

# Add user iotek to bencai
sudo gpasswd -a iotek bencai
# Change user bencai from last to first
newgrp bencai
touch test.out
# You can see test Out's group has changed
ls -l
# Remove user iotek from bencai

# Restrict group access
sudo gpasswd -R bencai
# bencai appears when viewing the file
less /etc/gshadow

12. File permission attribute

12.1 concept of document authority

For the directory, the executable representative is accessible

12.2 file permission setting

# View the properties of the directory itself
ls -ld newdir
# In non numeric form
umask -S
# Ordinary files do not have executable permissions
# Delete a directory

12.3 setting of document owner and group

# chown can also change the group. Only one group can be handled, so chgrp is used recursively
# : users in front and groups in the back
sudo chown iotek:iotek newdir
sudo chown :iotek newdir

13.Linux network services

13.1 use of FTP

ftp file transfer
ftp uses the file name and login password to log in
ftp has two transmission modes, binary mode and file mode. The binary file has no format conversion, and the file loss is less
Upload to the server and download to the local

Login with account and password

ls no! Is to list the directories on the server; ls plus! Yes lists the current directory
In addition to two transmission modes, the mode of data connection: active / passive mode

Passive mode on
Active mode, the server actively connects you
In passive mode, the server is waiting for you to connect

bi is binary mode
ascii is a file schema

Upload successful

Download successful

sign out

13.2 installation of FTP service

View process number

Uninstall or use auto remove
Only ping can be installed online
The files downloaded by apt are in this path

# Offline installation
sudo dpkg -i vs*.deb

13.3 startup, shutdown and configuration of FTP service

# View current status
sudo service vsftpd status

anonymous_enable anonymous login
Normally open local_enable and write_ Enable (refers to the ability to upload. Otherwise, it can only be downloaded)

Welcome interface

Ensure that the newly configured file is read again, restart and reload

13.4 use of SSH

sudo apt-get install ssh

windows connection

13.5 installation, startup, shutdown and configuration of SSH service

# Installation package
ls /var/cache/apt/archives/openssh*

vim /etc/ssh/sshd_config

14. Environment and functions of shell

14.1 command completion and command history

After logging in, the first execution is / bin/bash

14.2 command alias

type ls
which ls
alias myls='ls -ld --color=auto'

14.3 wildcards

ls -l test[0-9, a-z].sh
ls -l test[0-2]*.???

14.4 operation control

# Background operation
#[3] 28374 3 stands for job number and 28374 stands for pid
# Change homework 3 back to the front desk, which is occupied by the front desk
fg %3

bg will resume the background program from suspension and finish running

15.Shell variable

15.1 concept of variable and user-defined variable

Why do you sometimes add {} to a quote?
I need to speak English right away. The difference is whether it is a variable name or a variable name plus a string

15.2 environment variables of path

The host name and ip address are all in one configuration file. Read this configuration file after startup

bashrc only changes the current user
Change all etc directories

# View all environment variables
# Environment variables are saved in these two files (global)
less /etc/bash.bashrc
less /etc/profile
# Generally, don't change the overall situation
# At home
less ~/.profile
less ~/.bashrc
# PS (Prompt Sign): command prompt
# Load modified file
. ~/.bashrc
# Restart start shell
echo $PS1

ps related information

# Export environment variables
export PS1
# Generate sub shell

# The advantage of environment variables is that you don't have to add paths when running commands
# Same effect
cp bin/ls ils
# Running ils directly failed
/home/dwz/ils #Can run
# Temporarily add environment variables

15.3 other environmental variables and predefined variables

echo $PS1
# \u Is the current user,:@Meaningless format,\$Ordinary users are $,Privileged users are#
sudo -i
# PS2 is a prompt for line breaks
# ls -l \ then enter and wrap

echo $$
echo $0
# Kill the current shell
kill -9 $$
# echo $?
# 0 is success and 1 is failure
dasfhj #Random input
echo $? #Return non-zero
# Add & after the command to send it to the background
sleep 10000&
# [1] 28628
echo $!
kill -9 $!
# View current background tasks

15.4 location variables

There are only 9 position variables in total. When it is not enough, use shift to switch

Task 1:
vim test.txt
\Text content
ls -ld $1
ls -l $1
# . equivalent to source, load the file into the current shell to run
. test.txt /home

Task 2:
vim test.txt
\Text content
ls -ld $1
ls -l $1
ls $2
# Pass in three addresses, exceeding the number of variables in txt. When you meet shift, automatically push back a parameter, $2 refers to / ect Finally, / home and / etc are displayed
. test.txt /home /var /ect

16. Piping and redirection

16.1 use of pipes

# grep -v contains no fields
ps aux | grep "bash" | grep -v "grep"
# Standard error messages are not piped
# newdir is a normal file and asdhiffeljh is an error message. Finally, the error information is not filtered through the pipeline, but also directly output
ls -l newdir asdhiffeljh | grep test

16.2 output redirection

Overwrite redirection is to empty the original file
Additional redirection is added at the end of the file
Default descriptor file 1

16.3 redirect and merge input

# By default, cat outputs whatever the keyboard inputs ctrl + d end
cat output.txt
cat < output.txt
# When EOF ends, EOF can take end and BBB by itself
cat << EOF
> asfdjh
> asfhjk
> asdfhjk
# Although the error file cannot be written to output Txt file, but empty the original file
ls -l djbfa/ > output.txt
# Objective: to input both correct information and error information into the file
# The correct information is in, but the error information is not in
ls -l dfka/ /home/dwz > output.txt
# The error information is in, but the correct information is not in
ls -l /home/dwz dfka/ 2 > output.txt
# Correct operation
ls -l /home/dwz dfka/ & > output.txt

16.4 pipeline and redirection application

ls -l | grep bencai > output.txt
# cat was originally output to stdin and redirected input to output2 Txt
cat < output.txt > output2.txt
cat > output3.txt << FF

Device files were originally stored in memory and then entered into the

17.Shell script

17.1 script programming overview

Three ways for scripts to run

By outputting the process number and process name, you can see the difference between the two different ways of running scripts,

17.2 summary of branch structure

17.3 example – branch structure

# Scheme 1: symbol cascade:
test -x && echo "ok" || echo "failure"
# Scheme 2: if else branch:
# There are spaces before and after []
if [ -x $1 ]
	echo "ok"
# Two commands in a line, with semicolons
elif [ -x $2 ];then
	echo "$2 executable"
	echo "fail"
# Scheme 3: case branch:
# Assignment = no spaces allowed
case $Value in
		echo "A"
	# There are two semicolons
		echo "B"
	# *Wildcard
		echo "C"

17.4 description of cycle structure

17.5 example – cyclic structure


# First kind
#while [ -x $1 ]
# Second
until [ -x $1 ]
	[ $Count -ge 5] && break
	echo "running..."
	let Count++
# Third
for Var in aa bb cc dd ee ff
	echo "loop $Count"
	echo "$Var running..."
	let Count++

18.Shell script

18.1 example – analysis of scripts

18.2 presentation – scripting

#--Choose 10 multiples of 3, starting with a given natural number

read -p "Please enter a natural number:" Input
if [ $Input -gt 0] 2 > /dev/null ; then
	echo "You entered a natural number. The multiple of 10 3's starting from this natural number is"
	echo "What you entered is not a natural number. The program exits abnormally."
#--Process and output
while [ $Count -lt 10 ]
	let Num2=$Num%3
	if [ $Num2 -eq 0 ] ; then
		echo "$Num"
		let Count++
	let Num++

18.3 script debugging and debugging

/Contents of dev/null:
When the input is not a natural number, the error will be input into / dev/null

19. Advanced topics for shell scripts

19.1 shell functions

!!! Note: here are two $1, but the meaning is different. echo "$1 Hello" here represents the parameter "$1 Haitong", while nihao "$1 Haitong" here represents the parameter iotek input outside the file (see the figure below)

19.2 replacement of shell variables

# Delete a variable
unset today
# The variable today is empty
echo $Today
# Show friday
echo ${Today:=friday}
# Show friday
echo $Today
unset Torrow
# Default error reporting
echo ${Torrow:?"no set value"}
# Display output sunday
echo ${Torrow:?"no set value"}

19.3 case explanation - guessing game

bug fix:

Tags: Ubuntu

Posted by woocha on Wed, 18 May 2022 16:01:13 +0300