A Linux command with sufficient front end

Posted by huntrguy102 on Wed, 22 Dec 2021 02:18:42 +0100

preface

After translating the official documents of TypeScript, I used VuePress to build a blog and realized the automatic deployment of GitHub and Gitee Pages, but I finally decided to build my own website. In the process of building the website, linux commands are indispensable. Therefore, this article writes a basically sufficient Linux command, which will cover various commands used in blog building series, It is convenient for inquiry and learning.

0. Owner,Group,Others,Root

Linux system is a multi-user system, which divides file visitor identities into three types:

File Owner

When creating a user, Linux will create a home directory for the user. The path is / home / < username >. We can use cd ~, and quickly enter the home directory. If you want to put a private file, you can put it in your home directory, and then set it to be viewed only by yourself.

Group

Each user has a user group to assign permissions to a group of people when multiple people operate. When creating a user, a user group with the same name will be automatically created.

If a user belongs to multiple groups at the same time, the user needs to switch between user groups to have the permissions of other user groups.

Others

Users who are neither the file owner nor members of the group to which the file belongs are others.

Super user (Root)

Root users are a special class of users who can access all files.

1. adduser adds a user and passwd changes the password

# Add a user named git
adduser git
# Set the password of git user
passed git

However, due to the low permissions of the created user, sometimes we need to raise permissions for the user. At this time, we can do the following:

# The sudoers configuration file opens
sudo visudo

Note that it is also necessary to edit the sudoers configuration file. Using this command is safer than using sudo vim /etc/ sudoers. In addition to checking the syntax, it will also lock the file during multi-user editing.

After opening the sudoers configuration file, we add the following line of configuration:

# Allow git to run any commands anywhere
git ALL=(ALL:ALL) ALL 

Briefly explain the following sentence git ALL=(ALL:ALL) ALL:

  • git represents the user name of the rule application
  • The first ALL indicates that the rule applies to ALL hosts
  • The second ALL indicates that the rule applies to ALL users
  • The third ALL indicates that the rule applies to ALL groups
  • The fourth ALL indicates that the rule applies to ALL commands

After we save and exit, git users will get root privileges.

2. ls list files and directories

  1. ls lists files and directories
[root@iZ2ze learn-typescript.git]# ls
branches  config  description  HEAD  hooks  index  info  objects  refs
  1. ls -la consists of - a showing all files and directories (including hidden) and - l showing a detailed list:
[root@iZ2ze learn-typescript.git]# ls -la
 Total consumption 20
drwxrwxr-x  7 git git  132 12 December 15:33 .
drwx------  3 git git  127 12 May 15-14:51 ..
drwxrwxr-x  2 git git    6 12 December 15:21 branches
-rw-rw-r--  1 git git   66 12 December 15:21 config
-rw-rw-r--  1 git git   73 12 December 15:21 description
-rw-rw-r--  1 git git   23 12 December 15:21 HEAD
drwxrwxr-x  2 git git 4096 12 May 15-13:10 hooks
-rw-rw-r--  1 git git  217 12 December 15:33 index
drwxrwxr-x  2 git git   21 12 December 15:21 info
drwxrwxr-x 10 git git   90 12 December 15:33 objects
drwxrwxr-x  4 git git   31 12 December 15:21 refs

Each row has seven columns. Let's take branches as an example to explain the meaning of each column:

drwxrwxr-x2gitgit612:21, December 15branches
File type and permission informationNumber of links or first level subdirectoriesownerGroupFile size in bytesLast modification timefile name

Focus on the contents of column 1. Take drwxrwxr-x as an example. There are 10 bits in total. The first bit represents the file type, where - represents the ordinary file and d represents the directory file.

Bits 2 to 4 indicate the owner's permission, where r indicates read permission, w indicates write permission, x indicates executable permission, - indicates no permission, and bits 2 to 5 are rwx, indicating that the owner is readable, writable and executable.

Bits 5 to 7 indicate group user permissions, which is also rwx here.

Bits 8 to 10 indicate other user permissions. Here is r-x, indicating that there is readable and executable permission and no write permission.

Here is an additional point:

The default permissions for creating folders like root are rwxr-xr-x:

[root@iZ2ze www]# mkdir test
[root@iZ2ze www]# ls -l

drwxr-xr-x  2 root root  6 12 July 17-23:53 test

The default permission for creating files is rw-r--r --. Note that the x permission will be removed by default when creating files:

[root@iZ2ze www]# touch index.html
[root@iZ2ze www]# ls -l

-rw-r--r--  1 root root  0 12 July 17-23:54 index.html

This is why we sometimes need to add execution permissions after creating files.

3. chown can change the file owner or the file group at the same time

chown (change owner) syntax:

# -R: Recursively change the file group
chown [–R] Master name file name
chown [-R] Owner name: the name of the owner group and the file name

Index Change the owner of HTML to git:

[root@iZ2ze www]# chown git index.html
[root@iZ2ze www]# ls -

-rw-r--r-- 1 git  root  0 12 July 17-23:54 index.html

Index The owner and group of HTML are changed to git:

[root@iZ2ze www]# chown git:git index.html
[root@iZ2ze www]# ls -l

-rw-r--r-- 1 git  git   0 12 July 17-23:54 index.html

4. chmod change file permissions

In addition to r w x, permissions can also be expressed in numbers. The corresponding relationship between array and letter is as follows:

  • r:4
  • w:2
  • x:1

For example, if we want a file to be readable and writable, we can easily set the permission to 6 (4 + 2). Similarly, if we know that a permission is 3, we can also deduce that the permission is writable and executable, because only 2 + 1 can be equal to 3

Let's look at the specific syntax of chmod (change mode):

# -R: Recursively change the file group
chmod [-R] xyz File or directory

Where xyz represents the permissions of Owner, Group and Others respectively. If we set the permissions of a file as follows:

chomd 750 index.html

We can know that the Owner's permission is 7, which is readable, writable and executable, the Group's permission is 5, which is readable and executable, and the Others' permission is 0, which means that it is not readable, writable and executable. The corresponding letters are: rwxr-x ---.

In addition to this numerical method, there is another way to change permissions using symbol types:

In this way, we abbreviate the three identities Owner, Group and Others as u (User), g and o respectively, use a to represent all identities, and then use + - = to represent adding, removing and setting a permission, and r w x to continue to represent reading, writing and executing permissions, for example:

chomd u+x,g-x,o-x index.html

This means that the Owner adds the execution permission, and the Group and Others remove the execution permission.

Of course, we can also set permissions directly

chmod u=rwx,g=rx,o=r index.html

At this point, the permissions of the file are equivalent to -rwxr-xr--.

In addition, we can omit the content of identity such as ugoa and write it directly:

chmod +x index.html

At this time, it is equivalent to using a, and execution permission will be added to all identities.

5. su switch identities

# Switch to git user
su git

6. whoami displays the user name

# whoami 
root

7. pwd display current directory

[git@iZ2ze www]$ pwd
/home/www

9. cd switch working directory

# Go to / home/www/
cd /home/www

# Enter your home directory
cd ~

# Enter the upper two layers of the current directory:
cd ../..

10. mkdir create directory

  1. mkdir create directory:
mkdir new_folder
  1. mkdir -p create directory recursively:
mkdir -p one/two/three

11. Create a file with touch

It is used to modify the time attribute of a file or directory. When the file does not exist, the system will create a blank file

touch new_file

12. echo printout

echo is a Shell command for printout:

# Show escape characters
echo "\"test content\""

Create or overwrite a file with "test content":

echo "test content" > index.html

If you want to add content, use > >:

[root@iZ2ze www]# echo "test content" > index.html
[root@iZ2ze www]# cat index.html
test content
[root@iZ2ze www]# echo "test content" >> index.html
[root@iZ2ze www]# cat index.html
test content
test content

13. Connect the cat file and print it out

View file contents:

cat ~/.ssh/id_rsa.pub

Empty index HTML content:

cat /dev/null > index.html

Index The contents of HTML are written to second html:

cat index.html > second.html

Index The content of HTML is appended to second html:

cat index.html >> second.html

Index HTML and second HTML append write third html:

cat index.html second.html >> third.html

14. cp copy files or directories

Copy all files in the directory website / to the new directory static:

# -r: If the given source file is a directory file, all subdirectories and files in the directory will be copied.
cp –r website/ static

15. mv move and rename

File rename:

mv index.html index2.html

Hide files:

# Add a to the file name
mv index.html .index.html

Move files:

# Just move
mv  /home/www/index.html   /home/static/
# Move and rename
mv /home/www/index.html   /home/static/index2.html

Batch move:

mv  /home/www/website/*  /home/www/static

16. rm delete a file or directory

# The system will ask
rm file

# -f means direct deletion
# -r indicates that all files in the directory are deleted

# Delete all files and directories in the current directory
rm -r  * 

# make off with money
rm -rf /*

17. vi/vim

Linux built-in vi document editor, Vim is a text editor developed from vi.

Basically, vi/vim is divided into three modes: Command mode, Insert mode and Last line mode. We will introduce these three modes while operating:

We execute VIM index HTML, if there is no such file, the file will be created:

vim index.html

The interface is:

This is the command mode. In the command mode, any character entered will be regarded as a command. The following common commands:

  • i switch to input mode.
  • x deletes the character at the current cursor.
  • : switch to the bottom line command mode.

Press i to enter the input mode:

In input mode, there is -- INSERT -- flag in the lower left corner:

At this time, we can make various inputs. After input, press ESC to return to the command mode:

At this time, the INSERT in the lower left corner has disappeared. If we want to save and exit, we first enter:, and enter the baseline command mode:

In the baseline command mode, common commands are:

  • w save file
  • q exit program

We enter wq to save and exit, and then we will find and create an HTML file.

18. ssh Remote connection tool

Note that ssh listening is port 22.

The basic syntax is:

ssh [OPTIONS] [-p PORT] [USER@]HOSTNAME [COMMAND]

Listening port example:

ssh -p 300 git@8.8.8.8

Turn on debug mode:

# -v verbose mode, print debugging information about operation
ssh -v git@8.8.8.8

Series articles

Serial article directory address: https://github.com/mqyqingfeng/Blog

Wechat: "mqyqingfeng", add me to Yu Yu's only reader group.

If there is any mistake or lack of preciseness, please be sure to correct it. Thank you very much. If you like it or have some inspiration, welcome star, which is also an encouragement to the author.

Topics: Javascript Front-end Linux shell bash