7
头图

Preface

After translating the official TypeScript documentation, 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, and in the process of building a website, Linux commands must be used, so This article writes a basic enough Linux command, which will cover various commands used in the blog building series, which is convenient for query and learning.

0. Owner、Group、Others、Root

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

File owner (Owner)

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

Group

Each user has a user group, which is convenient for multiple people to assign permissions to a group of people. When a user is created, a user group with the same name as it is 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

A user who is neither the owner of the file nor a member of the group to which the file belongs, or someone else.

Super User (Root)

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

1. adduser add user and passwd change password

# 添加一个名为 git 的用户
adduser git
# 设置 git 用户的密码
passed git

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

# 会打开 sudoers 配置文件
sudo visudo

Note that you are also editing the sudoers configuration file. Using this command will be sudo vim /etc/ sudoers . In addition to checking the syntax, it will also lock the file when editing by multiple users.

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

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

Briefly explain this sentence git ALL=(ALL:ALL) ALL :

  • git represents the username to which the rule applies
  • The first ALL indicates that the rule is applied to all hosts
  • The second ALL indicates that the rule applies to all users
  • The third ALL indicates that the rule is applied to all groups
  • The fourth ALL indicates that the rule is applied to all commands

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

2. ls lists files and directories

  1. ls List 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
总用量 20
drwxrwxr-x  7 git git  132 12月 15 12:33 .
drwx------  3 git git  127 12月 15 14:51 ..
drwxrwxr-x  2 git git    6 12月 15 12:21 branches
-rw-rw-r--  1 git git   66 12月 15 12:21 config
-rw-rw-r--  1 git git   73 12月 15 12:21 description
-rw-rw-r--  1 git git   23 12月 15 12:21 HEAD
drwxrwxr-x  2 git git 4096 12月 15 13:10 hooks
-rw-rw-r--  1 git git  217 12月 15 12:33 index
drwxrwxr-x  2 git git   21 12月 15 12:21 info
drwxrwxr-x 10 git git   90 12月 15 12:33 objects
drwxrwxr-x  4 git git   31 12月 15 12:21 refs

Each row has 7 columns, we take branches as an example to explain the meaning of each column:

drwxrwxr-x2gitgit6December 15 12:21branches
File type and permission informationNumber of links or number of first-level subdirectoriesownerBelonging to the groupFile size in bytesLast Modifiedfile name

Focus on the content of the first column. Take drwxrwxr-x as an example. Here there are 10 digits in total, and the first digit indicates the file type, among which - indicates a normal file, and d indicates a directory file.

Bits 2 to 4 indicate owner permissions, among which r indicates read permission, w indicates write permission, x indicates executable permission, - indicates no permission, and the second to 5th digits are rwx , which indicates that the owner can read, write, and write. implement.

The 5th to 7th digits indicate the group user authority, which is also rwx here.

The 8th to 10th digits indicate the permissions of other users, here is r-x , which means that they have readable and executable permissions, but no write permissions.

Here is one additional point:

Like root , the default permission for users to create folders is rwxr-xr-x :

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

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

The default permission to create a file is rw-r--r-- . Note that creating a file will remove the x permission by default:

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

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

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

3. chown changes the file owner, and can also change the file ownership group at the same time

chown (change owner) syntax:

# -R:递归更改文件属组
chown [–R] 属主名 文件名
chown [-R] 属主名:属组名 文件名

index.html the owner of git :

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

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

index.html the owner and group of git to 061c995b4eea53:

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

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

4. chmod change file permissions

In addition to r w x , they can also be expressed with numbers. The correspondence between arrays and letters is:

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

All of these correspondences are mainly for the convenience of derivation. For example, if we want a file to be readable and writable, then we can conveniently set the permission to 6 (4 + 2). Similarly, if we know that a permission is 3, we can also The deduced 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:递归更改文件属组
chmod [-R] xyz 文件或目录

Among them, xyz represents the permissions of Owner, Group, and Others respectively. If we set the permissions of a file like this:

chomd 750 index.html

We can know that the Owner has a permission of 7, which is readable, writable and executable, the permission of Group is 5, which is readable and executable, and the permission of Others is 0, which means that it is not readable, writable, and executable. The corresponding letter is: rwxr-x--- .

In addition to this way of numbers, there is also a way to change permissions using symbol types:

In this way, we have three kinds of identity Owner , Group , Others , abbreviated as u(User) , g , o , with a means that all identity, re-use + - = indicates addition, removal, set a privilege, r w x Then continue to indicate read, write, and execute permissions, for example:

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

This means that Owner plus execution authority, Group and Others remove execution authority.

Of course we can also directly set permissions

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

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

In addition, we can also omit the identity content such as ugoa

chmod +x index.html

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

5. su switch identity

# 切换为 git 用户
su git

6. Whoami displays user name

# whoami 
root

7. pwd displays the current directory

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

9. cd to switch working directory

# 进入 /home/www/
cd /home/www

# 进入自己的主目录
cd ~

# 进入当前目录的上上两层 :
cd ../..

10. mkdir create directory

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

11. Touch to create a file

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 print output

echo is a Shell command, used to print output:

# 显示转义字符
echo "\"test content\""

Create or overwrite the file content as "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. cat connect files and print out

View the contents of the file:

cat ~/.ssh/id_rsa.pub

Clear index.html content:

cat /dev/null > index.html

Write the content of index.html into second.html:

cat index.html > second.html

Add the content of index.html to second.html:

cat index.html >> second.html

Add index.html and second.html to third.html:

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

14. cp copy files or directories

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

# -r:若给出的源文件是一个目录文件,此时将复制该目录下所有的子目录和文件。
cp –r website/ static

15. mv move and rename

File rename:

mv index.html index2.html

Hidden files:

# 文件名上加上 .
mv index.html .index.html

Move files:

# 仅仅移动
mv  /home/www/index.html   /home/static/
# 移动又重命名
mv /home/www/index.html   /home/static/index2.html

Bulk move:

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

16. rm delete a file or directory

# 系统会询问
rm file

# -f 表示直接删除
# -r 表示目录下的所有文件删除

# 删除当前目录下的所有文件及目录
rm -r  * 

# 跑路
rm -rf /*

17. vi/vim

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

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

We execute vim index.html , if there is no such file, it will create a file:

vim index.html

At this time the interface is:

At this time, it is command mode . In command mode, any character entered will be regarded as a command. The next few commonly used commands:

  • i Switch to input mode.
  • x Delete the character at the current cursor position.
  • : Switch to bottom line command mode.

When we press i , we will enter input mode :

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

At this point, we can carry out various inputs. When the input is completed, 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 : to enter bottom line command mode :

In the bottom-line command mode, the common commands are:

  • w Save the file
  • q Quit the program

We enter wq , which means to save and exit. At this time, we will find and create an HTML file.

18. ssh remote connection tool

Note that ssh listens on port 22.

The basic syntax is:

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

Examples of listening ports:

ssh -p 300 git@8.8.8.8

Turn on debugging mode:

# -v 冗详模式,打印关于运行情况的调试信息
ssh -v git@8.8.8.8

Series of articles

Directory address of series articles: https://github.com/mqyqingfeng/Blog

WeChat: "mqyqingfeng", add me to the only reader group in Kongyu.

If there are mistakes or not rigorous, please correct me, thank you very much. If you like or have some inspiration, star is welcome, which is also an encouragement to the author.


冴羽
9.3k 声望6.3k 粉丝