Linux: practical operations
1. Various tips
1.1 control+c(ctrl + c) force stop
-
When running certain Linux programs, if you want to force stop it, you can use shortcut keys
control+c (ctrl + c)
-
If the command is entered incorrectly, you can also use the shortcut key ctrl + c to exit the current input and re-enter.
1.2 You can log out of the account through the shortcut key: control + d (ctrl + d)
-
You can use the shortcut key: ctrl(control) + d to log out of the account.
-
Or exit the exclusive page of some specific programs
ps: cannot be used to exit vi/vim
1.3 Historical command search
-
You can use the history command to view the commands entered in history.
-
You can pass:! command prefix to automatically execute the last command matching the prefix.
- You can use the shortcut key:
control + r (ctrl +c)
to input content to match historical commands
If the searched content is what you need, then:
- Enter key can be executed directly
- You can get this command by pressing the left and right keys on the keyboard (not executed)
1.4 Cursor movement shortcut keys
- ctrl(control) + a, jump to the beginning of the command
- ctrl(control) + e, jump to the end of the command
- ctrl(option) + left key on keyboard, jump one word to the left
- ctrl(option) + right click on the keyboard, jump one word to the right
2. Software installation
2.1 Introduction
There are many ways to install software on the operating system, generally divided into:
- Download the installation package and install it yourself
- For example, win system uses exe files, msi files, etc.
- For example, mac system uses dmg files, pkg files, etc.
- Install from system app store
- If the win system has a Microsoft Store
- For example, the mac system has an AppStore store
Linux system also supports these two methods. Let's first learn to use: "App Store" in the Linux command line, yum Command to install software
2.2 yum command (requires root permission)
yum: RPM package software manager, used to automatically install and configure Linux software, and can automatically resolve dependency issues.
grammar:
- Options: -y, automatic confirmation, no need to manually confirm the installation or uninstallation process
- install: install
- remove: uninstall
- search: search
Case:
Here-y
automatically installs the current package and dependencies, without your need for intermediate confirmation.
Remove
search Use the yum command to search whether there is a wget installation package
wget and yum are two commonly used Linux commands used to download files from the Internet and install software packages.
-
wget is a command line tool used to download files from a specified URL. It can download any type of file and has many options to control the download process. Using wget, you can specify the URL of the file you want to download and save it to your local file system.
-
yum is a package manager used to manage the installation, update, and uninstallation of software packages on RPM-based Linux distributions (such as CentOS, Fedora). yum can automatically resolve dependencies between software packages and download and install software packages from configured software sources. It can also automatically download and install other dependent software packages during the package installation process.
the difference:
- Function: wget is mainly used for downloading files, while yum is mainly used for software package management.
- Usage scenarios: wget is suitable for downloading specific files, while yum is suitable for installing, updating and deleting software packages.
- Dependencies: wget does not check file dependencies, while yum can automatically resolve dependencies between packages.
- Convenience: yum provides more advanced functions, such as package search, update and dependency resolution, so it is more convenient in daily use.
To sum up, wget is mainly used to download files, while yum is mainly used to install, update and delete software packages, and can automatically resolve dependencies between software packages.
3. systemctl
Many software (built-in or third-party) in Linux systems support the use of systemctl command control: start, stop, and auto-start at boot.
Software that can be managed by systemctl is also generally called: service
grammar:
The system has many built-in services, such as:
- NetworkManager, the main network service
- network, deputy network service
- firewalld, firewall service
- sshd, ssh service (FinalShell uses this service to log in to Linux remotely)
4. Soft connection
4.1 Using the In command
Create soft links in the system to link files and folders to other locations.
Similar to "Shortcut" in Windows system
grammar:
-s
option to create a soft connection- Parameter 1: The linked file or folder
- Parameter 2: The destination to link to
Example:
ln -s /etc/yum.conf ~/yum.conf
ln -s /etc/yum ~/yum
5. date command
You can check the system time on the command line through the date command.
grammar:
-d
Display the date according to the given string, generally used for date calculation- Format string: Use specific string tags to control the displayed date format
- %Y year
- %y Last two digits of year (00…99)
- %m month (01…12)
- %d day (01…31)
- %H hours (00…23)
- %M minutes (00…59)
- %S seconds (00…60)
- %s The number of seconds since 1970-01-01 00:00:00 UTC
As above, because there are spaces in the middle, use double quotes to surround the format string as a whole.
5.1 Date command adds and subtracts dates
- -d option, you can display the date according to the given string, generally used for date calculation
The supported timestamps are:
- year year
- month month
- day after day
- hour hour
- minute minute
- second seconds
-d
The option can be used together with the format string.
5.3 Modify Linux time zone
The system default time zone is not the East Eighth District of China. Use root privileges and execute the following command to change the time zone to the East Eighth District time zone.
Delete the localtime file that comes with the system and link the /usr/share/zoneinfo/Asia/Shanghai file into a localtime file.
5.4 ntp program
We can automatically calibrate the system time through the ntp program
Install ntp:yum -y install ntp
Start and set up auto-start:
systemctl start ntpd
systemctl enable ntpd
When ntpd is started, it will regularly help us calibrate the system time online.
- You can also calibrate manually (requires root permission): ntpdate -u ntp.aliyun.com
Automatically calibrate through the service URL provided by Alibaba Cloud and the ntpdate (this command will be included after installing ntp) command
6. IP address host name
6.1 IP address
Every computer connected to the Internet will have an address for communicating with other computers
There are two main versions of IP addresses, the V4 version and the V6 version< a i=2> The address format of the IPv4 version is: a.b.c.d, where abcd represents a number from 0 to 255. For example, 192.168.88.101 is a standard IP address
You can check the IP address of this machine through the command: ifconfig. If you cannot use the ifconfig command, you can install:yum -y install net-tools
6.2 Special IP addresses
In addition to standard IP addresses, there are several special IP addresses that we need to know:
-
127.0.0.1, this IP address is used to refer to this machine
-
0.0.0.0, special IP address
- Can be used to refer to the local machine
- Can be used in port binding to determine binding relationships
- In some IP address restrictions, it means all IPs. For example, if the release rule is set to 0.0.0.0, it means that any IP is allowed to access.
6.3 Host name
- You can use the command: hostname to view the host name
- You can use the command:
hostnamectl set-hostname
host name, modify the host name (root required)
Log in again
6.4 Domain name resolution
IP addresses are really difficult to remember. Is there any way to replace the numeric IP address with a hostname or an alternative character address?
In fact, we always access the server through a characterized address and rarely specify an IP address.
For example, we open: www. baidu.com will open Baidu's website
Among them, www.baidu.com is Baidu's website, which we call: domain name
The process of accessing www.baidu.com is as follows:
6.5 Configure a fixed IP for the virtual machine
-
- Open the terminal program of the Mac system and execute the following command:
-
- Execute in the terminal: sudo su - and enter the personal system password to switch to the root user
- Execute in the terminal: sudo su - and enter the personal system password to switch to the root user
-
- Back up your files first:
cp /Library/Preferences/VMware\ Fusion/networking /Library/Preferences/VMware\ Fusion/networking.bakcup
-
- Modify the file through vim editor:
vim /Library/Preferences/VMware\ Fusion/networking
- Modify the file through vim editor:
This needs to be modified to 192.168.88.0
- 5. Backup files:
cp /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf.backup
Modify file:vim /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf
-
- Start VMware Fusion and enter the Linux virtual machine
Step 2: Modify fixed IP in Linux
- In the graphical page of the virtual machine, open the terminal and use su - to switch to the root user
- Use vim to edit the /etc/sysconfig/network-scripts/ifcfg-ens33 file and fill in the following content
- Execute: systemctl restart network to restart the network card, execute ifconfig and you will see that the IP address is fixed to 192.168.88.130
7.Network transmission
7.1 ping command
You can use the ping command to check whether the specified network server is reachable
Syntax:
Option: -c, the number of checks. If you do not use the -c option, you will continue to check an unlimited number of times
Parameters: ip or host name, the ip address or host of the server being checked name address
Example:
- Check whether baidu.com is connected to China Unicom
7.2 wget command
wget is a non-interactive file downloader that can download network files from the command line
Syntax:
Option: -b, optional, background download, the log will be written to the wget-log file in the current working directory
Parameters: url, download link
Example:
-
Download apache-hadoop version 3.3.0:
wget http://archive.apache.org/dist/hadoop/common/hadoop-3.3.0/hadoop-3.3.0.tar.gz
-
Download in the background:
wget -b http://archive.apache.org/dist/hadoop/common/hadoop-3.3.0/hadoop-3.3.0.tar.gz
-
You can monitor the background download progress through the tail command:
tail -f wget-log
7.3 curl command
curl can send http network requests and can be used to: download files, obtain information, etc.
grammar:
Option: -O, used to download files. When the url is a download link, you can use this option to save the file
Parameter: url, the network address to initiate the request
Example:
-
Initiate a network request to cip.cc:
curl cip.cc
-
Make a network request to python.itheima.com:
curl python.itheima.com
-
Download the hadoop-3.3.0 installation package through curl:
curl -O http://archive.apache.org/dist/hadoop/common/hadoop-3.3.0/hadoop-3.3.0.tar.gz
7.4 Ports
Port is the entrance and exit for the device to communicate with the outside world. Ports can be divided into two categories: physical ports and virtual ports
- Physical port: also called an interface, it is a visible port, such as USB interface, RJ45 network port, HDMI port, etc.
- Virtual port: refers to the port inside the computer, which is invisible and is used for interaction between the operating system and the outside world.
7.4.1 Port (virtual)
The physical port is often seen in our daily life, and its function is also known.
But what is the use of virtual ports? Why is it needed?
- Communication between computer programs can only lock the computer through IP, but cannot lock specific programs.
- Specific programs on the computer can be locked through ports to ensure communication between programs.
- The IP address is equivalent to the community address. There can be many households (programs) in the community, and the house number (port) is the contact address of each household (program).
The Linux system is a super-large community that can support 65535 ports. These more than 60,000 ports are divided into 3 categories for use:
- Recognized ports: 1~1023, usually reserved for some system built-in or well-known programs, such as port 22 for SSH service and port 443 for HTTPS service
- Unless there are special needs, do not occupy ports in this range.
- Registered port: 1024~49151, usually can be used at will, used to loosely bind some programs\services
- Dynamic ports: 49152~65535, usually not fixedly bound to the program, but used temporarily when the program connects to the external network.
- As shown in the picture, the WeChat of computer A is connected to the WeChat of computer B. The 50001 used by A is the dynamic port. Temporarily find a port as the exit.
- Computer B’s WeChat uses port 5678, which is the registered port. It is bound to this port for a long time and waits for others to connect.
7.4.2 Check port occupancy
You can check the port occupancy through Linux commands
- Use the nmap command to install nmap:
yum -y install nmap
Syntax:nmap
The IP address being viewed
As you can see, 5 ports on this machine (127.0.0.1) are now occupied by programs.
- You can check the occupancy of the specified port through the netstat command
Syntax:netstat -anp | grep 端口号
, install netstat:yum -y install net-tools
For example As shown in the figure, you can see that the current system port 631 is occupied by the program (process number 1212)
Among them, 0.0.0.0:6000 means that the port is bound to the IP address 0.0.0.0, which means it is allowed. External access
You can see that the current system port 12345 is not in use.
8. Process management
- Programs run in the operating system and are managed by the operating system.
- In order to manage running programs, each program is registered by the operating system as a process in the system when it is running.
- And each process will be assigned a unique: process ID (process number)
8.1 View progress
You can view process information in the Linux system through the ps command
Syntax:
Option: -e, display all processes
Option: -f, display information in fully formatted form (display all information)
Generally speaking, the fixed usage is: ps -ef lists all information of all processes
- In FinalShell, execute the command: tail. You can see that this command has been blocked there.
- In FinalShell, copy a tab page and execute: ps -ef to find out the process information of the tail program.
- Question: If you find that there is too much information listed, it is difficult to find it accurately or it is very troublesome. What should you do?
We can use the pipe character with grep to filter, such as:
ps -ef | grep tail
, you can accurately find the tail command information
- Filtering not only filters names, process numbers, user IDs, etc., they can also be filtered by grep.
- For example: ps -ef | grep 30001, filter process information with the 30001 keyword (generally refers to filtering the 30001 process number)
8.1.1 Shut down the process
grammar:kill -9 进程ID
9. Host status
9.1 Check system resource usage
- You can check the CPU and memory usage through the top command, similar to the Windows Task Manager
- refreshes every 5 seconds by default. Syntax: Just enter top directly. Press
q
orcontrol + c
to exit
Detailed explanation of top command content
PID: process id
USER: user to which the process belongs
PR: process priority, the smaller the higher the priority
NI: Negative value indicates high priority, positive value indicates low priority
VIRT: The process uses virtual memory, unit KB
RES: The process uses physical memory, unit KB
SHR: The process uses shared memory, unit KB
S: Process status (S sleeping, R running, Z zombie state, N negative priority, I idle Status)
%CPU: CPU usage rate of the process
%MEM: Memory usage rate of the process
TIME+: Total CPU time used by the process , unit 10 milliseconds
COMMAND: command or name of the process or program file path
The top command also supports options:
When top is run interactively (not started with the -b option), it can be controlled with the following interactive commands
9.2 Disk information monitoring
Use the df command to check the hard disk usage
Syntax:df [-h]
Options: -h, display in more human-friendly units
- You can use iostat to view CPU and disk related information
Syntax:iostat [-x] [num1] [num2]
- Options: -x, show more information
- num1: number, refresh interval, num2: number, how many times to refresh
- tps: The number of transfers per second of the device (Indicate the number of transfers per second that were issued to the device.). "A transfer" means "an I/O request." Multiple logical requests may be combined into "one I/O request". The size of the "one transfer" request is unknown.
- Use the -x option of iostat to display more information
rrqm/s: How many read requests related to this device are merged per second (when the system call needs to read When fetching data, VFS sends the request to each FS. If the FS finds that different read requests read the data of the same block, the FS will merge the requests to improve IO utilization and avoid repeated calls); %util: disk utilization svctm represents the average service time of each device I/O operation (in milliseconds) await: The average processing time of each IO request (unit is microseconds and milliseconds). avgqu-sz Average request queue length. There is no doubt that the shorter the queue length, the better. avgrq-sz Average request sector size wKB/s: Number of write requests sent to the device per second rKB/s: Number of read requests sent to the device per second wsec/: The number of sectors written per second. rsec/s: The number of sectors read per second; sectors
wrqm/s: How many write requests related to this device were merged per second.
9.3 Network status monitoring
- You can use the sar command to view network-related statistics (the sar command is very complex, and is only used for simple network statistics)
Syntax:sar -n DEV num1 num2
Options: -n, check the network, DEV means check the network interface
num1: refresh interval (check once if not filled in), num2: number of views (unlimited times if not filled in)
As shown in the figure, it is viewed twice, refreshed every 3 seconds, and finally summarizes the average records.
Information interpretation:
IFACE name of the local network card interface
rxpck/s Data packets received per second
txpck/s Data packets sent per second
rxKB/S Data packet size received per second, in KB
txKB/S per second Size of data packets sent, in KB
rxcmp/s Compressed data packets received per second
txcmp/s Compressed packets sent per second a>
rxmcst/s Multicast packets received per second
10. Environment variables Important! ! !
10.1 The role of environment variables
When explaining the which command, we know that the series of commands used are essentially executable programs.
For example, the body of the cd command is the program file /usr/bin/cd.
Do we have any questions about why the program: /usr/bin/cd can be executed no matter where the current working directory is?
This is the role of environment variables.
Environment variables are some key information recorded by the operating system (Windows, Linux, Mac) when it is running to assist the system in running.
Execute in Linux system: env command to view the environment variables recorded in the current system
Environment variables are a KeyValue type structure, that is, name and value. As shown below:
…
and a series of other information are used to assist the system in running
to obtain key information from environment variables
10.2 Environment variable: PATH
In the previous question, we said that no matter what the current working directory is, the program /usr/bin/cd can be executed. This is achieved with the help of the value of the PATH item in the environment variable.
PATH records the search path for any command executed by the system, as recorded in the figure above (the paths are separated by :
):
- /usr/local/bin
- /usr/bin
- /usr/local/sbin
- /usr/sbin
- /home/itheima/.local/bin
- /home/itheima/bin
- When any command is executed, the ontology of the program to be executed will be searched from the above path in order.
- For example, if you execute the cd command, you will find the cd command from the second directory /usr/bin and execute it.
10.3 $ symbol
In Linux systems, $
symbols are used to get the value of "variables".
In addition to being used by the operating system itself, the information recorded by environment variables can also be used if we want to access it.
The value of the environment variable can be obtained through the syntax: $environment variable name
For example: echo $PATH
You can get the value of the PATH environment variable and output it through the echo statement.
orecho ${PATH}ABC
, when mixed with other content, you can use {} to mark the variable being taken
10.3.1 Set environment variables yourself! ! !
Linux environment variables can be set by users themselves, which are divided into:
- Temporary setting, syntax: export variable name = variable value
- Effective permanently
- It takes effect for the current user and is configured in the current user’s ~/.bashrc file.
- Effective for all users, configured in the system: /etc/profile file
- And use the syntax: source configuration file to take effect immediately, or log in to Termius again to take effect.
Case:
Set a temporary variable
Reconnect Termius
** The variable MYNAME has no value **
The current user takes effect permanently
[root@centos01 ~]# vi ~/.bashrc
New content
Make the file effective through commands [root@centos01 ~]# source .bashrc
Disconnecting and reconnecting Termius will still take effect, but switching users will not take effect.
Globally effective
[root@centos01 ~]# vim /etc/profile
Effective command[root@centos01 ~]# source /etc/profile
10.3.2 Customize the environment variable PATH
In the /root directory, create a new myenv folder and create the mkhaha file
But I want to execute mkhaha directly anywhere. In this case, I need to do the following a>
The program can be executed anywhere
11. Upload and download
11.1 rz, sz commands
File transfer can be performed through rz and sz commands
rz and sz need to be installed yum -y install lrzsz
- rz command to upload, syntax: just enter rz directly
- sz download
12. Compression and decompression
12. 1 Compression format
There are many compression formats on the market
- zip format: Linux, Windows, MacOS, commonly used
- 7zip: commonly used in Windows systems
- rar: Commonly used in Windows systems
- tar: commonly used in Linux and MacOS
- gzip: commonly used in Linux and MacOS
Software commonly used in Windows systems, such as winrar, bandizip and other software, all support various common compression formats, so we will not discuss them here.
We now want to learn how to operate in the Linux system: tar, gzip, zipThese three compression formats< /span>
Complete file compression and decompression operations.
12.2 tar command
Linux and Mac systems commonly use two compression formats, the suffix names are:
.tar, called tarball, archive file, that is, simply assembling files into a . In the tar file, there is not much reduction in file size. It is just a simple encapsulation of
.gz, which is also commonly seen as .tar.gz. The gzip format compresses the file, that is, using the gzip compression algorithm to compress the file. Compressing files into one file can greatly reduce the compressed volume
For both formats, you can use the tar command to perform compression and decompression operations
Syntax:
- -c, creates a compressed file, used in compression mode
- -v, displays the compression and decompression process, used to view the progress
- -x, decompression mode
- -f, the file to be created, or the file to be decompressed, the -f option must be the last one among all options
- -z, gzip mode, without -z it is ordinary tarball format
- -C, select the decompression destination for decompression mode
Common combinations of tar are:
tar -cvf test.tar 1.txt 2.txt 3.txt
Compress 1.txt 2.txt 3.txt into the test.tar filetar -zcvf test.tar.gz 1.txt 2.txt 3.txt
Compress 1.txt 2.txt 3.txt into the test.tar.gz file, using gzip mode
Notice:
- If the -z option is used, it is usually in the first option position.
- -f option must be the last option
Decompression
Commonly used tar decompression combinations are
tar -xvf test.tar
Unzip test.tar and extract the file to the current directorytar -xvf test.tar -C /home/itheima
Unzip test.tar and extract the file to the specified directory (/home/itheima)tar -zxvf test.tar.gz -C /home/itheima
Decompress test.tar.gz in Gzip mode and extract the file to the specified directory (/home/itheima)
Notice:
- -f option, must be the last bit in the option combination
- -z option, recommended at the beginning
- The -C option is used alone, separate from other parameters required for decompression
12.3 zip command
You can use the zip command, and the compressed file is a zip package
Syntax:
- -r, when compressing the included folder, you need to use the -r option, which has the same effect as the -r of rm, cp and other commands.
Example:
zip test.zip a.txt b.txt c.txt
Compress a.txt b.txt c.txt into the test.zip filezip -r test.zip test itheima a.txt
Compress the test and itheima folders and the a.txt file into the test.zip file
12.3.1 unzip command to decompress files
Use the unzip command to easily decompress the zip package
Syntax:
- -d, specifies the location to decompress, the same as tar's -C option
- Parameters, the decompressed zip archive file
Example:
unzip test.zip
, extract test.zip to the current directory
unzip test.zip -d /home/itheima
, extract test.zip to the specified folder (/home/itheima )