2 Basics of Linux operation
2.1 Shell and Command Basics
2.1.3 File and Linux directory structure
2.1.3.1 Soft link ln -s
ln link command -s soft link (creating a soft link is equivalent to a shortcut in Windows)
usage ln -s source path /usr/local/bin
The link display after creating the link
2.1.3.2 Linux directory structure
2.2 Linux common operation commands
2.2.1 File directory operation commands
2.2.1.0 Common file and directory operation commands
2.2.1.1 mkdir create folder
2.2.1.2 Create a .sh file touch file name
2.2.1.3 Execute the .sh file bash file name
2.2.1.4 rm delete files and directories: rm [option] file or directory
Parameter -i prompts whether to delete
-r deletes recursively
-f deletes directly without prompting.
2.2.1.5 Move or rename mv source file target directory or new file name
2.2.1.6 Copy or replace cp [-r] source file target directory or target file name
2.2.1.7 cd directory name (cd followed by a space followed by a directory name with multiple spaces will report that the directory does not exist)
2.2.1.8 Detailed explanation of ls command
2.2.1.8.1 View by time format ls -l --time-style '+%Y/%m/%d %H:%M:%S'
-l: detailed information of all files in the current directory
-h: display file size [human-readable display method]
-r: reverse sort, reverse order
-t: sort by modification time
Example: ls -lrt: reverse sort by modification time [that is, the latest modification time is placed at the end]
-S: sort by file size
2.2.1.8.2 Display file time
Description: atime will be changed after vi filename
2.2.1.8.3 Display file size
ls -l size in bytes (default ls -l --block-size=k).
ls -l --block-size=m M size.
ls -l --block-size=g G size
2.2.1.8.4 file filename (view file type)
2.2.1.8.5 ll -ltr (ll) View file permissions ls -a View hidden files
2.2.1.9 Detailed explanation of ll command
2.2.2 Text file operation commands
2.2.2.0 Common File Operation Commands
2.2.2.1 Edit file vi filename
i means to enter the editing state, G, gg to jump to the bottom, 1G (shift+g) to jump to the head, I to locate to the beginning of the line, and A to locate to the end of the line.
Cancel editing: Press the Esc key or Ctrl+C
to save: Cancel editing and enter ":wq", then press Enter to save and exit (:wq! force save and exit, "wq" means Write and Quit, ! means force) Edit without saving: cancel editing, enter ":q", and then press Enter ("q!" means ignore the modification and force exit)
.
Look for:
syntax/pattern.
Description: /pattern and then Enter to find the next one, and press "n" to find the next one continuously.
Delete line dd
replace:
global replace: %s/vivian/sky/g: replace all vivian in each line with sky (the first of all lines in %s (s current line), /g global)
2.2.2.2 Edit the file gedit filename (suitable for graphical interface, and install gedit software to operate, the file format will be changed to txt format)
2.2.2.3 View file content cat and more
cat: cat [-n] filename 滚屏显示文件全部(-n 显示行号)
more:一页一页显示,空格显示下一页,ctrl+b 显示上一页 q 退出。
head,tail显示开头和结尾 语法 (head | tail) [-number] filename(number 显示行数) 。
less 兼容more所有功能,空格显示下一页,b 显示上一页 q 退出,且鼠标可能滚动。
2.2.2.4 Content Search
grep 【options】【pattern】【file】 : 查找文件内容(-n 显示行号)
【options】
-v: 逆反模示, 只输出"不含" RE 字符串之句子.
-r: 递归模式, 可同时处理所有层级子目录里的文件.
-i: 忽略大小写.
-n: 同时输出行号.
-o: 只输出符合 RE 的字符串
2.2.2.5 Text analysis tool awk
awk [options] 'script' var=value file(s)
options=-F时
-F fs or --field-separator fs
指定输入文件折分隔符,fs是一个字符串或者是一个正则表达式,如-F:。
示例:
awk '{print $1,$4}' log.txt (options为空时,每行按空格或TAB分割,输出文本中的1、4项)
awk -F ‘;’ '{print $1,$2}' log.txt (每行以;分割,输出文本中的1、2项)
2.2.2.6 The uniq command is used to check and delete repeated rows and columns in the text file, and it is generally used in combination with the sort command.
uniq syntax uniq [-icud]
[input file]
-i: Ignore differences in uppercase and lowercase characters
-c: Count
-u: Only display unique rows
-d or --repeated Only display repeated rows and columns.
2.2.2.7 sort Sort text files by row
sort [-r] filename (-r reverse sort)
2.2.2.8 Compare file content diff filename1 filename2
2.2.2.9 Replace file content sed 's/1/j/g' test2.txt (replace all 1 with j)
2.2.3 Packaging and compression, see the compression command
2.2.3.1tar format (this format is only packaged, not compressed)
Packaging: tar -cvf [target file name].tar [original file name/directory name]
Unpacking: tar -xvf [original file name].tar
Note: c parameter represents create (creation), x parameter represents extract (unpackage), v parameter represents verbose (detailed information), f parameter represents
filename (file name), so f must be followed by the file name.
2.2.3.2 tar.gz format
Method 1: Use the tar file that has been packaged before, and directly use the compression command.
Compression: gzip [original file name].tar
Decompression: gunzip [original file name].tar.gz
Method 2: Pack and compress at one time, decompress and unpack Pack
and compress: tar -zcvf [target file name].tar.gz [original file name/directory name] Decompress and unpack :
tar -zxvf [original file name].tar.gz
2.2.3.3 tar.xz format
Method 1: Use the tar file that has already been packaged, and directly use the compression command:
Compression: xz [original file name].tar
Decompression: unxz [original file name].tar.xz
Method 2: One-time packaging and compression, decompression and unpacking Packaging
and compression: tar -Jcvf [target file name].tar.xz [original file name/directory name] decompression and unpacking
: tar -Jxvf [original file name].tar.xz
2.2.3.3 Use the tar tvf command to view the list of files compressed by tar and gz:
tar tvf TF_mouse.tar.gz
2.2.3.4 Compression (zip) Decompression (unzip)
Compression: Usage zip [parameter] [packed file name] [packed directory path]
Decompression: usage unzip [parameter] [compressed file name] [decompressed directory path]
such as: unzip file.conf.zip -d /data/bak (decompress file.conf.zip to /data/bak directory)
2.2.4 Information display command
2.2.4.1 Commonly used information display commands
2.2.4.2 stat
2.2.4.3 df
The Linux system views the file system in units of disk partitions, and you can add parameters to view the remaining disk space information. The command format is: df -hl
2.2.4.4 of
2.2.4.5 uptime View the machine startup time, login users, average load, etc.
2.2.4.6 netstat
A more concise command netstat -lntp
to view port occupancy: netstat -ntulp | grep 8080
2.2.3 Using Shell
2.2.3.1 User working environment
2.2.3.2. Command history (the history command displays and edits historical commands.)
2.2.3.2. Redirection
2.2.3.3 Output redirection to clear file content
One, > filename (redirect null to filename through the shell)
2.2.3.4 nohup command combined with redirection
nohup syntax: nohup Command [ Arg ... ] [ & ] Run the command without hanging up.
Example 1 nohup /root/test.sh & (the output information of the program operation is placed in nohup.out of the current directory, and the current directory refers to the directory where the nohup command is run) Example 2 nohup command
> redirectfile 2>&1 & (the output is redirected to the redirectfile file)
demo: nohup java -jar /home/shenzhenair/sfwwk/sf.jar > /home/shenzhenair/sfw wk/nohup.out 2>&1 &
(create /home/shenzhenair/sfwwk/nohup.out if it does not exist)
Example 3: Execute the jar file nohup java -jar shareniu.jar & (nohup does not hang up the running command to close the window and the program runs, & runs in the background, but the program will stop when the window is closed)
2.2.3.4 Pipe |
Connect the output of one command to the input of another command.
Example: ls | grep redis (the current directory searches for the file or folder name with redis)
3 Multi-user multi-task management
3.1 Account Management
3.1.1 Account system documents mainly include the following:
/etc/passwd: Define user account information
/etc/shadow: Define user password information
/etc/group: Define user group information
/etc/gshadow: Define group password
information /etc/sudoers User, group permission configuration (before modification, check whether there is write permission)
sudo -l View current user permission information (refers to the permission defined in /etc/sudoers)
id username (view user group information)
3.1.2 Using command line tools to manage accounts
gpasswd command function: management group
Example
gpasswd -d shj root (remove shj user from root group)
3.1.3 Password Management
Check user password aging chage -l username
Example: passwd username (modify user password)
3.1.4 User and Group Status
Example: su-crq
newgrep staff
3.2 Rights Management
3.2.1 3 basic permissions
3.2.2 View permissions for files and directories
3.2.3 Change operation authority
3.2.4 Change the owner and group
3.3 Process
3.3.1 Processes in Linux
3.3.2 View process with ps command
3.3 Others
3.3.1 Command to switch users su username
To switch from a normal user to a root user, you can also use the command sudo su[i]
5.1 Linux network configuration
5.1.1 Local domain name resolution configuration file /etc/hosts
5.1.2 Remote domain name resolution configuration file /etc/resolv.conf
5.1.3 Set the hostname /etc/hostname
5.2 Linux network tools
5.2.1 lsof
View the file handle opened by a process: lsof -p 2862
View the usage of a port: lsof -i:8080
5.2.2 traceroute command (used to trace the gateway through which the ip is reached)
Command format traceroute [parameter option] hostname, domain name or IP address.
5.2.3 Software package management systems of major software vendors
Red Hat's RPM
Yellow Dog's yum (solves the package dependency problem)
Debian's APT (solves the package dependency problem)
6.2 Scheduled task service crond
6.2.1 crontab options command
6.2.1 crontab file format
6.3 OpenSSH service
6.3.1 Introduction to SSH
6.3.2 Introduction to OpenSSH
6.3.3 Configuring the OpenSSH service
7.1 System performance monitoring
7.1.1 Common tools for system performance monitoring
7.1.2 top command
1.%cpu
us CPU percentage occupied by user space, sy kernel space occupied CPU percentage, id idle CPU percentage.
2. Memory
total The total memory is 8G, and the display unit is KB free, which is free, and which has been used by userd.
3. Swap area memory
4. Process information
%CPU The percentage of CPU time occupied since the last update (the last update refers to the information displayed by top last time)
%MEM The percentage of physical memory used by
the process VIRT The total amount of virtual memory used by the process, in kb. VIRT=SWAP+RES
5. top -p pid Query the memory and cpu usage of the specified thread
7.1.3 Check Linux Check the number of physical CPUs, cores, and logical CPUs
Check the number of physical CPUs cat /proc/cpuinfo| grep “physical id”| sort| uniq| wc -l check the number of cores
(that is, the number of cores) in each physical CPU cat /proc/ cpuinfo| cpuinfo | grep name | cut -f2 -d: | uniq -c
8.1.2 Disable root account login
8.1.2.1 sudo
The sudo command executes commands as the system administrator (provided that the user must be defined in /etc/sudoers).
To configure sudo for ordinary users, you need to configure /etc/sudoers
visudo or vim /etc/sudoers (visudo can prevent two users from modifying at the same time)
modify the file as follows:
username ALL=(ALL) ALL
usergroupname ALL=(ALL) ALL
save and exit, username user has root authority, and sudo is added before use.
When the above method is invalid, you can change the user's group to root Command: usermod [-g|-G] root username (group to which -g belongs, additional group to which -G)
9.1 Firewall
9.1.1 firewall
1.0 默认配置文件:/etc/firewalld/firewalld.conf
1.1 防火墙关闭和开启 systemctl stop(start) firewalld
1.2 查看预定义区域: firewall-cmd --get-zones
1.3 查看默认区域: firewall-cmd --get-default-zone
1.4 查看默认区域所有规则: firewall-cmd --list-all
1.5 查看指定区域内允许访问的所有端口: firewall-cmd --zone= --list-ports
例 firewall-cmd --zone=public --list-prots 查看public区域下
1.6 默认区域规则文件:/etc/firewalld/zones/public.xml
1.8 添加或删除端口号55 firewall-cmd --permanent --zone=public --add[remove]-port=55/tcp
1.9 添加或删除端口 请重新加载防火墙配置: firewall-cmd --reload(执行后 查看下该区域下的端口是否更新, 以及更新到区域规则文件)
9.1.2 rich-rule advanced rule setting
#Step1: Delete the original 3306 port access rule
firewall-cmd --permanent --remove-port=3306/tcp
#Step2:添加规则
firewall-cmd --permanent --add-rich-rule=“rule family=“ipv4” source address=” 192.168.1.100" port protocol=“tcp” port=“3306” accept"
firewall-cmd --permanent --add-rich-rule=“rule family=“ipv4” source address=“127.0.0.1” port protocol=“tcp” port=“3306” accept”
#Step3: Effective rule
firewall-cmd --reload
#Step4: View the result
firewall-cmd --list-all