Experience basic Linux operation


One, Linux basic commands

Unlike Windows systems, Linux systems are more managed and configured under the command line

Next, we will learn the basic operations of the Linux system

1. Basic commands

1.1 TTY terminal

  • In the Linux default login mode, there are mainly two types, one is the login environment with only plain text interface, and the other is the login environment with graphical desktop.
  • Linux provides 6 virtual terminals by default to allow users to log in. The system sets the F1~F6 commands as ttyl-tty6
  • Use the Ctrl+Alt+Fn key combination to switch from the graphical interface to the nth virtual terminal of the command line interface
  • Fn stands for F1, F2. . . , F6 function keys (if it is a CentOS7 system, Fn represents F2, ..., F6, and F1 is a graphical interface)
  • Press Alt+Fn key combination to switch between virtual terminals

1.2 Common commands

  • For a long time, the character-mode Linux system has been widely used in the field of enterprise server due to its high efficiency, stability and reliability.
  • Many Linux/UNIX servers usually do not need to provide a monitor, and most of the server management and maintenance operations are carried out through remote login.
  • Below we focus on the basic commands commonly used in Linux systems

1.2.1 hostname

  • Execute the hostname command, you can view the full name of the current host
  • The usage is as follows:
    mark
  • The hostname command can also modify the host name of the current system. For example, executing “hostname xcf” means to modify the
    host name of the current system as xcf (the host name will take effect after you log out and log in again)
    mark
    mark

1.2.2 pwd

  • The pwd command is used to display the current working directory location of the user. The working directory is the starting point of the default location for the user to operate files or other subdirectories
  • Use the pwd command without adding any options or parameters
  • For example, when the root user executes the pwd command in the /root directory, the output information is "/root", which means that the current working directory is located in /root
    mark

1.2.3 cd

  • The cd command is used to change the user's working directory to another location . Usually, you need to switch to the target location (folder path) as a parameter.
  • If you do not specify the target location, it will switch to the current user's home directory (home directory) by default, which is the default working directory for Linux users after logging in to the system
  • For example, the following operation will change the
    working directory to /boot/grub2, and execute the pwd command to confirm the current location
    mark
  • When executing the cd command, you can also use a special directory parameter "-" (minus sign) to indicate the directory you were in before the last cd command was executed
  • For example, the following operation first switches to the current user's home directory by executing the "cd ~" command (same effect as executing the "cd" command alone), and then executes the "cd -" command to return to the original
    directory location
    mark

1.2.4 Absolute path and relative path

  • When expressing the location of a certain directory (or file) in the Linux system, two different forms can be used according to the starting directory it refers to, which are called absolute path and relative path respectively.
  • Absolute path : This method uses the root directory "/" as the starting point. For example, "/boot/grub2" means the grub2 directory in the boot subdirectory of the root directory; if you want to indicate exactly that grub2 is a directory (not a file), you can Add a directory separator at the end, such as "/boot/grub2/"
  • Because there is only one root directory in the Linux system, no matter which directory you are currently in, using an absolute path can very accurately indicate the location of a directory (or file); but if the path is long, the input will be more cumbersome
  • Relative path : This method generally uses the current working directory as the starting point, and does not use the "/" symbol at the beginning, so it is shorter and more convenient to use
  • Relative paths mainly include the following forms:
    • Directly use the directory name or file name to indicate the location of the subdirectory and file in the current working directory; for example, "grub.cfg" can indicate the grub.cfg file in the current directory
    • Start with a dot "." to clearly indicate that the current working directory is used as the starting point; for example, "/grub.cfg" can also indicate the grub.cfg file in the current directory
    • Start with two dots "...", which means that the upper-level directory (parent directory) of the current directory is used as the starting point; for example, if you are currently in the /boot/grub2/ directory, ".../vmlinuz" is equivalent to "/boot /vmlinuz"
    • Start with the form of " user name", which means that the home directory of the specified user is used as the starting point. When the user name is omitted, the default is the current user; for example, " root" means the home directory of the root user, and "~" means the host of the current user table of Contents
  • In comparison , the use of relative paths to represent directories (files) has a flexible and changeable path form, which is usually used to represent the location of directories (files) "near" to the current directory; and absolute paths are commonly used to represent the relative stability of the directory structure in the Linux system ( Directory (file) location that does not change frequently)
  • Therefore, when using a relative path or an absolute path, you should choose according to the actual situation

1.2.5 ls

  • The ls command is mainly used to display the contents of the directory , including related attribute information of subdirectories and files, etc.
  • The parameter used can be a directory name or a file name, allowing multiple parameters to be used in the same command at the same time
  • Different files are distinguished by color in the character mode. The color scheme predefined by the terminal is generally as follows: dark blue indicates directories, white indicates general files, green indicates executable files, yellow indicates device files, and red indicates compressed files
  • Execute the ls command without any options or parameters to display the subdirectories and file list information contained in the current directory (excluding hidden directories and files)
    mark
  • Executing ls-a can display the information of all subdirectories and files, including hidden directories and hidden files whose names start with a dot "."

1.3 Prompt information

  • The prompt under Linux system, such as "[root@xcf grub2]#" form, where "root" corresponds to the name of the user currently logged in, "xcf" corresponds to the host name of the machine, and "grub2" corresponds to the current user Working directory, the last "#" character means that the current login is an administrator user
  • Important operations need to have administrator privileges before they can be performed. If the current login is an ordinary user, the last "#" character will change to "$"
  • After the command prompt, you can enter various operation commands in the form of strings, and press Enter to indicate that the input is completed and executed

2. System related commands

Here are a few simple command line operations, which are mainly used to view various system information in the Linux host to familiarize yourself with the role and operation methods of the Linux command line

2.1 Check the system version

  • Execute the "lsb_release -a" command to view the system version of the current operating system
  • Need to install the redhat-lsb-core package
    mark

2.2 View CPU

  • Execute the "cat /proc/cpuinfo" command, you can view the CPU model, specifications and other information of the current host
    mark

2.3 View memory information

  • Execute the "cat /proc/meminfo" command to view the memory information of the current host
  • In the output information, the
    MemTotal line represents the total size of physical memory, and MemFree represents the size of free physical memory
    mark

2.4 Shutdown operation

  • To perform the shutdown operation, you need to execute the "shutdown -h now" or "poweroff" command
  • The Linux system can be shut down safely, and various services and processes will be shut down before the system is completely shut down.

2.5 Restart operation

  • Execute the "shoutdown -r now" or "reboot" command to safely restart the Linux system
  • Various services and processes will be shut down before restarting the system
  • When the system restarts, you will find that you cannot use Xshell to remotely connect to the server. Log in to the server and you will find that the previously configured IP address and modified host name are gone. This is because the previous commands used to configure the IP and host name are only temporary in the system. Yes, it will be lost after restarting the machine
  • If you want to permanently save the modified IP address and host name, you must modify the configuration file in the system
  • Below we will learn how to use the VI editor to modify the configuration file

Two, view and modify the configuration file

  • The network card configuration file is used to save the IP address and other information, which will not be lost after the server restarts
  • The default is stored in the "/etc/sysconfig/network-scripts/" directory, the file name is "ifcfg-XXX", where "XXX" is the name of the network card

1. The VI editor is easy to use

1.1 Introduction to VI Editor

  • A text editor is computer software used to write text, modify configuration files and programs. The most commonly used text editors in Linux systems are vi and vim
  • Linux system administrators usually use these two text editors to maintain various configuration files in the Linux system
  • Among them, vi is a powerful full-screen text editing tool, which has always been the default text editor for UNIX-like operating systems.
  • Vim is an enhanced version of the vi editor. It has expanded many practical functions on the basis of the vi editor, but it is customary to call vim vi
  • For ease of use, you can set a command alias to point vi to the vim program (the following explanations will all take the VIM program as an example)

1.2 The use of VI editor

1.2.1 Switching between three working modes and different modes

  • The vi editor has three working modes: command mode, input mode, and last line mode. The operations that can be performed on files in different modes are different
    • Command mode : After starting the vi editor, enter the command mode by default; this mode mainly completes related operations such as cursor movement, string search, and deletion, copying, and pasting of file content.
    • Input mode : The main operation in this mode is to input file content, and you can modify the text file body or add new content. When in the input mode, the last line of the vi editor will show the status message "–INSERT–"
    • Last line mode : In this mode, you can set the vi editing environment, save files, exit the editor, and perform operations such as searching and replacing file contents. When in the last line mode, a colon ":" prompt appears on the last line of the vi editor
  • Command mode, input mode and last line mode are the three states of the vi editing environment, which can be switched between different modes through different key operations
    • For example, from the command mode, press the colon ":" key to enter the last line mode
    • And if you press i, insert, etc., you can enter the input mode
    • In the input mode and the last line mode, you can press the Esc key to return to the command mode
  • After understanding the different editing modes (states) of the vi editor, the following will introduce the common operation methods in the command mode and the last line mode (input mode is used to enter text content, no special introduction)

1.2.2 Basic operation of command mode

  1. Copy operation: use the key command yy to copy the entire content of the current line to the clipboard, and use the form of #yy to copy the content of the # line starting from the cursor (where "#" is replaced with a specific number); the copied content needs Can be used after pasting
  2. Paste operation: In the vi editor, the last deleted or copied content will be saved in the clipboard buffer, press the p key to paste the content in the buffer to the cursor position, press the P key to Will be pasted before the cursor position
  3. Delete operation: use the Del key to delete a single character at the cursor; use the key command dd to delete the line where the current cursor is located, and use the form of #dd to delete the
    content of the # line starting from the cursor (where "#" is replaced with a specific number)

1.2.3 Basic operation of last line mode

In the command mode, press the colon ":" key to switch to the last line mode. The ":" prompt will be displayed in the last line of the vi editor. After the prompt, the user can enter a specific last line command to complete such as saving the file , Exit the editor, open a new file, read the content of other files, and replace strings

  • Save the file: After modifying and confirming the content of the file, you need to execute the ":w" command to save; if you need to save as another file, you need to specify a new file name, and if necessary, you can also specify the file path; for example, execute ": w /root/newfile" operation will save the currently edited file in the /root directory, the file name is newfile
  • Exit the editor: When you need to exit the vi editor, you can execute the ":q" command. If the content of the file has been modified but not saved, the ":q" command alone will not be able to exit successfully. At this time, you need to use the ":q!" command Forcibly exit (exit without saving)
  • Save and exit: To save the file and exit the vi editor, you can use a last line command ":wq" to achieve

2. Use VI to modify the configuration file

2.1 View network card configuration file

mark

cat /etc/sysconfig/network-scripts/ifcfg-ens33
...
...


DEVICE:设置网络接口的名称
ONBOOT:设置网络接口是否在 Linux 系统启动时激活
BOOTPROTO:设置网络接口的配置方式,分为“static”和“dhcp”
NETMASK:设置网络接口的子网掩码

2.2 Modify the network card configuration file

  • Execute "vim /etc/sysconfig/network-scripts/ifcfg-ens33" to enter the network card configuration file of ens33, press "i" to modify, ":wq" save and exit, the network card configuration file is modified

2.3 Modify the hostname configuration file

  • If you want to modify the host name of the Linux system, you can modify the configuration file "/etc/hostname" and execute the following command to open the configuration file, set the written host name, save and exit.
    mark
  • "Localhost.localdomain" in the configuration file is the default host name of the system, just change it to the newly modified host name
  • The settings in the configuration file are permanent and will not be lost even if the system is restarted

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/113854970