The most complete Linux interview questions in history (2020 latest edition)


The most complete Linux interview questions in history (2020 latest edition)
Author: thinkwon

Introduction:This article sorted out the latest Linux interview questions, nearly 30000 words, about 100 questions, shared here, I hope to help you.

1、 Linux overview

1. What is Linux

Linux is a set of free use and free dissemination of UNIX like operating system. It is a multi-user, multi task, multi-threaded and multi CPU operating system based on POSIX and UNIX. It can run major UNIX tools, applications and network protocols. It supports 32-bit and 64 bit hardware. Linux inherits the design idea of UNIX with network as the core, and is a stable multi-user network operating system.

2. What is the difference between UNIX and Linux?

Linux and UNIX are both powerful operating systems and widely used server operating systems. There are many similarities. Some people even mistakenly believe that UNIX and Linux operating systems are the same. However, this is not the case. Here are the differences between them.

1. Open source
Linux is an open source operating system, which can be used without paying for it; UNIX is a traditional commercial software that protects the intellectual property rights of the source code, so it needs to be paid and authorized to use.

2. Cross platform
Linux operating system has good cross platform performance and can run on a variety of hardware platforms; UNIX operating system has weak cross platform performance, and most of them need to be used with hardware.

3. Visual interface
In addition to the command line operation, Linux also has a form management system; UNIX is just a system under the command line.

4. Hardware environment
Linux operating system has lower requirements on hardware, and the installation method is easier to master; UNIX has more stringent requirements on hardware, so it is more difficult to install.

5. User groups
Linux has a wide range of users, both individuals and enterprises can use it; UNIX user group is relatively narrow, most of which are used by large enterprises with high security requirements, such as banks, telecom departments, or UNIX hardware manufacturers, such as sun, etc.

Compared with UNIX operating system, Linux operating system is more popular with computer enthusiasts. The main reason is that Linux operating system has all the functions of UNIX operating system, and can realize all UNIX features on ordinary PC computers. It is open-source and free, so it is easier to be widely used!

3. What is Linux kernel?

The kernel of Linux system is kernel. The kernel controls all hardware and software on a computer system, allocates hardware when necessary, and executes software as needed.

System memory management
Application management
Hardware device management
File system management

4. What are the basic components of Linux?

Like any other typical operating system, Linux has all these components: the kernel, shell and GUI, system utilities and applications. The advantage of Linux over other operating systems is that every aspect comes with other functions, and all the code can be downloaded for free.

5. Linux architecture

In a big way, Linux architecture can be divided into two parts:
The most complete Linux interview questions in history (2020 latest edition)

User space: user space includes user applications and C library.

Kernel space: kernel space includes system call interface, kernel and architecture dependent kernel code.

Why is Linux architecture divided into user space and kernel space?

1. Modern CPU implements different working modes, and the instructions and registers that CPU can execute are different in different modes.
In order to protect the security of Linux system, we divide it into two parts.

User space and kernel space are two different states of program execution. We can complete the transfer from user space to kernel space in two ways: 1) system call; 2) hardware interrupt.

6. What is the basic difference between Bash and DOS?

There are three main differences between Bash and DOS console

1. Bash commands are case sensitive, while DOS commands are not;
2. In Bash, / character is the directory separator and the escape character is. In DOS, / is used as the command parameter separator and \ \ is the directory separator
3. OS follows the Convention of naming file, that is, the file name of 8 characters is followed by a dot, and the extension name is 3 characters. Bash doesn’t follow this Convention.

7. Linux boot process?

1. Host power on self-test, load BIOS hardware information.
2. Read MBR boot files (grub, Lilo).
3. Boot Linux kernel.
4. Run the first process init (the process number is always 1).
5. Enter the corresponding operation level.
6. Run the terminal and input the user name and password.

8. The default running level of Linux system?


Shut down.
Stand alone user mode.
Multi user mode of character interface (network not supported).
Multi user mode of character interface.
Not allocated for use.
Multi user mode of graphical interface.

9. How to communicate between processes in Linux?

  • Pipe, flow pipe (s_ FIFO, pipe.
  • Signal.
  • Message queuing.
  • Shared memory.
  • Semaphore.
  • Socket.

10. What system log files does Linux have?

More important is the / var / log / messages log file.

The log file is a summary of many process log files, from which you can see any intrusion attempt or successful intrusion. In addition, if there is a centralized collection of elk logs in fat friend’s system, it will also be collected.

11. Is it helpful for Linux to install multiple desktop environments?

Usually, a desktop environment, such as KDE or gnome, is enough to run without problems. Although the system allows switching from one environment to another, this is a priority for users. Some programs work in one environment and cannot work in another, so it can also be seen as a factor in choosing which environment to use.

12. What is swap space?

Swap space is a certain space used by Linux to temporarily save some concurrent programs. This happens when ram does not have enough memory to hold all the programs being executed.

13. What is root account

The root account is like a system administrator account that allows you to take full control of the system. Here you can create and maintain user accounts, assigning different permissions to each account. This is the default account every time you install Linux.

14. What is Lilo?

Lilo is the Linux boot loader. It is mainly used to load the Linux operating system into main memory so that it can start running.

15. What is Bash?

Bash is an abbreviation for Bourne again shell. It was written by Steve Bourne as an alternative to the original Bourne shell (represented by / bin / SH). It combines all the features of the original Bourne shell, along with other features, to make it easier to use. Since then, it has been adapted to be the default shell for most systems running Linux.

16. What is cli?

Command line interface (CLI) is the most widely used user interface before the popularity of graphical user interface. It usually does not support the mouse. Users input commands through the keyboard, and the computer will execute the instructions after receiving them. It is also called the character user interface (Cui).

It is generally believed that the command line interface (CLI) is not as convenient as the graphical user interface (GUI). Because, the software of command line interface usually needs the user to memorize the operation command, but because of its own characteristics, the command line interface saves the computer system resources compared with the graphical user interface. On the premise of memorizing commands, the operation speed of command line interface is often faster than that of graphical user interface. Therefore, there is an optional command line interface in the GUI operating system.

17. What is GUI?

Graphical user interface (GUI) refers to the computer operation user interface which is displayed graphically.

Graphical user interface (GUI) is a kind of interface display format for communication between human and computer. It allows users to use mouse and other input devices to manipulate icons or menu options on the screen to select commands, call files, start programs or perform other daily tasks. Compared with the character interface, which uses keyboard to input text or character commands to complete routine tasks, graphical user interface has many advantages.

**18. What are the advantages of open source?

Open source allows you to distribute software (including source code) for free to anyone interested. Then, people can add features and even debug and correct errors in the source code. They can even make it run better and then be free to redistribute these enhanced source code again. This ultimately benefits everyone in the community.

19. What is the importance of the GNU Project?

This so-called free software movement has many advantages, such as the freedom to run programs and the freedom to learn and modify programs according to your needs. It also allows you to redistribute copies of the software to others, as well as the freedom to improve the software and distribute it to the public.

2、 Disk, directory, file

1. Simple Linux file system?

In Linux operating system, all the resources managed by the operating system, such as network interface card, disk drive, printer, I / O device, ordinary file or directory, are regarded as a file.

In other words, there is an important concept in Linux system * *: everything is a file * *. In fact, this is a reflection of UNIX philosophy, and Linux comes from rewriting UNIX, so this concept is inherited. In UNIX system, all resources are regarded as files, including hardware devices. In UNIX system, every hardware is regarded as a file, which is usually called a device file, so that users can access the hardware by reading and writing files.

Linux supports five file types, as shown in the figure below: file types

The most complete Linux interview questions in history (2020 latest edition)

2. What is the directory structure of Linux?

This question is not usually asked. More is the actual use, need to know.

The structure of Linux file system is distinct, just like an inverted tree. The top level is its root directory
Directory structure of Linux

Common catalog Description:

  • /Bin: stores binary executable files (LS, cat, MKDIR, etc.), where commonly used commands are usually located;
  • /Etc: store system management and configuration files;
  • /Home: the root directory where all user files are stored, which is the base point of user’s home directory. For example, the home directory of user is / home / user, which can be represented by ~ user;
  • **/Usr * *: used to store system applications / opt: the location where additional installed optional application packages are placed. Generally, we can install Tomcat and others here;
  • /Proc: virtual file system directory, which is the mapping of system memory. You can directly access this directory to obtain system information;
  • /Root: the home directory of the super user (system administrator) (privilege class o);
  • /SBIN: stores binary executable files that only root can access. Here are the system level management commands and procedures used by the system administrator. Such as ifconfig, etc;
  • /Dev: used to store device files;
  • /MNT: the installation point for the system administrator to install the temporary file system. The system provides this directory for users to temporarily mount other file systems;
  • /Boot: stores various files used for system boot;
  • **/Lib * *: stores library files related to system operation;
  • /TMP: used to store all kinds of temporary files. It is a public temporary file storage point;
  • /Var: it is used to store the files that need to be changed at runtime. It is also the overflow area of some large files, such as log files of various services (system startup log, etc.) Etc;
  • /Lost + found: this directory is usually empty, the system shut down abnormally and left “homeless” files (what is called. Chk under Windows) here.

3. What is inode?

Generally speaking, inode is not asked in an interview. However, inode is an important concept, which is the basis of understanding UNIX / Linux file system and hard disk storage.

Understanding inode starts with file storage.

Files are stored on the hard disk. The minimum storage unit of the hard disk is called “sector”. Each sector stores 512 bytes (equivalent to 0.5 KB).

When the operating system reads the hard disk, it does not read one sector at a time, which is too inefficient. Instead, it reads multiple sectors at a time, that is, a “block” is read at a time. This “block” composed of multiple sectors is the smallest unit of file access. The most common size of “block” is 4KB, that is, eight consecutive sectors form a block.

File data is stored in “blocks”. Obviously, we must find a place to store the meta information of the file, such as the creator of the file, the creation date of the file, the size of the file, and so on. This area for storing file meta information is called inode, which is translated as “index node” in Chinese.

Each file has a corresponding inode, which contains some information related to the file.

This paper briefly describes the working process of converting the logical structure and physical structure of files in Linux file system through I node?

Generally speaking, the interviewer is not very good at asking this question.

Linux transforms the logical structure and physical structure of files through inode node table.

· Inode node is a 64 byte long table, which contains the relevant information of the file, including the file size, file owner, file access permission method and file type and other important information. The most important content of inode node table is disk address table. There are 13 block numbers in the disk address table. The file will read the corresponding blocks in the order that the block numbers appear in the disk address table.
·Linux file system connects the inode node with the file name. When the file needs to be read, the file system looks up the entry corresponding to the file name in the current directory table, thus obtaining the corresponding inode node number of the file. Through the disk address table of the inode node, the physical blocks of the scattered files are connected into the logical structure of the file.

What are soft links and hard links?

1) Hard link

Since files in Linux are identified by inode, hard link can be regarded as a pointer to file index node, and the system does not reallocate inode for it. For each hard link added, the number of links in the file is increased by 1.

Disadvantages: 1) it is not possible to establish links between files in different file systems; 2) only super users can create hard links for directories.

2) Soft link

Soft link overcomes the shortage of hard link. There is no limitation of any file system. Any user can create symbolic link to directory. Therefore, it is now more widely used, it has greater flexibility, and even can link files across different machines and networks.

Disadvantages: because the linked file contains the path information of the original file, when the original file is moved from one directory to another directory, and then the linked file is accessed, the system can’t find it, while the hard link does not have this defect. You can move it as you want. In addition, it requires the system to allocate extra space for establishing new index nodes and saving the path of the original file.

In the actual scenario, the basic is to use soft links. The differences are summarized as follows:

· Hard link can not cross partition, software chain can cross partition.
· A hard link points to an inode node, while a soft link creates a new inode node.
· Deleting the hard link file will not delete the original file, while deleting the soft link file will delete the original file.

5. What is raid?

Raid is called redundant array of independent disks. The basic idea of raid is to combine several relatively cheap hard disks into a hard disk array group, so that the performance can reach or even surpass a hard disk with high price and huge capacity. Raid is usually used in the server computer, using the same hard disk to form a logical sector, so the operating system will only regard it as a hard disk.

Raid is divided into different levels, and each level makes different trade-offs in data reliability and read-write performance. In practical application, you can choose different raid schemes according to your actual needs.

Of course, because many companies use cloud services, it is very difficult for us to get into touch with the concept of raid. More likely, it is the concept of ordinary cloud disk and SSD cloud disk.

3、 Security

1. What security work does a Linux system need to do after initializing the environment?

1. Add normal user login, prohibit root login, and change SSH port number.

Modifying the SSH port is not necessarily absolute. Of course, if you want to expose the Internet, it is recommended to change. L

2. The server uses the key to log in, and the password is not allowed.

3. Open the firewall, close SELinux, and set the corresponding firewall rules according to the business requirements.

4. Install fail2ban, a software to prevent SSH from brute force.

5. Set to allow only the company’s office network export IP can log in to the server (depending on the actual needs of the company)

You can also install VPN and other software, only allow VPN to connect to the server.

6. The number of modification history command records is 10.

7. Only servers that need access to the Internet are allowed, and all others are prohibited.

8. Do a good job in software protection.
8.1 setting nginx_ WAF module prevents SQL injection.
8.2 start the web service with WWW users and change the owner and group of the website directory to www.

2. What is CC attack? What is DDoS attack?

CC attack, mainly used to attack the page, simulate multiple users to visit your page constantly, so that your system resources are exhausted.

DDoS attack, Chinese name is distributed denial of service attack, refers to the use of server technology to combine multiple computers as an attack platform to launch DDoS attacks on one or more targets.

Attack, that is, through a large number of legitimate requests to occupy a large number of network resources, in order to achieve the purpose of paralyzing the network.

How to prevent CC attack and DDoS attack?

Anti CC, DDoS attacks, these can only be done with hardware firewall traffic cleaning, the attack traffic into the black hole.

In this area of traffic cleaning, it’s OK to buy anti attack services from ISP service providers. The computer room generally has free traffic. We usually buy services. After all, the attack will not last for a long time.

3. What is website database injection?

Due to the uneven level and experience of programmers, most programmers do not judge the validity of user input data when writing code.

There are security risks in the application. Users can submit a piece of database query code, according to the results returned by the program, get some data he wants to know, which is called SQL injection.

SQL injection is accessed from the normal www port, and it seems that it is no different from the general web page access. If the administrator does not have the habit of looking at the log, it may not be detected for a long time.

How to filter and prevent?
The database webpage side injects this, may consider using nginx_ WAF is used for filtration and prevention.


1. What is a shell script?

A shell script is a text file containing one or more commands. As system administrators, we often need to use multiple commands to complete a task. We can add all these commands to a text file (shell script) to complete these daily tasks.

-What is the default login shell?

In Linux operating system, “/ bin / bash” is the default login shell, which is assigned when the user is created.

Use the Chsh command to change the default shell. An example is as follows:

##Chsh < user name > - s < New shell >

-How do I write comments in a shell script?

Comments can be used to describe what a script can do and how it works. Each line of comment begins with ා. Examples are as follows:


2. Grammar level

  • What types of variables can be used in shell scripts?

In shell scripts, we can use two types of variables:

System defined variables: system variables are created by the system itself. These variables are usually made up of uppercase letters and can be viewed through the set command.

User defined variables: user variables are generated and defined by system users, and the values of variables can be viewed through the command “echo $< variable name >”.

  • What is the use of the $? Tag in shell scripts?

When writing a shell script, if you want to check whether the previous command was executed successfully, use $? In the if condition to check the end state of the previous command.

· If the end status is 0, the previous command was executed successfully. For example:

  [email protected]:~## ls /usr/bin/shar

·If the end state is not 0, the command execution failed. For example:

 [email protected]:~## ls /usr/bin/share
  • What are the special variables in Bourne shell (bash)?

The following table lists the special variables Bourne shell sets for the command line.

Built in variable interpretation
  • How to cancel a variable or its assignment?

The unset command is used to cancel a variable or its assignment. The syntax is as follows:

##Unset < variable name >
  • How to nest if syntax in shell script?
If [condition]
  • How do I compare two numbers in a shell script?

Use the test command (- GT, etc.) in if then to compare two numbers. For example:

  • Syntax of case statement in shell script?

The basic grammar is as follows:

Case variable in

For loop syntax in shell script?

The basic grammar is as follows:-

For variable in loop list

While loop syntax in shell scripts?

·Like the for loop, the while loop repeats its command block as long as the condition holds.
·Unlike for loops, while loops iterate until their conditions are not true.

Basic grammar:

  • Basic format of do while statement?

The do while statement is similar to the while statement, but executes the command before checking the conditional statement. Here is the syntax for using the do while statement:

  • The function of break command in shell script?

A simple use of the break command is to exit an executing loop. We can jump out of the loop using the break command in the while and until loops.

  • The function of continue command in shell script?

The continue command differs from the break command in that it jumps out of the iteration of the current loop, not the entire loop. The continue command is useful in many cases, such as when an error occurs, but we still want to continue to execute a large loop.

  • How to make a script executable?

Use the Chmod command to make the script executable. Examples are as follows: Chmod a + X 。

#What is the role of! / bin / Bash?
  • How to debug shell scripts?

Use the – x ‘number (SH – x )Shell scripts can be debugged.
Another method is to use the – NV parameter (SH – NV) )。

  • How do I redirect both standard output and error output to the same location?

Method 1: 2 > & 1 (for example, LS / usr / share / doc > 0 out.txt 2>&1 ) 。
Method 2: & > (e.g. LS / usr / share / doc – >) out.txt ) 。

  • How do I test files in shell scripts?

The test command can be used to test files. The basic usage is as follows:

Test usage
  • How to define functions in shell scripts?

A function is a block of code that has a name. When we define the code block, we can call the function name in our script, and the block will be executed. An example is as follows:

$ diskusage () { df -h ; }

How to get shell to get input from terminal on script?

The read command can read data from the terminal (using the keyboard). The read command takes the user’s input and places it in the variable you give. Examples are as follows:

## vi /tmp/
  • How to perform arithmetic operations?

There are two ways to perform arithmetic operations:

1. Use expr command: ා#ාාාාාාාාාාාාාාා.
2. Use a dollar sign and square brackets ($\ [expression \]): test = $[16 + 4]; test = $[16 + 4].

4、 Programming questions

  1. Determine whether a file is a character device file. If so, copy it to the / dev directory?
  1. Add a new group as Class1, and then add 30 users belonging to this group. The user name is stdxx, where XX is from 01 to 30?
  1. Write a shell program to automatically delete 50 accounts. The account names are stud1 to stud50?
  1. Write a sed command to modify / tmp/ input.txt What is the content of the document?

·Delete all empty lines.
·In a row, if it contains “11111”, insert “AAA” before “11111” and “BBB” after “11111”. For example, change the line of “b21110000” into “bb1110000”.

[[email protected]~]## cat -n /tmp/input.txt

5、 Actual combat

1. How to choose the Linux operating system version?

Generally speaking, Ubuntu is preferred for desktop users; RHEL or CentOS is preferred for servers, and CentOS is preferred for both.

According to the specific requirements:

·For high security requirements, Debian or FreeBSD should be selected.

· Users who need to use advanced database services and e-mail network applications can choose SUSE.

·For new technology and new features, feddora is a beta and pre release version of RHEL and CentOS.

·[key] according to the current situation, most Internet companies choose CentOS. Now more commonly used is the 6 series, now the market share is about half. The other reason is that CentOS focuses more on the server domain and has no copyright restrictions.

CentOS 7 series will be used more slowly.

2. How to plan a Linux host? What are the steps?

1. Determine what the machine is used for, such as web, DB, or game server.

The configuration of the machine will be different for different purposes.

2. After confirming, it is necessary to determine how to install the system, which systems are installed by default, and how to do partitions.

3. Which parameters of the system need to be optimized, which users need to be created, etc.

**3. How to deal with users’ feedback that the website is slow to visit?


  • What kind of factors will lead to slow website access?

1. Insufficient server outlet bandwidth

The export bandwidth purchased by the server itself is relatively small. Once the amount of concurrency is large, the exit bandwidth allocated to each user will be small, and the access speed will naturally be slow.
Cross operator networks lead to bandwidth reduction. For example, if the company’s website is placed on the telecom network, the customer’s connection is Great Wall broadband or China Unicom, which may lead to the reduction of bandwidth.

2. The server is overloaded, resulting in a response failure

It can be analyzed from two aspects
Analyze the system load, use the w command or uptime command to view the system load. If the load is very high, use the top command to check the CPU and MEM usage. Either the CPU is busy or the memory is not enough.
If both are normal, use the SAR command to analyze the network card traffic and analyze whether it has been attacked. Once the cause of the problem is analyzed, corresponding measures are taken to solve the problem, such as deciding whether to kill some processes or forbid some access.

3. Database bottleneck

If there are more slow queries. Then developers or DBAs should assist in optimizing SQL statements.
If the database response is slow, consider adding a database cache, such as redis. Then, you can also set up MySQL master-slave. One MySQL server is responsible for writing, and several other slave databases are responsible for reading.

4. Website development code is not optimized well

For example, SQL statements are not optimized, resulting in database read and write time-consuming.

  • How to check the slow website access?

1. First of all, determine whether it is a client-side or a server-side problem. When you receive feedback from users that the visit is slow, you should visit the website immediately. If your visit is fast, you can basically conclude that it is a client problem. You need to explain to the customer patiently and help the customer solve the problem.

Don’t just look at the server. We must start from the source and gradually go down.

2. If the access is slow, you can use the browser’s debugging function to see which item of data takes too much time to load, whether the image loading is slow or some data loading is slow.

3. For server load. View server hardware (network, CPU, memory) consumption. For purchased virtual machines, such as Alibaba cloud, you can log in to the alicloud platform to provide monitoring of various aspects, such as CPU, memory, and bandwidth usage.

4. If you find that the hardware resource consumption is not high, you need to check the log, such as the MySQL slow query log, to see if a SQL statement query is slow, resulting in slow website access.

  • How to solve it?

1. If it is the export bandwidth problem, then apply for increasing the export bandwidth for a long time.
2. If there are many slow queries, developers or DBAs should assist in optimizing SQL statements.
For example, if you can add a slow response database to redis. Then you can also set up MySQL master-slave, one MySQL server is responsible for writing, and several other slave databases are responsible for reading.
4. Apply for CDN service and load the user’s access.
5. If the access is slow, you need to optimize the overall architecture. To achieve a dedicated role, multiple servers provide the same service.

4. What are the methods for Linux performance tuning?

1. Disable daemons.
2. Shutting down the GUI.
3. Changing kernel parameters.
4. Kernel parameters.
5. Tuning the processor subsystem.
6. Tuning the memory subsystem.
7. Tuning the file system.
8. Tuning the network subsystem.

6、 File management command

1. Cat command

The cat command is used to connect files and print them to a standard output device.

Cat has three main functions

1. Display the whole file at one time:

cat filename

2. Create a file from the keyboard:

cat > filename

Only new files can be created. Existing files cannot be edited.

3. Merge several documents into one file:

cat file1 file2 > file

– B output line number for non null
– N output all line numbers

  • example:

(1) Add the line number to the log2012.log file and enter it into the log2013.log file

cat -n log2012.log log2013.log

(2) Add the line number (blank line is not added) to the file contents of log2012.log and log2013.log, and then attach the contents to the log.log in

cat -b log2012.log log2013.log log.log

(3) Using here doc to generate a new file

cat >log.txt <<EOF

(4) Reverse listing

tac log.txt

2. Chmod command

There are three levels of file call permission in Linux / Unix: file owner, group and others. Chmod can be used to control how files are called by others.

Used to change the access rights of Linux system files or directories. Use it to control access to files or directories. The command has two uses. One is the text setting method including letters and operator expressions; the other is the number setting method containing numbers.

There are three groups of access rights for each file or directory. Each group is represented by three digits, which are read, write and execute permissions of the file owner; read, write and execute permissions of users in the same group as the owner; and read, write and execute permissions of other users in the system. LS – l can be used test.txt Find.

Take the file log2012.log as an example

-rw-r--r-- 1 root root 296K 11-13 06:03 log2012.log

The first column has 10 positions, and the first character specifies the file type. In the usual sense, a directory is also a file. If the first character is a horizontal line, it is a non directory file. If it is D, it is a directory. From the second character to the tenth nine characters, a group of three characters represents the permissions of three groups of users on the file or directory. The permission character uses a horizontal line to represent empty permission, R for read-only, w for write, and X for executable.

Common parameters:

-C report processing information when changes occur

Scope of authority:

u: The current user of a directory or file

Permission code:

r: Read permission, denoted by the number 4
  • example:

(1) Add the executable rights of all users in the file t.log

chmod a+x t.log

(2) Revoke all the original permissions, and then make the owner have readable permissions, and output processing information

chmod u=r t.log -c

(3) Assign read, write, and execute (7) permissions to the owner of the file, assign read and execute (5) permissions to the file group, and assign execute (1) permissions to other users

Chmod 751 t.log - C (or: Chmod u = RWX, g = Rx, o = x t.log - C)

(4) Add readable permissions to all files in the test directory and its subdirectories

chmod u+r,g+r,o+r -R text/ -c

3. Chown command

Chown changes the owner of the specified file to the specified user or group. The user can be the user name or user ID; the group can be the group name or group ID; the file is a list of files separated by spaces to change permissions, and supports wildcard characters.

-C displays information about the changed part
  • example:

(1) Change owners and groups and display change information

chown -c mail:mail log2012.log

(2) Change file group

chown -c :mail t.log

(3) Change the folder and sub file directory owner and group to mail

chown -cR mail: test/

4. CP command

Copy the source file to the destination file or multiple source files to the destination directory.

Note: for command line copy, if the target file already exists, you will be prompted whether to override it. In shell script, if you do not add the – I parameter, you will not be prompted, but will directly cover!

-I hint
  • example:

(1) Copy a.txt to the test directory and keep the time of the original file. If the original file exists, you will be prompted whether to replace it.

cp -ai a.txt test

(2) Suggest a link (shortcut) for a.txt

cp -s a.txt link_a.txt

5. Find command

It is used to find files in the file tree and make corresponding processing.

Command format:

find pathname -options [-print -exec -ok ...]

Parameter command:

Pathname: the directory path found by the find command. To represent the current system root directory, for example.

Command options:

-Name finds files by file name
  • example:

(1) Find files that have been modified within 48 hours

find -atime -2

(2) Find files ending in. Log in the current directory. . represents the current directory

find ./ -name '*.log'

(3) Find the file with permission 777 in the / opt directory

find /opt -perm 777

(4) Find files larger than 1K

find -size +1000c

(5) Find files equal to 1000 characters

find -size 1000c

-The exec parameter is followed by the command command, and its termination is marked by;, so the semicolon after this command is indispensable. Considering that the semicolon has different meanings in various systems, it is preceded by a backslash. {} curly brackets represent the file name found in the previous find.

6. Head command

Head is used to display the beginning of the file to the standard output. The default head command prints the first 10 lines of the corresponding file.

Common parameters:

-N < number of lines > the number of lines displayed (the number of lines is a complex number, which means the number from the last to the front)


(1) Display the first 20 lines in the 1. Log file

head 1.log -n 20

(2) Display the first 20 bytes of 1.log file

head -c 20 log2014.log

(3) Display the last 10 lines of t.log

head -n -10 t.log

7. Less command

Less is similar to more, but with less you can browse files at will, while more can only move forward but not backward, and less will not load the entire file before viewing it.

Common command parameters:

-I ignore case when searching


(1) PS view process information and display through less page

ps -aux | less -N

(2) View multiple files

less 1.log 2.log

You can use n to see the next, and P to see the previous.

**8. Ln command

The function is to establish a synchronous link for the file in another location. When the problem is needed in different directories, it is not necessary to create the same file for each directory. The link created by ln can reduce the disk usage.

Link classification: software link and hard link

Soft link:

1. Soft link exists in the form of path. It is similar to the shortcut in Windows operating system
2. Soft link can cross file system, hard link can’t
3. Soft link can link a nonexistent file name
4. Soft link can link the directory

Hard link:

1. Hard link, in the form of file copy. But it doesn’t take up real space.
2. It is not allowed to create hard links to the directory
3. Hard links can only be created in the same file system

It should be noted that:

First, the LN command keeps the synchronization of every linked file, that is, no matter where you change it, other files will change the same;
Second, the link of LN can be divided into two types: soft link and hard link. Soft link is the target file of LN – s source file. It will only generate a file image at the location you selected, and will not occupy disk space. The hard link ln source file target file has no parameter – S, It will generate a file of the same size as the source file in the location you choose. Whether it is a soft link or a hard link, the file changes synchronously.
Third, the LN instruction is used to link files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, all the previously specified files or directories will be copied to the directory. If multiple files or directories are specified at the same time and the last destination is not an existing directory, an error message appears.

Common parameters:

-B delete and override the previously established link


(1) Create a soft link to the file and display the operation information

ln -sv source.log link.log

(2) Create a hard link to the file and display the operation information

ln -v source.log link1.log

(3) Create soft links to directories

ln -sv /opt/soft/test/test3 /opt/soft/test/test5

9. Locate command

Locate can quickly find files by searching the built-in document database of the system. The database is updated by the updatedb program, and the updatedb is called periodically by cron daemon. By default, the locate command searches the database faster than using the entire hard disk data to search for data. However, the worse thing is that the files found by locate may not be found if they are recently created or renamed. In the internal settings, updatedb runs once a day, and the set value (etc / crontab) can be updated by modifying crontab.

Like the find command, locate can be used to find regular matches such as *,?, etc

Common parameters:

-L num (number of rows to display)


(1) Find all files related to PWD (including PWD in the file name)

locate pwd

(2) Search all the files that start with SH in the etc directory

locate /etc/sh

(3) Find the file ending with reason in the / var directory

Locate - R '^ / var. * reason $' (where. Represents one character, * indicates multiple tasks;. * represents any number of characters)

**10. More command

The function is similar to cat. More will display page by page to facilitate the user to read page by page. The most basic command is to press the space key to display the next page, and press the B key to display the page back.

Parameter command:

+N is displayed from the nth line

Common operation commands:

Enter down n lines, need to be defined. The default is 1 line


(1) Displays the contents of the file from line 3

more +3 text.txt

(2) In the list of file directory details, with the help of the pipeline to display 5 lines at a time

ls -l | more -5

Display the next 5 lines by space.

11. MV command

Move the file or modify the file name according to the second parameter type (such as directory, move the file; if it is a file, command the file again).

When the second parameter is a directory, the first parameter can be multiple files or directories separated by spaces, and then move multiple files specified by the first parameter to the directory specified by the second parameter.


(1) File test.log Rename to test1.txt

mv test.log test1.txt

(2) Move the files log1.txt, log2.txt, log3.txt to the root test3 directory

mv llog1.txt log2.txt log3.txt /test3

(3) Change the name of file1 to File2. If File2 already exists, ask if you want to override it

mv -i log1.txt log2.txt

(4) Move all files under the current folder to the upper directory

mv * ../

12. RM command

Delete one or more files or directories in a directory. If the – R option is not used, RM does not delete the directory. If you use RM to delete a file, you can usually restore the file to its original state.

RM [options] file

(1) To delete any. Log files, ask for confirmation one by one before deleting:

rm -i *.log

(2) Delete the test subdirectory and all files in the subdirectory, and do not need to confirm one by one

rm -rf test

(3) Delete files beginning with – F

rm -- -f*

13. Tail command

It is used to display the content at the end of the specified file. When no file is specified, it is processed as input information. Commonly used to view log files.

Common parameters:

-F loop read (often used to view incremental log files)

(1) Loop through the incremental contents of the file

ping > ping.log &

Background run: you can use jobs – L to view it, or use FG to move it to the foreground.

tail -f ping.log

(view log)

14. Touch command

The linux touch command is used to modify the time attributes of a file or directory, including access time and change time. If the file does not exist, a new file will be created.

LS – l can display the time record of the file.


Touch [- ACFM] [- d < datetime >] [- R < reference file or Directory >] [- T < datetime >] [-- help] [-- version] [file or directory ]

Parameter Description:
A change the read time record of the file.
M change the modification time record of the file.
C if the target file does not exist, a new file will not be created. The same effect as — no create.
F is not used and is reserved for compatibility with other UNIX systems.
R uses the time record of the reference file, which has the same effect as — file.
D set the time and date in various formats.
T sets the time record of the file in the same format as the date command.
– no create does not create a new file.
– help lists the instruction formats.
– version lists version messages.


Use the command “touch” to modify the time attribute of the file “testfile” to the current system time, and enter the following command:

$touch testfile ා modify the time attribute of the file

First, use the LS command to view the properties of the testfile file as follows:

$LS - L testfile ා view the time attribute of a file

After executing the instruction “touch” to modify the file attribute, check the time attribute of the file again, as shown below:

$touch testfile ා modify the file time attribute to the current system time

When using the command “touch”, a new blank file is created if the specified file does not exist. To create a blank file in the current directory, enter the following command for example:

$touch file ා create a new blank file named "file"

15. VIM command

VIM is a text editor developed from VI. Code completion, compilation, error jump and other convenient programming functions are particularly rich, and are widely used in programmers.

Open the file and skip to line 10: VIM + 10 filename.txt 。
Open the file and jump to the first matching line: VIM + / search term filename.txt 。
Open the file in read-only mode: VIM – R / etc / passwd.

Basically, VI / VIM is divided into three modes: command mode, insert mode and last line mode.

In short, we can think of these three patterns as the following icons to show:

**16. Whereis command

The whereis command can only be used to search program names, and only binary files (parameter – b), man description file (parameter – M), and source code file (parameter – s) are searched. If the parameter is omitted, all information is returned. Both whereis and locate search based on the built-in database of the system, so the efficiency is very high, while find is to traverse the hard disk to find files.

Common parameters:

-B locate the executable file.


(1) Find the location program related files

whereis locate

(2) Find the source code file of locate

whereis -s locate

(3) Find the help file for lcoate

whereis -m locate

17. Which command

To find a file in Linux, but do not know where it is, you can use the following commands to search:

Which view the location of the executable.

Which is to search the location of a system command in the path that is specified, and return the first search result. Using the which command, you can see whether a certain system command exists and where the command is executed.

Common parameters:

-N specifies the file name length, which must be greater than or equal to the longest file name in all files.

(1) Check if the LS command exists and which one to execute

which ls

(2) See which

which which

3) View CD

Which CD (display does not exist because CD is a built-in command and which search display is a command in path)

To view the current path configuration:

echo $PATH

Or use env to view all environment variables and their corresponding values

7、 Document editing command

1. Grep command

Powerful text search command, grep (Global regular expression print) global regular expression search.

Grep works by searching for string templates in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as file names. The results of the search are sent to standard output without affecting the contents of the original file.

Command format:

grep [option] pattern file|dir

Common parameters:

-A n -- after context displays n lines after matching characters

Grep’s regular expression:

^# anchor the beginning of a row, such as: '^ grep' matches all rows that begin with grep.


(1) Find the specified process

ps -ef | grep svn

(2) Find the number of specified processes

ps -ef | grep svn -c

(3) Read keywords from file

cat test1.txt | grep -f key.log

(4) Recursively finds lines that start with grep from the folder and lists only the files

grep -lR '^grep' /tmp

(5) Find row contents for non-x switches

grep '^[^x]' test.txt

(6) Displays content lines that contain the ED or at characters

grep -E 'ed|at' test.txt

2. WC command 

WC (word count) function is to count the number of bytes, words and lines in the specified file, and output the statistical results

Command format:

wc [option] file..

Parameter command:

-C count the number of bytes

(1) Find the file’s line number, word number, section number, file name

wc text.txt


7     8     70     test.txt

(2) Count the number of rows in the output

cat test.txt | wc -l

8、 Disk management command

**1. CD command

CD (changedirectory) command syntax:

CD [directory name]

Note: switch the current directory to dirname.

(1) Enter the desired directory

cd /

(2) Enter the “home” directory

cd ~

(3) Enter the last working path

cd -

(4) Use the parameters of the previous command as the CD parameter.

cd !$

2. DF command

Displays disk space usage. Get the information about how much space the hard disk is occupied and how much space is left. If no file name is specified, the available space of all currently mounted file systems will be displayed. By default, disk space is displayed in 1KB units, unless the environment variable posixly_ Correct is specified so that it will be displayed in 512 bytes:

-A list of all file systems

(1) Display disk usage

df -l

(2) Lists all file systems and their types in an easy to read manner

df -haT

3. Du command

The Du command is also used to view the used space, but unlike the DF command, the Linux Du command is used to view the space used by files and directory disks

Command format:

Du [options] [file]

Common parameters:

-A displays the size of all files in the directory

(1) Displays folder and subfolder sizes in an easy to read manner

du -h scf/

(2) Displays the size of all files in the folder in an easy to read way

du -ah scf/

(3) Displays the amount of disk space occupied by several files or directories, and counts the sum of them

du -hc test/ scf/

(4) Output the space used by each subdirectory in the current directory

du -hc --max-depth=1 scf/

4. LS command

Is the abbreviation of list, through the LS command, you can not only view the files contained in the Linux folder, but also view the file permissions (including directory, folder, file permissions)  view directory information and so on.

Common parameter collocation:

LS - a lists all files in the directory, including hidden files starting with

(1) Sort in reverse chronological order in an easy to read manner and display file details

ls -lhrt

(2) Displays file details in reverse order of size

ls -lrS

(3) Lists the details of all directories that start with “t” in the current directory

ls -l t*

(4) List absolute file paths (excluding hidden files)

ls | sed "s:^:`pwd`/:"

(5) List file absolute paths (including hidden files)

find $pwd -maxdepth 1 | xargs ls -ld

5. MKDIR command

The MKDIR command is used to create a folder.

Available options:
– M: set the access permission for the new directory, or use the Chmod command;
– P: can be a path name. At this time, if some directories in the path do not exist, after adding this option, the system will automatically create those directories that are not yet in the path, that is, multiple directories can be created at a time.

(1) Create a folder named t in the current working directory

mkdir t

(2) Create a directory with the path of test / T1 / T in the TMP directory. If it does not exist, create:

mkdir -p /tmp/test/t1/t

6. PWD command

The PWD command is used to view the current working directory path.

(1) View current path


(2) View the actual path of the soft link

pwd -P

**7. Rmdir command

To delete one or more subdirectory entries from a directory, you must also have write permission to its parent directory when deleting a directory.
Note: non empty directory cannot be deleted

(1) When the parent subdirectory is deleted and it becomes an empty directory, it will be deleted at the same time:

rmdir -p parent/child/child11

9、 Network communication command

1. Ifconfig command

Ifconfig is used to view and configure the network interface of Linux system.
View all network interfaces and their status: ifconfig – A.
Use the up and down commands to start or stop an interface: ifconfig eth0 up and ifconfig eth0 down.

2. Iptables command

Iptables is a command line tool for configuring Linux kernel firewall. The function is very powerful, for our development, we mainly master how to open the port. For example:

Directly reject the packet with source IP to access port 80: iptables – I input – s – P TCP — dport 80 – J reject.

Open port 80, because this port is used by the web

    iptables -A INPUT -p tcp --dport 80 -j ACCEP

Note that you should also use the save able command. Otherwise, the configured rules will be lost after the server restarts.

3. Netstat command

The Linux netstat command is used to display the network status.

Using netstat command can let you know the network situation of the whole Linux system.


Netstat [- acefghilmnnoprstuvvwx] [- a < network type >] [-- IP]

Parameter Description:
– A or – all displays sockets in all connections.
– a < network type > or – < network type > lists the relevant addresses in the connection for that network type.
– C or – continuous lists the network status continuously.
– C or – cache displays the cache information of router configuration.
– E or – extend displays other network related information.
– f or – FIB displays fib.
– g or – groups displays the list of members of the multi broadcast function group.
– H or – help online help.
– I or – interfaces displays the web interface information form.
– L or – listening displays the socket of the server under monitoring.
– m or – masquerade shows the masquerade network connection.
– N or – numeric uses the IP address directly, not through the domain name server.
– N or – netlink or – symbolic displays the symbolic connection name of the network hardware peripheral.
– O or – timers displays the timer.
– P or – programs displays the program ID and name of the program that is using the socket.
– R or – route displays the routing table.
– s or – statistics displays the network work information statistics table.
– t or – TCP displays the connection status of the TCP transport protocol.
– u or – UDP displays the connection status of UDP transport protocol.
– V or – verb displays the execution of the instruction.
– V or – version displays version information.
– W or – raw displays the connection status of raw transport protocol.
– X or – UNIX this parameter has the same effect as specifying the “- a UNIX” parameter.
– IP or – INET this parameter has the same effect as specifying the “- a INET” parameter.


How to check which ports are opened in the system?

[[email protected] ~ 13:20 #55]# netstat -lnp

How to check the network connection status?

[[email protected] ~ 13:22 #58]# netstat -an

How to count the number of current process connections?

Enter the command netstat – an | grep established | WC – L.
The output result is 177. There are 177 connections.

Use the netstat command with other commands to count the number of established state links to port 80 according to the source IP?

Strictly speaking, this topic tests the use of awk.

First, use the netstat – an|grep established command. The results were as follows:

tcp        0      0      ESTABLISHED

4. Ping command

The Linux ping command is used to detect the host.

To execute the ping command, ICMP transmission protocol will be used to send out a request response message. If the network function of the remote host is OK, it will respond to the message and know that the host is operating normally.

Specifies the number of times packets are received

ping -c 2

5. Telnet command

The Linux telnet command is used for remote login.

Execute the telnet command to start the terminal phase job and log in to the remote host.



Parameter Description:
– 8 allows the use of 8-character data, including input and output.
– a attempts to automatically log in to the remote system.
– B < host alias > use alias to specify the remote host name.
– C does not read the. Telnetrc file in the user’s exclusive directory.
– D to start debugging mode.
– e < escape character > set the escape character.
– e filters out the break characters.
– f this parameter has the same effect as specifying the “- F” parameter.
– F when Kerberos V5 authentication is used, add this parameter to upload the authentication data of the local host to the remote host.
– K < domain name > when Kerberos authentication is used, this parameter is added to make the remote host adopt the specified realm name instead of the domain name of the host.
– K does not automatically log in to the remote host.
– L < user name > specifies the user name to log on to the remote host.
– L allows output of 8-character data.
– N < record file > specifies the file to record relevant information.
– R uses a user interface similar to the rlogin instruction.
– s < service type > set the IP TOS information required for telnet connection.
– x if the host has the ability to support data encryption, use it.
– x < authentication form > turn off the specified authentication form.

Log in to the remote host

#Log in to the remote host with IP

**10、 System management command


1. Date command

Display or set the date and time of the system.

Parameter command:

-D < string > displays the date and time the string refers to. The string must be preceded by double quotation marks.

(1) Show next day

Date +% Y% m% D -- date = + 1 day // displays the date of the next day

(2) – D parameter usage

The 22nd of November this year is Wednesday

2. Free command

Displays system memory usage, including physical memory, swap, and kernel buffer memory.

Parameter command:

-B displays memory usage in byte

(1) Display memory usage


(2) Displays memory usage information in the form of sum

free -t

(3) Periodically query memory usage

free -s 10

3. Kill Command

Sends the specified signal to the corresponding process. If no model is specified, SIGTERM (15) is sent to terminate the specified process. If you can’t terminate the program, you can use the “- kill” parameter, and the signal it sends is sigkill (9), which will force the end of the process. Use the PS command or the jobs command to view the process number. The root user will affect the user’s process, and the non root user can only affect his own process.

Common parameters:

-L signal. If the number parameter of the signal is not added, the "- L" parameter will list all the signal names

(1) First use PS to find process pro1, and then kill it with kill

kill -9 $(ps -ef | grep pro1)

4. PS command

PS (process status) is used to view the status of the currently running process. It can be viewed at one time. If you need dynamic continuous results, use top

There are five states of processes on Linux
1. Running (running or waiting in the running queue)
2. Interrupt (in sleep, blocked, waiting for a condition to form or receive a signal)
3. Non interruptible (received signal does not wake up and can not run, the process must wait until an interrupt occurs)
4. Deadlock (the process has been terminated, but the process descriptor exists until the parent process calls wait4() system call and releases)
5. Stop (the process stops running after receiving sigstop, sigstp, sigtin, sigtou signals)

The PS tool identifies five status codes of the process

D non interruptible sleep (normally IO)

Parameter command:

-A shows all processes

(1) Display all current process environment variables and inter process relationships

ps -ef

(2) Show all current processes

ps -A

(3) Find a process with grep

ps -aux | grep apache

(4) Find out the PID numbers related to cron and syslog services

ps aux | grep '(cron|syslog)'

5. RPM command

The Linux RPM command is used to manage the suite.

RPM (RedHat package manager) is originally a program for Red Hat Linux distribution to manage Linux packages. It is popular because it follows GPL rules and has powerful and convenient functions. Gradually adopted by other distributions. The emergence of RPM suite management makes Linux easy to install and upgrade, which indirectly improves the applicability of Linux.

#Check the JDK provided by the system

6. Top command

Display the information about the process being executed in the current system, including process ID, memory usage, CPU utilization, etc

Common parameters:

-C displays the complete process command


top - 14:06:23 up 70 days, 16:44,  2 users,  load average: 1.25, 1.32, 1.35

The first five lines are the statistical information area of the current system.

The first line, task queue information, is the same as the execution result of uptime command. The specific parameters are as follows:

14: 06:23 – current system time

Up 70 days, 16:44 – the system has been running for 70 days, 16 hours and 44 minutes

2 users – there are currently 2 users logged in to the system

Load average: 1.15, 1.42, 1.44 – the following three numbers of load average are 1 minute, 5 minutes and 15 minutes respectively.

The load average data is the number of active processes checked every 5 seconds and calculated according to a specific algorithm. If this number is divided by the number of logical CPUs, if the result is higher than 5, the system is overloaded.

In the second line, tasks – tasks (process), the specific information is as follows:

There are 206 processes in the system, including 1 in running, 205 in sleep, 0 in stopped state and 0 in zombie state.

The third line, CPU status information, the specific attribute description is as follows:

5.9% us - percentage of CPU occupied by user space.

Note: the CPU usage ratio here is different from the concept of windows. You need to understand the Linux system user space and kernel space related knowledge!

The fourth line, memory status, details are as follows:

32949016k total - total physical memory (32GB)

In the fifth line, swap exchanges partition information. The specific information is as follows:

32764556k total - total exchange area (32GB)

The sixth line, empty line.

The seventh line is as follows: status monitoring of each process (task), and the item column information is described as follows:

PID - process ID

Top interactive command

H displays the top interactive command help information

7. Yum command

Yum (yellow dog updater, modified) is a shell front-end package manager in Fedora, red hat and SUSE.

Based on the RPM package management, it can automatically download and install RPM packages from the specified server, automatically handle dependency relationships, and install all the dependent software packages at a time without tedious downloading and installing.

Yum provides commands to find, install, and delete one, a group or even all software packages, and the commands are concise and easy to remember.

1. List all the updatable software list command: Yum check update
2. Update all software commands: Yum update
3. Install only the specified software command: Yum install < package_ name>
4. Update only the specified software command: Yum update < package_ name>
5. List all the installable software list command: Yum list
6. Delete package command: Yum remove < package_ name>
7. Find software package command: Yum search
8. Clear cache command:
Yum clean packages: clear packages in cache directory
Yum clean headers: clear the headers in the cache directory
Yum clean oldheads: clear old headers in cache directory
Yum clean, yum clean all (= Yum clean packages; Yum clean oldheads): clear the packages and old headers in the cache directory

Install PAM devel

[[email protected] ~]# yum install pam-devel

11、 Backup compression command

1. Bzip2 command

Create *. Bz2 compressed file: bzip2 test.txt 。
Unzip *. Bz2 file: bzip2 – D test.txt.bz2 。

2. Gzip command

Create a *. GZ compressed file: gzip test.txt 。
Unzip *. GZ file: gzip – D test.txt.gz 。
Displays the compression ratio: gzip – L *. GZ.

3. Tar command

Used to compress and decompress files. Tar itself does not have compression function, only has packing function. Compression and decompression are completed by calling other functions.

Clarify two concepts: packaging and compression. Packaging refers to a large number of files or directories into a total file; compression is to change a large file into a small file through some compression algorithms

Common parameters:

-C create a new compressed file

About gzip and bzip2 compression:

Gzip instance: compress gzip file name tar.gz  And. Tgz decompression: gunzip filename.gz  Or gzip - D filename.gz

(1) Package all the files into tar package

tar -cvf  log.tar  1.log, 2.log or tar - CVF log*

(2) Package all files and directories under / etc to the specified directory, and use GZ compression

tar -zcvf /tmp/etc.tar.gz /etc

(3) Check the contents of the just packed file (add Z, because it is compressed with gzip)

tar -ztvf /tmp/etc.tar.gz

(4) Pack / home, / etc, but not / home / dmtsai

tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc

4. Unzip command

    ·Unzip *. Zip file: unzip 。
    ·View the contents of the *. Zip file: unzip – L 。

Welcome to comment and correct. If you have any help, please click like + forward to share.

Welcome to the official account of the brother of migrant workers:Migrant workers’ technology road
The most complete Linux interview questions in history (2020 latest edition)