Basic operation of Linux

Linux command

1. Simple system command

# View ip address
	ip a
	ip addr
# ping network (test network connectivity)
	ip Of the target machine ip
# View system time
# cancellation
# Shut down
	shutdown now
# restart
# Clear screen

2.Linux file system

/binStorage location of basic commands available to all usersThere is no fixed command storage directory
/sbinAdministrator privileges are required to all commands
/bootFiles that need to be loaded and used when the linux system starts
/devAfter the peripheral is connected to linux, the corresponding file storage locationIt is similar to the symbol file of U SB flash disk and Cd in Windows.
/etcAfter the peripheral is connected to linux, the corresponding file storage locationSimilar to the registry in windows,
/homeHome directory. Every new user in linux will be automatically assigned a folder in homeSimilar to "My Documents" in windows, each user has its own directory.
/rootThe home directory of the root account, which is only used by the root accountSimilar to "My Documents" of Administrator account in windows
/liblinux commands and system startup need to use some public dependencies, which are placed in lib, similar to the jdk jar s that we need to introduce for code execution
/usrDefault installation path of many system softwareIt is similar to the Program Files directory under the C disk in windows.
/varLog files and cache files generated by system and program operation are placed here

3. Document management order

Note: commands are case sensitive

# View the list of files in the current directory
# View the files in the specified directory
	ls /
# View details, metadata information (user, group, size, creation time, permission information, file type)
	ls -l
# view hidden files
	ls -a 
# Parameter combination
	ls -la
# 2. Switch directories
	cd Destination folder
# Absolute path switching
	cd Absolute path
# Absolute path switching
	cd Relative path
# Example: switch to /etc/sysconfig/networks-scripts directory
# pwd view the directory where the current command is located
# Special catalog symbols
	~ Current user's home catalogue
	. current directory
	.. Upper level directory
# 4. Create new folders and files
# Create a new folder in the current location
	mkdir Folder name
# In the specified directory location, create a folder and create a parent folder
	mkdir -p /a/b/Folder name
# Create a new file in the current directory
	touch file name
# 5. Delete file
# Delete file
	rm file
# remove folders
	rm -r folder
# Force deletion without asking
	rm -rf file
# 6. Copy files
# Copy files
	cp Original document new document
# Copy folder
	cp -r Source folder new folder
# 7. Move the file or modify the file name
# Move source files to destination folder
	mv File folder
# Change the name of document A to document B
	mv file A file B
# 8. Obtain md5 fingerprint (digital signature) of the file
md5sum file name
# brief introduction
1. Digital signature, also known as digital fingerprint
2. You can verify whether the file has been modified
3. A string of strings calculated from a file,Unique mark of file content(The contents of the document remain unchanged,Fingerprints will not change)

4. Text content viewing command

cat command

# View all the information in the file (suitable for viewing small documents)
	cat file name
	cat -n test.log |grep "debug" Query keyword logs
	# Select the middle row of keywords Then check the logs of the first 10 rows and the last 10 rows of this keyword: tail -n +92 means to query the logs after 92 rows
	cat -n test.log |tail -n +92|head -n 20 

less command

# Browse the file information in paging mode (suitable for viewing large documents), and enter the browsing mode
	less file name
# Browse mode shortcut
	↑ #previous line
    ↓ #next row
    G #last page
    g #first page
    Space #next page
    /key word #Search keywords
# Exit browse mode and return to Linux command line mode
    q #sign out

tail command

# Scroll the last 10 lines of information of the file in real time (default 10 lines)
tail -f file name
# Display the last 20 lines of information in the file
tail -n 20 file name
tail -n -20 file name
# Display file information from line 20 to the end of the file
tail -n +20 file name
Parameter explanation:
-f This parameter is used for monitoring File File growth.
-c Number from Number Read the specified file at byte position
-n Number from Number Read the specified file at the line position.
-m Number from Number Read the specified file at the position of multi byte characters. For example, if your file includes Chinese characters, assume that it is specified-c Parameter, which may cause truncation, but use-m This problem will be avoided.
-b Number from Number Read the specified file at the 512 byte block position represented by.
-k Number from Number Indicated 1 KB The block position reads the specified file.

head command

Contrary to the tail command

# Query the first 10 lines of logs in the log file;
head -n 10 test.log 
# Query all logs except the last 10 lines of the log file;
head -n -10 test.log 

sed command

# Query the log according to the date. First, grep'2014-12-17 16:17:20 'test Log to determine whether there is this time point in the log
sed -n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p' test.log
# Delete rows that include keywords
sed -i '/key word/d' main.log

Using the more and less commands

# In this way, you can print pages by clicking the space bar
 cat -n test.log |grep "debug" |more 


# Use >xxx Txt save it to a file, and then you can pull down the file for analysis
cat -n test.log |grep "debug" >debug.txt

split command

The log file is too large, which can be viewed after cutting

#1 cut according to the number of rows
split -l 50000 main.log newfile_ --verbose
#2. Cut according to byte size: [split by 40mb per file]
split -b 40m main.log -d newfile_ --verbose 

grep command

# Print 5 lines before and after the matching line
grep -5 'parttern' inputfile
# Print 5 lines before and after the matching line
grep -C 5 'parttern' inputfile
# Print the last 5 lines of the matching line
grep -A 5 'parttern' inputfile
# Print the first 5 lines of the matching line
grep -B 5 'parttern' inputfile
-i:Ignore case when searching
-c:Output only the number of matching rows
-l:Only the matching file names are listed, and the specific matching lines are not listed
-n:List all matching lines and display line numbers
-h:Do not display file names when querying multiple files
-s:Do not display error messages that do not exist or have no matching text
-v:Show all lines without matching text
-w:Match whole words
-x:Match entire row
-r:Recursively searching 
-q:It is forbidden to output any results. The exited status indicates whether the search is successful
-b:Print the offset of the matching line spacing file header, in bytes
-o:And-b Used in combination, print the offset of the head of the matched word data file, in bytes

5. File search

File name lookup

# grammar
	find Search path -name "File name keyword"
# example
	find / -name "passwd"
	find / -name "ifcfg-*"

File content search

# grammar
	grep -Parameter the directory range to find
	# parameter
	-n Display the line number of the search result
	-R Recursively find all files in the directory
# example
	grep aries /etc
	grep aries /etc/passwd

6. System management

# Statically view system processes
	# Statically view system processes
	ps -ef Displayed results:
    1.UID       user ID
    2.PID        process ID
    3.PPID      Parent process ID
    4.C           CPU Occupancy rate
    5.STIME     start time
    6.TTY         To start this process TTY----terminal equipment
    7.TIME       Total time this process has been running
    8.CMD       Command name
# View the system process in real time
	# Shortcut key
		↑ Turn down
		↓ Upturn
		q sign out
# Close process
	kill process id 
# Force the process to close (use with caution)
	kill -9 process id

Force process shutdown psmisc tool

# Display the current process of the system in the form of tree
# Kill process
	killall Process name
# Show which process is using this file
	fuser /Target file

7. Output

Overwrite output

# Output the execution result of command 1 to the following file.
`Overwrite write`
	Command 1 > file
# example
	date > date.log

Append output

# Output the execution result of command 1 to the following file.
`Append write`
	Command 1 >> file
# example
	date >> date.log

8. Pipeline

# Syntax, take the output result of command 1 as the input of Command 2
 Command 1 | Command 2
# example
 lookup aries User: cat /etc/passwd | grep -n "baizhi"
lookup aries Group: cat /etc/group | grep -n "baizhi"
lookup sshd Process: ps -aux | grep sshd
 Find logs   tail log file | grep "keyword"
Paging lookup log   less log file | grep "keyword"
Find the entire log   more log file | grep "keyword"
Find groups in the system   cat File of system group | grep "keyword"

9. Document editing

System permissions | user groups

1. Create group
  `groupadd Group name`
2. delete group
  `groupdel Group name`
3. Find groups in the system
  `cat /etc/group | grep -n "Group name "`
  Note: each group information of the system will be stored in/etc/group In the file of


1. Create user
  `useradd -g Group name user name`
2. Set password
  `passwd user name`
3. Find system account
  Note: each user information of the system is saved in`/etc/passwd`In the document
4. Switch users
  `su user name`
5. delete user
  `userdel -r user name`

Permission setting

Syntax: chmod u±rwx,g±rwx,o±rwx file name
	- Delete permissions
	+ add permission
	= Assignment authority
## Add execution permission to the owner of the file
chmod u+x file name
## Delete all permissions to others in the file
chmod o-rwx file name
## Set read and write permissions for the group to which the file belongs
chmod g=wx file name
# The authority value of each owner of the file is calculated by using the sum of rwx.
# grammar
	`chmod [-R] nnn file` 
	-R Recursively set all files in the folder
# Set the permissions of the file to (the owner is readable, writable and executable, the group is readable and writable, and others are readable)
	chmod 764 file name

10. Compression and decompression

-zTo operate the tar.gz file, you need to use
-cCompress the specified storage location
-vDisplay the execution process information of compression or decompression
-fThe file to be processed must be placed at the end

.zip format decompression and compression

# Install zip software first
yum install zip  
# Installation without confirmation 
yum install -y zip  
Uninstall: yum remove 

zip Compressed file name original file
# Compressed file
zip -r Compressed file name original file
# Compressed file
zip Compressed file name original file	
zip a.txt
# Recursive if folder [if recursive compression is not specified, just compress the folder]
zip -r Compressed file name original folder	
zip -r bb

unzip Compressed file
#Extract file
unzip Compressed file name	;decompression

.gz format decompression and compression

# Install the file in.gz format first
gzip Original document
#Compressed to.gz format, the original file will disappear
gzip -c Original file compressed file
#Compress to.gz format, and keep the original file
#-c does not mean that you need to keep files, but to output the compressed results to the console
#>It means where to output the results
gzip -r catalogue
#Compress all sub files under the directory, but you cannot compress the directory
gzip  Original document	:  Compress the file, and the source file will disappear
gzip  a.txt
gzip -r Original folder	: Traverse the folder, compress the files in the folder, and the directory is not compressed
gzip -d Compressed file
gunzip Compressed file
#Unzip file directory
gunzip -r Compressed file directory	;decompression
#Unzip file
gunzip Compressed file

.bz2 format decompression and compression

bzip2 source file
#Compressed to.bz2 format, the source file cannot be retained
bzip2 -k source file
#Keep source files after compression
 be careful:bzip2 The command cannot compress directories
bzip2 -d source file
bunzip2 Compressed file
#Decompress, -k keep the compressed file
bunzip2 -k Compressed file

.tar.gz format

# Packaging Command & unpacking
 Packaging command:
tar -cvf Package file name source file
	-c		pack
	-v		Display process
	-f		Specify the packed file name
 tar -cvf learn.tar learn
 Unpacking command:
tar -xvf Package file name
	-x		Unpacking

Compression and decompression

# compress
tar -zcvf Compressed package name.tar.gz source file
	-z		Compress to.tar.gz format

tar -zcvf learn.tar.gz learn

tar -zxvf Compressed package name.tar.gz
	-x		decompression .tar.gz format
#Add -C to extract to the specified directory
tar -zxvf Compressed package name.tar.gz -C Specify directory
tar -zxvf learn.tar.gz  -C  catalogue
tar -zcvf learn.tar.gz learn

rmp software

1. install rpm Software
  Syntax:`rpm -ivh xxx.rpm`
2. Check whether this has been installed in the system rpm Software
  Syntax:`rpm -qa Software name`
3. uninstall rpm Software
  Syntax:`rpm -e Software name`
4. Example: installation tree tool
  Function: view the file information in a directory
  # View level 2 file information in a tree structure
  tree -L 2 Path to view

yum command

yum is implemented based on rpm. In addition to the functions of installing and uninstalling software of rpm, it also automatically finds and downloads software, automatically handles the dependencies between software, and downloads and installs dependent packages

## List all packages that can be installed
	yum list
## Install software
	yum install -y Software name
## Uninstall software
	yum remove Software name
## Find packages
	yum search all Software name

11.linux services

# For example: sshd network firewalld, etc
# Server management commands
	systemctl status service name
# Start service
	systemctl start service name
# Restart service
	systemctl restart service name
# Out of Service
	systemctl stop service name
# Prohibit the service from starting with linux.
	systemctl disable service name
# The setup service starts with linux.
	systemctl enable service name


[root@centos7 dirnew]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
----------------File content corresponding to the network card---------------------
    IPADDR="" # Just modify the ip address here
[root@centos7 dirnew]# systemctl restart network #Restart network card service


# Turn on the firewall
systemctl stop service name
# Temporarily turn off the firewall
systemctl stop firewalld
# Disable firewall startup
systemctl disable firewalld

firewalld It is a service name or a port
# View hostname
# Set hostname
hostnamectl set-hostname host name

Tags: Linux server

Posted by turboprop on Sat, 30 Jul 2022 10:06:04 +0300