Skip to content

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 ​

ContentsPurpose
/root directory
/bootStart directory, start related files
/devdevice file
/etcconfiguration file
/homeThe home directory of ordinary users, which can be operated
/libSystem library storage directory
/mntMobile device mounting directory
/mediaCD mounting directory
/miscTape drive mounting directory
/rootThe home directory of the super user, which can be operated
/tmpTemporary directory, operable
/procRunning kernel information mapping, mainly outputs process information, memory resource information, disk partition information, etc.
/sysDriver information for hardware devices
/varvariable
/binCommon basic commands, such as ls, chmod, etc., can also be used by ordinary users
/sbinBasic system commands, such as shutdown and reboot, are used to start the system and repair the system. Only administrators can run it
/usr/binis the running script for some software you install later
/usr/sbinPlace some necessary programs for system management installed by users

1.2 Basic command format ​

1.2.1 Command prompt ​
shell
[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

shell
drwxr-xr-x .  1 root  root   800 Sep 16 00:19 logs
drwxr-xr-x.1rootroot800Sep 16 00:19logs
File Types and PermissionsACL PermissionsHard Link Reference CountOwnerGroupFile SizeLast ModifiedFile 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.log
1.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.log
1.4.5.4 -user ​

Search by owner

find /root -user root
find /root -nouser
1.4.5.5 Search by time ​
find /nginx/access.log -mtime +5
ParametersMeaning
atimefile access time
ctimeChange file attributes
mtimeModify file content
ParametersMeaning
-5Files modified within 5 days
5Files currently modified 5 days ago
+5File modified 5 days ago
1.4.5.6 Search by size ​
  • k lowercase, M uppercase
find . -size 100k
ParametersMeaning
-8kLess than 8K
8kequal to 8K
+8kGreater than 8K
+8MLess than 8M
1.4.5.7 Search by i node ​
find . -inum 123456
1.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 ls
1.5.4 Keyword search ​
- man -k passwd
1.5.5 Shell internal help ​
  • If whereis is found, it is external; if not found, it is internal.
help cd

1.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.zip
1.6.2 gzip ​
CommandExampleMeaning
gzip source filegzip a.txtCompressed into a .gz format compressed file, the source file will disappear
gzip c source file > compressed filegzip -c yum.txt > yum.txt.gzCompressed into a compressed file in .gz format, the source file will not disappear
gzip -r directorygzip -r xxCompress all subfiles in the directory, but do not compress the directory
gzip -d compressed file namegzip -d yum.txt.gzDecompress the file without retaining the compressed package
gunzip compressed filegunzip yum.txt.gzdecompress 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.tar
1.6.5 tar.gz compression format ​
  • It can be packaged into .tar format first, and then compressed into .gz format. -z compresses into .tar.gz format. -x decompresses into .tar.gz format.

| 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 -c
1.7.2 init ​

Shut down

init 0

Restart

init 6

system 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

logout

1.8 Mount ​

1.8.1 Mount command ​
  • mount View the devices mounted on the system
mount 
mount -a
1.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/cdrom
1.8.4 Uninstall the optical drive ​
umount /dev/sr0
umount /mnt/cdrom
1.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"
symbolcolor
#30mBlack
#31Red
#32Green
#33Yellow
##34Blue
##35Magenta
##36Green
##37White

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.sh

Execute script via Bash call

bash hello.sh

2.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 ​

CommandMeaning
ctrl+cForcefully terminate the current command
ctrl+lclear screen
ctrl+aMove the cursor to the beginning of the command line
ctrl+eMove the cursor to the end of the command line
ctrl+uDelete from the cursor position to the beginning of the line
ctrl+zPut the command into the background
ctrl+rSearch 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 ​
devicedevice file namefile descriptortype
keyboard/dev/stdin0standard input
monitor/dev/stdout1standard output
Monitor/dev/stderr2Standard Error Output
TypeSymbolFunction
Standard output redirectionCommand > FileOutput the correct output of the command to the specified file or device in an overwriting manner
Standard output redirectionCommand >> FileOutput the correct output of the command to the specified file or device in append mode
Error output redirectionCommand>FileOutput the error output of the command to the specified file or device in an overwriting manner
Error output redirectionCommand>>FileOutput 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 timeCommand>File 2>&1Save 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 timeCommand>File 2>>&1Save both the correct output and the error output to the same file by appending
Save the correct output and error output at the same timeCommand &> FileSave the correct output and error output to the same file in an overwriting manner
Save the correct output and error output at the same timeCommand &>> FileSave the correct output and error output to the same file by appending
Save the correct output and error output at the same timeCommand>>File 1 2>File 2Append 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 executorsFormatFunction
;Command 1;Command 2Multiple commands are executed without any logical connection between the commands
&&Command 1&& Command 2Logical 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 -l
2.10.3 Wildcards ​

Match filenames and directory names

WildcardFunction
?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 ​
SymbolFunction
''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 nameMeaning
Command Mode command modemode waiting for input
Insert Mode input modeEdit 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/group stores all user group information in the current system
  • group: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 student

Modify group name

groupmod -n stu student

Modify group number

groupmod -g 111 stu

Add group and specify number

groupadd -g 222 teacher

Delete group

groupdel 222

Add group

groupadd teacher

Assign a group to the user

useradd -g teacher zhangsan

Assign a group to the user

useradd -g teacher lisi

Specify working directory for user

useradd -d /home/zhangsan zhangsan

Specify comments

usermod -c iamateacher zhangsan

Modify username

usermod -l zhangsan zhangsan2

Specify folder

usermod -d /home/zhangsan2 zhangsan2

Modify the group to which the user belongs

usermod -g stu zhangsan2

Delete user

userdel zhangsan2

Delete the folder it belongs to

userdel -r lisi

5. user command ​

Show logged in user name

whoami

Display the specified user information, including user number, user name, main group number and name, and subsidiary group list

id zhangsan

Display all groups to which user zhangsan belongs

groups zhangsan

Show user details

finger zhangsan

6. 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 ​
ParametersMeaning
-aDisplay all processes in a terminal
-uDisplay the owned user and memory usage of the process
-xShow processes without a controlling terminal
-lLong format display, display more detailed information
7.3.2 Result field meaning ​
DataMeaning
USERWhich user created this process
PIDID number of the process
%CPUThe percentage of CPU resources occupied by this process. The higher the occupancy, the more system resources are consumed
%MEMThe percentage of physical memory occupied by this process. The higher the occupancy, the more system resources are consumed
VSZThe percentage of virtual memory occupied by this process, in KB
RSSThe actual physical memory size occupied by this process, in KB
TTYIn 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
STATProcess status
STARTThe start time of the process
TIMEThis process takes up computing time of the CPU. The higher the value, the more system resources are consumed
COMMANDThe command name that spawned this process
7.3.3 Process status (STAT) ​
ParameterMeaning
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.txt
7.5.1 Options ​
OptionsMeaning
-bUse batch mode output, generally used in conjunction with -n
-ntimes, specify the number of times the top command is executed.Generally used with the -b option
-dNumber of seconds, specifies how many seconds the top command is updated every few seconds.Default is 3 seconds
7.5.2 Interactive mode commands ​
OptionsMeaning
? or hDisplay help in interactive mode
PSort by CPU usage, the default is this option
MSort by memory usage
NSort by PID
qexit top
7.5.3 Status bar ​
7.5.3.1 The first line contains task queue information ​
ContentDescription
12:12:12Current time of the system
up 1 day 5:33System running time, this machine has been running for 1 day, 5 hours and 33 minutes
2 usersTwo clients are currently logged in
load average 0 0 0The 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 ​
ContentDescription
Tasks: 100 totalTotal number of processes in the system
1 runningNumber of running processes
94 sleepingThe process of sleeping
0 stoppedProcess being stopped
0 zombieZombie process.If it is not 0, check it
7.5.3.3 The third line is CPU information ​
ContentDescription
Cpu(s): 0.1%usPercentage of CPU occupied by user mode
0.1%syPercentage of CPU occupied by system mode
0.0%niPercentage of CPU occupied by user processes that have changed their priority
99.7%idCPU percentage of idle CPU
0.1%waPercentage of CPU occupied by processes waiting for input/output
0.1%hiPercentage of CPU occupied by hard interrupt request service
0.1%siPercentage of CPU occupied by soft interrupt request service
0.0%stst (Steal time) virtual time percentage, that is, when there is
7.5.3.4 The fourth line is physical memory information ​
ContentDescription
Mem: 1030720k totalPhysical memory problem, unit is KB
551860k usedAmount of physical memory used
478860k freeThe amount of free physical memory, the virtual machine is allocated 1024M memory, 538M is used, and 467M is free
43180k buffersAs 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 ​
ContentDescription
Swap: 2047992k totalTotal swap partition (virtual memory) size
536k usedThe size of the swap partition that has been used
2047456k freeFree swap partition size
368164k cachedRead 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

sh
#!/bin/bash
i=0
while [ $i -le 1000 ]
 do
   echo $(date)
   sleep 1s
 done
sh
sh sleep.sh &
ps -ef | grep sleep.sh
sh
kill  进程号

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  0
8.1.1 (procs) process information field ​
ClassificationParametersMeaning
procsrThe number of processes waiting to run. The larger the number, the busier the system
procsbThe number of processes that cannot be awakened. The larger the number, the busier the system
8.1.2 memory (memory information field) ​
ClassificationParametersMeaning
memoryswpdThe size of the Swap space used, in KB
memoryfreeFree memory capacity, unit KB
memorybuffbuffer memory capacity, unit KB
memorycacheCache 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.
ClassificationParametersMeaning
swapsi(in)The amount of data swapped from disk to memory, in KB
swapso(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.
ClassificationParametersMeaning
iobi(in)Problem reading data from block device, unit is block
iobo(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.
ClassificationParametersMeaning
systemin(interrupt)Number of interrupted processes per second
systemcs(switch)Number of event switches per second
8.1.6 CPU (CPU information field) ​
ClassificationParametersMeaning
CPUus(user)Percentage of CPU computing time consumed by non-kernel processes
CPUsy(system)The percentage of CPU computing time consumed by the kernel process
CPUid(idea)Percentage of idle CPU
CPUwa(wait)Percentage of CPU consumed waiting for I/O
CPUst(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
sh
# free -m
             total       used       free     shared    buffers     cached
Mem:          1006        687        319          0         98        449
-/+ buffers/cache:        139        866
Swap:         1999          0       1999
8.3.1 First line ​
ParametersMeaning
totalTotal memory
usedThe amount of memory that has been used
freeNumber of free memory
sharedAmount of memory shared by multiple processes
buffersNumber of buffer memory
cachedNumber of cached memories
8.3.2 Second line ​
ParametersAlgorithmMeaning
- buffers/cacheused-buffers-cached in the first lineThe amount of memory that has been used minus cache and buffering
+ buffers/cachefree+buffers+cached in the first lineThe amount of free memory plus cache and buffer memory
8.3.3 The third line ​
ParametersMeaning
totalThe total number of swaps, the default unit is K
usedThe number of swaps that have been used, the default unit is K
freeThe number of free swaps, the default unit is K

8.6 View kernel related information ​

  • uname
sh
# 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.i686

8.7 Check the operating system bit number ​

shell
file /bin/ls

8.8 View release version ​

shell
lsb_release -a

8.9 View release version ​

shell
yum install redhat-lsb -y
lsb_release -v

9. 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
sh
systemctl restart crond.service
9.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 ​
sh
# .---------------- 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 executed
9.2.3.2 Location ​
ProjectMeaningScope
1st starMinutes in 1 hour0~59
2nd starHour of the day0~23
3rd starDay of the month1~31
4th starWhich month of the year1~12
5th starDay of the week0~6
9.2.3.3 Special symbols ​
SymbolMeaningExample
*represents any timeFor example, the first star represents execution every minute of an hour
,Represents discontinuous timeFor example, "1,2,3" means executing commands at 1, 2, and 3 minutes per hour
-represents a continuous time rangeFor example, "1-5 * **", represents the execution of the command from the 1st to the 5th minute of every hour
*/nRepresents how often to execute the commandFor example, "/10" means to execute the command every 10 minutes
0 0 1,10 * 1Executed on the 1st and 10th of each month, at 0:00 every week
9.2.3.4 Case ​
SymbolMeaning
10 22 *Executed at 22:10 every day
0 15 1Executed every week at 15:00
0 5 5,10Executed at 5 a.m. on the 5th and 10th of each month
10 5 1-5Execute the command at 5:10 am every Monday to Friday
/10 10Executed 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
sh
5 5 * * * echo `date` >> /root/date.log

10. Actual combat tasks ​

10.1 Monitor nginx ​

nginx.sh

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
fi

10.2 Monitor mysql status ​

mysql.sh

sh
#!/bin/sh
PortNum=`netstat -lnt|grep 3306|wc -l`
if [ $PortNum -eq 1 ]
then
 echo "mysqld is running."
else
 echo "mysqld is stoped."
fi

10.3 mysql backup script # ​

mysql_backup.sh

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.xx

If /root/.ssh/id_rsa is not generated, you can generate the key through the following command

ssh-keygen -t rsa

Turn off firewall ​

close iptables # ​

The iptables command is a commonly used firewall software on Linux

FunctionCommand
Stop firewallsystemctl stop firewalld.service
Permanently turn off the firewallsystemctl 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).

FunctionCommand
Check statusgetenforce
Check status/usr/sbin/sestatus -v
Temporary shutdownsetenforce 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