Article directory
1. Linux common commands
hot key
- Use
tab
key completion Ctrl + c
Retype using- use
Ctrl + insert
copy - use
Shift + insert
paste
ls
grammar: ls [选项] [目录或文件]
Function: For a directory, this command is to list all directories and files under the directory; for a file, this command is to list the file name and related information
Common options:
Options | describe |
---|---|
-a |
List all files in a directory, including hidden files . starting with |
-l |
List file details ( ls -l can be abbreviated as ll ) |
-t |
Sort by time |
-R |
List files in all subdirectories (recursively) |
Example: List details of all files and directories under the root directory
In Linux, white is for normal files, blue is for directories, green is for executable files, and red is for compressed files
pwd
grammar: pwd
Function: Display the absolute path of the directory where the current user is located
Example: After entering the bin directory, let's check the path where the current directory is located
cd
grammar: cd 目录名
Function: switch the current directory to the specified directory, the specified directory can be a relative path or an absolute path
Absolute path:/
starting with is an absolute path, indicating the full path from the root directory to the directory
Relative path: A relative path starts with .
or ..
, which means that the search starts from the current directory or the upper-level directory to the lower-level directory, and the specified directory is not a complete path.
Example 1: Cut from the root directory to the bin directory
Example 2: Cut from the bin directory to the root directory
touch
grammar: touch 文件名
Function: Create a new file, the file name can be an absolute path or a relative path
Example: Create a test.txt file in the home directory
echo
grammar: echo > 文件名
Function: write simple data to the file
Example: write text string in test.txt file
cat
grammar: cat 文件名
Function: View the contents of the file
Example: View the contents of the test.txt file
mkdir
grammar: mkdir 目录名
Function: Create a new directory, the directory name can be an absolute path or a relative path
Example: Create three directories 111, 222, and 333 in the home directory
tree
grammar: tree 目录名
Function: Through the tree structure, all the directories and files under the directory can be displayed more intuitively
Note: The tree command does not come with Linux, you need to install the tree command through the yum install tree -y
command to use
Example: View all directories and files under the home directory
rm
grammar: rm [选项] 目录或文件
Function: delete files or directories
Note: When deleting a directory, because there are subdirectories or files under the directory, you need to bring the -r option to delete the directory
Common options:
Options | describe |
---|---|
-f |
Force delete without asking |
-r |
Delete a directory and all files or subdirectories under it (recursively) |
-i |
Ask for confirmation one by one before deleting (enter y to confirm deletion, enter other to cancel deletion) |
Example: delete the test.txt file in the home directory
mv
grammar: mv [选项] 源文件或目录 目标文件或目录
Function: You can move the source directory or file to a new directory, and you can modify the file or directory name while moving (you can use mv to rename the file or directory)
Common options:
Options | describe |
---|---|
-f | If the file already exists, overwrite it without asking |
-i | If the file already exists, it will ask whether to overwrite |
Example 1: Move aaa.txt in the 111 directory of the home directory to the 222 directory of the home directory
Example 1: Move aaa.txt in the 222 directory of the home directory to the 111 directory of the home directory, and rename it to a.txt
Example 3: Create the 444 directory in the home directory, move it to the 111 directory, and rename it to 555
cp
grammar: cp [选项] 源文件或目录 目标文件或目录
Function: Copy files or directories, and can copy and rename while copying.
Note: cp cannot rename a file; cp cannot copy directories directly, you must add the -r option
Common options:
Options | describe |
---|---|
-f |
Forcibly copy a file or directory, whether the Bren target file or directory exists |
-i |
If the file exists, it will ask whether to overwrite |
-r |
Process files in the specified directory together with subdirectories |
Example 1: Copy a.txt in the 111 directory of the home directory to the 222 directory, and rename it to aaa.txt
Example 2: Copy the 555 directory in the 111 directory in the home directory to the 333 directory and rename it to 666
find
grammar: find 目录 -name 文件名或目录名
Function: Find files or directories with key names in this directory
Example: Search for files or directories with the bin keyword in the root directory
man
grammar: man 命令
Function: View the relevant content of the command (q to exit, up, down, left and right keys to move the page)
Example: View the relevant content of the cd command
less
grammar: less 文件名
Function: View file content, can open large files in seconds, but instead of directly loading all file content into memory, it displays how much is loaded (q to exit, up, down, left, and right keys to move the display page)
Example: View the contents of the virc file in the etc directory
vim
grammar: vim 文件名
Features: vim is a text editor that can create or edit files
Three modes: After opening a file with vim, there will be three modes
- Normal mode: The mode displayed as soon as you enter is the normal mode, which cannot be edited. Each key on the keyboard represents some special shortcut keys in this mode, such as entering the i key to enter the insert mode, and the input: key to indicate enter bottom row mode
- Insert mode: This mode is used to edit the content of the file. After editing, use the esc key to return to the normal mode
- Bottom line mode: This mode is used to save and exit vim, w means save, q means exit, wq means save and exit, q! means forced exit
Example: Use vim to edit the aaa.txt file in the 111 directory under the home directory, and enter the aaa string
date
grammar: date [OPTION] [FORMAt]
Features:
-
In terms of display time, you can set the pre-display format, the format is set as: date + "several marks"
mark meaning %H
Hour %M
minute %S
Second %X
equivalent to %H:%M:%S
%d
Day %m
moon %Y
year %F
equivalent to %Y-%m-%d
-
Convert the current time to a timestamp:
date +%s
-
Convert timestamp to time:
date -d@时间戳
Example 1: Display the current time in 年-月-日 时:分:秒
the format of
Example 2: Timestamp showing current time
Example 3: Convert the timestamp of Example 2 to a time
ps
grammar: ps aux
Function: View the processes running on the current system
Example: Display the processes currently running on the system
grep
grammar: grep [查找的内容]
Function: used to filter the output results (often used in conjunction with | pipeline, | is used to take the output of the previous command as the input of the next command)
Example: We open a new terminal, open vim, we use ps in this terminal and add the grep command to see if there is vim in the current process
netstat
grammar: netstat -anp
Function: View the network status on the system
Example 1: View all network status on the current system
Example 2: Check whether the 8080 port of the server is occupied (no search results indicate that it is not occupied, and more results indicate that it is occupied)
2. Linux permissions
User introduction and operation
There are two kinds of users in Linux: super user and ordinary user
- Super user: is the administrator, has the highest level of authority, and can do anything in Linux without restriction. The command prompt is
#
- Ordinary user: a user with limited permissions on Linux, cannot do things without permission. The command prompt is
$
create user
- **grammar: **
useradd 用户名
- Function: Create a new user
- Example: Create a user named test1
Configure password
- grammar:
passwd 用户名
- Function: Set or modify user password
- Example: Set a password for the test1 user
switch user
- grammar:
su 用户名
- Function: switch user
- Example: Switch from root user to normal user test1
Three categories of users in Linux
Users who access a file are divided into three categories in Linux:
- owner of files and file directories
- User in the same group as the owner of the file and file directory
- other users
File Types and Access Rights
In the root directory, through the ll
command , we can see the following results
The details displayed in the following dev directory introduce the meaning of specific parameters
Modify file permissions
grammar: chmod [选项] 权限 文件名
Function: Set the access permission of the file, only the owner or root user of the file can modify the file permission
Note: When modifying directory permissions, add options R
to recursively modify the permissions of directory files
-
Method 1: +, -, = to change permissions
u: for owner, g: for group, o: for other groups, a: for everyone (sum of u, g, o)
chmod u=rwx,g=rx,o=x
filename/directory name- Directive to give other groups write permissions:
chmod o+w
filename/directoryname - The command to remove the execution permission of everyone: chmod ax file/directory name
-
Method 2: Change permissions through numbers
r=4, w=2, x=1, rwx=4+2+1 (can be combined, such as 3=wx)
Directive:
chmod 751
filename/directory is equivalent tochmod u=rwx,g=rx,o=x
filename /directoryname
3. Set up a Java deployment environment
yum
basic introduction:
yum (Yellow dog Updater Modified) is a commonly used package manager under Linux (like a mobile phone's application store or a dependent central repository), and is mainly used on distributions such as Centos, RedHat, and Fedora.
Common commands of yum: All commands of yum must ensure that the network of the server is connected
-
View package list command:
yum list | grep 软件包关键字
-
Install the package command (requires root privileges):
yum install 软件包名字
-
Uninstall package command (requires root privileges):
yum remove 软件包名字
git
View the git installation package command:
yum list | grep git -w
-w parameter means full character match
Install git command:
yum install git.x86_64
Basic usage of git:
# 克隆
git clone
# 新增
git add
# 提交
git commit
# 推送
git push
Install JDK
View the JDK installation package command:
yum list | grep jdk
Install JDK command:
yum install java-1.8.0-openjdk-devel.x86_64
OpenJDK can be easily installed through yum, which is an open source JDK, slightly different from the official JDK, but the difference is small.
Select the suffix devel (representing software development kit) and the JDK version that matches your system. After installation, you can check whether the installation is successful by javac
using . If the installation is successful, the result is as follows
Install Maven
View the Maven installation package command:
yum list | grep maven -w
Install Maven command:
yum install maven.noarch
Verify Maven command:
mvn -v
If the installation is successful, the following results can be obtained through verification
Install Tomcat
Since the default yum source is Tomcat 7, and the following describes how to install Tomcat 8, it will not use yum for installation
installation steps:
-
Download the Tomcat compressed package (Tomcat official website download address is: https://tomcat.apache.org/download-80.cgi )
wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.77/bin/apache-tomcat-8.5.77.zip
wget Tomcat具体的下载链接地址
You can download the compressed package through (try it yourself, the download speed is a bit slow, it is recommended to download the compressed package directly to the local, and then drag it directly to the server, you need to install the following command to drag and dropyum install lrzsz
-
Unzip Tomcat
unzip apache-tomcat-8.5.77.zip
If the unzip command is not found, you need
yum install unzip
to install it with -
Modify executable permissions
# 进入 Tomcat 的 bin 目录 cd apache-tomcat-8.5.77/bin # 将所有 .sh 后缀的文件加上可执行权限 chmod a+x *.sh
*.sh
Indicates all files with the suffix .sh in this directory -
Start Tomcat (the following commands are performed in the bin directory of Tomcat)
sh startup.sh
-
Verify Tomcat
# 方式一:查看 Tomcat 进程是否存在 ps aux | grep tomcat # 方式二:查看端口 8080 是否被绑定 nestat -anp | grep 8080 # 方法三:使用 curl 命令访问默认 demo curl 127.0.0.1:8080
Note: When you are using a cloud server, the first time you complete the above operation, you may not successfully open Tomcat in the browser because the security group or firewall of the cloud server does not open port 8080. So you need to go to the console of your cloud server to open port 8080.
Install MySQL
MySQL can also be installed using yum, but the configuration to be modified will be more complicated. For this reason, the installation of MariaDB is introduced here. This is a database similar to MySQL. After MySQL was acquired, the author of MySQL passed the same source code. The created database, the two are compatible.
Note: If you want to install the MariaDB database, then you must ensure that MySQL is not installed in your cloud server, otherwise the following installation steps may go wrong, this is a personal test!
installation steps:
-
Install mariaDB service
yum install -y mariadb-server
-
Install mariaDB command line client
yum install -y mariadb
-
Install the mariaDB C library
yum install -y mariadb-libs
-
Install mariaDB development package
yum install -y mariadb-devel
Change configuration:
-
Change the
/etc/my.cnf.d/client.cnf
file and add the following configuration under [client]default-character-set=utf8
-
Change the
/etc/my.cnf.d/mysql-clients.cnf
file and add the following configuration under [mysql]default-character-set=utf8
-
Change the
/etc/my.cnf.d/server.cnf
file and add the following configuration under [mysqld]collation-server = utf8_general_ci init-connect='SET NAMES utf8' character-set-server = utf8 sql-mode = TRADITIONAL
Start the database:
-
start the service
systemctl start mariadb
-
Set the service to start automatically
systemctl enable mariadb
-
View service status
systemctl status mariadb
Test the connection:
-
Attempt to connect using the command line client
mysql -uroot
The results are as follows, indicating that the installed database can run normally
-
The current mariadb user has no password, and the default password is empty characters. To change the database password, you can do the following
mysql_secure_installation
After the change is completed, the next time you start the database, you can connect to the database with a password through the
mysql -uroot -p
command