table of Contents
1. Operation level description
2. Operation level description after CentOS7
2. Retrieve the root password (classic interview questions)
1. Start the system and quickly press "e" to enter the editing page
2. Find the linux16 line, enter init=/bin/sh, and then press ctrl+x to enter the single-user mode
4. Modify the root password and change it to your favorite
3. Commonly used commands (emphasis)
2.help: Get help information for shell built-in commands
3.psw: Display the absolute path of the current working directory
5.cd: switch to the specified directory
7.rmdir: delete empty directories
9.cp: copy files to the specified directory
10.rm: Remove files or directories
11.mv: Move files and directories, or rename
12.cat: View the contents of the file
13.more: paging display text content
14.less: View file content in split screen
15.echo: output content to the console
16.head: Display the beginning of the file, the first 10 lines of the file are displayed by default
17.tail: display the end of the file, the last 10 lines are displayed by default
22.locate instruction: quickly locate the file path
23.which command: Check which directory a certain command is in
24. grep instructions and pipe symbols
25.gzip/gunzip command: compression/decompression
26.zip/unzip instructions: compression/decompression, often used for project packaging and release
27.tar command: decompression is tar
One, specify the run level
1. Operation level description
0: Shut down
1: Single user (recover lost password)
2: There is no network service in the multi-user state
3: Multi-user status has network service
4: The system is not used and reserved for users
5: Graphical interface
6: System restart
The commonly used run levels are 3 and 5 , you can also specify the run level yourself:
#指定运行级别
init [0123456]
Let's demonstrate:
For example, we open our own graphical interface. Because it is a graphical interface, the run level is 5. We right-click to open the terminal, and enter init 3
We enter the carriage return and it will become the command line format:
2. Operation level description after CentOS7
#查看当前运行级别
systemctl get-default
#运行级别3
multi-user.target
#运行级别5
graphical.target
#设置运行级别,其中TARGET就是对应上面的级别3和5
systemctl set-default TARGET.target
Let's look at an example:
We first check that the current run level is 5, and then we set it to 3
2. Retrieve the root password (classic interview questions)
What should I do if you forget the root password and cannot log in to the root account?
1. Start the system and quickly press "e" to enter the editing page
On this page, we press e, a little faster, or we will skip to the next step by ourselves.
Once we press e, we will enter this edit page
2. Find the linux16 line, enter init=/bin/sh, and then press ctrl+x to enter the single-user mode
After entering the edit page, find the line headed by linux16. If you look at the above picture carefully, you may find that it does not have the line linux16, you need to pull down, and there are still things below.
We add after this line
init=/bin/sh
After the input is complete, press ctrl+x to enter the single-user mode, and the page looks like this:
3. Single user mode for input
Enter the following in single-user mode: (recommended to copy here, please pay attention to spaces)
mount -o remount,rw /
Enter after pressing enter:
passwd
There will be a passwd ... line at this time.
Continue to enter:
touch /.autorelabel
Then enter:
exec /sbin/init
At this time you will get this page:
Note at this time: don't move, wait. This may take a while, don’t move, everyone! ! !
Then, the page will automatically restart:
We can log in with root and the new password
4. Modify the root password and change it to your favorite
passwd root
3. Commonly used commands (emphasis)
1.man: get help information
Basic syntax:
man [命令或配置文件]
For example, if we enter man ls, the following will appear:
To see the next page, press the space bar.
To return to the command line state, press "q"
Note: Hidden files under linux start with "."
2.help: Get help information for shell built-in commands
3.psw: Display the absolute path of the current working directory
4.ls: View content
ls [选项] [目录或者文件]
-a: Display the directory of all files in the current directory, including hidden
-l: display information in a list
5.cd: switch to the specified directory
Back to the previous directory:
cd ..
Back to the home directory: cd
6.mkdir: create a directory
mkdir [选项] 要创建的目录
-p: Create a multi-set directory
7.rmdir: delete empty directories
If there is content in the directory, use: (recursively force deletion)
rm -rf 要删除的目录
8.touch: Create an empty file
touch 文件名称
9.cp: copy files to the specified directory
cp [选项] source dest
-r: copy the entire folder recursively
If you have the original file, and you copy it again, there will be a problem of overwriting. Of course, you can enter y to overwrite. When there are many files, this is troublesome. We can force overwrite without prompting:
\cp [选项] source dest
10.rm: Remove files or directories
rm [选项] 要删除的文件和目录
-r: delete the entire folder recursively
-f: no prompt for forced deletion
11.mv: Move files and directories, or rename
mv oldNameFile newNameFile
If the two file directories are in the same directory, it means renaming, otherwise it means moving.
12.cat: View the contents of the file
cat [选项] 要查看的文件
-n: display line number
Note: cat can only browse files, not modify files. For the convenience of browsing, it will usually bring "|more": (press enter to view the next line, press space to view the next page)
cat [选项] 要查看的文件 | more
13.more: paging display text content
more 要查看的文件
Because it is paging, there are some operation instructions after more:
Space bar: view the next page
Enter: view the next line
q: leave more
ctrl+f: scroll down one screen
ctrl+b: return to the previous screen
=: output the current line number
:f: output file name and line number of the current line
14.less: View file content in split screen
less 要查看的文件
The function is similar to more, but more powerful than more, and supports various real terminals. It is recommended to use less to view larger files
Space bar: view the next page
pagedown: page down
pageup: page up
/String: search downwards, n: search downwards, N: search upwards
?String: search upwards, n: search upwards, N: search downwards
q: leave
15.echo: output content to the console
echo [选项] [输出内容]
16.head: Display the beginning of the file, the first 10 lines of the file are displayed by default
head 文件名
#查看指定行数文件
head -n 数字 文件名
17.tail: display the end of the file, the last 10 lines are displayed by default
tail 文件名
#查看指定行数文件
tail -n 数字 文件名
#实时追踪文档的更新状况
tail -f 文件名
(1) Exit the monitoring state: ctrl+c
(2) In the virtual machine, we write something to the file through the echo command, and then the monitored tail will be updated:
One arrow indicates coverage, and two arrows indicate append .
(3) Display calendar information: cal
18.ln: soft link, also called symbolic link. Similar to the shortcuts in windows, it mainly stores the path to link other files
#给原文件创建一个软链接名
ln -s [原文件] [软链接名]
Example:
Make a shortcut for root under home and delete it
19.History command: view the history commands that have been executed, and you can also execute history commands
#查看所有历史命令
history
#查看最后10条历史命令
history 10
#执行历史编号为5的指令
!5
We first find out the historical commands in history, and then execute the historical commands through !5
20.date command: date
(1) Display the current date
#显示当前时间
date
#显示年
date +%Y
#显示月
date +%m
#显示日
date +%d
#显示年月日时分秒
date "+%Y-%m-%d %H:%M:%S"
(2) Set the date
date -s 字符串时间
(3) Display calendar
#显示当前日历
cal
#显示整年的日历
cal 2021
21.find instruction: recursively traverse all subdirectories from the specified directory downwards, and display the files or directories that meet the conditions on the terminal
find [搜索范围] [选项]
-name: search by file name
-user: search by user
-size: search according to file size (+n: greater than -n: less than n: equal to) (file unit k, M, G)
When we view the file through ls, in order to see the file size conveniently, we can add the h parameter .
22.locate instruction: quickly locate the file path
#如果是第一次使用,需要使用updatedb创建locate数据库
locate 搜索文件
Note: Because locate is based on the database for query, before the first run, you must use the updatedb command to create the locate database .
23.which instruction: view which directory a certain instruction is in
Check which directory ls is in:
24. grep instructions and pipe symbols
Pipe symbol: "|", which means that the processing result output of the previous command is passed to the subsequent command processing .
grep [选项] 查找内容 源文件
-n: display matching line and line number
-i: Ignore letter case
25.gzip/gunzip command: compression/decompression
#将文件压缩成*.gz文件
gzip 文件
#解压缩
gunzip 文件.gz
26.zip/unzip instructions: compression/decompression, often used for project packaging and release
#压缩
zip [选项] xx.zip 将要压缩的内容
#解压
unzip [选项] xx.zip
Common options for zip:
-r: Recursive compression, that is, compressed directory
Common options for unzip:
-d <directory>: the directory where the decompressed files are stored
27.tar command: decompression is tar
The tar instruction is a packaging instruction, and the final packaged file is the xx.tar.gz file
tar [选项] xx.tar.gz 打包的内容
-c: Generate .tar package file
-v: display detailed information
-f: Specify the compressed file name
-z: Pack and compress at the same time
-x: Unpack the .tar file
-C: Unzip to the specified directory
Example: