Linux basics and high-frequency interview points

users, user groups, permissions

Linux is a multi-user operating system. General users are assigned to a user group at the beginning of creation and enjoy general permissions. An operating system has only one super user named root, who belongs to a root group and has the highest authority.
insert image description here

user

root

Many advanced permissions such as useradd, chown and other advanced permissions can only be used by root users. At this time, if ordinary users want these advanced permissions, they need to use the "sudo" keyword to indicate that the command is run as root

Commands for common root privileges

useradd + passwd Add user and change password

useradd -m user_name
passwd user_name
  1. -m means to create a user group named by user_name when creating a common user_name, user is the initial user of the group, and user cannot leave this group, and initialize its home directory /home/username with the file under /etc/skel/
  2. There must be a passwd command! Otherwise, the new user cannot be used normally

userdel delete user

userdel user_name
userdel user_name -t 

Without -t, only the user name will be deleted, and the corresponding user's folder will not be deleted from /home. With -t, the user's folder will be deleted at the same time

usermod modifies a user's account

It is best not to use this command, because if some software has been installed in the home directory, it will cause software path indexing problems! ! !

  • -l Rename the user. It should be noted that the name of the user's home directory in /home will not change and needs to be modified manually.
  • -g modifies the group the user is in, for example usermod -g friends lion modifies the group of the lion user as friends.
  • -G allows the user to add multiple groups at once, such as usermod -G friends,foo,bar lion .
  • -a -G will let you leave the original group. If you don't want to do this, you have to add the -a parameter, which means append.

su switch to another user

su user_name

user group

Commands for common root privileges

groupadd to create a group, groupdel to delete a group, groups to view the user's group

groupadd user_name
groupdel user_name
groups user_name

Note:

  1. All user names and user group information are stored in the /etc/passwd file

    Format:

    user_name:passwd:user_id:group_id:description:home_path:login_shell_path
    

    其中UID:0—root; 1~499–sys-user; 500~65535—general-user ,sys-user cannot move! ! !

  2. The root user's home directory is /root/, and the normal user's home directory is /home/username/

  3. The /etc/shadow file saves the login passwords of all users

  4. How to check the current user and permissions

    The shell terminal starts with # as a super user, $ starts with a normal user, and uses whoami to return the current user name

File Permissions

Non-root privileged commands

View file permissions

ls -l dir_path-or-file_path

-l list (in single column format) the file mode, including the file's link number, owner name, group name, file size (in bytes), time information, and file name

For example output drwxr-xr-x 3 root root 17 May 6 00:14 .config, the interpretation of this information is as follows:

  • drwxr-xr-x: file type and permission, the first digit is the file type field (including d: directory, -: file, l: link file), the last 9 digits are the file permission field (each 3 digits is a group, a total of 3 groups, each group represents the file permissions for the file owner, the group to which it belongs, and other people. The 3 digits in a group of permissions are r, w, and x permissions, indicating readable, writable, and executable)
  • 3: Number of links
  • root: file owner
  • root: the group to which it belongs
  • 17: file size
  • May 6 00:14: The time when the file was last modified
  • .config: file name

Commands for common root privileges:

chgrp modify the group of the file

change group~

chgrp bar file.txt 

Indicates that the group of the file.txt file is changed to the group bar

chown change the owner of the file

chown -R 用户名称:用户组名称 文件或目录
  • -R Recursively set subdirectories and subfiles under the target folder

    That is, the permissions of all files and directories under the subdirectory are updated to this user and user group

chmod changes the permissions of the file owner, group, and others

chmod -R 655 /mnt/sda/smm/dataset/ScanNet/
chmod -R o+rw /mnt/sda/smm/dataset/ScanNet/

655 indicates that the permission to modify all files under the folder ScanNet is: owner rw-, same user group rx, others rx

o+rw means that other people increase r and w permissions

Parameter Description:

  • Use letters to assign permissions

    u : The abbreviation of user, which means the user, indicating the owner.

    g: The abbreviation of group, the meaning of group, means group users.

    o : The abbreviation of other, which means other users.

    a : The abbreviation of all, which means all users.

    + : Plus sign, which means adding permission.

    - : Minus sign, indicating removal of permissions.

    = : The equal sign means assigning permissions.

  • Determining Permissions Using Numbers

    permissions number
    r 4
    w 2
    x 1

    A directory's permissions are permissions on its file listing. Therefore, the r permission of the directory means that the file list can be read; the w permission means that the file list can be modified, specifically, adding and deleting files, and modifying the file name; the x permission can make the directory a working directory, and the x permission is r And the basis of w permissions, if a directory cannot be made a working directory, there is no way to read the file list and modify the file list

File editing and manipulation

Find files in directories, find file contents in files

find - Find files in a directory

Search by file name and root path

find -name "file.txt" --> 当前目录以及子目录下通过名称查找文件
find . -name "syslog" --> 当前目录以及子目录下通过名称查找文件
find /var/log -name "*syslog*" --> 查找包含syslog的文件 复制代码

Special characters that can be used in regular expressions in ""

Find by file size

find /var -size +10M --> /var 目录下查找文件大小超过 10M 的文件

Find by file last access time

find -name "*.txt" -atime -7  --> 近7天内访问过的.txt结尾的文件

Find only directories or files

find . -name "file" -type f  --> 只查找当前目录下的file文件find . -name "file" -type d  --> 只查找当前目录下的file目录复制代码

Action Find Results

find -name "*.txt" -printf "%p - %u" --> 找出所有后缀为txt的文件,并按照 %p - %u格式打印,其中%p=文件名,%u=文件所有者
find -name "*.jpg" -delete --> 删除当前目录以及子目录下所有.jpg为后缀的文件,不会有删除提示,因此要慎用
find -name "*.c" -exec chmod 600 {} ; --> 对每个.c结尾的文件,都进行 -exec 参数指定的操作,{} 会被查找到的文件替代,; 是必须的结尾
find -name "*.c" -ok chmod 600 {} ; --> 和上面的功能一直,会多一个确认提示复制代码

grep - globally match a regular expression in a file and print to the shell

grep -E [Pp]ath /etc/profile

Indicates to print the file content of /etc/profile always matching Path or path to the shell

Parameter Description:

  • -E is followed by the content of the regular expression included in "[]"

    * matches 0 or more characters

    ? matches any one character

    [list] matches any single character in list

    [ ^list] matches any character other than any single character in list

    [c1-c2] matches any single character in c1-c2 such as: [0-9]

    {string1,string2,…} matches one of string1 or string2 (or more)

    {c1...c2} matches all characters in c1-c2 such as {1...10}

  • -i ignores case, grep -i path /etc/profile

  • -n display line number, grep -n path /etc/profile

  • -v only show those lines where the search text is not, grep -v path /etc/profile

  • -r recursive search, grep -r hello /etc, there is also a rgrep command in Linux, which is equivalent to grep -r

File linking and deletion

Understand how Linux files are stored before understanding file links. Storing a Linux file includes three parts: file name, file content, and permission. Each file name is bound to the file content through the inode identifier in the file name list.

ln - establish a link

There are two types of links under Linux: hard links (physical links) and soft links (symbolic links).

insert image description here

  • Hard link (entity link)
ln file1 hard_ln1

Create a hard link named hard_ln1 to link to the inode corresponding to file1. When deleting the file content corresponding to hard_ln1, the file content will be deleted only when the reference number of the corresponding inode is 0

  • Soft link (symbolic link)
ln -s file1 soft_ln1

Create a soft link named soft_ln1, link to file1, display the content of file1, but in fact the inode of file2 is different from file1. If we delete soft_ln1, file1 will not be affected, but if we delete file1, soft_ln1 will become a dead link, because the pointed file is gone.

rm - delete a file

Delete files and directories. Since there is no recycle bin under Linux, it is very difficult to recover once deleted, so you need to be careful

rm file_path/file_name

【Common parameters】

  • -i Confirm to the user whether to delete;
  • -f file forced deletion;
  • -r Delete folders recursively, the famous delete operation rm -rf.

redirect

input redirection

A < B

Take B as the input of the A command, and use the content in name.csv as the input of cat as follows, and its operation result is the same as that of cat name.csv

cat < name.csv

output redirection

Covered>, Non-covered>>

cut -d , -f 1 notes.csv > name.csv

The above command means to cut the notes.csv file through "," and redirect the first part of the cut result to the name.csv file. If the output file does not exist it will create a new one, if the output file already exists, it will be overwritten.

cut -d , -f 1 notes.csv >> name.csv

The above command will append the name to name.csv.

pipe redirection

A | B means to make the result of the A instruction the target of the B instruction:

grep log -Ir /var/log | cut -d : -f 1 | sort | uniq

The above command means to search for /var/log text under the log folder, and then use the found log text as the target of the cut command, and use cut to obtain the cut first part as the target of the uniq command for deduplication

Notice:

  1. The parameters of grep indicate
    • -r means recurse, -I is used to exclude binaries

Install using a software repository

The software package suffix of the Red Hat family is generally .rpm, and the software package suffix of the Debian family is .deb.

yum is the default package management tool in CentOS, suitable for the Red Hat family.

apt is the default package management tool in Ubuntu, suitable for Debian family.

Use Cmake to compile the source code

If you want to use software that is not in the yum warehouse, you have to find the software source code yourself, compile and install the source code for use.

Cmake is an advanced compilation tool that can compile source code into executable binary files. The specific compilation steps are as follows:

  1. Download the source code or write your own

    For example, a hello.cpp file with "hello word!" is the source code

  2. Write CMakeLists.txt

    Equivalent to an instruction file, instructing cmake to execute the commands in CMakeLists.txt one by one

  3. Compile and generate executable files with cmake command

  4. Execute the executable file to verify that the software is installed successfully

Understanding CMakeLists.txt

The following is a CMakeLists.txt

#  CMakeLists.txt
# top-level CMake configuration file
cmake_minimum_required(VERSION 3.5)

project(CUT_PURSUIT_SEG)


#------------------------------------------------------------------------------
# internal cmake settings
#------------------------------------------------------------------------------
set(CMAKE_COLOR_MAKEFILE ON)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
set(CMAKE_MACOSX_RPATH ON)
include(FeatureSummary)

#------------------------------------------------------------------------------
# General settings
#------------------------------------------------------------------------------
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -std=c++11")

#------------------------------------------------------------------------------
# actual library
#------------------------------------------------------------------------------
add_subdirectory(src)

"cmake_minimum_required" is the instruction keyword, "()" is the variable passed in

Next, explain some command keywords and parameters that must be mastered.

Instruction keywords involved in CMakeLists.txt

PROJECT

It is used to specify the name of the project and the supported language. The following example indicates that the project name is HELLO , and the supported compilation languages ​​are C++ and JAVA

PROJECT (HELLO CXX JAVA)

Notice:

  • This keyword implicitly defines two variables PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR . The example defines the variables HELLO_BINARY_DIR and HELLO_SOURCE_DIR

SET

It is used to display the specified variable, as shown in the following example: 2 file paths of main.cpp and fun1.cpp are stored in the parameter SRC_LSIT

SET (SRC_LSIT main.cpp fun1.cpp)

Notice:

  • If the file contains special characters, you must use ""
  • Instruction keywords (variable 1 variable 2) -- instruction keywords are not case-sensitive, parameters and variables are case-sensitive

MESSAGE

It is used to specify to output custom information to end users , as shown in the following example

SATUS >> 在shell向用户输出前缀为 '--'的info, eg: '-- Generating done'
FATAL_ERROR >> 立即终止所有cmake过程
SEND_ERROR >> cmake过程产生错误, 过程被跳过

ADD_EXECUTABLE

It is used to specify to generate an executable file to the user , which means to pack all the files included in the SRC_LIST variable and generate the executable file hello_exe

ADD_EXECUTABLE(hello_exe ${SRC_LIST})

Notice:

  • ${} means to take the value in the variable in {}

ADD_SUBDIRECTORY

Used to specify the subfolder path , add ~/proj/src/ to the project directory, and compile the generated binary ".bin" file and put it under ~/proj/bin/

 ADD_SUBDIRECTORY(src bin)

Compile engineered source code

If you just want to output "hello word!" to the screen, you can completely ignore this section, but if you want to build a large-scale project, such as developing a speech recognition software, then engineering source code is a must-have skill for you!

Assuming that all your project files are under the folder ~/proj, the suggested layout of your folder is as follows:

proj
	bin/  编译好的可执行
	src/  存放proj的源代码
	  CMakeLists.txt  编写编译要执行的指令
	doc/  放工程文档
	CMakeLists.txt  一般用来把目录src/添加到cmake编译查询路径中
	READMD.md
	run_proj.sh  运行项目的脚本文件

If you have completed all the files of the project, just pack the proj folder to the customer, after the customer extracts the proj folder, execute the following command

cd proj
mkdir build && cd build
cmake .. -参数1= -参数2=  && make

proj/CMakeLists.txt is used as the entry of the cmake command, and the make compilation results will all be placed under proj/build, forming the following directory structure

proj
	build/
        - CMakeCache.txt 
        - cmake_install.cmake 
        - CMakeFiles/
        - Makefile
	bin/  编译好的可执行
	src/  存放proj的源代码
	    - CMakeLists.txt  编写编译要执行的指令
	doc/  放工程文档
	CMakeLists.txt  一般用来把目录src/添加到cmake编译查询路径中
	READMD.md
	run_proj.sh  运行项目的脚本文件

SSH connection to remote server

Suppose intranet server A, extranet server B

Establish an SSH connection and log in to the server without password

By transmitting the ssh public key between A and B, password-free login can be realized. Specific steps are as follows:

  1. Generate key on A

    Enter the following command in the shell of A to generate the public key and private key of SSH

ssh-keygen

Next, the system will ask whether to use the default path to generate, and the novice can press "Enter" all the way to complete the generation

  1. A will generate the public key to B

    Still enter the following command in the shell of A

ssh-copy-id [param] B_username@B_ip

Note: the meaning of some parameters

  • -i - the key generated by the non-default path

    eg: -i ~/.ssh/id_rsa.pub

  • -p - B's ssh port is not 22

    eg: -p 3085

  1. Log in to server B on A without using ssh
ssh B_username@B_ip

scp - securely copy files between different servers

Based on the SSH connection, the encrypted transmission of files between two servers can be realized. The following command can realize the transfer of files from server A to server B

scp -P B_port_num -r A_file-path B_username@B_ip:B_target_dir

Note:

  1. scp parameter
  • -P - the port of the target server

    -P 3085

  • -r - resource folder

    -r dir_path

  1. If the error "Permission denied" is reported, it may be because the ssh connection between AB and AB has not been established in advance. You can check the previous section to establish an SSH connection first, and then scp to transfer files.

    Still enter the following command in the shell of A

ssh-copy-id [param] B_username@B_ip

Note: the meaning of some parameters

  • -i - the key generated by the non-default path

    eg: -i ~/.ssh/id_rsa.pub

  • -p - B's ssh port is not 22

    eg: -p 3085

  1. Log in to server B on A without using ssh
ssh B_username@B_ip

scp - securely copy files between different servers

Based on the SSH connection, the encrypted transmission of files between two servers can be realized. The following command can realize the transfer of files from server A to server B

scp -P B_port_num -r A_file-path B_username@B_ip:B_target_dir

Note:

  1. scp parameter
  • -P - the port of the target server

    -P 3085

  • -r - resource folder

    -r dir_path

  1. If the error "Permission denied" is reported, it may be because the ssh connection between AB and AB has not been established in advance. You can check the previous section to establish an SSH connection first, and then scp to transfer files.

(continuously updated)

Guess you like

Origin blog.csdn.net/weixin_45549370/article/details/124937854