undefaned

undefaned

Learning Linux System

Environment Configuration#

Learning Linux requires an environment, we need to create a virtual machine and then install a CentOS system on the virtual machine for learning.

Virtual Machine Installation#

VMware 15.5

System Installation#

CentOS 7

Three modes of network connection:

  1. Bridged mode: The virtual machine can communicate directly with the outside (prone to IP conflicts)
  2. NAT mode: Network Address Translation mode, the virtual machine can communicate with external systems without causing IP conflicts
  3. Host-only mode: An independent system that does not communicate with the outside

Virtual machine cloning: directly copy the folder.

Virtual machine snapshot: for example, if an accidental touch occurs and you want to return to a previous state, this is called virtual machine snapshot management.

Introduction to Vi Vim#

The vi text editor is built into the Linux system.

Vim has the capability of program editing and can be seen as an enhanced version of Vi, which can actively distinguish syntax correctness with font colors for easier program design. Features that facilitate programming, such as code completion, compilation, and error jumping, are particularly rich and widely used among programmers.

Normal mode: Opening a file with V directly enters normal mode (this is the default mode). In this mode, you can use the arrow keys to move the cursor, use 'x' to delete a character or 'dd' to delete an entire line, and you can use 'y' to copy and 'p' to paste your file data.

Insert mode: You will enter edit mode only after pressing any letter such as l, o, O, a, A, r, R; generally, pressing i is sufficient.

Command line mode: Press esc, then type . In this mode, you can provide relevant commands to perform actions such as reading, saving, replacing, exiting V, and displaying line numbers!

wq write quit

Undo: u

  1. Copy the current line with yy, copy the current line and the next 5 lines with 5yy, and paste (input P).
  2. Delete the current line with dd, delete the current line and the next 5 lines with 5dd.
  3. Search for a specific word in the file [in command line / keyword, press enter to search, input to find the next].
  4. Set the line number for the file, cancel the line number for the file. [in command line: set nu and set nonu].
  5. Edit the /etc/profile file, in normal mode, use shortcuts to go to the last line of the document [G] and the first line [gg].
  6. Input "hello" in a file, in normal mode, and then undo this action with u.
  7. Edit the /etc/profile file in normal mode and move the cursor to input 20, then input shift+g.

Related management files:

/etc/passwd file records various information about users: username: password: user identifier: group identifier: comment description: home directory: login shell.

The shell refers to the command interpreter that allows the kernel to work based on user input commands.

/etc/shadow file saves password configuration information.

/etc/group file contains group configuration information, recording the group information included in Linux.

Run Levels#

Basic introduction to run levels:

  • 0: Shutdown
  • 1: Single user [to recover lost password]
  • 2: Multi-user state without network services
  • 3: Multi-user state with network services
  • 4: System not in use, reserved for users
  • 5: Graphical interface
  • 6: System restart

Common run levels are 3 and 5, and the default run level can also be specified.

init x switch run level.

I. Remote Connection to Linux#

1.1 Network Information Configuration#

Remote login to Linux—XShell.

Remote file management—XFtp.

Check if two systems can ping each other: ping 192.168.58.129.

192.168.58.129 is the IP address of the virtual machine, verified by the host.

ip addr to query the IP address.

ifconfig to query the IP address.

ls /etc/sysconfig/network-scripts/ to view network card information.
You can see that the configuration file name for the network card IP is ifcfg-ens33.

vi /etc/sysconfig/network-scripts/ifcfg-ens33 to view the contents of the ifcfg-ens33 file.

Configure network information based on the local area network settings.

1.2 Content to Configure for Internet Connection#

DNS=8.8.8.8

DNS1=114.114.114.114

BROADCAST sets the local area network broadcast address.

IPADDR is the static IP.

NETMASK is the subnet mask.

GATEWAY is the gateway or router address.

After changing, restart the network: service network restart.

ping www.baidu.com.

Check firewall status: firewall-cmd --state or systemctl status firewalld.service.

Start the firewall: systemctl start firewalld.service.

Stop the firewall: systemctl stop firewalld.service.

Restart: systemctl restart firewalld.service.

Enable on boot: systemctl enable firewalld.service.

Disable on boot: systemctl disable firewalld.service.

Check if it starts on boot: systemctl is-enabled firewalld.service.

II. Linux System Directory Structure#

Directory Structure Tree#

  • /bin: bin is short for Binaries, this directory stores the most commonly used commands.
  • /boot: This stores some core files used when booting Linux, including some linking files and image files.
  • /dev: dev is short for Device, this directory stores external devices of Linux. In Linux, accessing devices is the same as accessing files.
  • /etc: etc is short for Etcetera, this directory is used to store all configuration files and subdirectories needed for system management.
  • /home: The home directory of users, in Linux, each user has their own directory, generally named after the user's account, such as alice, bob, and eve in the above image.
  • /lib: lib is short for Library, this directory stores the most basic dynamic linking shared libraries of the system, which function similarly to DLL files in Windows. Almost all applications need these shared libraries.
  • /lost+found: This directory is generally empty, but after an illegal shutdown, some files may be stored here.
  • /media: The Linux system automatically recognizes some devices, such as USB drives, CD-ROMs, etc. When recognized, Linux mounts the recognized devices to this directory.
  • /mnt: The system provides this directory for users to temporarily mount other file systems. We can mount the CD-ROM on /mnt/ and then enter this directory to view the contents of the CD-ROM.
  • /opt: opt is short for optional, this is the directory for placing additional software installations. For example, if you install an ORACLE database, it can be placed in this directory. It is empty by default.
  • /proc: proc is short for Processes, /proc is a type of pseudo file system (also known as a virtual file system) that stores a series of special files representing the current kernel's running state. This directory is a virtual directory, a mapping of the system memory, and we can access this directory directly to obtain system information.
  • /root: This directory is the home directory of the system administrator, also known as the superuser.
  • /sbin: s stands for Super User, which is short for Superuser Binaries, this directory stores system management programs used by the system administrator.
  • /selinux: This directory is unique to Redhat/CentOS, Selinux is a security mechanism similar to the firewall in Windows, but this mechanism is more complex, and this directory stores files related to selinux.
  • /srv: This directory stores some data that needs to be extracted after services start.
  • /sys: This is a significant change in the Linux 2.6 kernel. This directory contains a new file system called sysfs introduced in the 2.6 kernel. The sysfs file system integrates information from the following three file systems: the proc file system for process information, the devfs file system for devices, and the devpts file system for pseudo-terminals. This file system is an intuitive reflection of the kernel device tree. When a kernel object is created, corresponding files and directories are also created in the kernel object subsystem.
  • /tmp: tmp is short for temporary, this directory is used to store some temporary files.
  • /usr: usr is short for unix shared resources, this is a very important directory, many application programs and files of users are placed in this directory, similar to the program files directory in Windows.
  • /usr/bin: Applications used by system users.
  • /usr/sbin: More advanced management programs and system daemons used by the superuser. /usr/src: the default directory for kernel source code.
  • /var: var is short for variable, this directory stores things that are constantly expanding, we habitually place directories that are frequently modified in this directory, including various log files.
  • /run: This is a temporary file system that stores information since the system was started. When the system restarts, the files in this directory should be deleted or cleared. If your system has a var/run directory, it should point to run.

In the Linux system, there are several directories that are quite important, and you should be careful not to accidentally delete or arbitrarily change internal files. /etc: As mentioned above, this is the configuration file in the system. If you change a file in this directory, it may cause the system to fail to start.

/bin, /sbin, /usr/bin, /usr/sbin: These are the directories for placing executable files preset by the system, for example, ls is in the /bin/ls directory. It is worth mentioning that /bin and /usr/bin are commands for system users (general users other than root), while /sbin and /usr/sbin are commands for root.

/var: This is a very important directory. If many programs are running on the system, each program will generate corresponding logs, and these logs are recorded in this directory, specifically in the /var/log directory. Additionally, the default placement for mail is also here.

III. Basic Knowledge of Linux File and Directory Management#

Absolute Path: The path is written starting from the root directory /, for example: /usr/share/doc.

Relative Path: The path is written not starting from /, for example: to go from /usr/share/doc to /usr/share/man, it can be written as: cd ../man, which is the relative path notation.

3.1 Common Commands for Handling Directories#

ls (list directory)

  • -a: All files, including hidden files, file names starting with (commonly used)
  • -d: Display detailed information (commonly used)
  • -l: View directory attributes (commonly used)
  • -h: Used with -l to display detailed file sizes

mkdir (create new directory)

mkdir -mp xxx

  • -m: Set file permissions
  • -p: Create recursively

touch (create empty file)

touch xxx

cd (change directory)

# Use mkdir command to create runoob directory
[root@www ~]# mkdir runoob

# Use absolute path to switch to runoob directory
[root@www ~]# cd /root/runoob/

# Use relative path to switch to runoob directory
[root@www ~]# cd ./runoob/

# Indicates returning to your home directory, which is /root
[root@www runoob]# cd ~

# Switch to the parent directory
[root@www ~]# cd ..

pwd (display current directory)

pwd

rmdir (remove empty directory)

rmdir [-p] directory_name

-p removes multiple empty directories starting from that directory (can only delete empty directories).

cp (copy files or directories)

[root@www ~]# cp -rp [source_file_or_directory] [target_directory]
[root@www ~]# cp [opt] test1

mv (move files and directories, or rename)

[root@www ~]# mv [source_file_or_directory] [move_address/rename]
[root@www ~]# mv /tmp/day1 /root/day02  move to another directory and rename
[root@www ~]# mv day1 day02  rename in the current directory
  • -f: If the target file already exists, it will overwrite without asking;
  • -i: If the target file already exists, it will ask whether to overwrite!

rm (remove files or directories)

  • -f: Force execution
  • -i: Will ask whether to delete before deletion
  • -r: Recursive deletion, most commonly used for deleting directories

3.2 Viewing File Contents in Linux#

cat displays file content starting from the first line

tac displays from the last line

more scrolls one page at a time

less scrolls one page at a time

head retrieves the first few lines of a file

tail retrieves the last few lines of a file

IV. Shutdown and Restart Commands#

4.1 Restart Commands:#

  • reboot means restart
  • shutdown -r now restart immediately (used by root user)
  • shutdown -r 10 automatically restart after 10 minutes (used by root user)
  • shutdown -r 20:35 restart at 20:35 (used by root user) If the restart is set using the shutdown command, you can use shutdown -c to cancel the restart.

4.2 Shutdown Commands:#

  • halt shut down immediately
  • poweroff shut down immediately
  • shutdown -h now shut down immediately (used by root user)
  • shutdown -h 10 automatically shut down after 10 minutes If the shutdown is set using the shutdown command, you can use shutdown -c to cancel the shutdown.

V. Permission Management Commands#

5.1 User Management#

User Information

Use the cat command to view the file that saves user information, which is stored in the /etc/passwd file.
cat /etc/passwd

Superuser: root, UID=0
Normal users: UID between 500 and 60000
Pseudo users: UID between 1 and 499

Pseudo users cannot log into the system and do not have a home directory.
Why pseudo users are needed: The Linux system still needs to run even when no user is logged in, as many processes are running, so some pseudo users are needed to execute those commands.

File that saves passwords: /etc/shadow

File that saves user groups: /etc/group

File that saves user group passwords: /etc/gshadow

User configuration file: /etc/default/useradd

Add User
useradd [options] username
Options:

  • -s: Set the default shell terminal for the new user
  • -u: Set user ID
  • -e: Set the termination date for the user
  • -f: Set the number of days after which the user will be permanently disabled after the password expires
  • -M: Do not create a user home directory
  • -G: Set the corresponding extended user group for the user

Modify User
usermod [options] username
Options:

  • -c: Modify the remark text of the user account
  • -d: Modify the home directory when the user logs in
  • -e: Modify the effective period of the user account
  • -f: Set how many days after the password expires to disable the account
  • -u: Modify the user account ID
  • -l: Modify the username
  • -L: Lock the user password, making the password immediately invalid
  • -U: Unlock the user password, restoring it to normal
  • -p: Set the new password for the user

Modify User Password
passwd [options] username
Options:

  • -l: Lock the user's password, preventing modification
  • -u: Unlock the user's password, allowing modification
  • -e: Force password change at next login
  • -d: Clear existing password
  • -S: Show current password status

Switch User
su [options] username

  • -l: When switching identity, also change the working directory
  • -m: When switching identity, do not change the working directory

Delete User
userdel [options] username
Options:

  • -f: Force delete the user account without asking
  • -h: Display help information
  • -r: Delete the user's home directory and all its subfiles

View Users
ls /home

5.2 User Group Management#

Add User Group
groupadd [options] groupname

  • -g: Set the user group ID
  • -r: Create a system user group as root
  • -o: Allow creating user groups with duplicate IDs

Change User Group
groupmod [options] groupname
Options:

  • -g: Set the group identification code
  • -n: Set the group name

Delete User Group
groupdel [options] groupname

  • -f: Force delete without asking

Modify User Group
usermod [options] username

  • -l: Modify the username
  • -L: Lock the user password, making the password immediately invalid
  • -u: Modify the user account ID
  • -U: Unlock the user password, restoring it to normal
  • -d: Modify the home directory when the user logs in

5.3 Modify File Permissions#

Change File Ownership
chgrp [options] username

  • -R: Recursively process all subfiles
  • -v: Display detailed information during execution

Change File or Directory Permissions
chmod [options] username

  • -c: Output success information after successfully changing file permissions
  • -f: Do not display error information after failing to change file permissions
  • -R: Recursively process all subfiles
  • -v: Display detailed information during execution

VI. Pipe Commands and Redirection#

6-1 Pipe#

  • more: Display content page by page
  • grep: Query specified text based on the command execution results

6-2 Redirection > and >>#

> indicates output, will overwrite the original content of the file
>> indicates append, will add content to the end of the existing file

VII. Software Disk Commands#

Manage Disk Partitions
fdisk [options] device_name

  • -H: Set the number of heads on the hard disk
  • -l: Display the status of the specified peripheral device partition table
  • -s: Display the size of the specified partition
  • -S: Set the number of sectors per track
  • -b: Set the size of each partition

Display System Memory Usage
free [options]

  • -b: Set display unit to Byte
  • -g: Set display unit to GB
  • -h: Automatically adjust to a suitable display unit
  • -k: Set display unit to KB
  • -l: Display low memory and high memory statistics
  • -m: Set display unit to MB
  • -s: Continuously display memory data

7-2 Packing/Unpacking#

gzip compress file
gzip -v t.txt compress the file to .gz, used for compressing a single file

  • -d: Uncompress the specified compressed file
  • -v: Display detailed information during execution
  • -k: Keep the original file
  • -l: Display file information inside the compressed package

gunzip uncompress file
gunzip -v t.txt.gz compress or uncompress multiple files

Zip compress file
zip [options] compressed_name files_to_compress

unzip uncompress file
unzip [options] compressed_package_name -d specified_directory_name

tar compress or uncompress file
tar [options] compressed_package_name custom file_or_directory_name
tar -czvf test.tar.gz -C /etc standard suffix is .tar.gz
-c: Create a new compressed package
-z: Use gzip compression format
-v: Display detailed information during execution
-t: Display the contents of the compressed package
-f: Specify the name of the compressed file
-x: Uncompress
-C: Uncompress to the specified directory
tar [options] compressed_package_name -C uncompress_directory_name
tar -xvf File.tar -C /etc uncompress File.tar file to /etc directory

7-3 Software Installation and Uninstallation#

yum command
yum [options] action package

7-4 Software Control Start/Stop#

systemctl [options] action service_name

7-5 rpm command RPM Package Manager#

The function of rpm is similar to "Software Management" in Windows' computer management tools, mainly used for managing software packages on Linux servers, including querying, uninstalling, and installing.
rpm -ql package_name
Options:

  • -a: Display all software packages
  • -e: Uninstall software package
  • -i: Install software package
  • -l: Display the file list of the software package
  • -q: Display whether the specified software package is installed
  • -U: Upgrade software package
  • -v: Display execution process information

Query the installation status of a certain software

Install software

View the installation path of the software

Uninstall a certain software

Upgrade a certain software package

8-1 Search Commands#

find command
find path condition filename

grep command
grep [options] filename

Similar to shortcuts in Windows
ln [options] source_filename target_filename

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.