Here to record my learning process, equivalent to their own notes, at the same time to share, if there are students just need and this article to help you, I will be very happy.

At the end of the article, I recommend a few free videos. There are many free learning videos on WEBSITE B and MOOCs, and some of them are of good quality.

1. File management in CentOS

1.1 Subdirectories under the root directory of the CentOS

When CD/goes to the root directory, ls can see a large number of subdirectories, as shown below:

These files are colored:

  • Blue is the folder;
  • Gray indicates ordinary files;
  • Green indicates executable files;
  • Red represents compressed files;
  • Sky blue for linked files (shortcuts);

The functions of common directories are as follows:

  • Bin: stores commands that can be executed by ordinary users.
  • Dev: Device directory, where all hardware devices and their peripherals are placed;
  • Boot: boot directory, including Linux kernel files and files required for startup.
  • Home:This is where your personal data is stored, each user’s Settings file, the user’s desktop folder, and the user’s data are stored. Each user has its own user directory, located as:/ home/username. Except, of course, the root user;
  • Usr: directory where applications are placed;
  • Lib: dynamic link library commonly used on the boot, the bin and sbin instructions will also call the corresponding lib library;
  • TMP: directory for storing temporary files.
  • Etc: directory for various configuration files, where most configuration properties are stored;

For more detailed directory functions, see < introduction to directories under the CentOS root directory – Zhihu >.

1.2 Directory commands

Some common commands are shown below:

role The command
Switch directory cd
Displays the full path of the current directory pwd
View information in directories (including hidden files) ls(ls -a)
Lists the files and details in the directory ls-lll)
Create a directory mkdir
Create a file touch
Copy files (folders) cp(cp -r)
Move/rename folders and directories mv
Delete a file (directory) rm(rm -rf)
Deleting an empty folder rmdir
Find files find
Get help man / info

There are also a few common shortcuts:

role shortcuts
Clear to the beginning of the line Ctrl + U
Clear to the end of the line Ctrl + K
Clear the screen Ctrl + L
The command that terminates execution Ctrl + C

It’s worth noting that these commands can be used on other systems as well.

1.3 tree Command to view a directory tree

Sudo yum -y install tree = sudo yum -y install tree = sudo yum install tree = sudo yum install tree = sudo yum install tree = sudo yum install tree = sudo yum install tree = sudo yum install tree = sudo yum install tree = sudo yum install tree = sudo yum install tree

2. How to use vim editor

Vim editor is a frequently used editor in CentOS system. Mastering the basic method of using it will be of great help to the future work.

Vim < filename > allows you to edit a document, and if the document name does not exist, a new document is created for editing.

Vim is divided into three modes: Command mode, Insert mode and Last line mode.

2.1 Command Mode

When you start Vim, you enter command mode, where keystrokes are considered commands, not input characters. Some common commands:

  • I switch to input mode to enter characters;
  • : Switches to the bottom-line command mode to enter the command on the bottom line.
  • A Switch to the input text mode;

The command mode contains only the most basic commands and relies on the bottom-line command mode to enter more commands.

2.2 Input Mode

Press I in command mode to enter input mode. In input mode, the following keys can be used:

function The command
Page up PageDown / Ctrl + F
Down the page PageUp / Ctrl + B
Jump to the first line of the file 1G / gg
Jump to the end line G
Jump to line # #G
The line number display :set nu
Line number display is cancelled :set nonu
insert d / Del
Delete current row dd
copy yy
Pastes the contents of the buffer after the cursor position p

There are other commands, such as delete from the cursor starting # line # dd, copied from the cursor starting # line # yy, etc, we can see the document < Linux vi/vim | novice tutorial >.

2.3 Bottom Line Command Mode

Press (Colon) in command mode to enter the bottom-line command mode.

In the bottom-line command mode, you can enter single or multi-character commands. There are many commands available.

In the bottom-line command mode, the basic commands are (colons have been omitted) :

  • :qExit the program;
  • :q!Discard changes to file contents and exit;
  • :wSave files;
  • :w /root/xxSave as.
  • :wqSave the file and exit.

2.4 Viewing file Contents

Some common commands to view file contents:

function The command
Browse the entire file more / less
View file content (display line number) cat (cat -n)
Finding strings in text files (display line numbers) grepGrep < Keyword > < File to Be searched > -n)

2.5 pipe,

Pipe, Will a command execution results as the input of another command to execute, format cmd1 | cmd2… | cmdn

For example, the/etc directory in the file name to pass at the beginning of the file list ls/etc | grep pass *

Again, for example, to view the contents of the/etc directory, and use less in the form of browsing the ls/etc | less

2.6 the redirection

function The command
Output redirection, the original file is replaced >
Output redirection, appended to the original contents if the original file exists >>
Input redirection, which means that the input of the command is not done by the keyboard, but by other means <
Error redirection 2 >
Output redirection is implemented simultaneously with error redirection & >

For example, output the result of the ls/command to ls / > 2.txt in 2.txt

3. Process management

2.1 Process Management

Process: a program or command that is being executed. Each process is a running entity with its own address space and occupies a certain amount of system resources.

The most important part of process management is the ps command:

  • ps aux, view all processes in the system, using the BSD operating system format;
  • ps -le, view all processes in the system, using the standard Linux command format;

The output of the ps command is as follows:

Output format meaning:

  • USER: the USER that created the process;
  • PID: indicates the ID of a process.
  • %CPU: Indicates the percentage of CPU resources occupied by processes.
  • %MEM: Indicates the percentage of physical memory occupied by processes.
  • VSZ: The size of the virtual memory occupied by the process, in KB.
  • RSS: indicates the actual physical memory occupied by a process, in KB.
  • Tty1-tty7 is the local console terminal, tty1-TTy6 is the local character interface terminal, tty7 is the graphical terminal, and PTS /0-255 is the virtual terminal. If yes? Is the system process;
  • STAT: Process status, R- running, S- sleeping, T- stopped, S- contains child processes, +- in background;
  • START: indicates the START time of the process.
  • TIME: indicates the CPU computing TIME occupied by the process. Note that it is not system TIME.
  • COMMAND, the name of the COMMAND that generates this process;

There is also the command pstree to display the process tree:

The top command displays the system health status, which is similar to the system manager in Windows.

In the interactive mode of the top command, you can run the following commands:

  • ?/h: Displays help for interactive modes;
  • P: sort by CPU usage, default
  • M: sort by memory usage;
  • N: sort by PID;
  • q: quittop;

Load average Refers to the average load of the system in the previous 1 minute, 5 minutes and 15 minutes respectively. It is generally believed that when the value is less than 1, the load is small. If the value is greater than 1, the system is overloaded. For a multi-core CPU, this number should be no more than the number of cores you have, such as 2 for a dual-core CPU.

It is possible that the process is in the process of terminating. If the process is still in the process of terminating, it needs to be checked manually.

The %CPU (s) id is the main thing to look at. It means the percentage of idle cpus. If it is less than 20, then the system status is stagnant.

3.2 Killing processes

The main commands to kill a process are:

function The command
Kill a process kill
Kill processes by process name killall
To kill a process by its name, add-tUsers can be kicked out based on the terminal number pkill

When killing a process, you can use a signal. There are many signals, commonly used signals:

The signal code The signal name instructions
1 SIGHUP Let the process shut down immediately, then restart after reading the configuration file again, and restart smoothly
2 SIGINT Program termination signal, used to terminate foreground process, equivalent to CTRL + C shortcut key
9 SIGKILL Forced termination, used to terminate the program immediately. This signal cannot be blocked, processed, or ignored
15 SIGTERM By default, the kill command indicates the normal end of the process. Sometimes the process cannot be terminated due to a fault. In this case, the -9 signal is used

Therefore, the commonly used commands to kill processes are: kill -1 2235 normally or kill -9 2235 forcibly

3.3 Changing the Process Priority

We can ps – le | more to see the process priority:

PRI indicates Priority and NI indicates Nice. Both values are priorities. A smaller number indicates a higher Priority for the process. You can only modify NI and cannot directly modify PRI, but the system finally takes the value of PRI + NI. The NI value ranges from -20 to 19. Normal users can adjust the NI value from 0 to 19. Root users can set the NI value to a negative value only when they adjust their own processes.

You can use the nice command to change the priority. Using the nice < options > command, you can directly assign the NI value to a newly executed command, but you cannot change the NI value of an existing process. The -n value option assigns an NI value to the command.

For example, change the priority of the Apache process. Nice -n-5 service HTTPD start

To change the priority of an existing process, run the renice command, renice < priority > PDID, where PID is the ID of a process.

For example, renice-10 2125 changes the NI value of process 2125 to -10.

4. Work management

  1. The current login terminal can only manage the work of the current terminal, but cannot manage the work of other login terminals.
  2. The commands placed in the background must continue to run for a certain period of time so that we can capture and manipulate the work.
  3. The command placed in the background cannot interact with the foreground user or need the foreground input. Otherwise, the command placed in the background can only be paused but cannot be executed.

There are two main commands for putting a process in the background:

  1. < command > &Put the command in the background and execute it in the background
  2. The < command >After execution, press CTRL + Z shortcut key, put in the background to pause

To view the background work, run the jobs [-l] command. -l is the PID that displays the work.

+ represents the last job to be put in the background and is the default job to be restored when work is restored, and – represents the penultimate job to be put in the background.

Restore to foreground:

  1. Fg % Work numberRestore the work suspended in the background to the foreground execution, here % can be omitted, pay attention to the difference between the work number and PID;
  2. Bg % Work numberThe background of the suspended work to resume the execution of the background, the background of the recovery of the execution of the command, is not and the foreground interaction, otherwise can not be restored to the background execution;

The method of executing background commands from the login terminal is as follows:

  1. The first method is to add commands that need to be executed in the background/etc/rc.localFile;
  2. The second method is to use the system scheduled task, let the system at a specified time to execute a background command;
  3. The third way is to usenohupCommand;

How to use the nohup command nohup < command > &

5. SSH operations

Secure Shell(SSH) is a Secure network protocol based on the application layer. It provides security for remote login sessions and other network services and effectively eliminates network vulnerabilities. SSH encrypts all transmitted data and prevents DNS spoofing and IP spoofing. An added benefit is that the data is compressed, so it speeds up the transfer process. It has become standard on Linux systems.

5.1 Logging In to the Server using SSH

ssh -p port <username>@<hostname or IP address>
Copy the code

For example, the Tencent cloud server I bought here can use SSH root@< public IP/ domain name > to connect, if you have set the mapping between domain name and IP, then write your domain name at the end of @ is also ok, for example, I can SSH root@sherlocked93 to connect to the server.

Then you can enter the CentOS system by entering the password, but every time you log in, you need a password, which is stupid and insecure. You can set up the SSH key to log in, which can be baidu.

After connecting to the system, you can use Ctrl+D or exit command to log out of the remote login.

After CentOS is connected, the meanings of the command prompt on the left of the command line are as follows:

5.2 Uploading/Downloading Files through SSH

SSH is a secure remote file copy command based on SSH login in Linux. SCP is short for Secure copy. You can use it to upload a local folder to a remote server or download a folder from the remote server to the local computer:

Upload folder to remote server
scp -P port -r /local/dir username@servername:/remote/dir
# scp -p 2333 -r /test/a [email protected]:/var/b

Download folder from remote server
scp -P port -r username@servername:/remote/dir/ /local/dir
# scp -p 2333 -r [email protected]:/var/b /test/a
Copy the code

The -r parameter indicates recursive replication, that is, copying files and directories under the directory. If you want to upload a single file, delete -r. The uppercase P indicates the port. If the default SSH port 22 has not been changed, then -p is not required.

5.3 SSH Setting Timeout Disconnection

When SSH is in use, the server will disconnect automatically because the idle time is too long, and then the server will have to reconnect, which is troublesome. You can set it to prevent the server from being kicked out frequently.

One way is to modify the server Settings, find the.ssh directory of the user, such as the root user. The directory is /root/.ssh/, and create the config file in the directory

vim /root/.ssh/config
Copy the code

Add the following sentence:

ServerAliveInterval 60
Copy the code

Then, run the :wq command to save and exit the configuration file and restart the shell of user root. In this way, the remote server will not be disconnected due to long-term operations.

There is another way to set the $TMOUT system environment variable

Vim /etc/profile add:

export TMOUT=0
Copy the code

If you set the value of TMOUT to 0, you can set the value to no timeout. If you set the value of ESC to :wq, you can save the configuration and exit from the vi editor, and then source /etc/profile for the configuration to take effect immediately.


Reference documents:

  1. CentOS root directory directory introduction – Zhihu
  2. Linux vi/vim | novice tutorial
  3. Introduction to Linux Basic Installation video tutorial I- MOOCs
  4. Linux System Editing and Management Video Tutorial II- MOOCs
  5. Introduction to Centos7-based Linux operating system and server configuration – Bilibili
  6. The connection is disconnected after the SSH login is idle for a long time

PS: MY blog address Github – SHERlocked93/blog, also welcome everyone to pay attention to my public number [front afternoon tea], refuting together ~

In addition, you can join the wechat group of “front end afternoon tea Communication Group”. Long press to identify the qr code below to add my friend, note to add group, I pull you into the group ~