This is the 13th day of my participation in Gwen Challenge

One, foreword

This article is the basics of Linux that a Java backend developer must master. Divided into

  • Linux overview
  • Basic Linux commands
  • The file to view
  • Vim is introduced
  • Account management
  • User Group Management
  • Disk management
  • Process management
  • Software installation

Introduction to Linux

2.1 an overview of the

The Linux kernel was originally written by Linus Torvalds, a Finn, as a hobby while he was a student at the University of Helsinki.

Linux is a unix-like operating system that is free to use and freely distributed. It is a multi-user, multi-task, multi-threaded and multi-CPU operating system based on POSIX (Portable Operating system Interface) and Unix.

Linux runs major UNIX tools, applications, and network protocols. It supports both 32-bit and 64-bit hardware. Linux inherits the design idea of Unix taking network as the core, and is a stable multi-user network operating system.

The 2.2 release

At present, the well-known distribution in the market are: Ubuntu, RedHat, CentOS, Debian, Fedora, SuSE, OpenSUSE, Arch Linux, SolusOS, etc.

2.3 Linux application domain

Today Linux distributions are used in everything from embedded devices to supercomputers and have established themselves in the server world, Usually the server uses LAMP (Linux + Apache + MySQL + PHP) or LNMP (Linux + Nginx+ MySQL + PHP) combination.

Linux is now used not only in homes and businesses, but also in government.

The Linux 2.4

In the Linux philosophy, everything is a file. Linux file systems start at the root directory and are divided into subdirectories:

  • /bin: bin is short for Binary. This directory houses the most frequently used commands.
  • /boot: This store is used to start Linux core files, including some connection files and image files.
  • /dev: Dev is short for Device. It stores Linux external devices. In Linux, the way to access devices is the same as the way to access files.
  • /etc: this directory is used to store all configuration files and subdirectories required by system management.
  • /home: the user’s home directory. In Linux, each user has a directory named after the user’s account.
  • /lib: This directory stores the system’s most basic dynamic connection shared library, which functions like DLL files in Windows. (Don’t move)
  • /lost+found: This directory is usually empty. When the system is shut down illegally, some files are stored here.
  • /media: The Linux system automatically identifies certain devices, such as USB flash drives and CD/DVD drives. After identifying these devices, Linux will mount them to this directory.
  • / MNT: The system provides this directory for users to temporarily mount other file systems, we can mount the CD-ROM drive to/MNT /, and then enter this directory to view the contents of the cd-rom drive.
  • /opt: This is the directory for installing additional software on the host. For example, if you install an ORACLE database, you can put it in this directory. The default is empty.
  • /proc: This directory is a virtual directory that is a map of system memory and can be accessed directly to obtain system information.
  • /root: This directory is the home directory of the system administrator, also known as the super permission user.
  • /sbin: s stands for Super User, which stores the system management program used by the system administrator.
  • / SRV: This directory stores data that needs to be extracted after the service is started.
  • /sys: This is a big change to the Linux2.6 kernel. This directory installed a new file system, SYSfs, in the 2.6 kernel.
  • / TMP: This directory is used to store temporary files. (Installation package)
  • /usr: This is a very important directory that houses many of the user’s applications and files, similar to the Program Files directory on Windows.
    • /usr/bin: application used by system users.
    • /usr/sbin: advanced hypervisors and system daemons used by superusers.
    • /usr/src: the default directory where the kernel source code is placed.
  • /var: This is the directory where things are constantly being expanded. We are used to putting directories that are constantly being modified in this directory. Includes various log files.
  • /run: is a temporary file system, and the information since the storage system was started. Files in this directory should be deleted or cleared when the system restarts.
  • / WWW: Save server related resources (self-built)

Linux basic commands

3.1 Command Format

cmd [options] [arguments]

Command option parameters

3.2 the wildcard

The Linux wildcards are as follows:

  • * : matches any character and any number of characters
  • ? : matches any character of a single number
  • [] : Matches any character within []
  • [!] : indicates not, and matches any character except the one in parentheses

3.3 Common Directory Commands

3.3.1 LS Lists Directories (Colors distinguish files and folders)
  • -a: View all files, including hidden files
  • -l: lists all files, including their attributes and permissions, excluding hidden files
  • All Linux can be combined:
ls -al
Copy the code
3.3.2 CD Switching a Directory

Absolute paths start with /, relative paths, how to find the current directory.. /. /

3.3.3 PWD Displays the current directory
3.3.4 mkdir Creating a folder
  • Mkdir -p: creates folders recursively

    mkdir -p test1/test2
    Copy the code
3.3.5 RMdir Deleting a directory
  • Rmdir -p: Deletes a hierarchical directory

    rmdir -p test1/test2/test3
    Copy the code
3.3.6 cp Copying a file or directory
[root@www ~]# cp [-adFilprsu] Source file destination file [root@www ~]# cp [options] source1 source2 source3.... directoryCopy the code
3.3.7 Rm File or Directory
  • -f stands for force. If a file does not exist, no warning message will be displayed
  • -i: In interactive mode, the system asks whether the user is deleted
  • -r: indicates recursive deletion. The most commonly used directory deletion is dangerous
3.3.8 Moving a File or Directory
[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory
Copy the code
  • -f: force Indicates the force. If the target file already exists, the system overwrites the file directly.
  • -i: If the destination file already exists, the system asks whether to overwrite it.
  • -u: Update only if the target file already exists and the source is new

The move process can be renamed:

[root@kuangshen home]# mv test mvtest
[root@kuangshen home]# ls
mvtest
Copy the code
3.3.9 touch Creating a file

3.4 Viewing File Contents

  • Cat displays the contents of the file starting from the first line. Cat can also be used to clear the contents of the file
 cat /dev/null >nohup.out 
# cat nohup.out 
Copy the code
  • Tac is displayed from the last line, and you can see that TAC is written cat backwards!
  • Nl display when, incidentally output line number!
  • Display file contents page by page (exit Q)
  • Less is similar to more, but better than more, it can move on!
  • Head only reads the first few lines
  • Tail only looks at the number of lines on the tail

3.5 Basic Attributes

3.5.1 Understanding File Properties

Linux system is a typical multi-user system, where different users are in different positions and have different permissions. We can check by using ls -l command

A. In Linux, the first character indicates whether the file is a directory, file, link file, etc.

  • [d] indicates a directory
  • [-] is a file.
  • If [L], it is a link file.
  • If [b] represents a storage interface device (random access device) in the device file;
  • [c] indicates a serial port device in a device file, such as a keyboard or mouse (a one-time reader device).

B. In the following characters, the three characters are a group and all are combinations of the three parameters of [RWX]. The position of these three will not change, if there is no permission, the minus sign will appear [-]

  • 【 R 】 stands for readable
  • 【 W 】 stands for write
  • 【x】 stands for execute.

A group of three, divided into ownership limits, ownership group permissions, what are the meanings of other user permissions?

With Linux, users are grouped, with a user belonging to one or more groups.

  • Ownership limit: is the permission of the file owner
  • Group permission: indicates the permission of the user in the same group as the file owner
  • Other user rights: Other users
3.5.2 Modifying File Properties
  • CHGRP: Changes the file owner group

    • CHGRP [-r] Indicates the name of the genus group
    • If the -r argument is added, the ownership group of all files in the directory changes
  • Chown: Changes the file owner or the file owner group. Commonly used!

    • Grammar:

      Chown [-r] Indicates the name of the owner group. Chown [-r] Indicates the name of the owner groupCopy the code
  • Chmod: Changes nine attributes of a file. Commonly used!

    • File attributes can be set in two ways: symbols and numbers.

    • Reference table of permission symbols:

      • R: 4
      • W: 2
      • X: 1.
    • RWX =4+2+1; RWX =4+2+1; Chmod -r 770 = 0

    • In Linux, user, group, others represent three groups of permissions, and A represents all. The abbreviation stands for U, G, O, and A. Modify permissions separately:

      [root@lei ~]# chmod u= RWX,g=rx,o=r test [root@lei ~]# ls-al test -rwxr-xr--Copy the code

3.6 Concepts of Linux links

There are two types of Linux links: hard links and soft links.

  • Hard link: A->B. Suppose B is A hard link of A, the two points to the same file, allowing A file to have multiple paths. Users can use this mechanism to establish hard links to some important files to prevent deletion.
  • Soft connection: Similar to the shortcut under Windows, delete the source file, the shortcut can not access

Iv. Introduction to Vim

Vim, a text editor developed from VI, is a prerequisite for operating Linux. With code completion, compilation and error jump and other convenient programming functions, is widely used.

Divided into three modes:

  • Command mode
  • The input mode
  • Baseline command mode

4.1 Command Mode

Vim goes into the edit file,

  • Enter I to enter the input mode
  • Enter: to enter the baseline command mode

In command mode, enter: / character string

For example, to search for user, type /user:

  • 1. After pressing Enter, you can see that Vim has moved the cursor to the character and highlighted the matching string

  • 2. To see the next match, press n(lowercase n)

  • 3. Jump to the previous match, press N (capital N)

4.2 Input Mode

Input mode is the mode in which we start editing the file. Some shortcuts are as follows:

  • Character key and Shift combination to enter characters
  • ENTER, ENTER, line feed
  • BACK SPACE deletes a character before the cursor
  • DEL, delete key, deletes a character after the cursor
  • Arrow keys to move the cursor in text
  • HOME/END, move the cursor to the beginning /END of the line
  • Page Up/Page Down
  • Insert, switch the cursor to input/replace mode and the cursor will change to vertical/underscore
  • ESC, exit input mode and switch to command mode

4.3 Baseline Command Mode

Enter the baseline command mode. The basic commands are as follows:

  • Q exit program
  • W save program
  • Press ESC to exit baseline command mode at any time

5. Account management

Linux is a time-sharing operating system with multi-user tasks. Any user who wants to use system resources must first apply for an account from the system administrator, and then use this account to access the system.

User accounts can be assigned different permissions to ensure system security.

5.1 User Account Management

User account management mainly involves adding, modifying, and deleting users

Adding a user account is to create a new account in the system and allocate resources such as the user name, user group, home directory, and login Shell to the new account

5.1.1 userAdd: Adds a user
Useradd Option user nameCopy the code
  • -c comment Specifies a comment.
  • -d directory Specifies the user’s home directory. If the home directory does not exist, use the -m option to create a home directory.
  • -g User group Indicates the user group to which the user belongs.
  • -g User group. User group Specifies the additional group to which the user belongs.
  • -m The user directory is automatically created if it does not exist.
  • -s Shell file Specifies the login Shell of the user.
  • -u User ID Indicates the user ID. If the -o option is available, the id of another user can be used repeatedly.
5.1.2 userdel Deletes a user
Userdel Option user nameCopy the code

To delete a user, you need to delete the user record from the /etc/passwd system file or delete the user’s home directory if necessary

The common command is -r. All it does is delete the user’s home directory.

5.1.3 usermod Modify the user and view the configuration file

Modify user properties, such as user name, home directory, user group, and login shell

Usermod Option User nameCopy the code
  • – a | – append # # appended to some group, the user only to use with the -g option

  • – c | – comment # # modify/etc/passwd file in paragraph 5 of the comment

  • – d | – home # # modify the user’s home directory is usually used with the -m option

  • The -e | – expiredate # # to specify the date of the user account is disabled, format YY – MM – DD

  • – f | – inactive # # user’s password expired after how many days will disable the account, 0 means to disable the account password has expired, 1 disables this feature, the default value is 1

  • – g | – gid # # modify user gid, reorganization must exist

  • – G | – groups # # appended to some group, the user only used with – a option

  • – l | – login # # modify the user login name

  • – the lock # # L | – lock the user’s password

  • – m | – move – home # # modify the user’s home directory is usually used with the -d option

  • -s | – shell # # modify the user’s shell

  • -u | – uid # # to modify the user’s uid, the uid must be unique

  • -u | – unlock # # to unlock the user’s password

5.1.4 Switching Users
  • 1. Run the following command to switch the user: su username
  • 2. Run the sudo su command to switch from a common user to root user
  • 3. Enter exit or logout on the terminal or use the shortcut CTRL + D to return to the original user. CTRL + D is also the exit command
  • 4. If you want to use the working environment of the new user after switching users, add – between su and username, for example, su – root.
  • $indicates a common user
  • # indicates the super user, that is, root user
5.1.5 Changing a User Password
Passwd usernameCopy the code

6. User group management

Each user has a user group. The system can centrally manage all users of a user (development, operation and maintenance, testing, and root). For example, users in Linux belong to a user group with the same name. This user group is created at the same time when users are created.

The addition, deletion, and modification of groups are essentially updates to the /etc/group file

6.1 Creating a user group groupadd

Run groupadd to create a group. After creating a user group, you can obtain an ID.

cat /etc/group
Copy the code

Groupadd -g Number: If this parameter is not specified, the number increases by 1

6.2 Deleting a User Group groupdel User group

6.3 Modifying the permission information and name of a user group groupmod

Groupmod Option User groupCopy the code
  • -g GID Specifies a new group ID for the user group.
  • The -o and -g options can be used at the same time. The new GID of a user group can be the same as that of an existing user group.
  • -n New User group Changes the name of the user group to the new name

7. Disk management

Df (lists the disk usage of the file system as a whole)

Du (Checking disk space usage)

Viii. Process management

Files, permissions. system

For us developers, Linux is actually more of a use.

In Linux, each program has its own process, and each process has an ID number.

For every process, there is a parent process.

General services are carried out in the background, and basic procedures are run in the foreground.

Ps Displays information about processes that are running in the system

Ps – xx:

  • -a: Displays information about all processes running on the terminal (current processes).
  • -u: displays user information about processes
  • -x: displays parameters of background processes
Ps - aux to see all the process of ps - aux | grep mysql ps - aux | grep redis ps - aux | grep Java # in Linux, this is called pipe, A | B grep: documents conform to the conditions of stringCopy the code
Ps - ef | grep mysql # at the parent process generally by a directory tree view pstree - p: show id - u: according to user groupCopy the code

Kill -9 Specifies the ID of a process (the process will be killed in an infinite loop).

Nine, centos software installation

1, the JDK

2, node. Js

3, Nginx

4, redis6

Mysql8 mysql8

Mysql8 mysql8

Ten, supplement

1. Copy part of the file to another file

head -n 100000 data.txt > sample.txt
Copy the code

2. Clear your files

Method 1. In the non-editing state, use the shortcut key GG to jump to the head of the first line, and then use dG. 2, cat /dev/null > /var/log/yum.log (replace the name of the content to be emptied)Copy the code

3. Check the file size

Du -h --max-depth=1 * du -h --max-depth=1 * du -h --max-depth=1 *Copy the code