Preface
Generally speaking, when we write a website, we need to deploy it to the server. At the server level, the most mainstream operating system is Linux. This chapter focuses on summarizingLinux environment setup and some of the most commonly usedcommands.
1. Linux environment setup
1. Purchase a cloud server
For Linux, there are many ways to build an environment, but compared with various methods, using cloud servers is the best choice. We can first purchase a cloud server (Tencent Cloud, Alibaba Cloud, Huawei Cloud...) and then deploy the Linux environment to the cloud server. And when we purchase a cloud server, it will come with an external network IP, which means that the website we publish at this time can be accessed by others. This is no different from publishing a formal website, and it also allows the programs we write to be used by others.
2. Linux environment setup
The following takes Tencent Cloud as an example to show the Linux environment setup: Here you may find that the system image selected is not Linux but CentOS. In fact, Linux is more accurately an operating system kernel, and a complete system is composed of an operating system kernel and supporting applications. Therefore, many organizations have developed different operating systems, also called distributions, based on the Linux kernel and different applications. CentOS here is a distribution built based on the Linux kernel.
Note: Remember to reset a complex password after purchasing a cloud server to prevent illegal intrusion.
3. Use terminal software to connect to Linux
Terminal software is a type of tool software that can establish a network connection with a remote host to perform some operations on the remote host. For here, the remote cloud server we purchased at this time is the object we want to connect to.
There are also many terminal softwares, here we take XShell as an example. In XShell, our usual approach is to save the connection in the form of a session, and just click on the session the next time you connect.
After the connection is successful, we will see the following information in the terminal interface:
4. Expansion: XShell common shortcut keys
- copy:
ctrl + insert
- Paste:
shift + insert
- Autocomplete: Use the
tab
key. All Linux commands we type can be completed using the tab key to speed up efficiency. - Cancel command: If you type the wrong command or directory, you can
ctrl + c
cancel the current command. - Lock the terminal:
ctrl + s
- Unlock the terminal:
ctrl + q
- Clear screen:
ctrl + l
- quit:
ctrl + d
2. Linnx directory structure
In a Linux system, the files and directories on the disk are organized into a directory tree, and each node is a directory or file. And a large part of the common commands in Linux are related to file directory operations. So before introducing specific Linux commands, let’s briefly familiarize ourselves with the directory structure in Linux:
The directory structure in Linux is a hierarchical file system structure, starting from the root directory /
and divided downwards into multiple subdirectories. The following are common Linux directory structures and their uses:
/
(Root directory): The starting point of the entire file system./bin
: Stores executable binary files (commands and system tools)./boot
: Stores startup-related files, such as kernel and boot loader./dev
: Mount point for device files, including hardware devices and external devices./etc
: Store system configuration files, such as network configuration, user configuration, etc./home
: The user's home directory, each user has an independent subdirectory./lib
: Stores library files shared by the system. Many programs depend on these library files./media
: The mount point for removable media devices, such as optical disks, USB and other external storage devices./mnt
: Used to temporarily mount other file systems or external devices./opt
: The installation directory of third-party software, usually placed under/opt
./proc
: A virtual file system that provides information about the current kernel state./root
: The home directory of the superuser (root)./sbin
: Stores system commands that only administrators can execute./srv
: Store service-related data and files./tmp
: Temporary directory, used to store temporary files./usr
: Stores application and file resources shared by users and systems./var
: Store frequently changing files, such as log files, database files, etc.
In Linux, you can also use absolute path and relative path To access files and directories:
1, Absolute path: The absolute path is the complete path starting from the root directory
/
. It specifies the exact location of a file or directory in the file system. Absolute paths begin with a slash/
, for example/home/user/file.txt
. An absolute path points to a specific file or directory regardless of the current working directory.
2. Relative path: A relative path is a path relative to the current working directory. Relative paths do not start with a slash/
but are paths starting from the current directory. Commonly used relative path symbols include.
(representing the current directory, which can be omitted) and..
(representing the upper-level directory). For example, assuming that the current working directory is/home/user/
, then the relative path./file.txt
refers to thefile.txt
file in the current directory; file in the parent directory.../docs/document.txt
refers to thedocs/document.txt
3. Common Linux commands (strictly case-sensitive)
1、ls
commandment
ls directory: Simply list the files and subdirectories in the current directory (excluding hidden files).
ls -l directory: Displays file and directory details in long format, including permissions, owner, size, modification date, etc. Usually abbreviated as ll
.
ls -R directory: Recursively display all files and subdirectories in the current directory, including files and subdirectories in subdirectories.
2、cd
commandment
cd directory name: Change the current working directory to the specified directory.
3、pwd
commandment
pwd: Displays the directory where the user is currently located
4、mkdir
commandment
Usage example:
Create a单级
directory:
mkdir mydir
Building多级
Memo:
mkdir -p mydir/subdir/subsubdir
5、touch
commandment
touch file/directory: The command parameters can change the date and time of 文件或目录
, including access time and change time. Or create a new one that does not exist文件
.
Usage example:
Create a file that does not exist
touch file.txt
6、cat
commandment
cat file: View the contents of the target file
7、echo
commandment
echo: Used to output the contents of text or variables in the terminal. It can print the specified text directly to the standard output (terminal).
Usage example:
Output text:
echo "Hello, World!"
Output the value of the variable:
name="Alice"
echo $name
Redirect output to a file:
echo "HelloLinux" > file.txt
This line of command will write the text "HelloLinux" to a file named file.txt
. If the file already exists, the original content will be overwritten. If the file does not exist, a new file file.txt will be automatically created and the output will be redirected to this file. If you want to append content to the end of the file, you can use the >>
operator, for example echo "More content" >> file.txt
.
8、vim
commandment
vim is a powerful text editor. It is also the text editor that comes with Linux, equivalent to Notepad on Windows.
Basic use of vim
1. Open the file:
vim [文件路径]
2. Edit the file: The default is normal mode. The keyboard keys in normal mode represent shortcut keys for some special functions. Usei
key can enter the insert mode and you can edit normally like Notepad.
3. Save and exit: Files cannot be saved in insert mode. You need to pressEsc
first to return to normal mode, and then use:wq
Save and exit simultaneously.
PS
: When using vim to edit a file, if the file does not exist, Vim will create a new empty file and open it.
9、rm
commandment
rm file/directory: Delete a file or directory.
Deletion is a very dangerous operation, so be careful when using it! ! !
Usage example:
Deleting files requires confirmation:
rm file.txt
Forcefully delete files without confirmation:
rm -f file.txt
Recursively delete a directory and all its contents, requiring confirmation:
rm -r mydir
Recursively delete a directory and all its contents, force deletion, no confirmation required:
rm -rf mydir2
Forcefully delete all files in the current directory
rm -rf *
10、cp
commandment
cp source file or directory target file or directory: Copies the file or directory to the specified location.
PS
: When using the cp command, please note that if the copied target file already exists, it will be overwritten.
Usage example:
Copy the file to the target location (and rename it):
cp file.txt file2.txt
Copy the directory to the target location:
cp -r mydir mydir2
11、mv
commandment
mv source file or directory target file or directory: used to move files and directories, and can also be used to change the name of a file or directory.
PS1
: Compared with cp
, the bottom layer efficiency of mv
is higher. The mv operation is equivalent to modifying the "path" attribute.
PS2
: When using mv, you also need to pay attention: if a file with the same name exists in the moved location, overwriting will occur.
PS3
: Since there is no concept of recycle bin in Linux, and the rm operation is very dangerous, when deleting a file or directory, you can use mv to move it to a separate directory. This separate directory can be imagined as a Recycle bin.
Usage example
File rename
mv file.txt newfile.txt
Move files to specified directory
mv newfile.txt mydir3
Move directory to specified directory
mv mydir2 mydir3
12、grep
commandments
grep function: used to search whether a file contains a specific string.
Usage example:
Recursively search all files in the current directory to see if they contain "hello" and display the line number:
grep -Rn "hello" *
13、ps
commandments
ps function: Used to view information about currently running processes.
Usage example (often used with grep):
Show all processes on the system
ps aux
Filter the processes specified 进程名
ps aux | grep "进程名"
Filter the processes specified 进程 id
ps aux | grep "进程id"
14、netstat
commandments
nestat function: Used to view the network status on the system.
Usage example (often used with grep):
Shows the status of all network connections on the system
netstat -anp
Display the network connection status of the specified 进程名
netstat -anp | grep "进程名"
Display the network connection status of the specified 端口号
netstat -anp | grep "端口号"
The two most commonly used scenarios for ps and netstat:
1. Use ps to view the pid of a process
ps aux | grep "进程名"
2. Use nestat to check the port number bound to a process
netstat -anp | grep "进程名"
15、man
commandments
Linux commands have many parameters. The Linux commands introduced above only list some commonly used parameter options. With so many parameters, it is impossible for us to remember them all. We can use man to view the online manual of Linux commands for help.
Usage example:
View the online manual for netstat
man netstat