Home >Operation and Maintenance >Linux Operation and Maintenance >Introduction to basic knowledge of Linux (essential for backend)

Introduction to basic knowledge of Linux (essential for backend)

青灯夜游
青灯夜游forward
2019-11-23 16:51:202643browse

This article will introduce to you the basic Linux knowledge necessary for back-end programmers. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Introduction to basic knowledge of Linux (essential for backend)

Before learning Linux, let’s first briefly understand the operating system.

Start with understanding the operating system

1.1 Introduction to the operating system

I introduce what operating system is through the following four points:

  • The operating system (Operation System, referred to as OS) is a program that manages computer hardware and software resources. It is the core and cornerstone of the computer system. ;
  • The operating system is essentially a software program running on the computer;
  • Provides an operating interface for users to interact with the system;
  • The operating system is divided into kernel and shell (we can understand the shell as the application program surrounding the kernel, and the kernel is the program that can operate the hardware).

Introduction to basic knowledge of Linux (essential for backend)

##1.2 Simple classification of operating systems

Windows: Currently the most popular personal desktop operating system, everyone knows it without much introduction.

Unix: The earliest multi-user, multi-tasking operating system. According to the classification of operating systems, it is a time-sharing operating system. Unix is ​​mostly used on servers, workstations, and now also on personal computers. It plays a very important role in creating the Internet, computer network or client/server model.

Introduction to basic knowledge of Linux (essential for backend)

Linux: Linux is a Unix-like operating system that is free to use and spread freely. There are many different Linux versions of Linux, but they all Using Linux kernel. Linux can be installed on a variety of computer hardware devices, such as cell phones, tablets, routers, video game consoles, desktop computers, mainframes, and supercomputers. Strictly speaking, the word Linux itself only refers to the Linux kernel, but in fact people are accustomed to using Linux to describe the entire operating system based on the Linux kernel and using various tools and databases of the GNU Project.

Introduction to basic knowledge of Linux (essential for backend)

2 A first look at Linux

##2.1 Introduction to LinuxWe have already introduced Linux above, and we only emphasize three points here.

    Unix-like system:
  • Linux is a free, open source Unix-like operating system
  • Linux kernel:
  • Strictly speaking Say, the word Linux itself only means the Linux kernel
  • The father of Linux:
  • A legendary figure in the field of programming. He is the earliest author of the Linux kernel, and later initiated this open source project. He serves as the chief architect and project coordinator of the Linux kernel. He is one of the most famous computer programmers and hackers in the world today. He also initiated the Git open source project and is the main developer.

Introduction to basic knowledge of Linux (essential for backend)

2.2 Introduction to the birth of Linux##In 1991, amateur computers in Finland Enthusiast Linus Torvalds wrote a system similar to Minix (a Unix-like operating system based on a microkernel architecture) and was named Linux by the ftp administrator. It was added to the GNU Project of the Free Software Foundation;

    Linux started with a As a symbol, the cute penguin symbolizes courage and love for life.
2.3 Classification of Linux

Linux is divided into two types according to the degree of nativeness:

Kernel version:
    Linux is not an operating system. Strictly speaking, Linux is just the kernel in an operating system. What is the kernel? The kernel establishes a communication platform between computer software and hardware. The kernel provides system services, such as file management, virtual memory, device I/O, etc.;
  1. Release version:
  2. Some organizations or companies A re-released version based on secondary development of the kernel version. There are many types of Linux distributions (ubuntu and CentOS are widely used, and beginners are advised to choose CentOS), as shown in the figure below:

Introduction to basic knowledge of Linux (essential for backend)

Three Overview of Linux File System

3.1 Introduction to Linux File System

In the Linux operating system, all resources managed by the operating system, such as network interface cards, disk drives, printers, input and output devices, ordinary files or directories are regarded as a file.

That is to say, there is an important concept in the LINUX system: Everything is a file. In fact, this is a manifestation of the UNIX philosophy, and Linux was rewritten from UNIX, so this concept has been passed down. In UNIX systems, all resources are regarded as files, including hardware devices. The UNIX system treats each piece of hardware as a file, usually called a device file, so that users can access the hardware by reading and writing files.

3.2 File Types and Directory Structure

Linux supports 5 file types:

Introduction to basic knowledge of Linux (essential for backend)

The directory structure of Linux is as follows:

The Linux file system has a distinct structure, like an inverted tree, with the top level being its root directory:

Introduction to basic knowledge of Linux (essential for backend)

Common directory description:

  • /bin: Stores binary executable files (ls, cat, mkdir, etc.), commonly used commands are generally here;
  • /etc: stores system management and configuration files;
  • /home: The root directory where all user files are stored is the base of the user's home directory. For example, the home directory of user user is /home/user, which can be represented by ~user;
  • /usr: Used to store system applications;
  • /opt: The location where additional installed optional application packages are placed. Under normal circumstances, we can install tomcat, etc. here;
  • /proc: The virtual file system directory is the mapping of system memory. You can directly access this directory to obtain system information;
  • /root: The home directory of the super user (system administrator) (privileged class ^o^);
  • /sbin: Stores binary executable files that can only be accessed by root. Stored here are system-level management commands and programs used by system administrators. Such as ifconfig, etc.;
  • /dev: Used to store device files;
  • /mnt: The installation point where the system administrator installs the temporary file system , the system provides this directory to allow users to temporarily mount other file systems;
  • /boot: Stores various files used during system boot;
  • /lib: Stores library files related to system operation;
  • /tmp: It is used to store various temporary files and is a public temporary file storage point;
  • /var: Used to store files that need to change data during operation. It is also the overflow area of ​​some large files, such as log files of various services (system startup logs, etc.), etc. ;
  • /lost found: This directory is usually empty. The system shuts down abnormally and leaves a "homeless" file (what is it called .chk under Windows) right here. .

4 Basic Linux Commands

The following are just some of the more commonly used commands. I recommend a Linux command quick check website, which is very good. If you forget some commands or don’t understand some commands, you can get solutions here.

Linux command list: http://man.linuxde.net/

4.1 Directory switching command

  • cd usr Switch to the usr directory under this directory
  • ##cd .. (or cd../) Switch to the upper level directory
  • ##cd / Switch to the system root directory
  • cd ~ Switch to the user’s home directory
  • cd - Switch to the previous directory

4.2 Directory operation commands (add, delete, modify, check)

1. mkdir directory name: Add directory

2. ls or ll (ll is the abbreviation of ls -l, the ll command can be used to see the detailed information of all directories and files in the directory): View directory information

3. Find directory parameters : Find directory (check) Example:

  • List all files and folders in the current directory and subdirectories: find .
  • Find in the /home directory and end with .txt File name: find /home -name "*.txt"
  • Same as above, but ignore case: find /home -iname "*.txt"
  • Find all files ending with .txt and .pdf in the current directory and subdirectories: find . \( -name "*.txt" -o -name "*.pdf" \)or find . -name "*.txt" -o -name "*.pdf"

4, mv directory name new directory name: Modify the name of the directory (change)

Note: The syntax of mv can not only rename the directory but also rename various files, compressed packages, etc. The mv command is used to rename files or directories, or move files from one directory to another. Another usage of the mv command will be introduced later.

5. mv directory name new location of directory : Move directory location --- cut (change)

Note: mv syntax Not only can you cut directories, but you can also cut files and compressed packages. In addition, the results of mv and cp are different. mv seems to have "moved" the files, and the number of files has not increased. When cp copies files, the number of files increases.

6. cp -r directory name directory copy target location : Copy directory (change), -r represents recursive copy

Note: The cp command can not only copy directories but also files, compressed packages, etc. When copying files and compressed packages, there is no need to write -r recursively

7, rm [-rf] directory :Delete directory (delete)

Note: rm can not only delete directories, but also delete other files or compressed packages. In order to enhance everyone’s memory, no matter you delete any directory or file, use it directlyrm -rf Directory/file/compressed package

4.3 File operation commands (add, delete, modify and check)

  1. touch file name: File creation (increment)
  2. ##cat/more/less/tail file name File viewing (check)

    • cat: Only the last screen content can be displayed
    • more can display the percentage, enter can go down a line, space can go down a page, q can exit the view
    • less You can use PgUp and PgDn on the keyboard to page up and down, q to end viewing
    • tail-10: View the last 10 lines of the file, Ctrl C to end
Note: The command tail -f file can dynamically monitor a certain file, such as the tomcat log file. The log will change as the program runs. You can use tail -f catalina-2016-11-11.log Monitor file changes

  1. vim file: Modify the content of the file (change)vim editor is a powerful component in Linux and an enhanced version of the vi editor. There are many commands and shortcuts for the vim editor, but they are not explained here one by one. You do not need to study them thoroughly. Use vim to edit and modify You just need to be able to use the file method basically.

    In actual development, the main function of using the vim editor is to modify the configuration file. The following are the general steps:

    vim file ------>Enter File----->Command mode------>Press i to enter editing mode----->Edit file------->Press Esc to enter bottom line mode--- -->Enter: wq/q! (Enter wq to write the content and exit, that is, save; enter q! to force exit without saving.)

  2. ##rm - rf file: Delete file (delete)Delete in the same directory: Memorize rm -rf

    file

4.4 Operation commands for compressing files

#1) Pack and compress files:

Packaged files in Linux are generally .tar At the end, compression commands generally end with .gz.

Generally, packaging and compression are performed together, and the suffix name of the packaged and compressed file is generally .tar.gz.

Command:


tar -zcvf Pack the compressed file name to pack the compressed file Among them:
z: Call the gzip compression command for compression

c: Package file

v: Display the running process

f: Specify the file name

For example: There are three files added to the test directory: aaa.txt bbb.txt ccc.txt, if we want to package the test directory and specify the compressed package name as test.tar.gz, we can use the command:

tar -zcvf test.tar.gz aaa. txt bbb.txt ccc.txt or: tar -zcvf test.tar.gz /test/

2) Unzip the compressed package:

Command: tar [-xvf] compressed file

Among them: x: represents decompression

Example:

1 To decompress test.tar.gz under /test to the current directory, you can use the command: tar -xvf test.tar.gz

2 Extract test.tar.gz under /test to the root directory /usr: tar -xvf xxx.tar.gz -C /usr (-C represents the specified decompression location )

4.5 Linux permission commands

Every file in the operating system has specific permissions, users and groups to which it belongs. Permissions are a mechanism used by the operating system to restrict resource access. Permissions in Linux are generally divided into three groups: readable, writable and excutable. Corresponding to the owner, group and other users of the file respectively, this mechanism is used to limit which users and which groups can perform what operations on specific files. Through the ls -l command we can view the permissions of files or directories in a certain directory

Example: In any directoryls -l

Introduction to basic knowledge of Linux (essential for backend)

The information in the first column is explained as follows:

Introduction to basic knowledge of Linux (essential for backend)

The file types will be explained in detail below. , what are the permissions and file owners, groups and other groups in Linux?

Type of file:

  • d: Represents directory
  • -: Represents file
  • l: Represents link ( It can be considered as a shortcut in window)

Permissions in Linux are divided into the following types:

  • r: represents readable permissions, r can also be represented by the number 4
  • w: represents the permission is writable, w can also be represented by the number 2
  • x: represents the authority is executable, x can also be represented by the number 1

The difference between file and directory permissions:

For files and directories, read and write execution have different meanings.

For files:

Permission name Executable operations
r You can use cat to view the contents of the file
w You can modify the contents of the file
x Can run it as a binary file

For directory:

Permissions Name Executable operations
r You can view the list under the directory
w You can create and delete files in the directory
x You can use cd to enter the directory

Every user in Linux must belong to a group and cannot be independent from outside the group. In Linux, each file has the concepts of owner, group, and other groups.

  • Owner

    is generally the creator of the file. Whoever creates the file automatically becomes the owner of the file. Use ls-ahl You can use the command to see the owner of the file. You can also use chown username filename to modify the owner of the file.

  • The group in which the file is located

    When a user creates a file, the group in which the file is located is the group in which the user is located. Use the ls-ahl command to see All groups of the file can also use chgrp group name file name to modify the group where the file is located.

  • Other groups

    Except for the owner of the file and the user in the group, all other users of the system are other groups of the file

Let’s take a look at how to modify the permissions of files/directories.

Command to modify the permissions of files/directories: chmod

Example: Modify the permissions of aaa.txt under /test to the owner All permissions, the owner's group has read and write permissions,
Other users only have read permissions

chmod u=rwx,g=rw,o=r aaa.txt

Introduction to basic knowledge of Linux (essential for backend)

The above example can also be represented by numbers:

chmod 764 aaa.txt

Add one Commonly used things:

If we install a zookeeper, what should we do if we ask it to start automatically every time we turn on the computer?

  1. Create a new script zookeeper
  2. Add executable permissions to the newly created script zookeeper. The command is:chmod x zookeeper
  3. Change the zookeeper The script is added to the startup items. The command is: chkconfig --add zookeeper
  4. If you want to see if the addition is successful, the command is: chkconfig --list

4.6 Linux User Management

Linux system is a multi-user, multi-tasking time-sharing operating system, any one needs to use system resources All users must first apply for an account from the system administrator, and then enter the system as this account.

On the one hand, user accounts can help system administrators track users who use the system and control their access to system resources; on the other hand, they can also help users organize files and provide security protection for users. .

Linux user management related commands:

  • useradd option username: add user account
  • userdel option username: delete user account
  • usermod option username: modify account
  • passwd username: change or create user Password
  • passwd -S username: Display user account password information
  • passwd -d username: Clear user password

The useradd command is used to create new system users in Linux. useradd can be used to create user accounts. After the account is created, use passwd to set the password for the account. You can use userdel to delete the account. The account created using the useradd command is actually saved in the /etc/passwd text file.

The passwd command is used to set user authentication information, including user password, password expiration time, etc. System administrators can use it to manage system user passwords. Only administrators can specify user names, and general users can only change their own passwords.

4.7 Linux system user group management

Each user has a user group, and the system can manage all users in a user group Carry out centralized management. Different Linux systems have different regulations on user groups. For example, a user under Linux belongs to a user group with the same name. This user group is created at the same time when the user is created.

The management of user groups involves the addition, deletion and modification of user groups. The addition, deletion and modification of groups are actually updates to the /etc/group file.

Commands related to the management of Linux system user groups:

  • groupadd option user group: Add a new user group
  • groupdel User group: To delete an existing user group
  • groupmod option User group: Modify the attributes of the user group

4.8 Other commonly used commands

  • pwd Display the current location
  • grep The string to be searched The file to be searched--color: Search command, --color represents highlighting
  • ps -ef/ps aux: These two Both commands view the current running processes of the system. The difference between the two is that the display format is different. If you want to view a specific process, you can use this format: ps aux|grep redis (View the process including the redis string)

    Note: If you use ps directly (( Process Status)) command will display the status of all processes. It is usually combined with the grep command to view the status of a certain process.

  • kill -9 pid of the process: Kill the process (-9 means forced termination.)

    First use ps to find the process, and then use kill kill

  • Network communication command:

    • View the network card information of the current system: ifconfig
    • View and The connection status of a certain machine: ping
    • Check the port usage of the current system: netstat -an
  • shutdown shutdown -h now: Specify an immediate shutdown now; shutdown 5 "System will shutdown after 5 minutes": Specify a shutdown after 5 minutes and send a warning message to the logged in user.
  • reboot reboot Reboot. reboot -w Make a simulation of restarting (only recording will not actually restart).

For more Linux knowledge, please visit the Linux Tutorial column!

The above is the detailed content of Introduction to basic knowledge of Linux (essential for backend). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete