Linux——4linux practical operations

Table of contents

4.1 Various tips (shortcut keys)

4.2 Software installation

4.3 systemctl

4.4 Soft connection

4.5 Date, time zone

4.6 IP address, host name

IP address

CPU name

DNS

Configure host mapping

Virtual machine configuration fixed IP

4.7 Network transmission

Downloads and network requests

port

4.8 Process management

4.9 Host status

4.10 Environment variables

Environment variable PATH:  

$ sign

Set environment variables yourself

Custom environment variable PATH

4.11 Upload, download

4.12 Compression and decompression

Compression format

tar command

zip command to compress files

4.1 Various tips (shortcut keys)

ctrl + c force stop

If you want to force stop some Linux programs from running, you can use the shortcut key ctrl + c.

If you make a command input error, you can also use the shortcut key ctrl + c to exit the current input and re- enter .

ctrl + d exit or log out

Cannot be used to exit vi/vim

Historical command search  

You can use the history command to view commands entered in history.

You can pass: !Command prefix to automatically execute the last command matching the prefix.

You can use the shortcut key: ctrl + r and enter content to search for historical commands. If the searched content is what you need, then: press the Enter key to execute it directly; press the left and right keys on the keyboard to get this command (without executing it)

Cursor movement shortcut keys

  • ctrl + a, jump to the beginning of the command
  • ctrl + e, jump to the end of the command
  • ctrl + left key on keyboard, jump one word to the left
  • ctrl + right click on the keyboard, jump one word to the right

clear screen

Through the shortcut key ctrl + l , you can clear the terminal content or through the command clear to get the same effect

4.2 Software installation

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

The Linux system also supports these two methods. Let's first learn to use: the "App Store" in the Linux command line and the yum command to install software.

yum command

yum: RPM package software manager , used to automatically install and configure Linux software, and can automatically resolve dependency issues.

grammar:

  1. Options: -y, automatic confirmation, no need to manually confirm the installation or uninstallation process
  2. install: install
  3. remove: uninstall
  4. search: search

Notice:

The yum command requires root privileges. You can switch to root with su, or use sudo to escalate privileges.

The yum command requires an Internet connection.

apt command - Ubuntu manager

The various Linux commands learned previously are all universal. But for software installation, CentOS systems and Ubuntu use different package managers. CentOS uses the yum manager, and Ubuntu uses the apt manager  . Through the WSL environment we learned earlier, we can get the Ubuntu operating environment.

grammar:

The usage is the same as yum. It also requires root permission. apt install wget, install wget apt remove wget, remove wget apt search wget, search wget

4.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:

 start starts; stop closes; status checks the status; enable turns on auto-start at power on; disable turns off auto-start on power on .

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).

In addition to the built-in services, some third-party software can also be controlled with systemctl after installation .

  • yum install -y ntp, install ntp software

          It can be controlled through the ntpd service name and systemctl.

  • yum install -y httpd, install apache server software

          It can be controlled through the httpd service name and systemctl.

Some software is not automatically integrated into systemctl after installation, we can add it manually.

4.4 Soft connection

Create soft links in the system to link files and folders to other locations. Similar to "Shortcut" in Windows system.

grammar:

 -s option, creates a soft link; parameter 1: the file or folder to be linked; parameter 2: the destination to be linked.

Parameter 1 must be an absolute path.

Example:

  • ln -s  /etc/yum.conf  ~/yum.conf
  • ln -s /etc/yum ~/yum

4.5 Date, time zone

data command

You can use the date command to view the system time on the command line.

grammar:

  1.  -d displays the date according to the given string, generally used for date calculation;
  2. Format string: Use specific string tags to control the displayed date format.
  • %Y year
  • %y The last two digits of the 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

Display the date in the format of 2022-01-01:

Display the date in the format of 2022-01-01 10:00:00:

 As above, because there are spaces in the middle, use double quotes to surround the format string as a whole.

date command to add and subtract dates

The -d option can display the date according to the given string, generally used for date calculation;

 The supported timestamps are:

year year month day day hour hour minute minute second second

The d option can be used in conjunction with format strings.

Modify Linux time zone

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.

ntp program

The system time can be automatically calibrated 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 ( root permissions required ): ntpdate -u ntp.aliyun.com

Calibrate automatically through the service URL provided by Alibaba Cloud and the ntpdate (this command will be included after installing ntp) command.

4.6 IP address, host name

IP address

Every computer connected to the Internet will have an address used to communicate with other computers. There are two main versions of IP addresses, V4 and V6 (V6 is rarely used and will not be covered in the course). The address format of the IPv4 version is: abcd, where abcd represents a number from 0 to 255. For example, 192.168.88.101 is a standard IP address.

You can use the command: ifconfig to check the IP address of the machine . If you cannot use the ifconfig command, you can install: yum -y install net-tools.

 special IP address

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 , a special IP address, can be used to refer to the local machine. It can be used to determine the binding relationship in port binding (explained later). In some IP address restrictions, it means all IPs , such as release rule settings. is 0.0.0.0, indicating that access from any IP is allowed .

CPU name

In addition to the external contact address (IP address), each computer can also have a name, called a host name. Whether it is a Windows or Linux system, you can set a host name for the system.

  • You can use the command: hostname to view the host name .
  • You can use the command: hostnamectl set-hostname hostname to modify the hostname (root required);
  • Log in to FinalShell again and you will see that the host name is displayed correctly.

DNS

IP addresses are really difficult to remember. Is there any way to replace the numeric IP address with a host name or an alternative character address? In fact, we always access the server through a characterized address and rarely specify an IP address. For example, if we open: www.baidu.com in the browser, it will open Baidu's URL. Among them, www.baidu.com is Baidu's website address is called a domain name.

The process of accessing www.baidu.com is as follows:

That is: 1. First check the records of this machine (private address book).

  • Windows看:C:\Windows\System32\drivers\etc\hosts
  • Look at Linux:/etc/hosts

2. Connect to the Internet and go to the DNS server (such as 114.114.114.114, 8.8.8.8, etc.) to ask.

Configure host mapping

For example, our FinalShell connects to the Linux server through the IP address. Is it possible to connect through the domain name (host name)?

Generally speaking, IP addresses are relatively long and difficult to remember. If the corresponding relationship between the IP address and the domain name is recorded in the local record, then you only need to remember the characterized name. Therefore, you can give the corresponding correspondence in the host file and use the domain name to access the corresponding IP.

Yes, we only need to configure the record in the Windows system: C:\Windows\System32\drivers\etc\hosts file.

 ​​​

 

 

It is set above that 192.168.88.130 corresponds to the domain name centos.

You can also connect successfully by entering centos in finalshell, as shown below:

Virtual machine configuration fixed IP

Why do you need a fixed IP?

Currently, the IP address of our virtual machine's Linux operating system is obtained through the DHCP service.

DHCP: Obtain the IP address dynamically, that is, it will be obtained every time the device is restarted, which may cause the IP address to change frequently.

Reason 1: It doesn’t matter if the IP address of the office computer changes, but we need to remotely connect to the Linux system. If the IP address changes frequently, we have to modify the adaptation frequently, which is very troublesome.

Reason 2: We have just configured the mapping between the virtual machine IP address and the host name. If the IP changes frequently, we also need to update the mapping relationship frequently.

To sum up, we need the IP address to be fixed and not to change.

Configure fixed IP in VMware Workstation

Configuring a fixed IP requires 2 big steps:

  1. Configure IP address gateway and network segment (range of IP addresses) in VMware Workstation (or Fusion)
  2. Manually modify the configuration file and fix the IP in the Linux system

First, let us take the first step and follow the pictures:

Now proceed to the second step, modify the fixed IP in the Linux system:

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.

4.7 Network transmission

Downloads and network requests

ping command

You can use the ping command to check whether the specified network server is reachable.

grammar:

  • Options: -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 name address of the server being checked

Example: Check whether baidu.com is connected to China Unicom

wget command

wget is a non-interactive file downloader that can download network files from the command line

grammar:

  • 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 3.3.0 version: 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

Note: No matter whether the download is completed or not, the files to be downloaded will be generated. If the download is not completed, please clean up the uncompleted and unavailable files in time .

curl command

Curl can send http network requests and can be used to: download files, obtain information, etc.

grammar:

  • Options: -O, for downloading files , when the url is a download link, you can use this option to save the file
  • Parameters: url, the network address to initiate the request

Example:

  • 1. Initiate a network request to cip.cc: curl cip.cc

  • 2. Initiate a network request to python.itheima.com: curl python.itheima.com
  • 3. 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 

port

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 a 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.

We often see physical ports in our daily lives and know their functions. But what is the use of virtual ports? Why is it needed?

Just by IP address

 

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 (commonly used in conjunction with well-known software), such as port 22 of the SSH service and port 443 of the HTTPS service. They are not specially needed and do not occupy this range. port
  • Registered port: 1024~49151, usually can be used at will, used to loosely bind some programs\services ( user-defined )
  • Dynamic ports: 49152~65535, usually not fixedly bound to the program, but used temporarily when the program connects to the external network. ( mostly used for export )

In the picture above, computer A's WeChat connects to computer B's WeChat. A uses 50001, which is the dynamic port. It temporarily finds a port as the exit. Computer B's WeChat uses port 5678, which is the registered port. It binds this port long-term and waits for others to connect.

View 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.

nmap 127.0.0.1 (check the local port occupancy)

As you can see, 5 ports on this machine (127.0.0.1) are now occupied by programs.

Port 22 is generally used by the SSH service, that is, the port used by FinalShell to remotely connect to Linux.

You can use the netstat command to check the occupancy of the specified port.

Syntax: netstat -anp | grep port number , install netstat: yum -y install net-tools

As shown in the figure, you can see that the current system port 6000 is occupied by the program (process number 7174), 0.0.0.0:6000, which means that the port is bound to the IP address 0.0.0.0, which means external access is allowed.

 As you can see, the current system port 12345 is unused.

Summarize:

nmap IP address, view the externally exposed port of the specified IP.

netstat -anp | grep port number to check the occupation of the specified port number on this machine .

4.8 Process management

process:

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).

Windows System Task Manager:

 Check the process on Linux system:

Check the progress:

You can view process information in the Linux system through the ps command.

grammar:

  • Option: -e, displays all processes
  • Options: -f, display information in fully formatted form (display all information)

Generally speaking, the fixed usage is: ps -ef lists all information of all processes .

The specific information of the process is displayed, from left to right:

  • UID: User ID to which the process belongs
  • PID: Process ID of the process
  • PPID: Parent ID of the process (other processes that start this process)
  • C: CPU usage of this process (percentage)
  • STIME: The start time of the process
  • TTY: The terminal serial number that started this process. If ? is displayed, it means non-terminal startup.
  • TIME: The time the process occupies the CPU
  • CMD: The name or startup path or startup command corresponding to the process

View the specified process:

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).

Shut down the process:

In Windows systems, you can select the process through the Task Manager and click End Process to close it.

Likewise, in Linux, a process can be shut down via the kill command.

Syntax: kill [-9] process ID

Option: -9 means forcefully shutting down the process. Not using this option will send a signal to the process asking it to shut down, but whether to shut down depends on the process's own processing mechanism.

 

4.9 Host status

Check system resource usage

You can check the CPU and memory usage through the top command , similar to the Windows Task Manager.

By default, it refreshes every 5 seconds. Syntax: Just enter top directly. Press q or ctrl + c to exit.

Detailed explanation of top command content:

first row:

top: command name, 14:39:58: current system time, up 6 min: started for 6 minutes, 2 users: 2 users logged in, load: 1, 5, 15 minutes of load.

second line:

 Tasks: 175 processes, 1 running: 1 process running, 174 sleeping: 174 processes sleeping, 0 stopped processes, 0 zombie processes

The third row:

%Cpu(s): CPU usage, us: user CPU usage, sy: system CPU usage, ni: percentage of CPU time occupied by high-priority processes, id: idle CPU rate, wa: IO waiting CPU usage, hi : CPU hardware interrupt rate, si: CPU software interrupt rate, st: forced waiting CPU occupation rate.

Fourth and fifth elements:

Kib Mem: physical memory , total: total amount, free: idle, used: used, buff/cache: buff and cache occupation

KibSwap: virtual memory (swap space), total: total amount, free: idle, used: used, buff/cache: buff and cache occupation

Detailed explanation of top command content:

  •  PID: process id USER: user to which the process belongs
  • PR: process priority, the smaller the value, the higher it is
  • NI: Negative values ​​indicate high priority, positive values ​​indicate low priority
  • VIRT: virtual memory used by the process, unit KB
  • RES: physical memory used by the process, unit KB
  • SHR: shared memory used by the process, unit KB
  • S: process status (S sleep, R running, Z zombie state, N negative priority, I idle state)
  • %CPU: CPU utilization rate occupied by the process
  • %MEM: memory occupied by the process
  • TIME+: Total CPU time used by the process, unit 10 milliseconds
  • COMMAND: The command or name of the process or the path to the program file

top command options:

 When top is run interactively (not started with the -b option), it can be controlled with the following interactive commands:

 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.). "One transfer" means "one I/O request". Multiple logical requests may be combined into "one I/O request". The size of the "one transfer" request is unknown.

  • More information can be displayed using the -x option of iostat

 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, view the network, DEV means view the network interface

num1: Refresh interval (if not filled in, check once and end),

num2: Number of views (unlimited times if left blank)

As shown in the figure, it is viewed twice, refreshed every 3 seconds, and finally summarizes the average records.

Interpretation of information:

IFACE The name of the local network card interface.

rxpck/s packets received per second

txpck/s packets sent per second

rxKB/S The size of the packets received per second, in KB

txKB/S The size of the data packet sent per second, in KB (generally more attention is paid to these two)

rxcmp/s Compressed packets accepted per second

txcmp/s compressed packets sent per second

rxmcst/s Multicast packets received per second

4.10 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 the env command in the Linux system to view the environment variables recorded in the current system. Environment variables are a KeyValue type structure, that is, names and values, as shown below:

The figure records: HOME:/home/itheima, the user’s HOME path

USER: itheima, the current operating user

PWD: Current working path...

A series of information, etc., are used to assist the system in obtaining key information from environment variables when running.

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 (paths are separated by:) .

When any command is executed, the ontology of the program to be executed will be searched from the above path in order.

$ sign

In Linux systems, the $ symbol is used to get the value of a "variable".

The information recorded by environment variables is not only used by the operating system itself, but can also be used if we want to access it.

To obtain the value of an environment variable, you can  obtain it through the syntax: $environment variable name

For example: echo $PATH can obtain the value of the PATH environment variable and output it through the echo statement.

 Or: echo ${PATH}ABC

 When mixed with other content, {} can be used to indicate who the variable is.

Set environment variables yourself

Linux environment variables can be set by users themselves, which are divided into:

  1. Temporary setting, syntax: export variable name = variable value
  2. 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 FinalShell again to take effect.

Custom environment variable PATH

The environment variable PATH records the search path for system execution commands. We can also add these search paths to PATH ourselves.

test:

  • Create the folder, myenv, in the current HOME directory, and create the file mkhaha in the folder
  • Through the vim editor, fill in the mkhaha file: echo hahahahaha

After completing the above operations, switch the working directory at will and try to execute the mkhaha command. You will find that it cannot be executed.

Modify the value of PATH

Temporarily modify PATH: export PATH=$PATH:/home/itheima/myenv, execute mkhaha again, and it can be executed no matter where it is.

Or fill in export PATH=$PATH:/home/itheima/myenv into the user environment variable file or system environment variable file.

4.11 Upload, download

We can use the FinalShell tool to easily exchange data with the virtual machine. In the lower form of the FinalShell software, the file system view of Linux is provided, which can be conveniently: 1.  Browse the file system to find the appropriate file, right-click to download , and then transfer it to the local computer 2. Browse the file system to find the appropriate file directory, expand the local computer files into it, and you can easily upload data to Linux.

Of course, in addition to transferring files through the lower form of FinalShell, you can also transfer files through the rz and sz commands .

The rz and sz commands need to be installed. You can install them through: yum -y install lrzsz .

  • rz command to upload, syntax: just enter rz directly.
  • sz command to download, syntax: sz file to download. The file will be automatically downloaded to the:fsdownload folder on the desktop.

Note that the rz and sz commands require terminal software support to run properly. Common terminal software such as FinalShell, SecureCRT, and XShell all support this operation.

4.12 Compression and decompression

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

Commonly used software in Windows systems, such as winrar, bandizip and other software, all support various common compression formats, which will not be discussed here. We now want to learn how to operate in the Linux system: tar, gzip, and zip are the three compression formats to complete file compression and decompression operations.

tar command

There are two compression formats commonly used in Linux and Mac systems, and the suffix names are:

  • .tar , called tarball, is an archive file that simply assembles files into a .tar file. It does not reduce the file size much, it is just a simple encapsulation.
  • .gz , also commonly known as .tar.gz, gzip format compressed files, that is, using the gzip compression algorithm to compress the files into one file, which can greatly reduce the compressed volume.

For both formats, you can use the tar command to perform compression and decompression operations.

grammar:

  1.  -c, creates a compressed file, used in compression mode
  2. -v, displays the compression and decompression process, used to view the progress
  3. -x, decompression mode
  4. -f, the file to be created, or the file to be decompressed, the -f option must be the last one among all options
  5. -z , gzip mode , if -z is not used, it is an ordinary tarball format (gzip mode will compress, otherwise it will be simply encapsulated )
  6. -C, select the decompression destination for decompression mode

tar command compression:

  • 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 file
  • tar -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.

Note: If the -z option is used, it is usually the first option; the -f option must be the last option.

tar decompression:

Commonly used tar decompression combinations include

  • tar -xvf test.tar Decompress test.tar and extract the file to the current directory
  • tar -xvf test.tar -C /home/itheima Decompress 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)

Note: The -f option must be the last digit in the option combination;

           -z option, recommended at the beginning;

           Use the -C option alone, separately from other parameters required for decompression.

zip command to compress files

zip compression

You can use the zip command, and the compressed file is a zip compressed package.

grammar:

 -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 file.
  • zip -r test.zip test itheima a.txt Compress the test and itheima folders and the a.txt file into the test.zip file.

unzip unzip

Use the unzip command to easily decompress the zip archive.

grammar:

  •  -d, specifies the location to decompress , the same as tar's -C option
  • Parameters, the decompressed zip package 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)

Guess you like

Origin blog.csdn.net/m0_49687898/article/details/131424373