Linux learning-practical articles

Linux learning

Practical articles

Learned from Teacher Wu Shengran of Shang Silicon Valley, combined with the content of the teacher's class and his own notes to write a blog post.

Shell introduction

Definition of Shell

Shell can be seen as a command interpreter, providing us with an interactive text console interface. We can
enter commands through the terminal console, which will be interpreted by the Shell and finally handed over to the kernel for execution. So the Shell is actually a bridge connecting external applications and the Linux kernel, and it is also the main means for us to perform operations on the Linux system.

The origin of Shell

In Linux, the specific implementation of Shell can be various. In the current Centos7, there is a command called sh under the bin, which is the entry of the entire shell command. This sh command is executed in the console interactive interface.insert image description here

  • The Shell version of Unix,
    Linux, originated from Unix, and the earliest interpreted and executed program in Unix was called Bourne Shell. It is very powerful and can be programmed very flexibly, but its interaction with users is poor.
  • Linux's shell source
    Linux has developed a new shell control terminal based on the Bourne Shell, called Bourne Again Shell. Then intercepted its initials, B, a, sh, so the shell used by most Linux distributions is called Bash Shell.
  • Some improvements in Bash Shell
    Bash Shell has a lot of functions, but the whole is bloated and complicated, so some distributions have simplified Bash Shell. Debian series of Linux distributions, such as Ubuntu, use a shell interpreter called Dash since version 6.4.

sh

Earlier we found that the sh icon has a small arrow. If we look at its properties, we will find that it is actually linked to bash, so the default shell tool under centos7 is actually bash.insert image description here

Test:
We use ls -l /bin/ | grep shthe command to go to the bin directory to filter out the projects with sh, and then list the complete information of each one in one line. Here you can also see that the sh link is bash.

insert image description here

man

man is the abbreviation of manual, which is our command manual. It is very rich in content, covering almost all aspects of Linux use.
Example:
Let's view the specific usage of ls through man ls:

insert image description here

①NAME: Explain that what the ls command does is to list the contents of the directory

②Summary: The basic usage syntax of the current command. First ls, and then various options can be given (this option is generally followed by a bar, and then a parameter), and the file name can be added at the end.

③Description: Lists the meaning of all parameters, this part basically includes all usages of this command.

Page turning and other operations: use the up and down keys to move line by line.
----If we think this move is too slow, just press the space to turn the page. And you can press Page Down and Page Up to turn pages down and up.
---- You can also use the shortcut key F to turn the page down and B to turn the page
up Exit: q

External and built-in commands

We man cd to check the usage of cd, and when we come in, we will find that this is an explanation of bash.insert image description here

This is because the system commands of some basic functions are directly embedded in the shell. After the system is loaded and started, it will be loaded together with the shell and reside in the system memory. This part of the command is called a built-in (built-in) command; the corresponding other commands are called external commands

Determine whether a command is an embedded command or an external command: typecommand.

You can see that cd is a built-in command, and there will be a corresponding prompt.

insert image description here

You can see that ls is not a built-in command, so there will be no corresponding prompt.

insert image description here

history command: List the commands we have typedinsert image description here

We can see that history is a built-in commandinsert image description here

How to view built-in commands:

Just add it directly between manand : . We can see that cd is described as follows. The man manual is managed in separate volumes, so cd is described in volume 1, volume 3tcl, and volume 1p. Among them, the p of 1p represents the POSIX standard, which represents the standard for developing a portable operating system interface proposed by IEEE.命令-fman -f 命令

insert image description here

Now look at the description of cd through man 1p cd, all in Englishinsert image description here

Then go to man 3tcl cd, you can see that there are Chineseinsert image description here

Simplified version of command query

man itself is also a command. We can see that the content is very complicated by direct man man. Sometimes we just want to see how the parameters of the command are used.

You can use the help command, but you can only query the help information of the shell built-in commands . For example, let's look at the usage of the cd command here, which is very concise.insert image description here

If you look at ls, you will report an errorinsert image description here

Query the brief usage of external commands: command --helpinsert image description here

Shortcut keys commonly used in Shell

①ctrl + c: stop the process

②ctrl+l: Clear the screen, which is equivalent to clear. In fact, this method is not clearing the screen in essence, it just pushes the display interface up.

③Clear the screen completely: reset. This method essentially restarts all the current shell environment, and at this time you have to scroll up and there will be nothing.

④Be good at using the tab key to prompt (more importantly, it can prevent typing mistakes)

⑤Use the up and down keys to find the executed commands

File Directory

1. Directory operation commands

1.1 View and switch working directory

pwd

The full name of pwd is print working directoryto print the absolute path of the current working directory (from the root directory to the current working directory), which is a built-in command. The little tilde in the terminal represents the current user's home folder.

Example: We don't open the terminal on the virtual machine to enter commands, because we usually use remote login in actual work, so we use XShell remote login to operate. We enter pwd [embedded], the result is as follows:insert image description here

When we switch directories, use pwd to see that the current working directory has changed.

Absolute path: every level from beginning to end is in

Relative path: for the current location

cd

The full name of cd is change directory, the usage is the cd parameter, and the parameter directory is as follows:

cd absolute path: switch path
cd relative path: switch path
cd ~ or cd: go back to your home directory
cd -: go back to the last directory
cd ...: go back to the previous directory of the current directory
cd -P: jump to the actual physical path, not the shortcut path

Example: Switch the desktop of the directory, you can see that the current working directory becomes /root/desktop

insert image description here

Question: Now there are two directories a/b/c/d and a/b/c/e, if I want to jump from d to e, do I have to add the previous a/b/c every time I use the cd command?

Answer: No, relative paths can be used. For example, we are currently in the /root/desktop directory, and there is a video directory under the root directory, then we can switch to the video directory through cd .../video/.

.../ Return to parent directory

insert image description here

insert image description here

1.2 List directory contents

ls

ls is the abbreviation of list, and its function is to list the contents of the directory, including files and folders. There can be many options behind ls, and the following two are commonly used.

Basic syntax: ls [options] [directory or file]

Common options

  • -a: List all files in the directory, together with hidden files (files and directories beginning with .)
  • -l: list long data strings, including file attributes and permissions, etc., equivalent to ll

Example: Using ls directly, you can see that all files and directories under the current directory are listed. The black ones are files, and the blue ones are directories.

insert image description here

Example: Using ls -a, you can see that hidden files and folders are listed. In Linux, files/directories starting with . are hidden.
insert image description here

Starting with . is a hidden file or folder

.We noticed that there is an and in it .., which .represents the current directory and ..represents the upper-level directory. This is why we can use jump to the upper directory in the current directory cd .., or use cd ./the relative path to jump to other directories, for example, here we cd ./桌面/jump to the desktop directory.insert image description here

.bashrc
·.bashrc· This file is very important. It is a very important configuration file for our current user. Many related environment variables and some aliases can be configured in it. This is something later, we will talk about it when we use it.
Example: The -l in ls -l means to list in long data strings, and l means long. That is to list all the attributes, permissions, and users of the current file in a row.insert image description here

You can see that many details of the file are listed. We used to distinguish between files and folders based on color, but this thing seems to be unreliable. If the color is wrong, we will not be able to figure out what it is. In fact, here is a very clear way to distinguish: if it is a general file, it starts with -. If it is a directory, it starts with d, which stands for directory.

ls -l and ll
ls -l and ll are the same thing, we can see that he is just an alias of ls -l through type ll.

insert image description here

1.3 Create and delete directories

mkdir

mkdir: short for make directory, create a directory
basic syntax: mkdir [option] directory to be created

  • Example: Create a directory a in the current directory by mkdir a

  • Example: Create a directory b in the current root directory by mkdir /b

  • Example: Create a directory b and a directory c in the current root directory through mkdir bc

insert image description here

Note: You cannot create directories nestedly. For example, I want to create a directory d, then create a directory e under d, and then create a directory f under e. Cannot be created directly by mkdir d/e/f.

Solution ①: Create directories at all levels in turn: mkdir d, mkdir d/e, mkdir d/e/finsert image description here

Solution ②: mkdir -p can create multi-level directoriesinsert image description here

is rm

rmdir: It is the abbreviation of remove directory, remove an empty directory.
Basic syntax: rmdir empty directory to be deleted
Example: Delete directory a in the current directory by rmdir a

Note: You cannot directly delete a directory that is not emptyinsert image description here

Solution ①: Delete nestedly from bottom to top:insert image description here

Solution ②: Delete by using rmdir -p, but it cannot be used directlyinsert image description here

At this time, you need to delete the bottom-level directory. If its parent directory becomes empty after deletion, you can delete it directly at this time.insert image description here

2. File operation commands

2.1 Create a file

touch

Basic syntax: touch file name
Example: By touch hellocreating a hello file in the current directory. When the file does not have a suffix, it is a text file by default and can be opened directly.

Example: By touch /home/xzz/hello2creating a hello2 file in the /home/xzz directory.

Example: A text file can be created through vim, but if the text file is exited with q, the file cannot be saved and the file will not exist. And touch can create empty files.insert image description here

2.2 Copy files or folders

cp

cp: It is the abbreviation of copy, copying a file or directory to another place.
Basic syntax: cp [option] source dest (copy source file to dest)

  • Example: Copy the initial-setup-ks.cfg file in the current directory to the /home/xzz/ directory
  • Example: Copy the initial-setup-ks.cfg file in the current directory to the /home/xzz/hello2 file, and the effect is to overwrite it.

Use vim to open the hello2 file, you can see that the content is overwritten

insert image description here

When we copy a file to another directory containing a file with the same name, it will also remind us whether to overwrite. Here we will initial-setup-ks.cfgcopy again to /home/xzz/the directory.

Cancel the prompt: Add a "cp" in front of the cp \to stop prompting whether to overwrite but directly overwrite. We will initial-setup-ks.cfgcopy it to /home/xzz/the directory below hello3.

Then you can see that the content of hello3 is overwritten

Reason analysis: We can see that cp is actually an alias of cp -i by using the type cp command

insert image description here

cpIt's a built-in command, let's cp --helptake a look at what this -ioption represents. You can see that -i means copying in an interactive form, so there will be reminders.

insert image description here

Similarly, lsit is also a built-in command. We cp --lscan see the alias of lsyes ls --color=auto, which means to display files and directories in different colors.

Adding a in front of the command \means using a native command, that is, without parameters. So we lsadd one in front \, and \lswe can see that the color of files and directories is no longer displayed.

Copy directory:
We can also copy the directory to other places, but it is meaningless to copy the directory directly, we need to copy all the contents of the directory together. At this time, you need to add -roptions, which means to recursively copy the entire folder .

Example: First create a directory under the current directory a, then initial-setup-ks.cfgcopy it to the directory a, and finally cp -rcopy all the contents of the entire directory a to /home/xzz/the next. You can see that everything was successfully copied, including this initial-setup-ks.cfgfile.

insert image description here

View the command of the alias: alias

2.3 Delete and move files

rm

Basic syntax: rm [options] The file to be deleted
Function description: Delete the specified file. With some options you can also delete all content in the directory.

Option description:

  • -r: recursively delete all contents of the directory
  • -f: Force delete without prompting for confirmation.
  • -v: Display the detailed execution process of the command

Example: By rm hellodirectly deleting the hello file in the current directory, it will ask us to confirm whether to delete it

This is because rmof rm -ithe alias of

You can not prompt by adding the parameter -f. Here we rm -f hello4delete the hello4 file.

Example: add -r to recursively delete all the contents of the directory. We use here to rm -r a\delete a\all the contents of the directory.insert image description here

It can be seen that we will always be prompted whether to delete, so add -r, that is, use the command rm -rf c\ to directly delete all the contents under the directory c\.insert image description here

Note: Do not use rm -rf /*, this is equivalent to deleting all the content under the root directory. This can be considered as a wildcard, which deletes all the contents of the directory, but retains the directory name.
Example: Here we have a directory e with a file hello inside. We rm -rf ./e/*delete all the contents of this directory, but keep this directory.insert image description here

mv

Basic syntax:
①mv the name of the file to be moved New file name
②mv the path of the file name to be moved

Function description: Syntax ① means to move the file to a new path and change its name. Syntax ② means to move the file to a new path.
Example: There is a file hello in the current directory and an empty directory e. Now mv hello ./e/move the file hello over with the command.

Example: Now use the command mv ./e/hello aaaato move the file hello in the directory e to the current directory, and then change the name to aaaa.

2.4 View files

cat

Basic syntax: cat [选项] 要查看的文件
Function description: Open the file directly to view its content without making any modification. But all the content will be displayed at one time. If the file content is large, you need to move up all the time to see the previous content.
Option description: -n: Display the line numbers of all lines, including blank lines
Example: By cat initial-setup-ks.cfgviewing the content of the configuration fileinsert image description here

more

Basic syntax: more The file to be viewed
Functional description: The more command is a text filter based on the VI editor, which displays the content of the text file page by page in a full-screen manner.
Instructions:

  • Enter: turn down a line
  • Space [space]: turn down one page
  • b: turn up one page
  • Ctrl+F: Scroll down one screen
  • Ctrl+B: Return to the previous screen
  • =: output the line number of the current line
  • :f: output the file name and the line number of the current line
  • q: means to leave more immediately, and no longer display the contents of the file

Example: By more initial-setup-ks.cfglooking at the configuration fileinsert image description here

less

Basic syntax: less The file to be viewed
Functional description: The less command is used to view the content of the file in split screen. Its function is similar to the more command, but it is more powerful than the more command and supports various display terminals. When the less command displays the file content, it does not display the entire file at one time, but loads the content according to the display needs, which is more efficient for displaying large files.

Instructions:

  • Space: Scroll down one page
  • pagedown: flip down one page
  • pageup: flip up one page
  • G: Jump to the end (similar to VIM)
  • g: jump to the beginning (similar to VIM)
  • =: display the file name, how many lines are displayed on the current page, etc.
  • /String: the function of searching "string" downward; n: search downward; N: search upward;
  • ?String: search upward for "string"; n: search upward; N: search downward;
  • q: leave less

Example: By less initial-setup-ks.cfglooking at the configuration fileinsert image description here

Use = see related informationinsert image description here

Use /fi to search down for the string fiinsert image description here

3. Other commands

3.1 Console display and output redirection

echo

Basic syntax: echo [option] [output content]
Function description: output content to the console

Option: -eRepresents character conversion that supports backslash control,

like

\\represents the output \itself,

\nrepresents a newline symbol,

\tRepresents a tab character (Tab key)

Example: Print different content to the console.insert image description here

Example: echo can also see the environment variables of the current system, the usage is echo $环境变量. If we don't know what environment variables to look at, echo $制表符(Tab)just enter y directly to display all environment variables.insert image description here

There are many environment variables we are familiar with, such as PATH, USER, and HOSTNAME. You can directly execute system commands under this path.

Commands such as ls and cp can be executed even though they are not in the current directory. This is because the paths where these commands are located are all declared in the environment variable PATH. So we call the command as if it were called in the current directory.

output redirection

Basic syntax:

  • ①ls > file (function description: write the content of the list into the file (overwriting))
  • ②ls >> file (function description: the content of the list is appended to the end of the file)
  • ③cat file 1 > file 2 (function description: overwrite the contents of file 1 to file 2)
  • ④echo "content" >> file (function description: append the string content to the end of the file)

Example: Write the contents of the list to the file infoinsert image description here

Example: add the string at the end of the info fileinsert image description here

3.2 Monitoring file changes

head

Basic syntax:

head file (function description: view the content of the first 10 lines of the file)
head -n 5 file (function description: view the content of the first 5 lines of the file, 5 can be any number of lines)

Function description: head is used to display the content at the beginning of the file. By default, the head command displays the content of the first 10 lines of the file.
Example: Use the head command to view the beginning of the info file.insert image description here

tail

Function description: tail is used to output the content at the end of the file. By default, the tail command displays the content of the last 10 lines of the file.
Basic syntax:

  • tail file (function description: view the content of the 10 lines at the end of the file)
  • tail -n 5 file (function description: view the content of 5 lines at the end of the file, 5 can be any number of lines)
  • tail -f file (function description: track all updates of the document in real time --> can be used in the log file)

Example: Use the tail command to view the end of the info file.insert image description here

Monitor file changes

We open two terminals, one for typing tail -f infoand the other for infoappending to the file.insert image description here

Now we append some strings to info, and we can see that the left side will also be updated synchronously.insert image description here

We press ctrl s on the left to pause the monitoring, and then continue to append strings on the right. You can see that there is no synchronous update on the left (but the added content is actually saved).insert image description here

We press ctrl q on the left to continue, and we can see that the things added during the previous suspension of monitoring suddenly appeared again.insert image description here

Finally press ctrl c to exit.
== Note: == If you do not append but directly overwrite, an error will be reported
Some special cases:
If we use vim to append content at the end of this file, then save and exit.insert image description here

The content monitored on the left will not increase: this is because all hard disk partition files in Linux will be assigned a number. We call this number index, which is the index node number. You can use the ls -i command to directly see this node number, and track it based on this index number. When we modify it with vim, the index number changes, so it can be considered that this file has essentially become another file.

3.3 Soft link

soft link

ln is the abbreviation of link. Obviously, it is to create a link to link to some other file or directory. So we generally use ln to create a soft link.
Soft
links are very similar to shortcuts in Windows. If we say that we want to create a soft link for a file or a directory, it is equivalent to having a separate file in another place, which is linked to the original file. The link file has its own data block, which mainly stores the path of the linked file or directory. In this way, through the current shortcut, this link can jump to the file we really want to access. This is actually a bit like a pointer. The link we created currently is equivalent to a pointer, which points to a variable corresponding to another memory space, and itself can also be regarded as a separate special variable.
Soft link case in linux
The bin directory under the root directory is a shortcut, which links to the bin directory under the user. Similarly, the sbin directory is also a soft link, which links to the sbin directory under the user.

ln

Basic syntax: ln -s [original file or directory] [soft link name]
soft link has another name,symbolic link. Because essentially, we're creating a symbol for the original file that links to it. In the process of using, it is generally necessary to add a fixed option after ln, that is -s. s is the first letter of soft, ln -swhich is the standard syntax for creating a soft link, followed by the name of our source file or directory, and the name of the created soft link.
Example: Linking a file
We want to create a link in user xzz's home directory to the info file in root's home directory.insert image description here

Create a link through the command in the home directory of user xzz ln -s /root/info Myinfo. After creation, we can see that its color is completely different from that of the previous ordinary files and directories. Let's use ls -l to see that it starts with l, indicating that this is a linked file.insert image description here

We use less to open the file to view, and we can find that the content is correct.insert image description here

Now we insert a 666 into the end through Myinfoinsert image description here

Then check it in the root directory, and you can find that the modification has been successfully made.insert image description here

Example: Link a directory
We create a folder directory under the root directory, and create a file file inside.insert image description here

We create a link in the home directory of user xzz to the floder folder in the root home directory. The name of this link is myFolder. As you can see, there is a file fold under the linkinsert image description here

We enter this soft link and use pwd to view the directory of the current file. You can see that the current directory is displayed, not the actual directory of the file.insert image description here

If you want to display the actual path, usepwd -Pinsert image description here

Next, we use the cd -P command in the root directory to go to the directory where the soft link is located. You can see that it will go to the actual directory where the directory pointed to by this soft link is located.insert image description here

Example: Delete a soft link directory
Use the command rm -rf link name to delete a link directory. You can see that the soft link is gone after deletion, but the original directory is still there.insert image description here

If rm -rf 链接名/the link directory is deleted, the content under the real directory corresponding to the soft link will be deleted. Here you can see that although the original directory folder is still there, the file inside is gone. insert image description here
Example: Delete the original directory without deleting the soft link file, and then use the soft link file to access the original directory.
It can be found that the folder does not exist.insert image description here

hard link

Create a link to the file alone

Basic syntax: ln [original file or directory] [soft link name]
Definition of hard link:

The file has a very important information called inode, which saves some meta information corresponding to the file, such as the type of file, the permission of the file, the current number of links, the creation time, and so on. There is a very critical information here is the unique number of this file, that is, the inode number. Linux allows different file names to point to the same inode node, so there is a usage, that is, to create a new hard link to link to the area where the data is stored. In this case, we can directly create the same file name pointing to the inode, which has no effect on the previous file, but simply adds a new link. This link method is called a hard link.

Features of hard links:

For example, if there are two hard links a and b both link to the same file, deleting a has no effect on b, because a and b are completely equal. For soft links, if the original file or directory is deleted, the original file cannot be accessed through the soft link.

Number of links to current file:

It does not refer to the number of soft links, because soft links can be considered as a separate link file. What it really refers to is the number of its hard links. The hard links are equivalent to being completely equal. If you delete one, the other can still be accessed directly. In actual use, hard links are rarely used, because this method is related to the inode of the file, and can only create a hard link of a file, but not a hard link for a directory.

3.4 View historical commands

history

Function description: View historically entered commands

Example: View all historical commands that have been executed through history

  • View the last 10 used instructions through history 10

  • View instruction 345 via !345

  • Use history -c to delete historical commands

time date class

date

Basic syntax: date [option]... [+time date format]
Option description:

-d <time string>: Display the time represented by the specified "time string" instead of the current time.

-s <datetime>: Set the system datetime.

Parameter Description:
<+datetime format>: Specifies the datetime format used when displaying

date displays the current time:

  • date (function description: display the current time)
  • date +%Y (function description: display the current year)
  • date +%m (function description: display the current month)
  • date +%d (function description: display the current day)
  • date "+%Y-%m-%d %H:%M:%S" (function description: display year, month, day, hour, minute, second)

Example: Display the current time directly through dateinsert image description here

Add %Y, %y, %m, %d and other options after the date to display the current year, month, day and other informationinsert image description here

Use the command date +%Y-%m-%d-%H:%M:%Sto display the current time in the form of year-month-day-hour:minute:second.

The difference between uppercase S and lowercase s: S represents the current number of seconds, and s represents the timestamp (displaying how many seconds have passed since January 1, 1970). Timestamps are useful when writing system logs.insert image description here

Example: Display yesterday, tomorrow, next hour, etc. time by date -d.insert image description here

Example: Modify the system time through date -s.insert image description here

The current time can be synchronized through the ntpdate server, and the time difference will be displayed.

asia.pool.ntp.org

insert image description here

cal

Basic syntax: cal [options] (function description: without options, display the calendar of this month)

options:

  • cal -[number]: Displays a calendar for the specified months before and after.

  • cal -m: put week 1 at the front

  • cal [specific year]: see the calendar of the specific year

  • cal -y: see the calendar of the current year

User permission class

1. User Management

background

Linux系统是一个多用户多任务的分时操作系统,所以可能会有很多人都用同一台机器进行操作,甚至有可能还是同时登陆同时操作的。所以人一多,我们就要进行很好的管理才行,必须对于不同的用户给予他们一个相应的身份呢。也就是说给用户一个特别的帐号,这个帐号可以设置自己的密码,然后就会有着不同的权限。用户想要登录系统的时候,就分配的账号去登录系统,然后就可以获取到不同的系统资源。这就是用户管理的基本的思想。(注:必须以root的身份去进行用户管理操作)

useradd add new user

Basic syntax: useradd username (function description: add new user)
Example: The home directory of the root user is /root, and the home directory of ordinary users is /home/user. Now by useradd tonycreating a normal user tony, see if it will exist in the /home/ directory.insert image description here

It can be seen that the created ordinary users appear in the /home/ directory.
Example: For normal users the home directory can be changed. We useradd -d /home/dave davidcreate a common user david and let his home directory be /home/dave.insert image description here

It can be seen that when we look in the home directory, dave's home folder name appears, but the user's name is david.

passwd password setting

Basic syntax: passwd username (function description: set user password)
Example: Set password for tony by passwd tony.insert image description here

After setting, you can log in with tony's account.

id Check if the user exists

Basic syntax: id username
Example: Check whether some users exist by id.

It can be found that the existing users will print uid (user ID) and gid (group ID), and dave is david's home directory rather than the user name, so it does not exist.

cat /etc/passwd to see which users have been created

Example: See which users are created by looking at the passwd documentation.insert image description here

It can be seen from it that:
①There are a bunch of other users on the top. We seem to have never seen these users, and they are useless. In fact, most of the users here are created by the system by default, such as bin, daemon, shutdown, etc. These are all related to system services, and these users are created separately to run corresponding system services. Therefore, these users are generally called system users, or pseudo-users, that is, they cannot log in as real users, but are used to run services in the system.
②The users created just now are at the end of the document. The uid, gid, home directory, and login interaction method are listed respectively.insert image description here

Each user needs to interact with the system when logging in to the system, and /bin/bash represents interaction with the shell. In the past, many system users interacted with the system by nologin, that is, they did not need to log in, and the system service was running by default.

su switch user

Basic syntax
su user name (function description: switch users, only get the user’s execution permission, but not environment variables)
su - user name (function description: switch to a user and get the user’s environment variables and execution permissions)
Case: switch user xzz through su xzz, you can see that the current home directory has also become /home/xzz.insert image description here

The current user is xzz, we continue to use su tony to switch to the tony user, and we can see that we need to enter a password. Execute ls, you can find that ordinary users cannot view the contents of the home directory of another ordinary user.insert image description here

Switch back to the root user
① via su root

②The current user switching jump is a layer-by-layer nested session, that is, root nests xzz nests tony. We can return layer by layer through exit.insert image description here

View the currently logged-in user
who am i to view the process-related information of the currently logged-in user.
whoamiView currently logged in users.
Case: Log in as the root user to view the logged-in user information. Then switch users, and then check the logged-in user information. It can be seen that who am i always displays the real logged-in user, because switching users is still based on the session created by the current root user, and this process is still created by root.insert image description here

sudo sets ordinary users to have root rights

Now we are logged in as a normal user tony and cannot access the root user's home directory.

If we want to temporarily give tony some super administrator privileges, we can use sudo plus commands.insert image description here

But there is still no success here, we need to modify this configuration file under the root user /etc/sudoers. You can see that the following line represents that root can execute any command anywhere.insert image description here

So we give tony the same permissions.insert image description here

Then test, at this time you can use sudo ls to view the contents of the root home directory.insert image description here

userdel delete user

Basic syntax:
userdel username (function description: delete the user but save the user's home directory)
userdel -r username (function description: delete both the user and the user's home directory)
== Note: == This operation must be performed under the root user.
Example:

Use userdel tony to delete the tony user, but keep the home directory. Generally, this situation is often used, because although the user is no longer there, his previous files may still be useful.

Use cat /etc/passwd to see that there is no tony user

We userdel -r david delete david, you can see that the main directory is gone.

At the same time, there is no david user in the passwd file.

2. User group management

User Group Management

A user group is equivalent to a group. For example, a company has a research and development group, a testing group, an operation and maintenance group, and so on. Users in each group are responsible for different things, which is equivalent to having different permissions in the system. When creating a new user in Linux by default, a group name with the same user name and a group ID with the same user ID will be created.insert image description here

Now, we want to create a new meifa group, use the command groupadd meifa. Then use the command cat /etc/group to view the group configuration file, and you can see that the meifa group has been successfully created.insert image description here

If you want to modify the group name, you can group -n 新组名 老组名complete the modification by . Here we change meifa to harcut.

Now we want to add tony and david to the haircut group, it works usermod -n 用户组 用户名.

Check the gids of tony and david at this time, and you can find that it has changed to 1005.insert image description here

Finally, since we moved tony and david to the haircut group, we delete the original david and tony group. Can be groupdel 组名deleted by .

Looking at the group configuration file, it can be found that it has been deleted.

wheel user group

There is a wheel user group in the sudoers configuration file, which is quite special and is the management group. Users in this group have permission to execute all commands.insert image description here

We can first move the atguigu user to the wheel group, then switch to the atguigu user and execute sudo ls. It can be found that although the atguigu user is not set in the sudoers configuration file, commands can still be used directly, which is why atguigu is in the wheel working group.insert image description here

You can see that you still need to enter the password here, you can use the following line, that is, add NOPASSWD:ALL after the wheel in the sudoers configuration file. In this way, you don't need to enter a password to use sudo.insert image description here

3. File attributes and permissions

background

问题: 前面我们介绍了用户管理和用户组管理的相关命令,我们就会思考一个问题:用户组划分出来之后是为了干什么?
答: 就是为了把很多用户做集中化管理。
问题: 这些用户集中在一起构成了一个用户组,怎样去针对他和其他的组进行区别?
答: 他们的区别在于拥有不同的系统操作权限。
问题: 用户除了可以访问各自的主目录里面的内容,其他的目录里的内容谁才可以访问呢?
答: 除了用户自己的主目录访问外,别的文件和文件夹针对不同的用户、用户组也有一个权限的划分。这就是文件权限的管理。

file properties

The Linux system is a typical multi-user system, and different users are in different positions and have different permissions. In order to protect the security of the system, the Linux system has different regulations on the permissions of different users to access the same file (including directory files), as shown in the following figure.insert image description here

The reason why file permissions involve hardware is that everything in the Linux system is a file, and all devices are also managed by files. Of course, there must be corresponding file types. Corresponding to these device files, you can see all the contents under the /dev/device directory. In Linux, we can use the ll or ls -l command to display the attributes of a file and the user and group to which the file belongs.insert image description here

File permission identification characters:
①The first digit of 0 indicates type
-: indicates file
d: indicates directory
l: indicates link file (link file)
c: device file of character type. Such as the mouse and keyboard, their input operations may involve characters.
b: block device file. Such as hard disk.insert image description here

② The 1st-3rd digits determine the owner (the owner of the file) has the permissions of the file—User
③ The 4th-6th digits determine the group (users in the same group as the owner) have the permissions of the file—Group
④ The 7th-9th digits determine the permissions of other users to own the file—Other
rwx Different interpretations of the role of files and directories:
① Effects on files:
[r] means readable (read): can be read, check
[w] means writable (write): can be modified, but it does not mean that the file can be deleted , the prerequisite for deleting a file is to have write
permission for the directory where the file is located, in order to delete the file.
[x] stands for executable (execute): can be executed by the system
② to the directory:
[r] stands for readable (read): can be read, ls to view the contents of the directory
[w] stands for writable (write): can be modified, create + delete + rename the directory in the directory [
x] stands for executable (execute): can enter the directory, that is, the cd command
Example: The following directory folders have read, write, and executable permissions for the owner root, and group and other users have read and execute permissions.insert image description here

We can test it.
① Back to the root directory /, and then ll to see the permissions of the current directory. You can see that root has permission to access the root directory, and users in the root group can also access it, but other users cannot.insert image description here

② Use ls -al, where -a means you can see hidden files and directories (starting with a dot). Therefore, we can see here the permissions corresponding to . and . Other users cannot access the current directory.insert image description here

Example: Copy the two cfg files here, anaconda and initialsetup, to the user directory of ordinary user xzz.insert image description here

You can see that the ownership of these two files has not changed, and the permissions have not changed. Therefore, using cat to access the configuration file of anaconda will cause an error.insert image description here

The initialsetup configuration file is accessible, but not writable, unless sudo and wq! are used.insert image description here

Explanation of basic file attributesinsert image description here

Among them, if you view a file: the number of links refers to the number of hard links. If viewing a folder: the number of links refers to the number of subfolders.
Example: There are 2 subfolders inside the public directory here.insert image description here

But after entering ls, it was found to be empty.insert image description here

Use ls -a to see that there are . and ..., which are essentially two foldersinsert image description here

4. Change file permissions

chmod change permissions

Background: If you want to access or modify the contents of a file, but you don’t have permission, the easiest way to do this is of course to become the root user, or add sudo, but this permission is obviously very large. Generally, when I only want to access a certain file, I only need to change the permissions corresponding to this file, which is equivalent to providing us with richer and more refined management of permissions.

basic grammar

The first way to change permissions: chmod [{ugoa}{±=}{rwx}] file or directory
where: ugoa represents the owner, group, others, and all. + - = represents increase, decrease, and specified permissions respectively. rwx stands for read, write, execute.

Example: Use chmod u+x initial-setup-ks.cfgthe command to add the execution permission of the owner to the initial configuration file.insert image description here

By specifying read and write permissions to the initial configuration filechmod a=rw initial-setup-ks.cfg for all users .insert image description here

The second way to change permissions: chmod [mode=421] [file or directory]
Among them, r=4 w=2 x=1, rwx=4+2+1=7, actually corresponds to the three-digit binary value of rwx.
rwx 1-x。001 2-w。010 3-wx。011 4-r。100 5-rx。101 6-rw。110 7-rwx。111
Example: Use to chmod 777 initial-setup-ks.cfgspecify all permissions for the initial configuration file.insert image description here

Modifying the permissions of the directory
The problem arises: Modifying the permissions of the directory involves another problem: we operate on the directory, and the current directory has read, write, and executable permissions. If there are other subdirectories and subfiles under it, it is still unreadable and unwritable, so what should I do?
Answer: At this time, we hope to nest recursively, and all sub-files and sub-folders inside have relevant permissions.

The specific method is: add -R , such as chmod -R 777 xiyou/. This is a relatively powerful move. Next, this directory can be used by anyone, but be careful, this is equivalent to opening up complete permissions, anyone can modify it, and anyone can delete it.

chown change owner

Basic syntax: chown [options] [end user] [file or directory]

(Function description: change the owner of a file or directory)
Options: -R: recursive operation, used to change the owner of a directory.

chgrp change group

Basic syntax: chgrp [end user group] [file or directory] (function description: change the group to which a file or directory belongs)
Example: Modify the owner and group user of the initial configuration file.

5. Comprehensive application cases

case description: There are different departments and different groups in the company. For example, big data is a department, and testing is a department. People in the same group can read and modify files and directories belonging to the group, but members of other groups can only access them.

1. Create groups and users

Use groupadd bigdataand groupadd testcreate large data sets and test sets.

Looking at /etc/groupthe configuration file, you can see that it was created successfully.

Use useradd -g 组名 用户名to add members to the big data group and test group, and use id 用户名to view related information.insert image description here

You can also see that each user in the home directory also has a corresponding home directory folder.insert image description here

2. Create a file

Switch to user Xiaoming and create a code file in his home directoryimport_codeinsert image description here

The permissions of the file are as follows.

At this time, if you switch to Xiao Liang in the same group, you can see that Xiao Liang cannot access Xiao Ming's home directory.

Switch to the home directory, you can see that Xiaoming's home directory has all permissions only to Xiaominginsert image description here

Therefore, it is necessary to let Xiaoming's home directory enable executable permissions (representing that the directory can be entered) and access permissions for group members.insert image description here

At this point Xiaoliang can enter Xiaoming's main directory and access the import_code file.insert image description here

but cannot modify the file

Even if wq! is added, it cannot be saved, because adding an exclamation point is equivalent to executing sudo, and Xiaoliang does not have sudo authority.insert image description here

If you want the file to be modifiable by members of the group, switch to Xiaoming, and then modify the import_code permission.insert image description here

3. Access by members outside the group

Assuming that Xiaohong in the test group wants to access the file, you can see that even Xiaoming's home directory cannot enter, so you need to specify permissions for Xiaoming's home directory.insert image description here

Then Xiaohong can go in and access the import_code directory, but still cannot modify it.insert image description here

If Xiaolan in the test group transfers to the big data group at this time, you can see that the transfer has been successful.insert image description here

At this time, Xiaolan also has permission to modify the import_code file.

insert image description here

file lookup class

1. find Find files or directories

find

Function: The find command will recursively traverse each subdirectory from the specified directory, and display the files that meet the conditions on the terminal.
Basic syntax: find [search range] [option]
option description:

-name<查询方式>: Find files according to the specified file name search mode
-user<用户名> : Find all files belonging to the specified user name
-size<文件大小>: Find files according to the specified file size, the unit is:

  • b - block (512 bytes)
  • c - bytes
  • w - word (2 bytes)
  • k - kilobytes
  • M - megabytes
  • G - gigabytes

Add + or - or = before the file size, representing greater than, less than, or equal to the file size.
Example: There is an info file in the current directory, I also create an info file in the public directory, and finally use find -name info to find the info file. It can be seen that the two info files are listed successfully.insert image description here

We can also specify the search scope, we specify the scope here as /root/publicinsert image description here

Example: Not only can we search for files with corresponding names, but we can also search for files according to the pattern of the name. For example, our most commonly used asterisk can represent a wildcard, and all similar files can be specified. Now we use to find /root -name "*.cfg*find all .cfg files in the root directory, and we can see that hidden files are also found.insert image description here

Example: Use the command find /home -user tonyto find files belonging to user tony in the /home directory.insert image description here

Example: Find by file size. We can check the size of the fileinsert image description here

This doesn't look very good, you can use ll -lhinsert image description here

Now use find -size +1M to view files larger than 1M in the current directory.insert image description here

2. locate to quickly locate the file path

locate

Function: The locate command utilizes the locate database of all file names and paths in the system established in advance to quickly locate a given file. The locate command does not need to traverse the entire file system, and the query speed is faster. In order to ensure the accuracy of query results, the administrator must update the locate time regularly.

Basic syntax: locate searches for a file

Tips and tricks: Since the locate command queries based on the database, you must use the updatedb command to create the locate database before running for the first time.(If there is an error indicating that the locate database is not installed, yum -y install mlocate)
Example: Use locate tmp to find all files and directories with tmp.insert image description here

Find command:

  • ①Use the which command. Here we use which ls to find the location of ls, locate, which commands.
  • ②Use the whereis command. Here we use whereis ls to find the location of ls, locate, whereis commands.

3. grep filter search and "|" pipe symbol

grep

Function: The previous locate and find commands search for the file name, while grep searches for the specified content in the file, which is similar to searching for the specified content in the command line mode in vim.

Basic Syntax: grep option finds content source files

Option description: -n Display matching lines and lines
Example: There are some boots in the initial configuration file, we use to grep -n boot initial-setup-ks.cfgfind boots in this file, and display the line number.

pipe character

Function: The pipe symbol | indicates that the processing result output of the previous command is passed to the subsequent command for processing, and it is usually used with grep.

Example: There are currently two .cfg files on the desktop, so we use ls | grep .cfgto filter out the files from ls and the files ending in .cfg in the directory.insert image description here

Example: Before we counted the boot in the initial file, now we want to count the number of boot occurrences. We need to use the wc command here, wc means word count. For example, if I want to count info files, I can use wc info to see the statistics of the number of lines, words, and bytes.insert image description here

Now we use the grep, |, and wc commands in combination to count the number of boot occurrences in the initial file. It can be seen that there are 8 occurrences.

insert image description here

Compression and decompression class

1. gzip/gunzip compression

gzip/gunzip

Basic syntax:
gzip file (function description: compressed file, can only compress the file into a .gz file)
gunzip file.gz (function description: decompress file command)
experience skills:

①You can only compress files but not directories
②Do not keep the original files
③Multiple files will generate multiple compressed packages at the same time

Example: Here we have a 7M file, which is compressed using gzip, and it can be found that it is compressed to 3M.

insert image description here

Two, zip/unzip compression

zip/unzip

Basic syntax:
zip [option] XXX.zip content to be compressed (function description: command to compress files and directories)
unzip [option] XXX.zip (function description: decompress file)
option description:

  • zip option: -r compresses the directory
  • unzip option: -d <directory> specifies the directory where the decompressed files are stored

Note:The zip compression command is common in both windows/linux, and can compress directories and retain source files.

Example: Since zip can retain the source directory and files, we use to zip -r myRoot.zip /rootcompress /rootthe directory and name the compressed file myRoot.zip.

It can be seen that the compression is successfulinsert image description here

Now we use the command unzip -d /tmp myRoot.zip to decompress this compressed package into the /tmp directory.

insert image description here

Now we go to the /tmp directory to view the decompressed content, and we can find that the root directory is here.insert image description here

Enter the root directory, and you can find that the content inside is the same as that of the root home directory.

insert image description here

Three, tar packaging

tar

Basic syntax:
tar [option] XXX.tar.gz content to be packaged (function description: package directory, compressed file format.tar.gz)

Option description:

  • -c: Generate a .tar package file and create a new archive
  • -v: show detailed information
  • -f: Specify the compressed file name
  • -z: use gzip for compression/decompression
  • -x: Unpack the .tar.gz file
  • -C: Unzip to the specified directory

Example: Use the command tar -zcvf temp.tar.gz initial-setup-ks.cfg 公共/将initialconfiguration file and public directory to package and then compress, the compressed file name is temp.tar.gz.insert image description here

Now tar -zxvf temp.tar.gz -C /tmpextract temp.tar.gz to /tmp directory using

Enter the /tmp directory, and you can find that these two files and directories are here.

insert image description here

Disk Management

1. View the space occupied by the directory

background

①Display directory details
As we said earlier, you can use ls -lor llcommand to display directory information in detail. But if there are nested subdirectories in its subdirectories, and there are many levels, it is inconvenient for us to see it directly at this time. For this kind of display, there is another small tool in Linux, tree. It lists all subdirectory levels under the directory you want to list in a standard tree directory. There is no such tool in the default Centos7, and it can be yum install treeinstalled through. After the installation is complete, view the structure of the current directory, as shown below. Because there are Chinese characters in it, these numbers are the corresponding Unicode.insert image description here

②Display disk usage
However, we still have another requirement, that is, in the actual operation process, we not only want to know the current directory structure, but as the usage time gets longer and more file directories become more and more, this hierarchical structure will become more and more complex, and the hard disk space occupied will also become larger and larger. Therefore, we should be concerned about the current hard disk usage at any time. Although ls -lh can clearly see the size of each file under the current directory, but if we use ls -lg / to view the file size of the root directory, we can find that the size is only 28K.

insert image description here

This is because ls -lhthe sizes of all the files and directories listed here are simply superimposed, and when the sizes are listed here, the total size of each directory is not nested and calculated together. For example, the root here, as we have seen before, there are many tens of megabytes of things in the root, but here it is only 4K. In fact, it only counts the current directory.
So what we hope more is that there is a single command that can directly count all the contents in the current directory, including the size of the hard disk space occupied by the sub-files and sub-folders below it.

of

du: disk usage Disk usage
Basic syntax: du directory/file (function description: display the disk usage of each subdirectory under the directory)
If you use du directly, all the things that occupy space in the current directory will be listed. It will be messy to look at this way. In fact, we just want to see how big the current directory is in a relatively simple way. We can directly look at the last line, which is the size of the current directory. Of course, du has many optional options that can make the output more friendly.

insert image description here

Option description:

  • -h Displays itself in formats such as GBytes, MBytes, KBytes, etc. that are easier for people to read;
  • -a not only check the subdirectory size, but also include files
  • -c After displaying the size of all files and subdirectories, display the sum
  • -s only show the sum
  • --max-depth=n specifies the depth of the statistics subdirectory as the nth level

Use du -ah to simplify the display.insert image description here

Use du -sh to display the total size of the current directoryinsert image description here

Use to du --max-depth=1 -ahdisplay the total size of the current directory, but only the first level of depth.

insert image description here

Second, check the disk usage

background

We can now use to du -sh /view the size of the root directory, and we can see that the size is 11G.insert image description here

But this is not the total size of our current hard disk. It looks similar, but it is still not the same as the real disk. Before installing the system, I did the disk partition operation, and divided a boot partition, a swap partition and a root partition. Usually, all the files we use are stored in this root partition. Because the Linux file system is a virtual directory, we have no way to see which disk occupies how much space from the directory structure. At this time, we can't use the du command, we need to use df

df

df: disk free free disk
Basic syntax: df option (function description: list the overall disk usage of the file system, check the disk space usage of the file system)
Example: Use df -h to view the disk usage, as shown below.

insert image description here

can be seen:

  • ①We allocated 55G memory for the root partition before, and the corresponding device is /dev/sda3
  • ②We allocated 1G memory for the boot partition before, and the corresponding device is /dev/sda1
  • ③The remaining information is not entirely disk occupation information, because we can see that their file system is not managed under the dev directory. This file system is called tmpfs or dvtmpfs, which means a temporary file system.

memory-based file system

tmpfs or dvtmpfs, which represents a temporary file system

It is a special type of file system in Linux. So here we not only have a display for the hard disk, but also have a display for the devices corresponding to the memory.
Question: What kind of device corresponds to the temporary file system set by the memory?
Answer: Real memory and swap partition. The swap partition is mainly used to exchange pages with the memory, which can be used as virtual memory, so the file systems of the two are consistent, and both use the temporary file system tmpfs.
Here we can see it very clearly:

  • ① There is a file system tmpfs, and the mount point is /dev/shm. shm is shared memory, that is, shared memory. That is to say, this memory space is shared by all system processes, and all these processes can access this directory. The size of this memory is half of the system memory by default, 2G.
  • ② There is a file system tmpfs, and the mount point is /run. These are all related to the runtime, so we can think of the memory and virtual memory swap partition as something related to the system or runtime.
  • ③There is a file system devtmpfs, which means that the linux kernel will create a file system when it starts, and then create a /dev directory, which is the mount point of the current file system, which is mainly used to manage all our current devices. So these 4 2Gs add up to 8G. Our swap was allocated 4G at that time, and the memory itself is also 4G, so the total is 8G.

free

Example: Use free -hto view memory usage.

insert image description here

We can see the total space of the current physical memory and virtual memory (swap partition), how much space is used, how much space is free, and the memory space being shared.

3. Check the device mounting status

lsblk

Function description: Check the device mounting status.
The previous two commands did not see the real disk information in detail, but lsbkc can. lsblk is the abbreviation of list block, which lists the mounting status of all current block devices (storage devices such as hard disks and optical disks).
Example:
Use lsblk to view the disk mount status.

insert image description here

As can be seen:

  • ①There is currently a storage device sda ​​whose type is disk-hard disk.
  • ②This hard disk is divided into 3 partitions
  • ③The following mountpoint is the corresponding mount point.
  • ④The first boot partition is mounted under boot, the second swap partition is mounted under swap, and the last partition is mounted under the root directory. These partitions are also the same size as we originally allocated.

In addition to the hard disk sda for cdrom
, we see that there is also a sr0 below, and its type is rom. We used a CD when we installed the system. We used the CD drive to load the ISO image file into it, and started the system from the CD drive. Now although the information in that CD is not used, and it is not directly mounted, the CD-ROM device is still there, and its type is cdrom.
All devices are under the /dev/ directory, we ls /dev/ | grep sr0can find them through.

Use ll /dev/ | grep sr0show complete information.

It can be found that the type of sr0 is b, that is, block, a block storage device. In addition, there is a cdrom, which is a soft link (type l), directly linked to sr0. This is because Linux directly gave sr0 a more understandable name, called cdrom, for the convenience of our management.

The origin of the name of the hard disk

When you use lsblk to view the device mounting status, you will see different names depending on the hard disk, such as sda, hda, and vda. Here we mainly look at the meaning of the first letter, the most common is sda. Regardless of whether it is a personal computer or a server, the hard disks we use are mainly divided into the following categories according to different interface types:
IDE hard disks : IDE hard disks used more in the early days, but now it is rare, because its read and write performance is relatively poor.
SATA hard disk : Hard disk with Serial ATA interface specification, supports hot swap. Compared with the traditional IDE hard disk, it is much faster, and has a large data storage capacity and lower cost, so now our personal computers generally use SATA hard disks.
SCSI hard disk :
SCSI is a computer system standard interface, so not only hard disks can use this interface, but also various computer peripherals such as optical drives and scanners can use this interface. So it itself has a dedicated controller for data operation and data transmission, and its data transmission speed will be faster. The server requires stronger performance, so there will be more SCSI hard disks. When we were doing virtual machine configuration before, we selected SCSI hard disk as the default hard disk.

insert image description here

Linux rules for naming hard disk partitions

  • ①If there are multiple hard disks, such as IDE hard disks, their names all start with HD. The first hard disk is called HDA, the second is HDB...and so on.

  • ②If there are multiple SATA or SCSI hard drives, they all start with SD. Similarly, the first hard disk is called SDA, and the second hard disk is called SDB.

  • ③If you use a virtualized analog device, it starts with VD.

  • If each hard disk needs to be partitioned, just add numbers 1, 2, 3... after the name. So we saw sda1, sda2, sda3 earlier.

lsblk -f

lsblk -f: View detailed device mounting status and display file system information.
Example: Use lsblk -f to see file system information.

insert image description here

As can be seen:

  • ①In addition to the current hard disk and partition name and the following mount point, the two main information, there is also the type of file system.
  • ②We chose XFS at that time, a 64-bit high-performance log file system. If we chose the EXT4 fourth-generation extended file system at that time, then the display here must be EXT4.
  • ③The file type of the second partition is different, it is swap.
  • ④ There is a string of characters behind each partition, which is UUID. UUID is a unique symbol created by the system for each current partition, with a total of 40 bits. Use this ID to uniquely specify the current partition.

Fourth, mount and uninstall

background

①Before we found that in addition to the mounted hard disk, there is also a CD underneath. This disc has no mount point, so it cannot be accessed directly, because all devices must correspond to a directory. Under /dev/ are our device files, and it is definitely impossible to find things in the CD. To find the contents of the disc, the disc must be mounted somewhere.
②When talking about hard disk partitions, we mentioned the mount point. We currently have a hard disk, and we divide it into three partitions: boot partition (1G), swap partition (4G), and root partition (45G). Usually, when performing operations, it does not say which partition to directly put a large file into. Generally speaking, there is a file system with many directories in it, such as root directory, root directory, home directory and so on. Now we can directly put this file under the corresponding directory.
③Which storage location on our hard disk does this directory correspond to? This correspondence needs to be represented by a mapping, which is what we call a mount point. The file system is originally a virtual directory structure. When we use this directory, we don't care where its underlying storage location is. Linux needs to know this correspondence. As long as it is configured once when the hard disk is partitioned, we don't need to worry about it later.
④ During this configuration process, we know that the mount point of the first partition is mounted under the boot, so as long as all your files under the boot directory will be stored in the disk space of the first partition of 1G. The 4G of the second partition is the swap partition, we will not put files in it, this is our swap partition, virtual memory. In the end, the 45 G are directly mounted under the root directory, so except for the content in the boot directory, we are all branches based on the extension of this root directory. So all the remaining files and folders are stored in the 45G space of the third partition.
⑤So now we have another CD, although we don't need to partition it anymore, we should also consider the files in the current CD. As long as we set a mount point for this disc and mount it, we can access it through the file system. We said before that there is a directory called /mnt/, which originally allows us to mount some external storage devices, so we mount this CD under the /mnt/ directory, and then we can access the contents of the CD in this directory

mount/umount

Preparations:
Next, let's do the actual operation. Since we want to mount a CD, we must first have this CD. You can use the image file when installing Centos, we first do some configuration on the virtual machine. Select which image file, and select the status as connected.

insert image description here

When we go back to the desktop, we will find that a disc icon pops up here, which is actually the same as in Windows. It defaults to a desktop environment for the entire system, and it will directly mount it for us. After loading, you can click directly to see the content inside.

insert image description here

But we don't want to access the CD-ROM files directly on the desktop, we still want to access them on the command line. Let's first check where the mount point of the current image file is, and you can check it through lsblk.

insert image description here

Now we want to try the mount command manually, so eject it first.

insert image description here

It can be seen that it is not mounted.

insert image description here

Basic syntax of mount
: mount [-t vfstype] [-o options] device dir (function description: mount device)
parameter function: as shown in the figure below

insert image description here

We create a new cdrom folder under /mnt, and then prepare to mount the CD there.

insert image description here

It is directly mentioned that the media cannot be found here, because we did the pop-up operation just now. After the CD/DVD is ejected, it is not connected again. If you choose Connected again, because we have a graphical interface here, it will automatically mount it for us.

insert image description here

Solution:
① Log out firstinsert image description here

② Then go to select Connected.

insert image description here

③Finally, log in again and use lsblk to see that it is not automatically mounted.

insert image description here

So execute the mount command again mount /dev/cdrom /mnt/cdrom/(you can also replace cdrom with sr0), and you can see that the mount is successful.

insert image description here

Execute lsblk again to see that the mount is successful.

insert image description here

We enter /mnt/cdrom/ and click ls to see the file content of the CD.

insert image description here

Basic syntax of umount
: umount device file name or mount point
Example: use to umount /mnt/cdromuninstall sr0/cdrom.

insert image description here

If we visit this /mnt/cdromdirectory again, we will find that this directory still exists, but there is nothing in it.

insert image description here

Set up automount

Background: Every time I boot up and come in, I hope that it can recognize the disc above by default, and mount it directly, just like a partition I have divided in advance.
Automatic mount: Need to modify /etc/fstabthe configuration file.
When we do automatic mount configuration, we need to change a configuration file called fstab (file system tab). Let's go in and see what's inside.

insert image description here

It can be seen that it actually contains the mount information of all devices
①The mount point, UUID, and file type of the three partitions.
② There are two 0s at the end, which represent the following meanings:

  • ---- The first 0 indicates the dump option. When we were installing the system, there was a check box called kdump, and we removed it directly. The main purpose of kdump is to make regular backups for the entire system. If a failure occurs, it can be rolled back to the previous state directly, so this flag indicates whether to make backups. If 1 is given here, it means that the backup operation is performed regularly every day, and 0 means no backup.
  • ---- The second 0 indicates the priority of the current file system check. There is a command called fsck, which is file system check, which is used to check and repair the current file system. When booting, the system will use this command to check the file system by default. When checking, it will determine the order of checking the file system according to the priority set here. If it is set to 1 here, the priority is the highest, and the root directory will generally be set to 1. If it is set to 2, it is to wait for 1 to be checked before checking 2. We now set it to 0 to indicate no check, and do not use fsck to check the file system when booting.

Add information: We don't need to write UUID, just write the device name directly. Then follow the mount point, file type, default options, two 0's.

insert image description here

Five, disk partition

background

Now we know:
① Check the usage of the current hard disk
② Check the mounting status of the current block device
③ Mount a newly added device
Thinking: What should I do if I mount the hard disk for the previously mounted CD? This is not just a matter of mounting. We first partition it, then format it according to different file system types, and finally set the mount point. For hard disk partition formatting operations, there is a special command, namely fdisk.

fdisk -l

fdisk -l: Just check the partition details of the current disk, and the information you see will be more detailed than the partition information seen by lsblk.
Example: View the partition details of the current disk.

insert image description here

As can be seen:

  • ①Disk name: /dev/sda
  • ②Disk size: 64.4GB
  • ③Specific number of bytes, number of sectors, logical sector size, physical sector size, IO size, corresponding label type, symbol
  • ④Information for each partition:
  • ----Device: partition name, here are sda1, sda2, sda3
    ----Boot: indicates whether it is the current boot partition (boot partition). If there is an asterisk here, it means that this is the boot partition. Here it is obvious that the mount point of sda1 is boot.
    ----Start and End: The location where the partition starts and ends. At the beginning of our configuration, we always start from 2048, so the current size is a little bit vacant.
    ----Blocks: the capacity of the current partition
    ----ID: the ID of the current partition type
    ----System: the type of the current system partition. The two partitions sda1 and sda3 are the standard partitions of the system, and their names are called Linux. The second swap partition is Linux swap or Salaries.

fdisk

Some configurations:
Currently we only have one hard disk, and we need to add another one.

insert image description here

The default disk type is SCSI, create a new virtual disk, the disk size is 20GB, and the default name.

insert image description here

Then it can be seen that there is a new hard disk

insert image description here

View: We can see that there is still only the original sda hard disk
when we enter it .fdisk -l

insert image description here

Then input lsblkcan also see that there is only the hard disk sda.

insert image description here

This is because hard disks are not like CDs that can be hot-started and hot-swapped. We have to reboot now, so we reboot directly. Now execute lsblk again, you can see that there is sdb.

insert image description here

Execute fdisk -l, you can see that there is an additional sdb, and its type is also a hard disk. There is no partition under it, and there is no mount point, so the next thing we need to do is to process sdb.

insert image description here

Hard disk partition

Basic syntax: fdisk hard disk name
We execute it here fdisk /dev/sdb, see here is a very clear Chinese prompt

insert image description here

We type M for help

insert image description here

The main command operations we can use are as follows:

  • n: Add a new partition.
  • q: Quit without saving any changes.
  • w: save and exit (write the information of the partition table into the hard disk, and then exit directly).
  • p: Print the information of the current partition table, which is the same as the information seen by fdisk -l outside, but what we see is only the information of the disk sdb itself. We enter n to partition, and then we need to select the type of partition.

**p: p refers to primary, the primary partition. ** For Linux, there are up to 4 primary partitions in one disk. e: If you want to divide more partitions, you can replace a primary partition here with an extended partition. After replacing it with an extended partition, several logical partitions can be expanded. Strictly speaking, a logical partition is not a real partition. The maximum number of logical partitions can be divided into 12. The first four primary partitions are 1 2 3 4, and the subsequent extended partition numbers are 5 to 16.
We enter p here, or just press Enter. Then enter the partition number, we can directly enter 1.

insert image description here

Next, select the default option, set the starting sector to 2048, and the partition size to 20G

insert image description here

Then enter p to view.

insert image description here

Next, press w to save and exit directly, and start synchronizing the disk

insert image description here

Then we enter lsblk -fto view, and we can find that the partition information of sdb already exists. But there is no file system and no UUID here, so the system has no way to recognize it. Because we haven't formatted it, we don't know what file system to use, of course, no UID will be assigned, and there is no corresponding mount point.

insert image description here

format

Basic syntax: mkfs -t file system type device name (make file system)
we input mkfs -t xfs /dev/sdb1, the result is as follows.

insert image description here

Input lsblk -f, you can see that the type here is also there, and the UUID behind it is also there. Now it is the last step to mount.

insert image description here

mount

Thinking: Where shall we mount sdb1 next? Before we had a hard disk called sda, which had 3 partitions. The first partition is mounted under the boot, the second partition is swap, and the third partition is directly mounted under the root directory. Except for the content under boot, all other content is stored in the third partition of the hard disk sda, so there are too many things stored in it. Are there home directories of various ordinary users under our /home/ directory, such as atguigu? We hope to put all the things under atguigu on a new hard disk, that is, the new hard disk we added sdb,
mount
the basic syntax: mount device name mount point
We enter mount /dev/sdb1 /home/atguigu/, you can see that sdb1 is mounted under atguigu.

insert image description here

Test:
Use df -hto check the space occupied by the current hard disk. Some initial configuration information has been written in the current sdb disk, which occupies 33MB.

insert image description here

We now copy a large file (xshell installation package) to /home/atguigu, and we can see that the used size of sdb1 has become larger.

insert image description here

uninstall

We use to umount /dev/sdb1uninstall sdb1, and then use it df -hto see that the uninstallation is successful without any sdb information.

insert image description here

Using lsblk can also recognize this hard disk, but it has been uninstalled, and the corresponding files cannot be accessed.

insert image description here

The /athhuigu directory is also divided into the sda3 partition. Let's check the content under atguigu, and we can see that there is no installation package, only these two configuration files.

insert image description here

Process management class

1. Check the process

1.1 Basic usage

basic concept

A process is a program or command being executed. Each process is a running entity, has its own address space, and occupies certain system resources. Some processes stay in memory for a very short time (such as ls, cd commands), while some processes (such as network services) will always stay in memory. This kind of process is called a service.

In the Linux system, processes can be divided into two categories:
①Processes displayed in the foreground
②Processes running in the background.
System services often belong to background processes, and the process that specifically executes these system services is generally called a daemon process. The commands of these daemons all end in d.service, and they are all in /usr/lib/systemd/system.

insert image description here

Then ls /usr/lib/systemd/system | grep d.servicefilter, you can see many daemons.

insert image description here

There is a sshd.service in it. When we use Xshell for remote connection, we use the SSH service to connect to our virtual machine. sshd.service is the daemon process of the SSH service.

windows view process

ctrl + alt + del to open the task manager, these processes are divided into two categories: application and background processes, we can think that what is running in the foreground is a user process, and most of these background processes are system services.

Linux view process

ps: process status process status
basic syntax:
ps aux | grep xxx (function description: view all processes in the system)
ps -ef | grep xxx (function description: you can view the relationship between child and parent processes)
option description:

  • a: list processes of all users with terminals
  • x: List all processes of the current user, including processes without a terminal
  • u: User-friendly display style
  • -e: list all processes
  • -u: list all processes associated with a user
  • -f: display process listing in full format

Example: Use ps to display processes as shown below.

insert image description here

It only displays the processes invoked by the current user and all processes associated with the current terminal console, so there are very few.
Option classification:

  • ①The style with bars is the standard unix style.
  • ②The style with the bar is the standard BSD style. Linux inherited from Unix, and during the evolution of Unix, there was a derivative version, BSD, which had an impact on later Apple systems.

BSD-style:
example: ps auxshow related process information using

We can see that there are a lot of processes here, and we can make a pipeline ps aux | moreto display them by flipping pages.

Example: Execute ps -ef | morethe command to display process information.

There is a PPID in it, which is actually the ID of his parent process.
Note: You need to be careful when writing these commands, for example, ps auxdon’t write them as ps -auxx, because if there happens to be user x, the process related to x will be displayed. But by default, if the X user cannot be found, Linux will automatically understand it ps aux.

1.2 Detailed process information

Information about ps aux

Enter ps aux | moreto display information about the process.

  • USER: Which user spawned the process
  • PID: ID number of the process
  • %CPU: The percentage of CPU resources occupied by the process, the higher the occupation, the more resources the process consumes
  • %MEM: The percentage of physical memory occupied by the process, the higher the occupation, the more resources the process consumes
  • VSZ: The size of the virtual memory occupied by the process, in KB
  • RSS: the size of the actual physical memory occupied by the process, in KB
  • TTY: Which terminal the process is running in.
----问号:表示它没有任何的终端
----tty1:图形化终端(早期的交互式终端就是一个类似于打字机的东西,所以叫tele type writer)
----tty2-tty6:本地的字符界面终端。前面我们按ctrl +alt +F1~F6进入的大黑屏就是这几个东西。
----pts/0-255:代表虚拟终端。不管是在当前的虚拟机里边直接打开一个终端,还是在X shell里面打开一个终端,其实产生的都是一个虚拟终端。
  • STAT: Process status.
----R:运行状态
----S:睡眠状态。进程已经启起来了,但是现在没有进行处理,可能在等待某个事件或信号来唤醒它继续执行。
----T:暂停状态
----Z:僵尸状态。这个进程已经马上要结束了,但是它有一些信息还没有删除,父进程可能还需要它的一些信息,只剩下了一个空壳。正常情况下当它的父进程退出的时候就会把它彻底清掉。
----s:包含子进程
----l:多线程
----+:前台显示
----<:很高的优先级
----N:很低的优先级
  • START: The start time of the process
  • TIME: CPU computing time occupied by the process
  • COMMAND: The command name that spawned this process

Note:
①Virtual memory occupies more, which is larger than physical memory. Isn't the swap partition used to expand physical memory? Why do you start using virtual memory when the physical memory is not used up?

Answer: Linux has a complete set of mechanisms for memory management. If there are some pages in the memory that are not commonly used, it is not to wait until the memory is full before replacing it with the virtual memory outside. Instead, as long as it is judged that some pages in the current memory have not been used for a long time, they will be directly replaced in the virtual memory. Only those that are frequently used are kept in the current physical memory, the current memory usage will be less, and it will be more efficient, and the physical memory can be freed up as much as possible to handle more things.

②The first one is the number one process with pid 1, /usr/lib/systemd/systemdthe command executed by this process.

The second process is responsible for the scheduling and management of all kernel threads and always runs in the kernel space. So you can see later that many system-level services run in the form of system threads

ps -ef info

Run ps -ef, the information is as follows.

insert image description here

The main bodies are similar, the biggest difference is that there is an extra PPID and C.

PPID: PID of the parent process. The parent process of process number one systemd and process number two kthreadd is process number 0. Process 0 is special, it is an idle process. Because process number one is the first user process started, the parent process that starts it can only be a system-level process.

C: Factor used by the CPU to calculate execution priority. A larger value indicates that the process is a CPU-intensive operation, and the execution priority will be lowered; a smaller value indicates that the process is an I/O-intensive operation, and the execution priority will be increased.

Summarize

If you want to check the CPU usage and memory usage of the process, you can use aux;
if you want to check the parent process ID of the process, you can use -ef

1.3 Check the remote login process

The remote login process is related to ssh. Using ps -ef | grep sshdthe filter and ssh-related processes, three results were found.

insert image description here

① The first one is the command that we directly start the sshd service. The PID of the corresponding process is 1130, and its parent process is directly the No. 1 process. We use to systemctl status sshdview the status of the ssh service, we can see that it is running, and it is started at boot, that is, it is started directly by the first process.

insert image description here

②The second means that our XShell opened a virtual terminal as root to log in to our remote server. It is a child process created by the sshd daemon process.
③ Finally, there is a grep --color=auto sshd, which is the corresponding one generated by our current ps and then screening sssh command.
Create a new remote connection, log in as root user
Create a new link in XShell, log in as root

insert image description here

insert image description here

Then execute it ps -ef | grep sshd, and you can see that there are more pts/1, which is our current remote connection.

insert image description here

Create a new remote connection and log in as a normal user t
Create a new link in XShell and log in as a normal user.

insert image description here

insert image description here

Then execute it ps -ef | grep sshd, and you can see that there are two more sshd processes.

insert image description here

Reason: atguigu@pts/1Obviously, we have opened another remote login terminal here, and the user is atguigu. There is one above atguigu [priv], the user is root. This is mainly for privilege separation. A process is listed individually. It retains the Root identity, and we use this process to perform operations when we want to use root privileges to perform some operations. If it is an ordinary Aite Silicon Valley user to operate, use the following remote login process to operate. In this way, permissions are separated, and we can get better guarantees in terms of security and performance.

2. Terminate the process

kill terminates the process

①kill [option] process number (function description: kill process by process number)
②killall process name (function description: kill process by process name, also supports wildcards, which is very useful when the system becomes very slow due to excessive load) example: Now we log
in two root and one atguigu user remotely

insert image description here

Now terminate the atguigu user. You can use kill 3081 or kill 3085. Now look at the process information, you can find that the atguigu process is gone.

insert image description here

Going back to XShell, you can also see that the link showing atguigu has been closed by other hosts.

insert image description here

Example: You can also kill another remote connection process that uses root login

insert image description here

Back in XShell, you can see that the link is broken.

insert image description here

Example: You can also kill yourself, and disconnect directly after Killing.

insert image description here

Example: Now we reconnect these three remote connection users, and then prepare to kill their daemon process 1163

insert image description here

We execute kill 1163, and we can see that the parent processes of the three remote login processes have all changed to 1.

insert image description here

In addition, we can no longer log in other users through remote connections. And if these three remote connection processes are closed, they will no longer be able to connect.
Solution: Check the status of the sshd service, you can see that it is closed

insert image description here

So we can reopen it, and we can see that it is running.

insert image description here

Now we log in the atguigu user again, and we can see that the parent process of the atguigu process is the child process of the sshd daemon process 3740 we restarted.

insert image description here

Note: We have always had a process representing the current sshd command here grep -color=auto sshd, let's see who its parent process is. passps -ef | grep 3862

insert image description here

You can see that its parent process is our terminal interface bash. Now the calling relationship of sshd is obvious: 1->sshd->remote connection->bash.

kill -9 Forcefully terminate the process

We want to see all current bash

insert image description here

Now we want to kill the virtual terminal process 3402 of pts1, but we cannot kill it after trying. Because the Shell console is a running process, it will not work if you kill it directly.

insert image description here

Solution: kill 9 process
This -9 represents the signal value of the system, representing the kill signal.

insert image description here

Now we forcibly kill the bash of pts2, and we can see that it was successfully killed.

insert image description here

killall sshd
killall needs to be used carefully. For example, if we use killall sshd here, it can be seen that all windows are disconnected. Even the daemon process is turned off, we can't reconnect, we can only go to the server to turn on sshd.

insert image description here

3. View the process tree

pstree

①Basic grammar: pstree [选项]
②Option description:

  • -p: display the PID of the process
  • -u: Display the user who owns the process

Example: Use pstree to view the process tree, you can see that the initial process is systemd, and then many processes are extended from it.

insert image description here

Use pstree -pto display the pid.insert image description here

Use pstree -pto display the belonging user.

insert image description here

4. Real-time monitoring process

top Real-time monitoring system

ps: It’s more like a snapshot. It takes a snapshot of the currently active process information, and then uses more or less to display it in pages to see what’s inside.
top: real-time monitoring display, it will open an interactive interface for us, and then refresh the current process information in real time.
Example: Run top, the information is as follows, there are two parts.

insert image description here

① The first line of the upper part
: top is the name of the current program; display the current time; an up; the duration since the system started running; how many users have logged in now (1 on the server side + XShell 3 + the server-side graphical desktop environment was also a root user when it came in); Row: the total number of tasks (processes)
. Followed by the number of processes in various states.
The third line: CPU usage.

  • ----us represents the ratio of CPU time occupied by the user process (the user process without changing the priority by default);
  • ----sy represents the percentage of the current CPU time occupied by the system process.
  • ----ni is the first two letters of the nice command. The nice command can assign a friendly value to the running process. The higher the friendly value, the friendlier the process, the lower its priority, and it will always make way for others. If the friendliness value is lower, its priority is higher, which means that it is particularly powerful and should be run first. So here refers to the proportion of all user processes in the CPU running time after the current priority is adjusted by the nice command;
  • ----id represents the time when the CPU is idle;
  • ----wa is the first two letters of wait, showing the proportion of time spent waiting for IO operations. Many processes may have finished their work, but they need to wait for an IO input and output message, then they will wait at this time, and the time taken up is displayed here.
  • ----hi is the abbreviation of hardware interrupt, which represents the proportion of hard interrupt service request time;
  • ----si is the abbreviation of softwareinterrupt, which represents the proportion of soft interrupt service request time;
  • ----st represents the proportion of time our machine is occupied by virtualization devices.

The fourth line: the current memory usage, the unit is KB.
The fifth line: the current virtual memory usage, the unit is KB.
②The following part is about the status of the overall system operation.

  • ----PID represents the number of the process
  • ----USER represents which user is calling the process
  • ----PR(priority) represents the priority of the current task scheduling
  • ----NI represents the nice value specified by the user
  • ----VIRT represents the size of virtual memory occupied
  • ----RES represents the size of the actual memory usage
  • ----SHR represents the size occupied by shared memory
  • ----S represents the state of the current process. Currently most of them are S(sleeping)
  • ----%CPU represents the proportion of CPU computing time of the current process
  • ----%MEM represents the proportion of the current process memory
  • ----TIME+ represents the running time of the process (the total time occupied by the CPU). This plus sign means that the current accuracy is accurate to two percent behind the second, that is, accurate to 0.01 second.
  • ----COMMAND represents the command to generate the current process.

Toggle sort order:

  • ①M or shift m, sort according to the occupied memory from large to small.

  • ②P or shift p, sort according to the occupied CPU time from large to small (this is the default sort).

  • ③N or shift n, sort according to PID from large to small.

Option Description

  • -d seconds: Specify the top command to update every few seconds, the default is 3 seconds.
  • -i: Make top not display any idle or dead processes.
  • -p: Only monitor a certain process by specifying the monitoring process ID.

Example: Use the top -p command to filter out idle processes. The standard of idleness is that the CPU has not been occupied since the last time it was displayed until the present time. Therefore, the process status may not always be R. It may occupy the CPU and sleep again, so it may also be S.

insert image description here

Example: During monitoring, press the lowercase u, and then enter the user name to monitor the process of the specified user. For example, we log in to atguigu in XShell, and then come here to specify the process to display XShell.

insert image description here

Run, you can see the process related to atguigu.

insert image description here

Example: Press k during the monitoring process, and then enter the PID to kill the specified process.

insert image description here

Then you need to enter the signal name or signal value

insert image description here

5. Network status and port monitoring

netstat

Basic usage:
netstat -anp | grep process number (function description: view the process network information)
netstat –nlp | grep port number (function description: view network port number occupation)
option description:

  • -a: Display all listening and unlistening sockets
  • -n: Refuse to display aliases, and all numbers that can be displayed are converted into numbers
  • -l: Only list the service status that is listening
  • -p: indicates which process is calling

Example: Use the command netstat -anp | lessto view

insert image description here

Proto: The network protocol corresponding to the network transmission using sockets. The tcp here refers to some protocols related to IPV6.

Recv-Q: The number of bytes that has not been copied by the user program connected to the current socket, that is, the number of bytes that have been received but not yet copied.

Send-Q: The number of bytes that have been sent but the remote host has not confirmed the receipt, it may be data that has been lost and needs to be retransmitted.

Local Address: The current address (in the form of socket), that is, IP+colon+port number.

----0.0.0.0: All local addresses of the machine
----127.0.0.1: Loopback address, generally used for local monitoring and testing

Foreign Address: remote address

192.168.123.1 is the IP address of our physical PC. Here we have two users connected to port 22 of the VM through different ports, and the status is ESTABLISHED.

insert image description here

6. System timing tasks

Basic syntax: crontab [选项]

The daemon process of crontab: crond

Option description:

  • -e: edit crontab timing tasks
  • -l: query crontab tasks
  • -r: delete all crontab tasks of the current user

Parameter description:
①Enter the crontab editing interface. will open vim to edit your work

insert image description here

②Special symbols

insert image description here

③ Execute commands at a specific time

insert image description here

Example: Enter crontab -eto enter the edit page, and then enter */1 * * * * echo "hello, world" >> /root/hello, which means adding the sentence hello, world to the hello file every minute.

insert image description here

Then look at the hello file every minute, and you can see that there is content.

insert image description here

We can also use tail -f hello to view the changes in the hello file

insert image description here

You can also use crontab -l to view the contents of the scheduled task file

insert image description here

Finally, crontab tasks can be cleared with crontab -r

Guess you like

Origin blog.csdn.net/AN_NI_112/article/details/131503314