This article brings you the basic knowledge of the Linux operating system, including knowledge about the program development environment, common commands and environment variables. I hope it will be helpful to everyone.
Linux is a UNIX-like kernel implementation that can be freely released. It is the underlying core of an operating system. Linux was developed by Linus from the University of Helsinki in Finland, with help from a large number of UNIX programmers on the Internet. It was originally a program inspired by his professor's Minix (a small UNIX-like system). It was purely a personal hobby, but later it gradually developed into a complete system in its own right.
To understand what Linux is , we must first understand the systems and applications that were born before Linux-UNIX and GNU software.
The UNIX operating system was originally developed by Bell Labs, which at the time was a member of the telecommunications giant AT&T (American Telegraph and Telephone Company). Today, it has become a very popular multi-user, multi-tasking operating system. The UNIX operating system can run on a large number of different hardware platforms, ranging from PCs to multi-processor servers and supercomputers.
There are some typical styles and characteristics of program development on UNIX:
1) Simplicity: Many useful UNIX tools are very simple. "Small and simple" is a technique worth learning. Larger, more complex systems are bound to contain larger, more complex bugs.
2) Centralization: It is better to have a program perform one task well than to pile all functions together. Programs with bloated functionality are difficult to use and maintain, and single-objective programs are more likely to be improved as better algorithms are developed. In UNIX, when new needs arise from users, we usually combine small tools to complete more complex tasks, rather than trying to put all the functions expected by a user into a large program.
3) Reusable components: Implement the core of the application as a library. Libraries with simple and flexible programming interfaces can help others develop similar programs or apply these techniques to new application areas.
Having an operating system kernel is not enough. The development of Linux to this day is the result of the joint efforts of countless people. The operating system kernel itself is only a small part of the available development systems. General UNIX systems also include applications that provide system services and tools. For Linux systems, these programs are written and freely released by many programmers.
The Linux community supports the concept of free software, that is, the software itself should not be restricted and they comply with the GNU (GNU is the recursive abbreviation of GNU’s Not UNIX) General Public License (GPL).
The Free Software Foundation was founded by Richard Stallman. Its purpose is to try to create an operating system and development system that is compatible with UNIX systems but is not restricted by the UNIX name and source code privacy rights. environment.
The GNU Project has provided the software community with many imitations of applications on UNIX systems. For example, the famous one:
GCC: GNU Compiler Collection, which includes the GNU C compiler.
G: G compiler, part of GCC.
GDB: Source code level debugger.
GNU make
bash: command interpreter (a type of shell)
With these available free software, coupled with the Linux kernel, we can say: create The goal of a GNU, free UNIX-like system has been achieved through the Linux system. Due to the contributions made by GNU software, many people now usually refer to the Linux system as GNU/Linux.
If you want to do your job well, you must first sharpen your tools. If you want to develop Linux programs, you need to build a development environment first.
Linux development requires the installation of a Linux system environment. Generally, VMWare is used to install the Ubuntu system for development. For the specific installation process, you can refer to the following post https://zhuanlan.zhihu.com/p/38797088. I won’t go into too much introduction here.
Secure Shell (SSH) is a secure network protocol based on the application layer developed by IETF (The Internet Engineering Task Force). Traditional network service programs, such as FTP, Pop, and Telnet, are inherently insecure; because they transmit data, user accounts, and user passwords in plain text on the network, they are easily vulnerable to man-in-the-middle attacks.
SSH is divided into client openssh-client and server openssh-server. We need to install the ssh server under the ubuntu system in order to use the ssh client to log in to the system.
sudo apt update
sudo apt install openssh-server
sudo service ssh start
ip addr show
Command to view ubuntu system ipIt can be seen that the current ubuntu IP address is 192.168.176.148
After installing the ssh server on Ubuntu, next Use MobaXterm to connect under Windows system and test whether the ssh server is installed successfully. The operation steps are as follows:
Run the Xterm software, select "Session", as shown in the figure below:
Click "SSH" and set it, as shown in the figure below:
In this dialog box, select "Protocol" as "SSH", "Host Name" as the IP address of ubuntu, and the user name as the current user name. Use the default value of 22 for the port number. After the settings are completed, click "OK" and the following command line terminal will appear, indicating that the virtual machine's Ubuntu system has been connected.
Through this method, after confirming that the network communication between the host and the development board is normal, you can log in to the development board.
Visual Studio Code (VSCode for short) is a free editor from Microsoft, available for Windows, Linux and macOS version, is a cross-platform editor. You can search for the installation process by yourself. Here we mainly introduce the expansion packages used for C/C program development. The installation steps are shown in the figure below:
The following plug-ins are commonly used and need to be installed:
C/C.
C/C Snippets, C/C reused code blocks.
C/C Advanced Lint, that is, C/C static detection.
Code Runner, that is, code running.
Include AutoComplete, that is, automatic header file inclusion.
Rainbow Brackets, rainbow curly braces, help reading code.
One Dark Pro, the theme for VSCode.
GBKtoUTF8, convert GBK to UTF8.
ARM, which supports ARM assembly syntax highlighting.
Chinese(Simplified), that is, Chinese environment.
vscode-icons, VSCode icon plug-in, mainly icons for each folder under the resource manager.
compareit, comparison plug-in, can be used to compare the differences between two files
DeviceTree, device tree syntax plug-in.
Markdown Preview Enhanced, markdown preview plug-in.
Maridown pdf, convert .md files to other formats.
There are hundreds of Linux commands. For beginners, there are not many commonly used ones, and they do not need to be mastered. In the daily study and work process, if you encounter unfamiliar Linux commands, you will naturally remember them after checking more information over time. The following is an introduction to the 17 most basic commands in Linux.
#clear
ip addr
date
The paths of directories and files are divided into absolute paths and relative paths. Absolute paths are calculated from the root (/), such as /usr/etc/readme.txt. The relative path is calculated from the current working directory. If the current working directory is /usr, etc/readme.txt is equivalent to /usr/etc/readme.txt. In addition, it should be noted that: one dot. indicates the current working directory; two dots... indicates the upper-level directory of the current working directory.
2. The root directory is closely related to system startup, repair, and restoration;
Therefore, the root directory must contain startup software, core files, and programs required for startup. , function libraries, repair system programs and other files, as shown in the following table.
usr (note not user), the full name is Unix Software Resource. This directory is used to store system software resources. FHS recommends that developers place software product data in subdirectories of the /usr directory rather than creating separate directories for their products.
In the Linux system, all system default software is stored in the /usr directory. The /usr directory is similar to the combination of C:\Windows\ C:\Program files\ in the Windows system.
The FHS recommends that the /usr directory should have subdirectories as shown in the following table.
/var directory is used to store dynamic data, such as cache, log files, files generated during software running, etc. Generally, it is recommended that this directory contain these subdirectories as shown in Table 4.
pwd
cd directory name
Example:
cd /tmp
cd ..
cd
ls
is the abbreviation of list. Through the ls
command, you can not only view directory and file information, but also view the permissions, size, owner, group and other information of directories and files.
Options -l
List directory and file details.
Example:
ls
ls -l
Regular expressions are also called regular expressions and wildcards. Both directory and file names support regular expressions and regular expression rules. There are many, here we only need to master the two most commonly used ones: asterisk "*".
Asterisk "*": Match any number of characters.
For example: ls open*.zip
mkdir aaa
mkdir aaa/bbb -p
rm [-rf]Directory or file
-r can delete the directory. Without -r, only the file can be deleted.
-f indicates forced deletion and does not require confirmation.
rm aaa -r
rm -f ccc.c
Note: -r means recursion, English recursion
option
Directories can be copied, if there is no option -r
can only copy files. Example:
The above two commands have the same effect.
<p><code>cp -r aaa /tmp/.
The above two commands have the same effect.
ping -c Number of packets ip address or domain name
ping is used Determine whether the local host can successfully exchange data packets with another host and determine whether the network is smooth.
ping -c 5 127.0.0.1
ping -c 5 www.baidu.com.cn
It can be seen that Baidu’s server can ping
.
ping -c 5 www.google.com
You can see that Google’s server is ping
It doesn’t make sense.
There are three commands to display the contents of a text file: cat
, more
and tail
.
cat file name
cat command displays the contents of the entire file at one time.
cat main.cpp
more file name
In order to facilitate reading, the more command displays the contents of the file in pages. Press the space bar to display the next page, press the b key to display the previous page, and press the q key to exit.
tail -f file name
tail -f
is used for display The last few lines of the text file will be refreshed in real time if the content of the file increases. For programmers, tail -f
is extremely important. It can dynamically display the log of the background service program and be used to debug and track the running of the program.
wc(word count) function is to count the number of bytes and words in the specified file , number of lines, and output the statistical results
wc file name
grep "Content" file name
Note that if there are no special characters such as spaces in the content, it does not need to be enclosed in double quotes.
Example:
grep signal main.cpp
in the main.cpp file
find directory name-name file name
Parameter description:
Directory name: The directory to be searched. When searching for files, in addition to this directory name, it also includes its subdirectories at all levels.
File name: Rules matching the file name to be searched.
Example:
Start searching from the OPC directory and display all *.cpp files.
find OPC -name *.cpp
locate file name
Example :
Search open62541.c file
locate open62541.c
Note: When using this command for the first time, you may need to update the database. Just execute the command according to the prompts.
find is mostly used for fuzzy search in a certain directory, while locate is for accurate positioning of the whole area.
is used to change the access permissions of Linux files or directories. This command has two uses. One is a text setting method that includes letters and operator expressions; the other is a numeric setting method that includes numbers.
Each file or directory has three groups of access permissions, each group is represented by three digits, which are the read, write and execute permissions of the file owner; the read, write and execution permissions of users in the same group as the owner. Execute permissions; read, write, and execute permissions for other users in the system.
Example: ls -l example
There are 10 positions in the first column, and the first character specifies the file type. In the usual sense, a directory is also a file. If the first character is a dash, it means it is a non-directory file. If it is d, it means a directory. There are a total of 9 characters starting from the second character to the tenth. A group of 3 characters represents the permissions of 3 groups of users on the file or directory. The permission characters use a horizontal line to represent empty permission, r represents read-only, w represents write, and x represents executable.
Commonly used parameters:
-c When changes occur, report processing information
-R Process the specified directory and its subdirectories All files
Permission scope:
u: The current user of the directory or file
g: The current group of the directory or file
o: A user or group other than the current user or group of the directory or file
a: All users and groups
Permission code:
r: Read permission, represented by the number 4
w: Write permission, represented by the number 2
x: Execution permission, represented by the number 1
-: Delete permissions, represented by the number 0
s: Special permissions
Example:
chmod a-x example
chmod 751 example -c
chmod u r,g r,or -R text/ -c
df [-h] [-T]
option-h
Display information in an easy-to-read format:
df -h
Options -T
List files System type:
df -h -T
The execution of programs (operating system commands and applications) requires a running environment, which is composed of multiple environment variables.
System environment variables: public, effective for all users.
User environment variables: User-private and customized personalized settings, which only take effect for this user.
Permanent environment variables: Configured in the environment variable script file, these scripts will be automatically executed every time the user logs in, which is equivalent to permanent effect.
Temporary environment variables: temporarily defined in the Shell when used, and will become invalid after exiting the Shell.
Linux environment variables are also called Shell environment variables. They start with an underscore and a letter and consist of an underscore, letters (case-sensitive) and numbers. They are customarily used Capital letters, such as PATH, HOSTNAME, LANG, etc.
In Shell, use the env command to view all environment variables of the current user.
The above picture only captures some of the environment variables, not all.
Note: When using the env command, many environment variables are displayed on the screen, which is inconvenient to view. You can use grep to filter them.
env|grep Environment variable name
For example, check the environment variable whose name contains PATH.
env|grep PATH
echo $environment variable name
For example: echo $LANG
Search directory for executable programs. Executable programs include Linux system commands and user applications. The specific usage of the PATH variable is described in detail in later chapters of this article.
Language, region, and character set of the Linux system.
echo $LANG
The host name of the server.
echo $HOSTNAME
The Shell parser currently used by the user.
echo $SHELL
The number of historical commands saved.
The username of the currently logged in user.
echo $USER
The home directory of the currently logged in user.
echo $HOME
Current working directory.
echo $PWD
C/C language dynamic link library file search directory, it is not Linux default environment variables, but they are very important for C/C programmers. The specific usage is described in detail in the later chapters of this article.
export variable name='value'
If the value of the environment variable does not have spaces or other special Symbols can be included without single quotes.
Example:
export PATH=$PATH:.
The above command adds .directory to the current shell’s environment variable PATH (yes.)
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
The above command adds the .directory to the environment variable LD_LIBRARY_PATH of the current shell
Environment variables set using export will become invalid after exiting the Shell and need to be reset the next time you log in. If you want the environment variables to take effect permanently, you need to configure them in the login script file.
System environment variables are effective for all users. There are three ways to set system environment variables.
When the user logs in, execute the /etc/profile file to set the system environment variables. However, Linux does not recommend setting system environment variables in the /etc/profile file.
/etc/profile will execute all script files under /etc/profile.d every time it is started. /etc/profile.d is easier to maintain than /etc/profile. If you don’t want any variables, just delete the corresponding shell script under /etc/profile.d. There are many script files in the
/etc/profile.d directory, for example:
ls /etc/profile.d
The environment variables configured in this file will affect the bash shell used by all users. However, Linux also does not recommend setting system environment variables in the /etc/bashrc file.
User environment variables only take effect for the current user. There are many ways to set user environment variables.
In the user's home directory, there are several special files, which are invisible with ls
but can be seen with ls .bash*
.
Executed when the user logs in, each user can use this file to configure their own environment variables.
This file will be read when the user logs in and every time a new Shell is opened. It is not recommended to configure user-specific environment variables in it, because every time the user opens the For each shell, the file will be read once, and the efficiency will definitely be affected.
Execute this file every time you exit the system (exit the bash shell).
saves the historical commands used by the current user.
The execution sequence of environment variable script files is as follows:
/etc/profile ->/etc/profile.d->/etc/bashrc->User's .bash_profile->User's .bashrc
Environment variables with the same name, if in multiple scripts If there is a configuration, the configuration in the last executed script shall prevail.
Search directory for executable programs, executable Programs include Linux system commands and user applications. If the directory of the executable program is not in the directory specified by PATH, the directory needs to be specified when executing.
export PATH=Directory 1: Directory 2: Directory 3:… Directory n:.
Example:
ls /bin/ls
You can see that the ls
command is located in the /bin directory
At this time we execute the command export PATH=
Clear the PATH variable and then execute the ls
command. The system will prompt that the ls
command cannot be found.
You can use the absolute path, /bin/ls
After adding the /bin directory to the environment variable PATH , ls
can also be executed.
Example:
When the example command is executed in the current directory, it prompts that it cannot be executed.
After adding the dot. to the environment variable PATH, you can execute it.
export PATH=$PATH:.
C/ The directory where C language dynamic link library files are searched. It is not a default environment variable in Linux, but it is very important for C/C programmers.
The LD_LIBRARY_PATH environment variable also stores a list of directories. The directories are separated by colon:, and the final dot . indicates the current directory, which has the same format as PATH.
export LD_LIBRARY_PATH=Directory 1: Directory 2: Directory 3:... Directory n:.
We will use this environment variable next time This lesson will introduce Linux static libraries and dynamic libraries in detail.
1) Under Shell, the environment variables set with export take effect immediately on the current Shell and become invalid after the Shell exits.
2) The environment variables set in the script file will not take effect immediately. They will only take effect after exiting the Shell and logging in again, or use the source command to make it take effect immediately, for example:
source /etc/profile
Although there are many ways to set environment variables, it is recommended that you set the system environment variables in /etc/profile Configure in the .d directory, and user environment variables are configured in the user's .bash_profile. It is not recommended to configure environment variables in other script files, as it will increase the trouble of system operation and maintenance and is prone to errors.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of Let's talk about common commands and environment variables of Linux operating system. For more information, please follow other related articles on the PHP Chinese website!