Linux Learning Notes 2

Keywords: Linux Operation & Maintenance CentOS cloud computing

tar command

Use the tar command to package, compress, and decompress files:

Compress the file using gzip and specify the compression name tar_gzip.tar.gz

[root@ikta ~]# tar -czvf tar_gzip.tar.gz find_test1 find_test2
find_test1
find_test2

Compress the folder using bzip2 and specify the compression name tar_bzip2.tar.bz2

[root@ikta ~]# tar -cjvf tar_bzip2.tar.bz2 tar_test/
tar_test/
tar_test/find_test1
tar_test/find_test2

Compress the file using XZ and specify the compression name tar_xz.tar.xz

[root@ikta ~]# tar -cJvf tar_xz.tar.xz find_test1
find_test1

New File file1.txt, file2.txt, file3.txt

[root@ikta ~]# touch file{1..3}.txt

Compress files file1.txt and file2.txt (using gzip), exclude file3.txt (i.e. no compression of file3)
And specify the compression name tar_file.tar.gz

[root@ikta ~]# tar --exclude=file3.txt -czvf tar_file.tar.gz file1.txt file2.txt 
file1.txt
file2.txt

New file file4.txt, add file4.txt to tar_ In file.tar.gz

[root@ikta ~]# tar -rvf tar_file.tar file4.txt
file4.txt

View compressed package tar_ Which files and directories are in file.tar.gz (see only, uncompress)

[root@ikta ~]# tar -tvf tar_file.tar.gz 
-rw-r--r-- root/root         0 2021-11-17 00:29 file1.txt
-rw-r--r-- root/root         0 2021-11-17 00:29 file2.txt
-rw-r--r-- root/root         0 2021-11-17 23:24 file4.txt

Unzip tar_gzip.tar.gz to the specified directory tar_ Test (created without this directory)

[root@ikta ~]# tar -xvf tar_file.tar.gz -C tar_test/
file1.txt
file2.txt
file4.txt

Unzip tar_xz.tar.xz

[root@ikta ~]# tar -xvf tar_xz.tar.xz 
find_test1

2. Create a Linux.txt in the / root directory on Linux and windows.txt on Windows
Connect linux on cmd

The directory we are in now is the root directory. We have PC at the near end and Linux at the far end.
Note: cd switches to the remote directory; lcd switches local directories

Push windows.txt on windows to linux through the get and put commands of sftp

Same as

Push linux.txt on Linux to windows through the get and put commands of sftp

Upload the file windows.txt to linux using rz

Download the file linux.txt to windows using sz

environment variable

Operations on Common Variables

Create the common variable local_data=1 and access

[root@ikta ~]# local_data=1
[root@ikta ~]# echo $local_data
1

Operation of user variables

Create environment variable root_data, accessible only to root users

[root@ikta ~]# Vim.bash_ Profile ------ Edit the environment variable for root (root_data=root)
[root@ikta ~]# Source.bash_ Profile - --- Make the file valid
[root@ikta ~]# echo $root_data ----- Print
root

Create environment variable normal_user_data, accessible only to normal user testuser1

[root@ikta ~]# su - testuser1 ----- Switch to testuser1 user
[testuser1@ikta ~]$ vim .bash_profile  ------Edit the user's environment variables ( normal_user_data=testuser1)
[testuser1@ikta ~]$ source .bash_profile    ------Take effect
[testuser1@ikta ~]$ echo $normal_user_data  ------Print
testuser1

Operation of global variables

Create environment variable root user and normal user can access

[root@ikta ~]# Vim/etc/profile ----- Global environment variable (global_data=global)
[root@ikta ~]# Source/etc/profile ----- Effective
[root@ikta ~]# echo $global_data        
global

Alias alias

Create 3 files test1.txt, test2.txt, test3.txt

Use find to find test1.txt,test2.txt, test3.txt

[root@ikta ~]# touch test{1..3}.txt
[root@ikta ~]# find . -name "test*.txt"
./test1.txt
./test2.txt
./test3.txt

aliases

Use aliases: Name the top command myfind

[root@ikta ~]# alias myfind='find . -name "test*.txt"'
[root@ikta ~]# myfind
./test1.txt
./test2.txt
./test3.txt

Unalias (unlias)

[root@ikta ~]# unalias myfind

history command

View the 10 most recently used history commands

[root@ikta ~]# history 10

Execute two commands simultaneously

Print 123 and switch from root to normal user on one line (semicolon)

[root@ikta ~]# echo "123";su - testuser1
123
[testuser1@ikta ~]$

Wildcard use

Create 3 files file1, file2, file3

1. * To match 3 files

[root@ikta ~]# ls -l file*

2. Match 3 files

[root@ikta ~]# ls -l file?

3. [] Matches file1 and file3

[root@ikta ~]# ls -l file[13]

4. [^] Matches file2

[root@ikta ~]# ls -l file[^13]

5. [!] Match file2

[root@ikta ~]# ls -l file[!13]

6. {} Matches file1 and file3

[root@ikta ~]# ls -l file{1,3}

Use of no quotation marks, single quotation marks, double quotation marks, inverse quotation marks, $()

Examples of quotation marks: no quotation marks, single quotation marks, double quotation marks, reverse quotation marks, $()

No Quotes

[root@ikta ~]# data=1
[root@ikta ~]# echo $data
1                            -----String 1

Single quotation mark

[root@ikta ~]# data='1'
[root@ikta ~]# echo $data
1                            -------Output as is whatever you write

Double Quotes

[root@ikta ~]# data=1
[root@ikta ~]# echo $data
1
[root@ikta ~]# data2="$data 123"
[root@ikta ~]# echo $data2
1 123                         --------Extract the value of a variable before outputting it

backquote

[root@ikta ~]# data3=`pwd`
[root@ikta ~]# echo $data3
/root                         ----------Commands are generally used to execute commands inside inverted quotes before passing values to variables

$()

[root@ikta ~]# data4=$(pwd)
[root@ikta ~]# echo $data4
/root                        ------------Same as inverted Quotes

Detailed description of three Linux user types

(1) Types of users in linux

Super User - User name root, which has all the permissions, logs on as a super user only for system maintenance (e.g., user creation, etc.) or other necessary situations to avoid system security problems.

System User (Pseudo User) - A built-in user necessary for a Linux system to function properly. Mainly to meet the requirements of the corresponding system processes for file ownership, such as: bin, daemon, adm, lp and other users. System users cannot be used to log on.

Ordinary users - built to enable users to use Linux system resources, most of our users fall into this category.

Super User's UID-0 

System User's UID-1~999 

Ordinary user's UID-≥1000

(2) Types of user groups in linux

Base Group (Private Group): When you create an account, if you do not specify the group to which the account belongs, a group with the same user name will be created. This group is the Base Group. The Base Group only contains one user. When other users are added to the group, the base group becomes an additional group.

Additional Groups (Public Groups): Can accommodate multiple users, in which users have rights owned by the group.

System group: generally joins some system users.

/etc/field description in passwd, shadow, group, gshadow files

In Linux, user accounts, passwords, group information, and group passwords are stored in different configuration files.

File functionFile Name
User account file/etc/passwd
User Password File/etc/shadow
User group account file/etc/group
User group password file/etc/gshadow

1) User account file --/etc/passwd

Passwd is a text file used to define user accounts for the system. Since all users have read access to passwd, it only defines user accounts, not passwords.
Each row consists of seven fields separated by':'in the following format:

Account Name: Password: UID:GID:Personal Data: Home Directory: Shell

Field description:

Account nameName used by the user to log on to the Linux system.
PasswordWhere the password was previously saved in encrypted format, it is now saved in the / etc/shadow file, where it is simply the password placeholder "x" or "*". If "x", the password is protected by shadow
UIDThe user's identity is a numeric value used to distinguish between different users
GIDThe identity of the basic group in which the user belongs is a numeric value used to distinguish between different groups where the same group has the same GID.
personal dataYou can record the user's complete name, address, office phone, home phone and other personal information.
home directoryA personal directory like Windows, usually / home/username, where username is the user name, switches the current directory to the personal home directory when the user executes the cd to command.
ShellDefines the shell that is activated after the user logs in, defaulting to Bash Shell

2) User password file - /etc/shadow

Login name: Encrypted password: Last modified time: Minimum time interval: Maximum time interval: Warning time: Inactive time: Failure time: Flag

The nine fields per line in the /etc/shadow file mean:

fieldMeaning
Login nameLogin name
encrypted passwordThe password encrypted using the SHA-512/SHA-256/MD5 algorithm, if blank, means that the user can log on without a password, if'*'means that the account cannot be used to log on to the system, if'!!!' means that the account password has been locked
Last Modified TimeThe date the password was last changed, in days from January 1, 1970
Minimum time intervalHow many days can't the password be changed. The default value is 0, meaning no limit
Maximum time intervalHow many days after the password must be changed. The default value is 99999, meaning no restrictions
Warning TimeWarn user how many days in advance password will expire, default value is 7 days, 0 means no warning
Inactive TimeDisable this user after how many days of password expiration
Failure TimePassword expiration date, expressed as days from January 1, 1970, is blank by default, indicating permanent availability
signRetain unused for future development

3) User group file - /etc/group

For each group in the system, there is a line record in the / etc/group file
Any user can read the user group account information profile.
The true password for the user group is saved in the/etc/gshadow configuration file.

group file field description:

fieldExplain
GroupnameGroup name
PasswdEncrypted password for group
GIDThe G ID field in the /etc/passwd field is the number used to specify the user's base group.
UserlistIs a "," separated user name, listing the members of the additional group.

4) User group password - /etc/gshadow

Store group passwords
slightly

Posted by john_wenhold on Tue, 23 Nov 2021 12:10:00 -0800