“Linux private house dishes” reading notes
chapter 0
CPU
central processing unit
Main functions, management and operations (arithmetic logic unit, control unit)
The CPU focuses on calculations and judgments, which come from memory
At present, the mainstream CPU is more than dual-core. Originally, single-core means only one computing unit, while multi-core means that multiple computing cores are installed in a CPU shell
CPU performance comparison
1 Internal microinstruction set
2 CPU frequency – the number of CPU operations per second
For example, a CPU with a frequency of 3GHZ can do 3 x 10 to the ninth power per second
Frequency and frequency
External frequency: the frequency of data transmission and computation between the CPU and external components
Internal frequency: A multiple within the CPU used to speed up performance
That’s the CPU frequency when you multiply them
Software program running
Design a language that humans can understand, and then create a “compiler” to convert human-written programming language into machine language, the common compiler language, C ++
The operating system
A group of programs that manage all the activities of a computer and drive all the hardware in a system
Let the CPU start to judge the logic and calculation values, let the memory start to load data
So that’s the kernel of the operating system
Hardware is managed by the kernel, so if you want to develop software, you need to refer to the previous functions of the kernel, so the operating system provides a set of development interfaces
1 The OS kernel is written based on hardware specifications. Therefore, the same OS cannot run on different hardware architectures
2 The development of the application program refers to the development interface provided by the operating system, so the application program can only run in the operating system
For example, Windows software cannot run on Linux
The kernel
The kernel mainly manages the hardware and provides reasonable allocation of resources, such as CPU resources and memory resources
- Program management
Multitasking – a computer may have a lot of work waiting to be done by the CPU at the same time, and the kernel needs to control that work so that the CPU can allocate it efficiently
Good CPU scheduling (where the CPU gets that work started first) can speed up overall system performance
- Memory management
Control system memory
All program code and data in the system must be stored in memory first
- File System Management
Such as data input and output I/O
Files in this format cannot be used if the kernel does not know a file system
- Device drivers
memory
The data read by the CPU comes from the memory. Both software programs and data must be read into the memory before they can be used
The memory of the PC is zero
Memory capacity is also important, because data has to be stored in memory first
In general, the larger the memory, the faster the system, because you don’t have to free the data in memory
chapter 1
What is a Linux
Linux is an operating system
A computer consists of a collection of hardware devices that operating system users manage and control (the kernel)
More than that, the operating system provides a set of system call interfaces for programmers to develop software better
Linux provides a complete operating system, the lowest level of hardware control and resource management of the complete architecture, this is followed by Unix, quite stable and powerful
Linux distributions
Linux is the lowest level of the operating system kernel and its kernel management tools, anyone can obtain the source code and execute this kernel program
As more and more people use Linux, and more and more software can run on Linux, Linux + software can be a fairly complete operating system
In order to make Linux accessible to users, someone has integrated Linux with running software, plus a system that lets users install and manage Linux directly on CD or over the Internet. We call this fully installable system Linux Distribution – the fully installable suite
- Several common Linux distributions
Red hat — Red hat
Ubuntu — No Bantu
centos
-
Linux Distribution uses the Linux kernel provided at www.kernel.org. The software used is similar. The biggest difference is the software installation mode
chapter 3
Each component or device is a file under Linux
disk
It is mainly composed of disk mechanical arm spindle motor
partition
The simplest partition: distinguish root directory \ and memory swap space swap can be
A little trickier: Separate important directories that are read and written frequently from the root directory, such as /, /usr, /var, etc
chapter4
Swap space refers to?
When data is stored in physical memory that is not often accessed by the CPU, the infrequently used program is dumped into the swap space on the hard disk, freeing up the faster physical memory space for the program that really needs it
So if the system is not busy and the memory is hit, there is no need for swap
chapter 5
basis
Format of a command
commond [option]
Copy the code
-
Case sensitive
-
Press Enter to execute the command
Some simple commands
The command | meaning |
---|---|
date | Get the date |
cal | Access to the calendar |
bc | Enter calculator mode |
- cal
Just type CAL to display a calendar for the current month
cal // Current month
cal 2018 // All months of 2018
cal 8 2018 / / in August 2018
Copy the code
- BC enters calculator mode
To exit calculator mode, type Quit and press Enter
Important keys
-
TAB command and file name completion
-
CTR + c Terminates the current command
Linux Online help man Page
For example, you want to see some information about the date command
man date
Copy the code
If you look at the first line here we have a DATE(1)
The numbers here mean (here are a few common ones)
digital | meaning |
---|---|
1 | Commands or executable files that can be operated by the user in the shell environment |
4 | Device file |
5 | Configuration files or the format of certain files |
8 | Management commands available to the system administrator |
Linux online help info page
Another online help command is info page
Basic commands
To turn it off
shutdown
shutdown
Copy the code
Then the server failed to log in successfully
You need to go to the cloud console to restart it
Take a look at parameter Settings
shutdown [options] [time] [wall]
Copy the code
The commonly used options
options | meaning |
---|---|
-t digital | Power off after a few seconds |
-r | Stop system services and restart the system |
-c | Example Cancel the shutdown command in progress |
-h | After the system service is stopped, power it off immediately |
Shutdown -h 21:00 // Power off at 21:00 Shutdown -h +10 // Power off after 10 minutesCopy the code
other
- The halt to turn it off
Halt is actually a call
shutdown -h
Copy the code
Halt kills the application process to execute the sync system call
- reboot
Reboot works much like HALT, except that it causes the host to restart, whereas HALT shuts down
- init
Init is the ancestor of all processes and its process number is always 1, so sending TERM signals to init terminates all user processes, daemons, and so on. Shutdown uses this mechanism. Init defines eight runlevels: init 0 for shutdown and init 1 for restart.
chapter 6
Configure file permissions and directories
Three kinds of identity
File owner, user group, others
All accounts and common users in the system, as well as root information, are recorded in the /etc/passwd file, each person’s password is recorded in the /etc/shadow file, and all group names are recorded in the /etc/group file
- The user
Linux is a time-sharing operating system with multiple users and tasks. Any user who wants to use system resources must first apply for an account from the system administrator and then use this account to access the system
- User groups
Each user has a user group. The system can manage all users in a user group in a centralized manner. For example, users in Linux belong to a user group with the same name. This user group is created at the same time when users are created
Ls -al Displays information about files in the current directory
Ls Displays the file name and related properties
Al lists the attributes of the file’s permissions in detail
-rw-r--r-- 1 zhengzaijiazai staff 66 4 25 10:21 .gitignore
drwxr-xr-x 2 zhengzaijiazai staff 68 4 25 11:32 dist
Copy the code
- drwxr-xr-x
d rwx r-x r-x
Split them in 1, 3, 3, and 3 formats
The command | meaning |
---|---|
d | The first character indicates whether this is a directory file or a link file, etc |
rwx | Permissions of the file owner |
r-x | Permissions for files and user groups |
r-x | Permissions of other users that are not local users |
R (read) Reads w (write) writes x (execute) executes
Meaning of file permissions
For the file
R indicates whether the file can be viewed. W indicates whether the file can be operated, but the file cannot be deleted. X indicates whether the file can be executed by the system
For directory
The main content of a directory is a list of record file names
R indicates that the list of directory structures can be read
W indicates the permission to change the directory list
W Indicates the meaning of permissions for files |
---|
Create new files and directories |
Delete files and directories (regardless of file permissions) |
Renames existing files and directories |
Move files, directory locations |
In general, the w permissions for directories are related to file name changes in that directory
X indicates whether the user can access a directory such as CD
test time
There is a directory with permissions for
drwxr--r-- 3 root root .......
Copy the code
The current system account is userA. UserA does not belong to the root group. What permission does userA have for this account
- answer
UserA has read permission
However, the permission of X is required for directory switching, so it cannot be switched to this directory
Modify file properties and permissions
The command | meaning |
---|---|
chgrp xxx filename | Change the permission of the user group to which a file belongs |
chown xxx filename | Change the owner or group of a file |
chmod xxx filename | Change file permissions |
chown
Chmod Changes the permission
- Use the score
Using scores can also represent permissions
The command | score |
---|---|
r | 4 |
w | 2 |
x | 1 |
rwx 7 rw- 6 r-x 5
- Use symbol types
There are three identities, so I’ll use u, G, and O for the permissions of the three identities
identity | character |
---|---|
The user | u |
User groups | g |
others | o |
All status | a |
The command | meaning |
---|---|
+ | increase |
– | Minus the |
= | Set up the |
For example, you need to set the permission of a file to -rwxr-xr-x
So u is RWX and g/o is r-x
chmod u=rwx,go=rx readme.md
Copy the code
Directory configuration
Standard the FHS
If everyone were to configure file directories according to their own standards, there might be administrative headaches
FHS is used to specify what data should be placed in each particular directory
FHS defines for the directory tree what data should be placed in the three directories
directory | meaning |
---|---|
/ (root root directory) | Related to the boot system |
/usr | It is related to the installation and execution of software |
/var | It is related to the system operation process |
Usr (UNIX Software Resource) UNIX operating system Software Resource
FHS definition (/) The following directories must be in the root directory
directory | The file content |
---|---|
/bin | Commands in /bin can be used by root and common accounts, such as cat cp |
/etc | System main configuration files, such as account password files, which service revelation files |
/tmp | Executing programs Where files are placed FHS recommends that data under/TMP be deleted at boot time |
Q: How is /usr/bin different from /usr/bin
A:
/bin is a set of system instructions
Bin, short for binary, is used to store the necessary executables of the system such as cat, cp, chmod df, dmesg, gzip, kill, ls, mkdir, more, mount, rm, su, tar, etc
/usr/bin is the run script for some software you install later
C++, GCC, CHDRV, diff, dig, du, eject, elm, free, gnome*, Gzip, htpasswd, KFM, kTOP, last, less, locale, M4, make, man, McOpy, NCFTP, newaliases, NSLookup passwd, quota, SMB *, wget, etc
Absolute and relative paths
- An absolute path
The file name or directory written from the root directory
- Relative paths
Relative to the current path
Chapter 7 Related to Directory
Special directory
mark | meaning |
---|---|
. | This layer directory |
. | Upper level directory |
– | Previous working directory |
Q: In the root directory of Linux, do you have the upper directory (..)? The presence of
A:
Let’s look at it as a regular user
Let’s see as root
Manipulate files and directories
The command | meaning |
---|---|
cd | Change directory change directory |
pwd | Show current directory |
mkdir | Creating a new directory |
rmdir | Deleting an Empty Directory |
cp | copy |
mv | mobile |
- Create a directory mkdir
Mkdir can only create directories one by one. How to create multiple directories at once — with the -p parameter
mkdir -p webpack/w1-code/dist
Copy the code
- Delete the directory
rmdir config
Copy the code
Only one empty directory can be deleted
If you want to forcibly delete a non-empty directory and everything below it
rm -r node_modules
Copy the code
- Check the directory
ls -a // Display all, along with hidden files
Copy the code
- mv
// Move a file to a folder
mv mian.js newdir
// Rename the folder
mv oldDir newdir
Copy the code
- Cp copy
Copying files requires r (readable) permission
// Copy the index file to the current location
cp ./config/index.js .
Copy the code
By default, the source file and the destination file of cp have different permissions. The owner of the destination file is the command operator
View document contents
Cat (Concatenate continuous)
The command | meaning |
---|---|
cat | Check the content |
cat -n | View the line number displayed in the content |
cat -b | Check the line number displayed in the content [blank part does not count line number] |
Nl adds line number printing
Nl can automatically add line numbers to the output and the default result is somewhat similar to cat-n
- nl [-bnw] filename
– b command | meaning |
---|---|
a | Empty lines are also added with line numbers |
t | Empty lines do not add line numbers |
The -n command | meaning |
---|---|
ln | Line numbers are displayed on the left |
rn | The line number is displayed on the right without the addition of 0 |
rz | The line number is displayed plus 0 to the right |
Nl index.js nl -b a index.js Empty lines also add line numbersCopy the code
more
Nl cat displays everything to the command line at once and more has page-turning capabilities
more index.js
Copy the code
The -n command | meaning |
---|---|
The blank space | Turn the next page |
Enter or down | Scroll down one line |
q | Quit the display |
- lookup
Press/and enter the character you want to look for [but didn’t find any highlighting]
less
The use of less adds the ability to scroll up more than more
less index.js
Copy the code
- lookup
Press/then enter the character to look for [search down] Press? Then enter the character to look for [lookup up]
Select data Head tail
- Head is shown from the front
Select data and show a few lines
head [-n number] filename
Copy the code
Number indicates how many lines are displayed. By default, the first 10 lines are displayed
- Tail is shown from the back
tail [-n number] filename
Copy the code
Number indicates how many lines are displayed. By default, the last 10 lines are displayed
Get lines 100 to 120
cat filename | head -n 120 | tail -n +100
Copy the code
Display 50 lines starting with 100
cat filename | tail -n +100 | head -n 50
Copy the code
Tail -n +100: displays from line 100 to line 100
The permission of the file is default umask
When a new file is created, its default permissions are related to umask
The umask value is used to specify the default permissions of the current user when creating a directory or file. Specifically, the umask value is just a mask that masks the permissions of the corresponding location from the default permissions when creating a file
0022 The permission is a number
As you can see, there are four groups of permissions. The first group is for special permissions. We usually only need the last three
U = RWX indicates the permissions as symbolic types
The properties of the default permissions for files and directories are different
-
666 (rw-rw-rw-rw -)
-
User creates a directory file, because x is related to whether the directory can be accessed, so all permissions are open by default, namely 777 (RWXRWXRWX).
Q: Assuming that the user’s umask is 003, what is the permission to create a new file or folder
A: Write w and x for other
Rw – rx – r – the file
Folder RWXRXWR –
Find file
Find [specify path] [specify condition] [specify action]Copy the code
By default, the current directory and subdirectories are searched, and the results are printed to the screen
Relationship between permissions and files
! [important]] (p1-jj.byteimg.com/tos-cn-i-t2…).
- What are the basic permissions for a user to enter a directory as a working directory
conditions | content |
---|---|
Available command | CD Switch to the working directory |
Directory Permissions required | X must have executable permissions |
- What are the basic permissions for a user to read a file in a directory
conditions | content |
---|---|
Available command | Cat more less tail head |
Directory Permissions required | X must have executable permissions |
File Required Permissions | R must have readable permissions |
- What are the permissions for users to modify a file
conditions | content |
---|---|
Available command | Vi vim etc. |
Directory Permissions required | X must have executable permissions |
File Required Permissions | Rw must have read and write permissions |
- What are the basic permissions for users to create a new file
conditions | content |
---|---|
Available command | Touch etc. |
Directory Permissions required | W x Must have readable and executable permissions |
$PATH
Why is it possible to execute ls — the environment variable PATH — anywhere
According to the setting of PATH, the system queries the ls executable file in each directory that defines PATH. If the directory defined by PATH contains multiple ls executable files, the command with the same name queried first is executed first
Chapter 8 Disk and File Management
EXT2
The most traditional Linux disk filesystem (filesystem) uses EXT2
Disk partitioning refers to the area that tells the operating system that the disk can access in this partition slot from column A to column B
This allows the operating system to know that it can read/write/search for file data within the specified block. In other words, disk partition means to specify the beginning and end of the partition slot magnetic column
The file system
The disk also needs to be formatted after partitioning. This is because each operating system has different file properties/permissions configured. In order to store the data required for these files, the partition slot needs to be formatted to become a file system format that the operating system can utilize
- Running of the file system
This has to do with the operating system’s file data. File data on newer operating systems often has a lot of attributes in addition to the actual contents of the file
For example, file permissions (RWX) and file attributes (owner, group, and time parameters) in Linux. File systems typically store these two pieces of data in separate blocks, with permissions and attributes in inodes and the actual data in data blocks
In addition, there is a superblock that records the overall information of the entire file system, including the total number of inodes and blocks, usage, and remaining amount
attribute | meaning |
---|---|
inode | Records the properties of a file. Each file occupies an inode and records the block number of the file |
block | Actually record the contents of the file. If the file is too large, multiple blocks will be occupied |
superblock | Record the overall information about the filesystem, including the total inode/block quantity, usage, and remaining amount, as well as the filesystem format and related information |
inode
The contents of inodes record the permissions and attributes of files, while blocks record the actual contents of files
File systems, on the other hand, have inodes and blocks planned out from the beginning. Inodes and blocks stay fixed until they are reformatted
Ext2 file systems are formatted into block groups, each of which has its own inode/block/superblock system
block
Data blocks are used to store file contents and data. Ext2 file systems support block sizes of 1K, 2K, and 4K. The size of the block is fixed during formatting
1 In principle, the size and number of blocks cannot be changed after formatting (unless reformatting is performed). 2 Each block can contain at most one file 3 blocks. If the file is larger than the block size, a file will occupy more than one block. If the file is smaller than a block, the remaining capacity of the block can no longer be used (disk space is wasted)
Q: Suppose you have an Ext2 file system that uses 4KB de Block and has 10,000 small files of 50 bytes each. How much disk capacity are you wasting?
A:
4kb = 4 * 1024 bytes = 4096 bytes
So each one will waste 4096-50 bytes
Therefore, 4046 x 10000 bytes = 38.585666 = 38.6MB
Relationship to the directory tree
- File directory
When we create a directory on the Linux ext2 file system, ext2 allocates an inode and at least one block to the directory
The inode records the permissions and attributes of the directory and the block number allocated to it
A block is the file name and inode number that the file name occupies in the directory
- file
When we create a normal file in Ext2 under Linux, ext2 allocates an inode and a number of blocks relative to the file size to the file
For example, if I have a block of 4 Kbytes and I want to create a file of 100 Kbytes, Linux will allocate an inode with 25 blocks to store the file
The inode itself does not record the file name; the file name is recorded in the block of the directory. Therefore, in chapter 6 of the instructions on file and directory permissions, we will mention that “adding/deleting/renaming the file name is related to the W permissions of the directory”.
The file name is recorded in the block of the directory, so when we want to read a file, we must pass through the inode and block of the directory. Then we can find the inode number of the file to be read, and finally we can read the data in the block of the correct file
File system operations
All data must be loaded into memory before the CPU can process it
Think about it, if you’re constantly editing a large file and you’re constantly writing to disk, disk writes are much slower than memory, so you’re constantly waiting for hard disk to write/read
To address this problem of efficiency, Linux uses an approach called asynchronous processing
When the system loads a file into memory, if the file has not been modified, the file data in the memory segment is configured as clean. However, if the file data in memory has been changed (for example, if you edit the file with nano), the data in the memory will be configured as Dirty. At this point all actions are still running in memory and not written to disk! The system periodically writes Dirty data back to disks to ensure data consistency between disks and memory
Memory is much faster than hard disk, so if you can put frequently used files in memory, doesn’t that add performance
- On Linux, the file system has a lot to do with memory:
1 The system places frequently used file data into the main memory buffer to speed up the file system’s read/write, so Linux’s physical memory is eventually used up! This is normal! 3 you can manually use sync to force files configured as Dirty in the memory to be written back to the disk. 4 If the shutdown is normal, the shutdown command will actively call sync to write data back to the disk. 5 If the shutdown is abnormal (such as power jump, crash, or other unknown reasons), Because the data has not been written back to disk, you may spend a lot of time verifying the disk after a reboot, and may even cause file system corruption (not disk corruption)
Disk and directory capacity
The overall disk data is stored in the Superblock, but the capacity of each individual file is recorded in the inode
The command | meaning |
---|---|
df | Lists the total disk usage of the file system |
du | Evaluate the disk usage of a file system (often used to estimate directory capacity) |
- df
Since df mainly reads data for almost the entire file system, the scope of reading is mainly the information in the Superblock, so this command displays the results very fast
- du
Unlike df, du directly searches the file system for all file data
By default, the output is designed in kilobytes. If you want to know how many MEGabytes the directory occupies, use the -m argument
Connection file
Chapter 10 Vim Editor
Vi text editor VIM program development tools
vi
Vi is divided into three modes, namely “General mode”, “Edit mode” and “Command line command mode”.
General pattern
Open a file with VI and enter normal mode directly (this is the default mode)
Common buttons in common mode
delete
- Key D correlation
The command | meaning |
---|---|
dd | Delete the entire line |
ndd | Delete n lines |
d$ | Deletes the last character on the line from where the cursor is (deletes backwards to the end) |
d0 | Deletes the first character on the line from the cursor position (forward to the front) |
| | x x x to delete a character back forward to delete a character | | nx | | continuous delete n characters
| y | copy the entire line | | | p paste the entire line |
Edit mode
In normal mode, pressing any letter such as “I, I, O, O, a, a, r, R” will enter editing mode
Command line command mode
In common mode, enter: /? Any of the three buttons move the cursor to the bottom line
chapter 11 bash
shell
You need a computer to output music. What support does this process need
1 the hardware
Requires your hardware to have a sound card chip
2 Kernel Management
You need an operating system kernel that can support the chipset and drivers for the chip
3 Applications
The user is required to enter a command to play the sound
The above three steps are a simple way to output sound. The user needs to output a command before the hardware executes the command to work. How does the hardware know what you’re doing, that’s the kernel’s job
We need to communicate the incoming commands through the shell to the kernel so that the kernel can control the hardware to work
An operating system is a set of software that controls the hardware and manages the monitoring of the system’s activity. If this set of software can be manipulated at will, the system will crash.
So you can’t let the user use it at will. Hence the creation of an application on the operating system ->shell
In fact, the function of shell is only to provide an interface to the user’s operating system, so the shell needs to be able to call other software.
In short, any interface that operates on an application is called a shell
Linux的shell
Note There are many types of shells in Linux. Example: /etc/shells
Each shell has similar functionality, but some syntax implementations differ.
Linux uses bash by default
bash
Several advantages of Bash
1 Command memory function
2 Command and file completion
3 Name alias Settings
4. Job control, foreground and background control
5 Program Scripts
When you log in to Linux, a shell is given according to the Settings of the /etc/passwd file (bash by default) and you can operate the shell according to the commands above
type
Internal commands: Commands built into bash
External commands: External commands that are not built into bash
Is querying a command external or built-in to bash? — type
You can see that the CD command is built into bash
Variable functionality of the shell
Linux is a multi-user, multi-task environment, and everyone has a bash after logging in.
Read the variable
Variables must be preceded by a $sign when displayed
echo $PATH
echo ${PATH}
cd $work
Copy the code
Set a variable
workdir='workapce/mxx'
Copy the code
Setting rules for variables |
---|
The variable name can contain only letters and numbers, but cannot start with a number |
Special characters inside double quotes can retain their original features, such as $correctly reading variables in a string, while single quotes do not |
name="my name is $USER"
echo $name// mu name is tom <! --> name='my name is $USER'
echo $name
// mu name is $USER
Copy the code
Cancel the variable
unset workdir
Copy the code
Q: Set a variable to represent the working directory
A variable defined in the parent process cannot be used in the child process. For example, if you define the variable and then start a new process, the variable cannot be read
However, you can use this variable by setting it as an environment variable with export
The function of environment variables
To query the current environment variables, run the env export command
env
Query all environment variables in the current shell environment
set
Shell not only has environment variables, but also various custom variables
Set Queries all variables
export
The difference between environment variables and custom variables is whether they can be used by a quilt process
The child process inherits the environment variables of the parent process, but not the custom variables of the parent process
Export can convert custom variables into environment variables
export
// export WORKDIR=$WORKDIR:workspace/blued-shopping/blued-shop
Copy the code
You can see all of the current environment variables
Why can child processes use environment variables
1 When a shell is created, the operating system allocates a memory block for the shell to use. The variables in this memory can be used by child processes
2. If the parent process uses export, you can write custom variables to the above memory block
When loading another shell (starting the child, leaving the parent), the child shell can import the parent shell’s memory block into its own environment variable block
ulimit
Bash can limit certain system resources for the user, such as the number of open files, available CPU time, total available memory, and so on, set with ulimit
Argument parsing | meaning |
---|---|
unlimited | unlimited |
0 | unlimited |
history
View History Command
history
history 10
history-w // Write ~./bash_historyCopy the code
The bash operating environment
Search order of paths and commands
There are multiple names with the same name on our system, so which command does the Bash shell actually use? The order followed is as follows
1. Execute command 2 on a relative/absolute path. Find the command by alias to perform 3. Executed by the bash built-in commands 4. Executed by the first command found in the order of $PATH
[root@localhost ~]# alias echo='echo -n'
[root@localhost ~]# type -a echo
echo is aliased to `echo -n'
echo is a shell builtin
echo is /bin/echo
Copy the code
You can see alias first in the built-in command at the end of the PATH
Bash login information
These can all be configured
Environment profile
Why is it possible to use variables when you are already in bash, such as CNPM installed when you open a bash? This is due to the existence of the system’s environment configuration files, which are read once Bash is started
It is divided into the configuration file of the whole system and the user’s personal hobby configuration file
/etc/profile belongs to the overall system configuration. You are not advised to modify this file. The configuration file that every user must read when they get bash
After bash finishes reading the overall environment profile /etc/profile, it starts reading user profiles. There are three main user preference profiles involved
1 ~/.bash_profile 2 ~/.bash_login 3 ~/.profile
Bash reads only one of the above, in order 1->2->3
Take a look at the contents of the ~/.bash_profile file
# nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
export PATH=$PATH:/Users/zhengzaijiazai/adt/sdk/platform-tools:/Users/David/adt/sdk/tools
Copy the code
Set PATH variable and use export to export it as an environment variable
Since PATH is set in /etc/profile, the configuration here is handled in a cumulative manner
There is another ~/.bashrc for configuration files
Some ~/.bash_profile ends with lines like this
if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi
Copy the code
Bashrc is in the home folder. If so, read in its configuration information
- source
Both the /etc/profile and ~/. Bash_profile profiles are obtained when the user login shell is used. How do I modify the configuration files to take effect
1 Log out or 2 source
sourceConfiguration file nameCopy the code
This allows you to read the configuration file into your current shell environment
Data flow redirection
Data stream redirection is the transfer of some of the data that is going to appear on the screen to somewhere else
The data flow
Input data stream: Take writing a file as an example, characters entered from the keyboard are input to the data stream
Output data stream: Take reading a file as an example. When the file content is displayed on the screen, the output character stream is displayed
The output data flow can be divided into: correct output error output
echo config.js > copy.js
Copy the code
All config information that should appear on the screen is imported into copy.js
The copy file is created as follows
1 If the file does not exist, the system automatically creates it
2 If the file exists, the system clears the file before writing data to it
That is, if you output to an existing file as >, that file will be overwritten
If you do not want to be overwritten and continue to append information, use >>
In the command line judge && | |
ls tmp/info && touch tmp/info/a
Copy the code
The pipe
The bash command runs with output data! So if the data has to go through several procedures to get the format we want, how should we configure it?
-
grep
-
Uniq to heavy
-
Wc statistics
Chapter14 Account Management and Permission Settings
Account and user group
When logging in to the Linux host, enter our account ID and the corresponding account in /etc/passwd
Every logged-in User gets at least two ids, a User ID (UID) and a Group ID (GID).
- How does a file identify its owner and group
Each file has an owner ID and an owner group ID. When we need to display file attributes, the system will find the account and group name corresponding to the UID/GID according to the contents of /etc/passwd and /etc/group and display them
- What happens when you enter your account and password to log in to the shell
1 Run the /etc/passwd command to check whether the account you entered is present
If it does not, it displays the UID and GID of the account (in /etc/group). In addition, the home folder and shell configuration of the account are also read
2 again is to check the password list!
Linux will go into /etc/shadow to find the corresponding account and UID, and then check that the password you just entered matches the password in it
3 If everything is OK, enter the Shell control stage
/etc/passwd
Each line represents an account number, and the number of lines represents the number of accounts in your system
/etc/shadow
We know that many programs run with permissions, and permissions are associated with Uids/Gids! Therefore, programs need to read /etc/passwd to know the permissions of different accounts. Therefore, the /etc/passwd permission should be set to -rw-r–r– in this case, although the password was encrypted earlier, it was placed in the second field of /etc/passwd! This makes it easy for someone to steal, encrypted passwords can also be brute force to try and error!
Because of this relationship, a technique was developed to separate passwords by moving them to /etc/shadow
/etc/group
The GID corresponds to the user group name
Each line of this file represents a group
Let’s look at the relationship between these three
Effective Group vs. Initial Group
Each user has a GID in the fourth column of his /etc/passwd file, which is the initial group. In other words, as soon as the user logs in to the system, he or she immediately has the rights related to the group
However, a user can be added to multiple user groups. For example, USERA can belong to both group1 (initial user group) and group2 (manual addition).
When the user creates a new file, which group does the new file belong to
It depends on the valid user group at the time
Example Query the user group to which the current user belongs
groups
Copy the code
The first output group is the effective group
To create a file with this user identity, the user group of the file is the valid user group
Switch valid groups
newgrp newGrooup
Copy the code
However, there is a limitation to using Newgrp, that is, the groups you want to switch to must be groups that you already support, that is, my users can switch to valid groups as long as they have supported groups
Account management
Add a user
usersadd newName
Copy the code
The CentOS system mainly handles several projects for us
1 Create a line of data related to the account in /etc/passwd, including the UID/GID/ home directory
2 In /etc/shadow, enter the password related parameters of the account, but there is no password
3 Add a group name in /etc/group that is the same as the account name
4 Create a directory under /home with the same name as the account as the user home directory and the permission is 700
User identity switch
-
su
-
sudo
Chapter16 Routine work crontab
What is routine work?
A node-schedule task in a node
There are two kinds of tasks, periodic and one-time
Periodicity: like sleeping every day
One-off: like finding money occasionally
The AT command is used to process one-time tasks
The crontab command is used to process periodic work
Routine work scheduling common to Linux
-
Rotation of log files
-
Delete temporary files
Some software generates cached files during operation, but when the software is shut down, these cached files may not be actively removed.
The system deletes these cached files by running a command called tmpwatch through a routine work schedule
At One-time job scheduling
Atd is responsible for handling these iterations, but not all Linux distributions turn them on by default
/etc/init.d/atd restart
Copy the code
Use the at command to generate the job you want to run, write it to the /var/spool/at/ directory as a text file, and wait for the atD service to pick it up and run
The/etc/ats. Allow and/etc/palawan deny
These two files limit who has the right to call at
1 /etc/at.allow
Users in this file can only use AT, and users not in this file cannot use AT (even if not in at.deny).
2 if the/etc/ats. Allow does not exist, and sought the/etc/palawan deny this file Everything is written to cannot use at the (wasn’t) can be used
3 If neither file exists, only root can use the at command
- Query the number of AT job schedules in the current machine
atq
Copy the code
- Example Delete a job schedule
atrm [jobnumber]
Copy the code
batch
Batch is another AT, but batch does the work you assign when the CPU workload falls below 0.8
- CPU workload
If a program needs to use the CPU all the time, then the CPU utilization may reach 100% but the CPU workload will approach 1 because the CPU is only responsible for one job
If you run two of these programs at the same time, the CPU utilization is still 100%, but the workload becomes 2
crontab
User Settings
Crontab is used to process periodic work. For security reasons, you can restrict which identities can use crontab
-
/etc/cron.allow
-
/etc/cron.deny
When the user uses the crontab command to create a work schedule, the work is logged to /var/spool/cron/ by account
For example, when dmtsai uses crontab, his work will be logged to /var/spool/cron/dmtsai
By default, you can run crontab -e to edit your own routine life command as long as the user is not placed in /etc/cron.deny
The time and position are five numbers * * * * * representing minutes, hours, dates, months, weeks
write
crontab -e
Copy the code
Let’s write one that outputs the current time per minute
*/1 * * * * echo $(date) > /tmp/test.txt
Copy the code
Check whether the crontab is working
crontab -l
Copy the code
restart
/bin/systemctl start crond.service
Copy the code
System-level crontab
Crontab -e is for users. If it is a routine task of the system, it needs to be handled separately
Need to edit /etc/crontab file
The minimum detection limit of cron service is “minutes”, so “Cron will read the data contents in /etc/crontab and /var/spool/cron every minute”. Therefore, once the /etc/crontab file is edited and saved, The cron configuration runs automatically
Manually restart the Crond service
/etc/init.d/crond restart
Copy the code
- MAILTO
When the routine work command in /etc/crontab is incorrect, or the result of the routine work is STDOUT/STDERR, the system will send the error message or the screen display information to the root (by default, the system will send a mail directly to root).
- PATH
The command path
Anacron wakes up work tasks during downtime
Anacron does not specify when to run a task. Instead, anacron does it on a daily basis or immediately after it starts. It detects crontab tasks that should have been run during the downtime but did not
Anacron will detect crontab tasks that are not running on the system for one day, seven days, and one month
Anacron is usually run at two times, one is run during system startup and the other is written to the crontab schedule. This allows you to analyze crontab work that is not running on the system at a specific time
Chapter17 Program Management
17.1 Process Procedures
When any event is triggered, the system defines it as a process and gives the process an ID, PID. At the same time, according to the user and related attributes of the program, give the PID a group of effective permission configuration
The program is usually placed on a physical disk and triggered by the user’s movement. When triggered, it will be loaded into memory as an individual, which is the program. In order to the operating system can manage this program, so the program has to give the operator permission/attributes and other parameters, including the program required command code and data or file data, and finally give a PID. The PID is used by the system to determine whether the process has permission to work
- The program
The binary file that the system needs to boot
Usually a binary program is placed in a storage medium and exists as a physical file
- process
When the program is triggered, it is loaded into memory as an individual, which is called a process
After the program is triggered, the permissions and attributes of the performer, the program code and the required data of the program will be loaded into the memory, and the operating system will give this memory unit an identifier PID
Subroutine and parent
Programs are related to each other. As you can see from the diagram above, after two bash runs in a row, the parent of the second bash is the previous one
Resident process
Processes generated by certain commands are terminated quickly, such as ls display file touch create file, etc. For example, the system will scan /etc/crontab every minute to schedule work. Crond is managed by a program that starts and runs in the background continuously. This is the process that’s always in memory
Resident programs are usually responsible for the functions provided by the system to serve the user’s tasks, so they are called daemons. There are many services in the system, but they are generally divided into the services required by the system itself, such as CROND and ATD just mentioned
The network service launches a port that can handle network listening to provide external clients with access requirements
Linux multi-worker environment
In fact, when running a command under Linux, the system will load the relevant permissions, attributes, program code and data into the memory, and give this unit a program identification code (PID). Finally, the tasks that the command can perform are related to the permissions of the PID
- Multitasking behavior
Linux allows the CPU to switch between jobs, meaning that each job takes only a few CPU commands, so the CPU can switch between processes per second
- Special program management behavior
Linux can kill a stuck program at any time and then run it again without restarting it
- Job Control in Bash
Once you log in to bash, you get a bash PID, and almost everything else you run under this environment is a child process
Under this single Bash interface, multiple jobs can be handled
Job control
After logging in to the bash shell, perform behavior management of multiple jobs simultaneously on a single terminal interface
The environment that allows you to work on the prompt bytes is called foreground, and other work can be paused or run through background
- Throw the command directly into the background to “run” &
Like starting a project
node index.js
Copy the code
So this is always starting in the foreground, and we can put this in the background
node index.js &
Copy the code
The addition of the & puts the command in the background, at which point bash outputs a working number of 1 and PID 90328
- Query current work in the background
jobs
Copy the code
parameter | meaning |
---|---|
l | In addition to the job number and command string, list the PID numbers |
r | Only the work being run in the background is listed |
s | List only the work that is being stopped in the background |
You can see that there’s a + – sign
- Represents the last job number to be placed in the background – represents the last job number to be placed in the background and after the last third job, there is no +/- symbol
- Fg (foreground)
fg// By default, the + work is taken outfg- // Take out the - workfg% job number // Retrieves the corresponding job for this job numberCopy the code
- Admin work kill
kill -signal %jobnumber
Copy the code
Signal parameters | meaning |
---|---|
1 | Re-read the configuration file of the parameter (like reload) |
2 | Represents the same action as typing [CTRL]-c on the keyboard |
9 | Force a job to be deleted immediately |
15 | To terminate a job in the normal procedural manner. It’s not the same thing as minus 9 |
Process management
Why process management
Linux is a very busy system, so when the entire system resources are about to be used up, can you find the program that consumes the system the most, and then delete the program to restore the system to normal
In addition, if a program is not written well and there is a problem in memory, how do you find it and remove it?
If there are five or six jobs running on your system at the same time, but one of them is the most important, how do you make that one priority
Check the process
- ps
The command | meaning |
---|---|
ps -l | Query only processes related to your own bash |
ps aux | Current memory all processes |
Ps is static. The output is the state of the process at a point in time
- top
Top can monitor the process working status of the entire system
top [-dDigital] | top [- BNP]Copy the code
-d Can be followed by seconds. The default value is 5 seconds, which indicates the time for updating process resources each time
Key commands that can be used during the top run
parameter | meaning |
---|---|
? | Displays key commands that can be typed in top |
P | Displays in order of CPU usage |
M | Display in order of resources used by Memory (default) |
N | Sort by PID |
T | The cumulative CPU TIME (TIME+) order used by the Process |
k | Give a PID a signal |
r | Reformulate a NICE value for a PID |
q | Leave the top software button |
How do I find the most CPU-consuming processes
Use top and press P
Process management
By giving the program a signal to tell the program what to do
Query all available Singals
kill -l
Copy the code
Some commonly used signals
The numerical | The name of the | meaning |
---|---|---|
1 | SIGHUP | Starting a terminated program causes the PID to re-read its own profile, similar to a reboot |
- Killall-singal Command name
Delete the process based on the process name
For example, we start the service in the background via Node index.js &
Then delete killall-9 node
To remove a program, we can use the PID or the name of the command that started the program,
What if you want to delete a service? The simplest way is to use killall to delete all programs started with a command name.
Priority of the process
Linux gives programs what it calls a “priority running order (PRI),” with a lower PRI value indicating higher priority
Earlier we looked at a process called Ps-L
You can see the parameter PRI
PRI is dynamically tuned by the kernel, and the user has no right to interfere with PRI
If you want to change the priority of an application, you need to use Nice. Nice is the NI of the table above
In general, the correlation between PRI and NI is as follows:
PRI(new) = PRI(old) + nice
But if the original PRI was 50, it’s not that we give a nice = 5, it makes the PRI 55.
Because PRI is determined “dynamically” by the system, although NICE value can affect PRI, the final PRI is still determined after system analysis.
In addition, the nice value is positive or negative, and since the smaller PRI is, the earlier it is run, when the nice value is negative, the program will lower the PRI value, which will become a priority for processing
- nice
The newly executed command deals with nice
- renice
The existing process NICE is retuned
Viewing System Resources
Free Check memory usage
free [-b|-k|-m|-g] [-t]
Options and Parameters
-b: If you enter free, the display unit is Kbytes. You can use b(bytes), M (Mbytes), K (Kbytes), and g(Gbytes) to display the units
-t: Displays the total amount of physical memory and swap
The Mem line shows the amount of physical memory, while Swap is the amount of virtual memory. Total is the total amount, used is the amount used, and free is the amount left available. Shared /buffers/cached is the amount of buffers and buffers that have been used.
The Linux test host is pretty mundane and doesn’t work at all, but is the physical memory almost used up
However, at least 132MB is used for buffers and 287MB is used for cached, meaning that the system is “very efficient at using up all memory” in order to speed up access
Many friends ask the question, “Why is my system running out of memory when it’s so easy?” It’s normal to be used up! It’s the amount of swap that you need to pay attention to
In general, swap is best not to be used, especially swap is best not to be used more than 20%, if you find that swap usage is more than 20%, then it is best to buy physical memory
The system will use swap, definitely because the entity is out of memory
In order to speed up the system performance, the Linux system will cache the most frequently used or recently used file data, so that the system needs to use the file directly from the memory, without re-reading the hard disk, of course, speed up the above! Therefore, it is normal for physical memory to be used up
Uname: View system and core information
Uptime: view the system startup time and workload
Special documents and procedures
Chapter 18 System Service
For some function, the system must provide some service (whether system or network). This service is called service
But a service always needs to run. The program that implements this service is called daemon
For example, crond is a daemon that implements a round-robin routine work scheduling service
The rest is to be sorted out
information
Creating a New identity
- Creating a User Group
groupadd work
Create a new identity after logging in to the server as root
useradd vbird
Copy the code
This creates a new role
But at this point you try to log in to the server using this identity
ssh vbird@xxx
Copy the code
You will be prompted
Permission denied, please try again
Copy the code
Permission error, not password error
We need to set the password for vbird
passwd vbird
Copy the code
Then enter your password. This password will be your server login password.
Modify the project configuration information to deploy with the new identity
Linux file permissions
Three kinds of identity
-
The user
-
User groups
-
others
By default, all system accounts and common users, as well as root information, are stored in /etc/passwd
The personal password is in /etc/shadow
All Linux group names are in /etc/group
When a user logs in to Linux, Linux does not recognize the account name, so it searches for a file named /etc/passwd and resolves the name by identifying the UID. The user group name also needs to be resolved, but it resolves the name through the /etc/group file. User passwords are stored in the /etc/shadow file, and user group passwords are stored in the /etc/gshadow file.
Creating a New identity
- Creating a User Group
groupadd work
Create a new identity after logging in to the server as root
useradd vbird
Copy the code
This creates a new role
But at this point you try to log in to the server using this identity
ssh vbird@xxx
Copy the code
You will be prompted
Permission denied, please try again
Copy the code
Permission error, not password error
We need to set the password for vbird
passwd vbird
Copy the code
Then enter your password. This password will be your server login password.
Modify the project configuration information to deploy with the new identity
Chapter16 Routine work crontab
What is routine work?
A node-schedule task in a node
There are two kinds of tasks, periodic and one-time
Routine work scheduling common to Linux
-
Rotation of log files
-
Delete temporary files
At One-time job scheduling
- Write the at
Format for
at TIME
Copy the code
Like writing a task that will be executed in three minutes
at now + 3 minutes
echo "hello" > /tmp/test.txt
Copy the code
CTRL + D to exit the AT editing environment
- Query the number of AT job schedules in the current machine
atq
Copy the code
The preceding number 1 indicates the job number for this job scheduling
- Example Delete a job schedule
atrm [jobnumber]
Copy the code
Crontab Indicates cyclical work scheduling
Edit crontab work content
crontab -e
Copy the code
The VI writing interface is displayed
Let’s write one that outputs the current time per minute
*/1 * * * * echo $(date) > /tmp/test.txt
Copy the code
Save wq after you write
View the cronTab working content
crontab -l
Copy the code
Restart your crontab
/bin/systemctl start crond.service
Copy the code
Delete all crontabs
crontab -r
Copy the code
If one of them is deleted, please crontab -e to compile the file
Create crontab as user
When the crontab is created, the work scheduler is logged to /var/spool/cron/
Every work done by cron is recorded in the /var/log/cron log file
Create a crontab as vbird and see if /var/spool/cron/vbird is available
Okay, I don’t have access to it. Switch to root
System-level crontab
Crontab -e is user-level scheduled work, which is not necessary if it is routine work.
The system routine works in the /etc/carontab file. If you want to handle routine tasks on your system, you can simply change /etc/carontab.
The cron service has a minimum detection limit of minutes, so /etc/carontab is read every minute. So as soon as the edit is saved, the cron Settings are automatically executed.
To write a task that periodically deletes folders
Two cases
A command that displays the result and then returns to the command prompt to wait for the next command input
The other is to enter the command environment and not return to the command prompt environment until the command is finished
Enter view state
When entering the view state
For example, enter man date
Page turning: press the space or up and down keys
And then look for some keywords that hit the/character and then you can type the character that you’re going to search for
The shift to cooperate; Then type q to exit