Article directory
This is a practical collection for junior operation and maintenance engineers. There is no complicated list, only various scenarios and corresponding solutions in actual combat.
The following commands are based on the CentOS operating system, and the command lines of different systems are slightly different. It is recommended to compare and study.
To check the system version, use the following command
cat /etc/redhat-release
The result is as follows, CentOS 7.5 distribution
CentOS Linux release 7.5.1804 (Core)
Add the above cat command
cat
Command: Generally used to view the content of the file, the format is as follows
cat [-AbeEnstTuv] [--help] [--version] fileName
Don't memorize a lot of it, generally used forcat 文件名即可
9+ scene
1. System firewall status
When deploying a Web site, it is often necessary to check the status of the system firewall and check the open port number of the server. The relevant commands are as follows
Check the system firewall
The command line is as follows
systemctl status firewalld
The results obtained note the green and red areas
Turn off the firewall
The command line is as follows
systemctl stop firewalld
After closing, you can use the above view status command again to get the status of the firewall at this time as follows
inactive (dead)
Restart the firewall
The command line looks like this
systemctl start firewalld
Here you can expand the knowledge point of learning is the systemctl
command , the specific learning direction is as follows
Commands related to managing system startup and managing system services are systemctl
implemented .
systemctl start [unit type]
:start up;systemctl stop [unit type]
:stop;systemctl restart [unit type]
: restart;systemctl status[unit type]
: View status;systemctl reload [unit type]
:Reload;
2. System ports
As a back-end operation and maintenance, you will encounter a lot of scenarios and need to check the open services and ports in the system. The command line is as follows
netstat -ntlp
The command to learn here is netstat
, if you don't have it, use yum install -y net-tools
to install it.
netstat
The common parameters of the command are described as follows:
-a
:all;-t
: Display the TCP port;-u
: Display the UDP port;-p
: Display the process identifier and program name;-l
: show only listening socket programs;-n
: Do not perform DNS polling, display IP.
At this point netstat -ntlp
, it is easy to understand if you are looking at it. You can combine various commands for use.
Here, with the filter command, you can improve the efficiency of use.
netstat -ntulp |grep 80
The grep command is as follows
grep is used to perform keyword searches in text. For example, the query result of the above command is:
Similarly, you can modify it to grep 27
get the following result
key parameter:
-b
: Search executable files (binaries) as text files;-c
: only display the number of lines found;-i
: ignore case;-n
: display line number;-v
: Inverse selection, listing lines without keyword.
For example, view python related port data
netstat -ntulp |grep python
View the specific situation of a port
lsof -i tcp:80
This involves a command called lsof
(list open files), which is a tool used to list the current system open files
The parameters involve the following:
-a
: List the processes that have opened the file;-i<条件>
: List eligible processes, which can be protocol, :port, @IP;
At this time, the following commands can be re-learned.
listen on port 22
lsof -i :22
Listen on ports 1~1024
ls -i:1-1024
3. Process related
In centos, you need to view the status of the processes running in the system. The relevant commands are as follows
View the detailed information of a process The
ps command is the most common process viewing tool in Linux systems. It is mainly used to display static snapshots containing complete information about the currently running processes. For example, the following commands
ps 8000
To view all processes, use the following command
ps aux
The command parameters are as follows:
a
: Display all process information under the current terminal, including processes of other users;u
: User-dominated process statusx
: usually used together with parameter a to display the process information of the current user under all terminals
ps aux
There is another command similar to ps -ef
, which is different from the previous command in the System V display style.
ps -ef
In addition to the ps
command , the top
process will also be used. This command will display the process ranking in the current terminal in a full- screen interactive interface, and track the CPU, memory and other system resource usage in time. By default, it refreshes every 3 seconds, similar to Windows The task manager in the system.
Aborting the process
Viewing the process, killing the process, are all common operations, and the killing process uses the kill
command :
kill -9 8000
-9
Indicates to force the process to stop immediately.
4. File permissions chmod
The chmod command is used to view and modify file permissions.
The following permissions exist in CentOS, and files have three identities:
- owner: file owner
- group: the same user group
- others: other non-user groups.
When modifying permissions, the three identities of each file (folder is also a file) have three permissions respectively. - read: read
- host:写
- execute: execute
Use ls -al
to view the permissions of all files in the current directory, as shown in the following figure
The permission characters of the file are -rwxrwxrwx
, each 3 is a group, and the corresponding identity is owner\group\others
.
Permissions can be represented by numbers, among them r=4
, w=2
, x=1
, -=0
, and then look at 777 when setting permissions, is it easy to understand?
- The numbers for settings
rwx (可读写运行)
permission settings are4 + 2 + 1 = 7
; - The numbers for settings
rw- (可读写不可运行)
permission settings are4 + 2 = 6
; - The number of settings
r-x (可读可运行不可写)
permission settings is4 +1 = 5
.
The command format is as follows
chmod xyz file文件名
The xyz
corresponding three permissions represent the permissions of User, Group, and Other.
There is another way of writing, as follows:
chmod 777 file (相同的命令 chmod u=rwx,g=rwx,o=rwx file 或 chmod a=rwx file)
chomd
Needs more practice to use (but not very often)
The help manual for viewing commands in the CentOS system can be added later --help
, for example
chmod --help
ps --help
5. Create soft links
Soft links (similar to shortcuts in Windows operating systems) can be configured in CentOS.
Create soft links
ln -s [源文件或目录] [目标文件或目录]
remove soft link
rm -rf /home/python
Don't add it at the end, /
otherwise it will delete the file (running with a bucket)
6. Search related
which command
which
is used to search the path and alias of the command
The syntax of the whereis command is as follows
whereis [option] name
Find the path to the executable, source code files of a specific program
The syntax of the find command is as follows
find [搜索范围] [搜索条件]
The locate command
locate
command find -name
is another way of writing, but it is faster than the latter because it does not search a specific directory, but a database /var/lib/locatedb
that contains all local file information.
Before use, you need to install locate first. The installation command is as follows:
yum -y install mlocate
The following error will occur during the first execution, which needs to be configured
[root@xxxx ~]# locate install.sh
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
Execute the updatedb
command and locate it again.
7. RAM and HDD
During the operation and maintenance time, it is often necessary to check the memory and hard disk usage. Use the following command
top command
. As mentioned above, you can use to q
exit .
View memory usage
free -m
The meaning of each column of the output is
- used(used memory);
- free(free memory);
- buff/cache (buffer cache/page cache);
Hard disk usage
df -h
8. Run commands without hanging up
Use nohup
(no hang up) to run the program without hanging up. Note that there is no background running function. The command syntax is as follows
nohup command [arg...] [&]
The &
last means to run in the background, but when the user exits (suspends), the command exits automatically.
The case is as follows:
nohup command > file.log 2>&1 &
- 0 – stdin (standard input) standard input
- 1 – stdout (standard output) Standard output
- 2 – stderr (standard error) standard error output
2>&1
is to redirect standard error (2) to standard output (&1), and standard output (&1) is redirected file.log
to .
9. Other small commands
pwd command to
query the path of the user's location
cd command to
change directory
mkdir command to
create directory files
tree command
displays the directory structure in a tree structure
cp command
Copy command
mv command
move command
touch command to
create file or update file timestamp
stat command to
view file attributes
tar command to
compress and package
power on command
reboot # 重启机器
poweroff # 关闭系统
record time
Flag of 2022, 589 / 1024 articles written.
You can follow me, like me, comment me, bookmark me.
more exciting