Linux basics and documentation
- Linux is a Unix-like operating system that is free to use and freely disseminated
- It has a very wide range of applications in the server-side field and embedded field.
2. Version
Divided into kernel version and hairstyle version
- kernel
- Each manufacturer will make its own distribution version
- redhat
- CentOS
- ubuntu
- fedora
3. The difference between Linux and Windows
- Linux is strictly case sensitive
- All content in Linux is saved in the form of files, including hardware, users and files.
- Linux does not distinguish file types by extensions, but by permissions. However, there are some agreed extensions for administrators to see.
- Compressed package .gz .bz2 .tar.bz2 .tgz
- binary file .rpm
- Web page file .html .php
- Script file .sh
- Configuration file .conf
- Programs under Windows cannot be installed and run directly in Linux
- Linux uses more character interfaces
- Occupies less system resources
- Reduces the possibility of errors and attacks, making the system more stable
How to buy a server
- Alibaba Cloud ECS
- Amazon AWS
- Baidu Cloud
4. Connect to server
- git bash
- mac shell
- xshell4
- xftp4
5. Partition
Disk partitioning is dividing the disk into several logical parts using a partition editor
Once the disk is divided into multiple partitions, different types of directories and files can be stored in different partitions.
6. mount point
- In order to make these partitions accessible in the Linux system, these partitions need to be mounted to the corresponding directories.
- In Linux, the directory is called a mount point.
- The process of linking directories and partitions together is called mounting
- / is the root directory and must be mounted on a partition. By default, all subdirectories will be written to this partition.
- All subdirectories under the same level directory can have their own independent storage space
- Required partitions
- /root partition
- Swap partition (swap partition, virtual memory, generally 2 times the memory, not more than 2G)
- Recommended partition
- /boot (boot partition, 200M) separate partition to avoid the system being unable to start due to the partition being full.
linux common commands
1. Directory
| Contents | Purpose |
|---|---|
| / | root directory |
| /boot | Start directory, start related files |
| /dev | device file |
| /etc | configuration file |
| /home | The home directory of ordinary users, which can be operated |
| /lib | System library storage directory |
| /mnt | Mobile device mounting directory |
| /media | CD mounting directory |
| /misc | Tape drive mounting directory |
| /root | The home directory of the super user, which can be operated |
| /tmp | Temporary directory, operable |
| /proc | Running kernel information mapping, mainly outputs process information, memory resource information, disk partition information, etc. |
| /sys | Driver information for hardware devices |
| /var | variable |
| /bin | Common basic commands, such as ls, chmod, etc., can also be used by ordinary users |
| /sbin | Basic system commands, such as shutdown and reboot, are used to start the system and repair the system. Only administrators can run it |
| /usr/bin | is the running script for some software you install later |
| /usr/sbin | Place some necessary programs for system management installed by users |
1.2 Basic command format
1.2.1 Command prompt
[root@localhost ~]#- root currently logged in user
- localhost host name
- ~ The current working directory, the default is the current user's home directory, root is /root, ordinary users are /home/username
- Prompt: Super user is #, ordinary user is $
1.2.2 Command format
- command [options] [parameters]
- When there are multiple options, they can be written together
- General parameters can be written in two ways: simplified and complete. -a is equivalent to --all
1.2.3 ls
- Query the contents of the directory
- ls [options] [file or directory]
- Options
- -a displays all files, including hidden files
- -l show detailed information
- -d View the properties of the directory itself rather than the subfiles ls /etc/
- -h displays file size in a user-friendly manner
- -i displays inode, which is the i node. Each node has an ID number.
- Default file list in the current directory
1.2.3.1 -l
Show details
drwxr-xr-x . 1 root root 800 Sep 16 00:19 logs| drwxr-xr-x | . | 1 | root | root | 800 | Sep 16 00:19 | logs |
|---|---|---|---|---|---|---|---|
| File Types and Permissions | ACL Permissions | Hard Link Reference Count | Owner | Group | File Size | Last Modified | File Name |
1.2.3.2 File types and permissions
-rw-r--r--- File type - file, d directory, l soft link file
- u(owner), g(group), o(others)
- r(read) reads, w(write) writes, x(execute) executes
1.3 File processing commands
1.3.1 mkdir
- Make directory make directory
- mkdir -p [directory name]
- -p create recursively
1.3.2 cd
- Change directory change directory
- cd [directory]
- ~ Home Directory
- home directory
- last directory
- . current directory
- .. Parent directory
- Relative paths refer to the current directory
- The absolute path starts from the root directory
- Press TAB to complete commands and directories
1.3.3 pwd
- show current directory pwd
1.3.4 rmdir
- remove directory remove empty directory
- rmdir [directory name]
1.3.5 rm
- Delete a file or directory remove
- rm [file or directory]
- -r delete directory
- -f force deletion
- rm -rf file or directory] Forcefully delete all directories recursively
1.3.6 cp
- copy copy command
- copy [source file or directory] [destination file]
- -r copies directories, the default is to copy files
- -p copy with file attributes
- -d If the source file is a linked file, copy the connection attributes
- -a is equivalent to -rpd
1.3.7 mv
- Move a file or rename it move
- mv [source file or directory] [target file]
When the second parameter type is a file, the mv command completes file renaming. It renames the given source file or directory to the given target file name.
When the second parameter is the name of an existing directory, there can be multiple source files or directory parameters, and the mv command will move all the source files specified by each parameter to the target directory.When moving files across file systems, mv first copies and then deletes the original file, and the link to the file will also be lost.
# 将webdata 移动到/bin/usr/中
mv webdata /bin/usr/
# 将 /usr/lib/下所有的东西移到/zone/中。
mv /usr/lib/* /zone
# 是将lib下以txt结尾的所有文件移到/zone中。
mv /usr/lib/*.txt /zone
# 将文件temp.txt 修改名字为tempTest.txt
mv temp.txt tempTest.txt[options]Main parameters (several important parameters)-i: Interactive operation.If the mv operation will cause overwriting of an existing target file, the system will ask whether to rewrite and require the user to answer "y" or "n" to avoid accidentally overwriting the file.-f : Interactive operations are prohibited.The mv operation does not give any instructions when overwriting an existing target file. After specifying this parameter, the i parameter will no longer have any effect. - r Instructs mv to recursively move all directories and subdirectories listed in the parameter.
1.3.8 ln
- Link command, generate link file link
1.3.8.1 Hard link characteristics
- Having the same i-node and storage block can be regarded as the same file
- Can be accessed through i-node
- Cannot cross partitions
- Cannot be used against directories
- Generally not used
1.3.8.2 Soft link characteristics
- ln -s [source file] [destination file]
- -s creates soft link
- Similar to Windows shortcuts
- Soft links have their own i-nodes and Blocks, but only the file name and i-node number of the source file are stored in the data block, and there is no actual file data.
- lrwxrwxrwx l soft link The file permissions of soft links are all 777
- Modify any file and the other will change
- Delete the source file and the soft link cannot be used
- The soft link source file must have an absolute path
1.4 File search command
1.4.1 locate
- Searching by file name in the background database is faster
- The data is stored in the /var/lib/mlocate backend database and is updated once a day.
- You can update the database immediately with the updatedb command
- Only file names can be searched
- /etc/updatedb.conf Indexing configuration file
- PRUNE_BIND_MOUNTS = "yes" All take effect, enable search restrictions
- PRUNEFS does not search file systems
- PRUNENAMES ignored file types
- PRUNEPATHS ignored paths /tmp
1.4.2 whereis
- Search the path where the command is located and where the help documentation is located
- whereis command name whereis ls
- -b Find only executable files
- -m only searches help files
1.4.3 which
You can see the alias which ls All you can see are externally installed commands. Unable to view the commands that come with the Shell, such as which cd
1.4.4 Environment variables
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
- Defines the path of the system search command
- echo $PATH
1.4.5 find
- file search command
- find [search scope] [search criteria]
1.4.5.1 Search by name
- Avoid large-scale searches, which will consume a lot of system resources
find / -name aaa.log1.4.5.2 Wildcard characters
- find searches for file names that meet the conditions in the system. If a match is required, use wildcard matching. The wildcard character is an exact match.
- wildcard
*matches anything?matches any character[]matches any character within brackets
find . -name "ab[cdef]"1.4.5.3 -i
Not case sensitive
find / -iname A.log1.4.5.4 -user
Search by owner
find /root -user root
find /root -nouser1.4.5.5 Search by time
find /nginx/access.log -mtime +5| Parameters | Meaning |
|---|---|
| atime | file access time |
| ctime | Change file attributes |
| mtime | Modify file content |
| Parameters | Meaning |
|---|---|
| -5 | Files modified within 5 days |
| 5 | Files currently modified 5 days ago |
| +5 | File modified 5 days ago |
1.4.5.6 Search by size
- k lowercase, M uppercase
find . -size 100k| Parameters | Meaning |
|---|---|
| -8k | Less than 8K |
| 8k | equal to 8K |
| +8k | Greater than 8K |
| +8M | Less than 8M |
1.4.5.7 Search by i node
find . -inum 1234561.4.5.8 Comprehensive application
find /tmp -size +10k -a -size -20k- Find files in the /etc directory that are greater than 10KB and less than 20KB
- -a and Logical AND, both conditions are met
- -o or Logical OR, only one of the two conditions is met
find /tmp -size +10k -a -size -20k -exec ls -lh {} \;- exec operates on the results of the previous command
1.4.5.9 grep
- Match matching strings in files
- grep "10" access.log
- -i ignore case
- -v excludes the specified string
- The find command searches for file names that meet the conditions in the system. If a match is required, use wildcard matching. The wildcard character is an exact match.
- The grep command searches for strings that meet the conditions in the file. If a match is required, use a regular expression to match. The regular expression includes matching.
1.5 Help commands
1.5.1 Basic usage
- man command Get help for a specified command
- man ls View help for ls
1.5.2 Man level
- 1 View the help of the command
- 2 View help for functions that can be called by the kernel
- 3 View help for functions and function libraries
- 4 View help for a special file
- 5 View help for configuration files
- 6 View the game’s help
- 7 View other help
- 8 View help for commands available to your system administrator
- 9 View help for kernel-related files
1.5.3 View command level
- View command level
- 1p: POSIX utilities
- POSIX stands for Portable Operating System Interface of UNIX (abbreviated as POSIX). The POSIX standard defines the interface standards that the operating system should provide for applications.
man -f ls
whatis ls
man 1 ls
man 1p ls1.5.4 Keyword search
- man -k passwd1.5.5 Shell internal help
- If whereis is found, it is external; if not found, it is internal.
help cd1.6 Compression and decompression commands
Suffix .zip .gz .bz2 .tar.gz .tar.bz2
1.6.1 zip format
- Compressed file zip compressed file name source file
- Compressed directory zip -r compressed file name source directory
- Unzip unzip compressed file name
mkdir book
touch book/1.txt
touch book/2.txt
zip -r book.zip book
unzip book.zip1.6.2 gzip
| Command | Example | Meaning |
|---|---|---|
| gzip source file | gzip a.txt | Compressed into a .gz format compressed file, the source file will disappear |
| gzip c source file > compressed file | gzip -c yum.txt > yum.txt.gz | Compressed into a compressed file in .gz format, the source file will not disappear |
| gzip -r directory | gzip -r xx | Compress all subfiles in the directory, but do not compress the directory |
| gzip -d compressed file name | gzip -d yum.txt.gz | Decompress the file without retaining the compressed package |
| gunzip compressed file | gunzip yum.txt.gz | decompress the file without retaining the compressed package |
- Compression is to compress files in a directory
1.6.3 .bz2 format compression
| Command | Example | Meaning | | -- | -- | -- | -- | | bzip2 source file | bzip2 1.txt | Compressed to a file in .bz2 format, without retaining the source file | | bzip2 -k source file | zip2 -k 1.txt | Compress to .bz2 format file, retain the source file | | bzip2 -d compressed file name | bzip2 -d 1.txt.bz2 | Decompress the compressed package | | bunzip2 compressed file name | bunzip2 1.txt.bz2 | Unzip the compressed package |
- bzip2 cannot compress directories
1.6.4 tar
- Packaging command
- tar -cvf package file name source file
- -c package
- -v show process
- -f specifies the packaged file name
tar -cvf book.tar book
gzip book.tar
bzip2 book.tar- x unpack
tar -xvf book.tar1.6.5 tar.gz compression format
- It can be packaged into
.tarformat first, and then compressed into.gzformat.-zcompresses into.tar.gzformat.-xdecompresses into.tar.gzformat.
| Command | Example | Meaning | | -- | -- | -- | -- | | tar -zcvf compressed package name .tar.gz source file | tar -zcvf book.gar.gz book | can be packaged into .tar format first and then compressed into .gz format | | tar -zxvf compressed package name .tar.gz | tar -zxvf book.tar.gz | Decompress tar.gz compressed package | | tar -jcvf compressed package name .tar.bz2 source file | tar -zcvf .tar.bz2 book | can be packaged into .tar format first and then compressed into .bz2 format | | tar -jxvf compressed package name .tar.bz2 | tar -zxvf book.tar.bz2 | Decompress tar.bz2 compressed package |
1.7 Shutdown and restart commands
1.7.1 shutdown
shutdown shutdown command
- -c cancels the previous shutdown command
- -h shutdown
- -r restart
shutdown -r 06:00
shutdown -c1.7.2 init
Shut down
init 0Restart
init 6system runlevel
- 0 shutdown
- 1 but user
- 2 Not fully multi-user, does not include NFS services
- 3 fully multi-user
- 4 not allocated
- 5 Graphical interface
- 6 Restart
1.7.3 logout
Log out
logout1.8 Mount
1.8.1 Mount command
- mount View the devices mounted on the system
mount
mount -a1.8.2 Mount command format
- mount [-t file system] [-o special option] device file name mount point
- Options
- -t file system ext4 iso9660
- -o special options
1.8.3 Mount the optical drive
mkdir /mnt/cd
mount -t iso9660 /dev/sr0 /mnt/cdrom1.8.4 Uninstall the optical drive
umount /dev/sr0
umount /mnt/cdrom1.8.4 Mount U disk
fdisk -l
mount -t vfat /dev/sdb1 /mnt/usb- Linux does not support NTFS format by default
1.9 View logged in user information
1.9.1 w
View logged in user information
- USER Login username
- TTY login terminal tty1 local terminal pts/0 remote terminal
- FROM Login IP
- LOGIN login time
- IDLE user idle time
- JCPU The time occupied by all processes in this terminal
- PCPU Time occupied by the current process
- WHAT command being executed
1.9.2 who
View logged in user information
- USER Login username
- TTY login terminal tty1 local terminal pts/0 remote terminal
- LOGIN login time (login IP)
1.9.3 last
View current login and past login user information. Read /var/log/wtmp file by default.
- Username
- Log in to the terminal
- Login IP
- Login time
- Exit time (online time)
1.9.4 lastlog
View the last login time of all users
- Username
- Log in to the terminal
- Login IP
- Last login time
2. shell
- The shell is a command line interpreter that provides users with an interface to send requests to the Linux kernel in order to run programs system-level programs
- Users can use Shell to start, suspend, stop or write some programs
- Shell is also a very powerful programming language, easy to write, easy to debug, and highly flexible.
- Shell is a scripting language for interpretation and execution. Linux system commands can be directly called in Shell.
2.1 View supported shells
- /etc/shells
2.2 echo
- Output command
- --e supports backslash-controlled character conversion
| Control characters | Function | | -- | -- | -- | | \a | Output warning sound | | \b | Backspace key, also known as the left delete key | | \n | newline character | | \r | Enter key | | \t | Tab character, also known as Tab key | | \v | vertical tab character | | \onnn | Output characters according to the octal ASCII code table, where 0 is the number zero and nnn is a three-digit octal number | | \xhh | Output characters according to the hexadecimal ASCII code table, where hh is a two-digit hexadecimal number |
- 30m-47m
- echo -e "\e[1;31m warning \e[0m"
| symbol | color |
|---|---|
| #30m | Black |
| #31 | Red |
| #32 | Green |
| #33 | Yellow |
| ##34 | Blue |
| ##35 | Magenta |
| ##36 | Green |
| ##37 | White |
2.3 Writing an execution shell
#!/bin/bash
echo -e "\e[1;34m hello world \e[0m"Grant execution permissions and run directly
chmod 755 hello.sh
./hello.shExecute script via Bash call
bash hello.sh2.4 Aliases
- Command alias == nickname
- Temporarily effective
- alias
- alias rm="rm -i"
- Write environment variable configuration file vi ~/.bashrc
- source ~/.bashrc
- unalias alias delete alias
2.5 Order of effectiveness of commands
- absolute path or relative path
- Alias
- bash internal commands
- The first command found in the directory search order defined by the $PATH environment variable
2.6 Command shortcut keys
| Command | Meaning |
|---|---|
| ctrl+c | Forcefully terminate the current command |
| ctrl+l | clear screen |
| ctrl+a | Move the cursor to the beginning of the command line |
| ctrl+e | Move the cursor to the end of the command line |
| ctrl+u | Delete from the cursor position to the beginning of the line |
| ctrl+z | Put the command into the background |
| ctrl+r | Search in command history |
2.7 Historical commands
- history [options] [history command save file]
- Options
- -c Clear command history
- -w writes the history commands in the cache to the history command save file ~/.bash_history
- 1000 entries are saved by default /etc/profile HISSIZE=10000
2.8 Call
- Use the up and down arrows to recall previous history commands
- Use !n to repeatedly execute the nth historical command
- Use !! to repeat the previous command
- Use the ! character to repeat the last command starting with this string
2.9 Output redirection
2.9.1 Standard input and output
| device | device file name | file descriptor | type |
|---|---|---|---|
| keyboard | /dev/stdin | 0 | standard input |
| monitor | /dev/stdout | 1 | standard output |
| Monitor | /dev/stderr | 2 | Standard Error Output |
| Type | Symbol | Function |
|---|---|---|
| Standard output redirection | Command > File | Output the correct output of the command to the specified file or device in an overwriting manner |
| Standard output redirection | Command >> File | Output the correct output of the command to the specified file or device in append mode |
| Error output redirection | Command>File | Output the error output of the command to the specified file or device in an overwriting manner |
| Error output redirection | Command>>File | Output the error output of the command to the specified file or device in append mode |
| Save the correct output and error output at the same time | Command>File 2>&1 | Save both the correct output and the error output to the same file in an overwriting manner |
| Save the correct output and error output at the same time | Command>File 2>>&1 | Save both the correct output and the error output to the same file by appending |
| Save the correct output and error output at the same time | Command &> File | Save the correct output and error output to the same file in an overwriting manner |
| Save the correct output and error output at the same time | Command &>> File | Save the correct output and error output to the same file by appending |
| Save the correct output and error output at the same time | Command>>File 1 2>File 2 | Append the correct output to File 1 and append the error output to File 2 in an overwriting manner |
2.9.2 Input redirection
command <file takes a file as input to the command
Command << identifier identifier uses the content between identifiers as input to the command
wc < access.log
wc <>
2.10 Pipe symbols
2.10.1 Sequential execution of multiple commands
| Multiple command executors | Format | Function |
|---|---|---|
| ; | Command 1;Command 2 | Multiple commands are executed without any logical connection between the commands |
| && | Command 1&& Command 2 | Logical AND When command 1 is executed correctly, command 2 will be executed. When command 1 is executed incorrectly, command 2 will not be executed |
| ` | ` |
- date;ls;date;ls
- ls && echo yes || echo no
2.10.2 Pipe symbol
- The correct output of command 1 will be used as the operation object of command 2
- Command 1|Command 2
ls /etc/ | more
netstat -an | grep ESTABLISHED | wc -l2.10.3 Wildcards
Match filenames and directory names
| Wildcard | Function |
|---|---|
| ? | matches any character |
| * | matches 0 or any characters, that is, it can match any content |
| [] | matches any character in the square brackets |
| [-] | matches any character in the square brackets, - represents the range |
| [^] | matches a character that is not enclosed in square brackets |
2.10.4 Other symbols
| Symbol | Function |
|---|---|
| '' | single quote.All special symbols within single quotes, such as $ and ` have no special meaning |
| "" | Double quotation marks. Special symbols in double quotation marks have no special meaning, except $ ` , which has the meaning of calling variable values, quoting commands and escaping |
| `` | Backticks, expanded are system commands |
| $() | Same as backtick |
| # | In shell scripts, lines starting with # represent comments |
| $ | The value used to call the variable |
| \ | escape symbol |
a=`ls`
b=$(ls)3. vi editor
- VI visual interface
- Visual interface
- Similar to notepad in windows
- vim supports multi-level undo
- Cross-platform
- syntax highlighting
- Support graphical interface
3.1 Operating modes
| Schema name | Meaning |
|---|---|
| Command Mode command mode | mode waiting for input |
| Insert Mode input mode | Edit mode, used to enter text |
| Last Line Mode bottom line mode (last line, last line) | You can enter commands, search, and save |
- vim + abc
- vim +3 abc
- vim +5 abc
- vim +/xxx abc
- vim a b c :n
3.2 Common commands
3.2.1 Bottom row mode
- :w save
- :q exit
- :! Force save
- :ls lists all files
- :n next
- :N Previous
- :15 Jump to the specified line
- /xxx searches the xxx string backward starting from the cursor position
- ?xxx Search forward starting from the cursor position
3.2.2 Command mode
h Move cursor left
j Move cursor down
k Cursor moves up
l Move cursor right
ctrl+f page down (front)
ctrl+b page up
ctrl+d to turn down half a page
ctrl+u turn up half a page
dd deletes the line under the cursor
o Insert a line below the line where the cursor is and switch to input mode
yy copies the line where the cursor is located
p Paste below the line where the cursor is
P Paste above the line where the cursor is
4. Users and user groups
- Everyone who uses the operating system is a user
- A user group is a group of users with the same system permissions
4.1 User groups
4.1.1 /etc/group
/etc/groupstores all user group information in the current systemgroup:x:123:abc,def- Group name: Group password placeholder: Group number: List of user names in the group
- The root group number is 0
- 1-499 System-reserved numbers reserved for installed software and services
- User groups manually created by users start at 500
- The group password placeholders are all x
4.1.2 /etc/gshadow
- Store the password information of the user group in the current system
- One-to-one correspondence with the records in the group
Group: * : :abc- Group name Group password Group manager User name in the group
4.1.3 /etc/passwd
- Store information about all users in the current system
user:x:123:456:xxxxx:/home/user:/bin/bash- Username: Password placeholder: User number: User comment information: User home directory: Shell type
4.1.4 /etc/shadow
- Store the password information of all users in the current system
user:xxx:::::::- Username: Password:
4.2 User operations
Add group
groupadd studentModify group name
groupmod -n stu studentModify group number
groupmod -g 111 stuAdd group and specify number
groupadd -g 222 teacherDelete group
groupdel 222Add group
groupadd teacherAssign a group to the user
useradd -g teacher zhangsanAssign a group to the user
useradd -g teacher lisiSpecify working directory for user
useradd -d /home/zhangsan zhangsanSpecify comments
usermod -c iamateacher zhangsanModify username
usermod -l zhangsan zhangsan2Specify folder
usermod -d /home/zhangsan2 zhangsan2Modify the group to which the user belongs
usermod -g stu zhangsan2Delete user
userdel zhangsan2Delete the folder it belongs to
userdel -r lisi5. user command
Show logged in user name
whoamiDisplay the specified user information, including user number, user name, main group number and name, and subsidiary group list
id zhangsanDisplay all groups to which user zhangsan belongs
groups zhangsanShow user details
finger zhangsan6. Appendix
6.1 System startup
6.1.1 BIOS
- After the computer is powered on, the first thing is to read the boot program flashed into the ROM chip. This program is called (Basic Input/Output System)
6.1.2 Hardware self-test
- The BIOS program first checks whether the computer hardware can meet the basic conditions for operation. This is called "Hardware Self-Test" (Power-On Self-Test)
- If there is a problem with the hardware, the motherboard will beep with different meanings and the startup will be aborted.If there is no problem, the screen will display CPU, memory, hard disk and other information.
6.1.3 Startup sequence
- After the hardware self-test is completed, the BIOS transfers control to the next stage of the startup program.
- At this time, the BIOS needs to know which device the "next stage startup program" is stored in.
- The BIOS needs to have a ranking of external storage devices, and the device ranked first is the device that gives priority to transfer control.This sorting is called "Boot Sequence"
- The BIOS follows the "boot order" and transfers control to the storage device ranked first.
- At this time, the computer reads the first sector of the device, which is the first 512 bytes.If the last two bytes of these 512 bytes are 0x55 and 0xAA, it indicates that the device can be used for booting; if not, it indicates that the device cannot be used for booting, and control is transferred to the next device in the "boot sequence".
- The first 512 bytes are called the "Master boot record" (MBR)
6.1.4 Structure of Master Boot Record
- The "Master Boot Record" is only 512 bytes, so it can't hold much.Its main function is to tell the computer where to find the operating system on the hard disk.
- (1) Bytes 1-446: are used to record the startup information of the system and call the machine code of the operating system
- (2) Bytes 447-510 (64 bytes): Partition table. The function of the partition table is to divide the hard disk into several areas.
- (3) Bytes 511-512: Master boot record signature (0x55 and 0xAA)
6.1.5 Partition table
- Disk partitioning is dividing the disk into several logical parts using a partition editor
- Once the disk is divided into multiple partitions, different types of directories and files can be stored in different partitions.
- The "Master Boot Record" therefore must know which partition to transfer control to
- The length of the partition table is only 64 bytes, which is divided into four items, each item is 16 bytes.Therefore, a hard disk can only be divided into four first-level partitions, also called "primary partitions"
- (1) The 1st byte: If it is 0x80, it means that the primary partition is the active partition, and control is transferred to this partition.Only one of the four primary partitions can be activated.
- (2) Bytes 2-4: The physical location of the first sector of the primary partition (cylinder, head, sector number, etc.).
- (3) The 5th byte: Primary partition type, such as FAT32, NTFS, etc.
- (4) Bytes 6-8: The physical location of the last sector of the primary partition.
- (5) Bytes 9-12: The logical address of the first sector of the primary partition.
- (6) Bytes 13-16: The total number of sectors in the primary partition.
6.1.6 Hard disk boot
- The control of the computer will be transferred to a certain partition of the hard disk.
- Of the four primary partitions, only one is active.The computer will read the first sector of the activated partition, which is called the "Volume boot record" (VBR).
6.1.7 Operating system
- After control is transferred to the operating system, the operating system's kernel is first loaded into memory.
- Taking the Linux system as an example, first load the kernel under the /boot directory.After the kernel is loaded successfully, the first program to run is /sbin/init.It generates an init process based on the configuration file (/etc/initab for Debian systems).This is the first process after Linux starts. The pid process number is 1, and other processes are its descendants.
- Then, the init thread loads various modules of the system, such as window programs and network programs, until the /bin/login program is executed, the login interface jumps out, and the user enters the user name and password.
7 Process Management
7.1 Process
- A process is a program or command being executed. Each process is a running entity, has its own address space and uses certain system resources.
- A process is a program that is being executed
7.2 Introduction to process management
- Determine the status of the server
- View all processes in the system
- Kill the process. Kill the process only if it cannot be closed.
7.3 Process view command (ps)
- ps aux View all processes in the system, using BSD operating system format
- ps -le View all processes in the system, using Linux standard format
- TTY is an abbreviation for TeleType, which originally refers to a teletype machine, which is a device that reads and sends information through a serial line using a printer keyboard.
pts(pseudo['su:doʊ]-terminal slave)is the so-called pseudo terminal or virtual terminal
7.3.1 Options
| Parameters | Meaning |
|---|---|
| -a | Display all processes in a terminal |
| -u | Display the owned user and memory usage of the process |
| -x | Show processes without a controlling terminal |
| -l | Long format display, display more detailed information |
7.3.2 Result field meaning
| Data | Meaning |
|---|---|
| USER | Which user created this process |
| PID | ID number of the process |
%CPU | The percentage of CPU resources occupied by this process. The higher the occupancy, the more system resources are consumed |
%MEM | The percentage of physical memory occupied by this process. The higher the occupancy, the more system resources are consumed |
| VSZ | The percentage of virtual memory occupied by this process, in KB |
| RSS | The actual physical memory size occupied by this process, in KB |
| TTY | In which terminal this process is running.tty1~tty7 represent local control terminals, tty1~tty6 are character terminals, and tty7 is a graphical terminal.pts/0~255 represents a virtual terminal, ? indicates that this terminal is started by the system |
| STAT | Process status |
| START | The start time of the process |
| TIME | This process takes up computing time of the CPU. The higher the value, the more system resources are consumed |
| COMMAND | The command name that spawned this process |
7.3.3 Process status (STAT)
| Parameter | Meaning |
|---|---|
| R(Runing) | Running |
| S(Sleep) | Sleep |
| T(Terminated) | Stop |
| S(Son) | Contains child processes |
+ | in the background |
7.4 pstree
pstree [选项]- -p displays process PID
- -u displays the user who owns the process
7.5 Viewing processes (top)
- top
top -b -n 1 > top.txt7.5.1 Options
| Options | Meaning |
|---|---|
| -b | Use batch mode output, generally used in conjunction with -n |
| -n | times, specify the number of times the top command is executed.Generally used with the -b option |
| -d | Number of seconds, specifies how many seconds the top command is updated every few seconds.Default is 3 seconds |
7.5.2 Interactive mode commands
| Options | Meaning |
|---|---|
| ? or h | Display help in interactive mode |
| P | Sort by CPU usage, the default is this option |
| M | Sort by memory usage |
| N | Sort by PID |
| q | exit top |
7.5.3 Status bar
7.5.3.1 The first line contains task queue information
| Content | Description |
|---|---|
| 12:12:12 | Current time of the system |
| up 1 day 5:33 | System running time, this machine has been running for 1 day, 5 hours and 33 minutes |
| 2 users | Two clients are currently logged in |
| load average 0 0 0 | The average load of the system in the previous 1 minute, 5 minutes, and 15 minutes.It is generally believed that the load is smaller if it is less than 1 hour, and the load is greater than 1 hour |
7.5.3.2 The second line of process information
| Content | Description |
|---|---|
| Tasks: 100 total | Total number of processes in the system |
| 1 running | Number of running processes |
| 94 sleeping | The process of sleeping |
| 0 stopped | Process being stopped |
| 0 zombie | Zombie process.If it is not 0, check it |
7.5.3.3 The third line is CPU information
| Content | Description |
|---|---|
| Cpu(s): 0.1%us | Percentage of CPU occupied by user mode |
| 0.1%sy | Percentage of CPU occupied by system mode |
| 0.0%ni | Percentage of CPU occupied by user processes that have changed their priority |
| 99.7%id | CPU percentage of idle CPU |
| 0.1%wa | Percentage of CPU occupied by processes waiting for input/output |
| 0.1%hi | Percentage of CPU occupied by hard interrupt request service |
| 0.1%si | Percentage of CPU occupied by soft interrupt request service |
| 0.0%st | st (Steal time) virtual time percentage, that is, when there is |
7.5.3.4 The fourth line is physical memory information
| Content | Description |
|---|---|
| Mem: 1030720k total | Physical memory problem, unit is KB |
| 551860k used | Amount of physical memory used |
| 478860k free | The amount of free physical memory, the virtual machine is allocated 1024M memory, 538M is used, and 467M is free |
| 43180k buffers | As the amount of buffer memory, it can store the data that needs to be written to the hard disk to speed up the writing of data |
7.5.3.5 The fourth line exchanges partition information
| Content | Description |
|---|---|
| Swap: 2047992k total | Total swap partition (virtual memory) size |
| 536k used | The size of the swap partition that has been used |
| 2047456k free | Free swap partition size |
| 368164k cached | Read data that needs to be read frequently from the hard disk into the memory, speeding up the reading of data |
7.6 Kill the process (kill)
7.6.1 Process signals
- kill -l to view available process signals |Signal |Code |Signal name |Description |Example| | -- | -- | -- | -- | -- | |1 |SIGHUP |This signal causes the process to shut down immediately, then rewrite and read the configuration file before restarting, and restart smoothly |kill -1 -HUP process number| |2 |SIGINT |Program termination signal, used to close the foreground process, equivalent to ctrl+c | |9 |SIGKILL |Used to end the running of the program immediately. This signal cannot be blocked, processed or ignored. It is generally used to force a stop | |15 |SIGTERM |A signal to terminate the process normally, the default signal of the kill command.If it cannot be terminated normally, the SIGKILL signal will be tried |
7.6.2 Kill the process
sleep.sh
#!/bin/bash
i=0
while [ $i -le 1000 ]
do
echo $(date)
sleep 1s
donesh sleep.sh &
ps -ef | grep sleep.shkill 进程号8. System resource view
8.1 vmstat
- Monitor system resource usage status
vmstat [刷新延时 刷新次数]
vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 532 329932 99388 459768 0 0 16 81 59 50 3 1 96 0 08.1.1 (procs) process information field
| Classification | Parameters | Meaning |
|---|---|---|
| procs | r | The number of processes waiting to run. The larger the number, the busier the system |
| procs | b | The number of processes that cannot be awakened. The larger the number, the busier the system |
8.1.2 memory (memory information field)
| Classification | Parameters | Meaning |
|---|---|---|
| memory | swpd | The size of the Swap space used, in KB |
| memory | free | Free memory capacity, unit KB |
| memory | buff | buffer memory capacity, unit KB |
| memory | cache | Cache memory capacity, unit KB |
8.1.3 swap (swap partition information)
- If the larger the number of si and so, it means that data often needs to be exchanged between disk and memory, and the system performance will be worse.
| Classification | Parameters | Meaning |
|---|---|---|
| swap | si(in) | The amount of data swapped from disk to memory, in KB |
| swap | so(out) | The amount of data swapped from memory to hard disk, in KB |
8.1.4 io (disk reading and writing)
- The larger the bi and bo numbers are, the busier the disk I/O is.
| Classification | Parameters | Meaning |
|---|---|---|
| io | bi(in) | Problem reading data from block device, unit is block |
| io | bo(out) | The total amount of data written to the block device, in blocks |
8.1.5 system (system information field)
- The larger the in and cs numbers are, the busier the communication between the system and the interface device is.
| Classification | Parameters | Meaning |
|---|---|---|
| system | in(interrupt) | Number of interrupted processes per second |
| system | cs(switch) | Number of event switches per second |
8.1.6 CPU (CPU information field)
| Classification | Parameters | Meaning |
|---|---|---|
| CPU | us(user) | Percentage of CPU computing time consumed by non-kernel processes |
| CPU | sy(system) | The percentage of CPU computing time consumed by the kernel process |
| CPU | id(idea) | Percentage of idle CPU |
| CPU | wa(wait) | Percentage of CPU consumed waiting for I/O |
| CPU | st(steal) | Percentage of CPU stolen by the virtual machine |
8.3 free
- View memory usage status
free [-b|-k|-m|-g]- Options
- -b in bytes
- -k in KB bytes
- -m in MB bytes
- -g in GB bytes
# free -m
total used free shared buffers cached
Mem: 1006 687 319 0 98 449
-/+ buffers/cache: 139 866
Swap: 1999 0 19998.3.1 First line
| Parameters | Meaning |
|---|---|
| total | Total memory |
| used | The amount of memory that has been used |
| free | Number of free memory |
| shared | Amount of memory shared by multiple processes |
| buffers | Number of buffer memory |
| cached | Number of cached memories |
8.3.2 Second line
| Parameters | Algorithm | Meaning |
|---|---|---|
| - buffers/cache | used-buffers-cached in the first line | The amount of memory that has been used minus cache and buffering |
| + buffers/cache | free+buffers+cached in the first line | The amount of free memory plus cache and buffer memory |
8.3.3 The third line
| Parameters | Meaning |
|---|---|
| total | The total number of swaps, the default unit is K |
| used | The number of swaps that have been used, the default unit is K |
| free | The number of free swaps, the default unit is K |
8.6 View kernel related information
- uname
# uname -a
Linux localhost 2.6.32-279.el6.i686 #1 SMP Fri Jun 22 10:59:55 UTC 2012 i686 i686 i386 GNU/Linux
# uname -s
Linux
# uname -r
8.6.32-279.el6.i6868.7 Check the operating system bit number
file /bin/ls8.8 View release version
lsb_release -a8.9 View release version
yum install redhat-lsb -y
lsb_release -v9. System scheduled tasks
- Some tasks, such as backing up databases, need to be performed when the system is idle.
9.2 crontab
- Scheduled tasks can be executed cyclically and regularly
- cron
systemctl restart crond.service9.2.3 crontab settings
- crontab [options]
- Options
- -e edit crontab scheduled tasks
- -l Query crontab tasks
- -r deletes all crontab tasks for the current user
* * * * * 执行的任务9.2.3.1 Syntax
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed9.2.3.2 Location
| Project | Meaning | Scope |
|---|---|---|
| 1st star | Minutes in 1 hour | 0~59 |
| 2nd star | Hour of the day | 0~23 |
| 3rd star | Day of the month | 1~31 |
| 4th star | Which month of the year | 1~12 |
| 5th star | Day of the week | 0~6 |
9.2.3.3 Special symbols
| Symbol | Meaning | Example |
|---|---|---|
* | represents any time | For example, the first star represents execution every minute of an hour |
, | Represents discontinuous time | For example, "1,2,3" means executing commands at 1, 2, and 3 minutes per hour |
- | represents a continuous time range | For example, "1-5 * **", represents the execution of the command from the 1st to the 5th minute of every hour |
*/n | Represents how often to execute the command | For example, "/10" means to execute the command every 10 minutes |
0 0 1,10 * 1 | Executed on the 1st and 10th of each month, at 0:00 every week |
9.2.3.4 Case
| Symbol | Meaning |
|---|---|
| 10 22 * | Executed at 22:10 every day |
| 0 15 1 | Executed every week at 15:00 |
| 0 5 5,10 | Executed at 5 a.m. on the 5th and 10th of each month |
| 10 5 1-5 | Execute the command at 5:10 am every Monday to Friday |
| /10 10 | Executed every 10 minutes at 10 a.m. every day |
9.2.3.5 Precautions
- All options cannot be empty and must be filled in
- The minimum unit of crontab is minutes and the maximum unit is days.
- Regardless of whether you write commands or scripts, use absolute paths.
9.3 System scheduled tasks
crontab -e is a command executed by the user. Different user identities can execute their own scheduled tasks. If you need the system to perform scheduled tasks, you can edit the /etc/crontab file /etc/crontab can specify the shell, path, email sending and home directory
9.3.1 /etc/crontab
- Modify the /etc/crontab configuration file
5 5 * * * echo `date` >> /root/date.log10. Actual combat tasks
10.1 Monitor nginx
nginx.sh
#!/bin/bash
local nginx
nginx=`ps -ef |grep nginx|grep -v grep|wc -l`
if [ $nginx -gt 2 ];then
echo "your nginx is running"
exit 0
else
/bin/systemctl start nginx.service
exit 1
fi10.2 Monitor mysql status
mysql.sh
#!/bin/sh
PortNum=`netstat -lnt|grep 3306|wc -l`
if [ $PortNum -eq 1 ]
then
echo "mysqld is running."
else
echo "mysqld is stoped."
fi10.3 mysql backup script #
mysql_backup.sh
#!/bin/bash
DATE=$(date +%F_%H-%M-%S)
HOST=127.0.0.1
DB=test
USER=root
PASS=abcd1#EFG
MAIL="xxxx@qq.com"
BACKUP_DIR=/data/db_backup
if [ ! -d "$BACKUP_DIR" ];then
mkdir -p $BACKUP_DIR
fi
SQL_FILE=${DB}_FULL_$DATE.sql
BAK_FILE=${DB}_FULL_$DATE.zip
cd $BACKUP_DIR
if mysqldump -h$HOST -u$USER -p$PASS -B $DB > $SQL_FILE; then
zip $BAK_FILE $SQL_FILE && rm -rf $SQL_FILE
if [ ! -s $BAK_FILE ]; then
echo "$DATE 备份失败" | mail -s "备份失败" $MAIL
fi
else
echo "$DATE 备份失败" | mail -s "备份失败" $MAIL
fi
find $BACKUP_DIR -name '*.zip' -ctime +14 -exec rm {} \;Login authorization
After authorization, you don’t need to enter your password the next time you log in.
ssh-copy-id 47.104.220.xxIf /root/.ssh/id_rsa is not generated, you can generate the key through the following command
ssh-keygen -t rsaTurn off firewall
close iptables #
The iptables command is a commonly used firewall software on Linux
| Function | Command |
|---|---|
| Stop firewall | systemctl stop firewalld.service |
| Permanently turn off the firewall | systemctl disable firewalld.service |
Confirm to disable selinux
Security-Enhanced Linux (Security-Enhanced Linux), referred to as SELinux, is a Linux kernel module and a security subsystem of Linux. The main function of SELinux is to minimize the resources accessible to the service process in the system (the principle of least privilege).
| Function | Command |
|---|---|
| Check status | getenforce |
| Check status | /usr/sbin/sestatus -v |
| Temporary shutdown | setenforce 0 |
| permanently closed | /etc/selinux/config SELINUX=enforcing改为SELINUX=disabled |
Install dependent modules
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim