[Operation and maintenance engineer learning] Linux common commands

0. Verify system version

cat /etc/centos-release

insert image description here

[root@localhost ~]#

Contains a lot of information, as follows:

  • root: Indicates the current login user name
  • @: It is just a separator, which can be understood as "at", but it means "in" and "at".
  • localhost: Indicates the current host name
  • ~: Indicates the root directory of the current user (the root directory of the root user is /root, so cd /rootthe cd ~result is the same. Similarly, the root directory of another user siman is /home/siman, and cd /home/simanthe cd ~result is the same)
  • #: Indicates that the current user is the root (root) user, and non-root users are represented by $

1. Verify system type (32-bit or 64-bit)

uname -a

insert image description here

2. Verify partition status

df -h

insert image description here
dfIt is a tool used to view the capacity information of the disk. -hThe parameter is added for the readability of the capacity, that is, M or G will be automatically used as the unit , and if no -hparameter is added, the unit is byte .

name mean
Filesystem File system
Size total capacity
Avail available (remaining free space)
Use% Used as a percentage of the total
Mounted on mounted directory
Used used

Swap is not available to users, so the swap status cannot be checked through the df tool. In addition, tmpfs is a memory file system under Linux . For /dev/shmthis directory, you can save files like a directory on a hard disk, but it will be lost after the system restarts, because it is a space divided in memory.

3. Verify CPU configuration

cat /proc/cpuinfo

insert image description here

name mean
processor Indicates the core ID, starting from 0, if there are 8 cat cores in total, the ID of the last one is 7
physical id Indicates the physical CPU ID, starting from 0, if there are 2 physical CPUs in total, the ID of the last one is 1

4. Verify memory configuration

cat /proc/meminfo

insert image description here

name mean
MemToal total memory capacity
free

insert image description here

  • This method displays the status of the swap partition (swap) in addition to the physical memory status
  • The swap partition of linux is equivalent to the virtual memory under windows, which opens up a space on the hard disk and uses it as memory to improve system efficiency.
    In order to improve the efficiency and speed of reading and writing, the Linux kernel will cache files in the memory. This part of the memory is Cache Memory (cache memory).
    Even after your program finishes running, Cache Memory will not be released automatically. This will cause you to find that the available physical memory becomes less after the program frequently reads and writes files in the Linux system. When the physical memory of the system is not enough, a part of the physical memory needs to be released for use by currently running programs. The freed space may come from some programs that have not been operated for a long time. These freed spaces are temporarily saved in the Swap space, and when those programs are about to run, the saved data is restored from the Swap partition to the memory. In this way, the system always performs Swap when the physical memory is not enough.

5. File operation

name mean
pwd Display the full path of the current directory
ls Display the files in the specified directory. If the specified directory is not followed, the current directory information will be displayed.
cd Enter the specified directory

1、pwd

pwd

insert image description here

2、cd

cd Desktop
cd ~

insert image description here

3、ls

ls

insert image description here

ls -l

insert image description here

The output is to use ls -lthe command to display detailed information of all files and subdirectories in the current directory . Each line of the output contains some basic information about the file/folder, the following is the meaning of each field:

total 0                      总文件数
drwxr-xr-x. 2 gee gee 6 Jun 24 21:18 Desktop   
文件权限、硬链接数、所有者、组、大小、时间、名称
  • Total Files : The total number of files/folders
  • File permissions : file/folder permissions, represented by r, w, x
  • Number of hard links : For files, it indicates how many files are linked to this node; for directories, it indicates how many subdirectories are linked to this node
  • owner : the owner of the file/folder
  • Group : The group to which the file/folder belongs
  • Size : the size of the file, the directory size is usually 4096 bytes
  • Time : The time when the file was last modified
  • name : the name of the file/folder
  • rw-r--r--Permission information of the current file (folder), r:读, w:写, x:执行. 3 bits are a group , such as rw-, r–, r– represent 当前用户, 用户组, respectively 其它用户的权限.

For example, the second line indicates the Desktop folder in the current directory, the file permission is rwxr-xr-x.2, the number of hard links is 2, the owner and the belonging group are both gee, the size is 6, the last modification time is 21:18 on June 24, and the file name is for Desktop. The first letter dindicates that the object is a directory, -and it indicates an ordinary file.

ls -a

-a: Indicates to display hidden files. The "." in front of the file name indicates that the file is a hidden file . To access a hidden file, you need to enter ".". If you want to view .bash_profile, you need to enter cat .bash_profile
insert image description here

ll

insert image description here

  • llEqual ls –l, but ll is not a command (program), but an alias for ls -l , saving typing time. You can customize the alias of any command, for example, you can define la equal tols –a

6. How to define ll as an alias of ls -l, so to set the definition alias

In Linux, aliases can be defined by modifying the terminal configuration file. There are many kinds of terminal programs, the most common of which is the Bash terminal. In the Bash terminal, ~/.bashrcaliases can be defined by modifying files.

laYou can set the alias as the following steps ls -a.

1. Open ~/.bashrcthe file

Open the file with a text editor ~/.bashrc, for example:

nano ~/.bashrc

2. Define an alias

Add the following two lines at the end of the file:

alias ll='ls -l'
alias la='ls -a'

The first line of command defines an llalias and sets it as ls -lthe abbreviation of ; the second line of command defines an laalias and sets it as ls -athe abbreviation of .

3. Active alias

Save the file and exit the text editor. To put the new alias into effect and enter the current session, enter the following command:

source ~/.bashrc

This immediately reloads ~/.bashrcthe file, allowing the defined aliases to take effect.

llNow you can use the command instead of ls -lthe command and the command lainstead of the command in the terminal ls -a.

4. Practical effect

  • before setting

insert image description here

  • set configuration file

insert image description here

  • after setting

insert image description here

7. File operation (mkdir, rm, cp, mv)

  1. mkdir, mk is the abbreviation of make (creation, generation), dir is the abbreviation of directory (directory), make directory means to create a directory (folder)

insert image description here

  1. rmIt is the abbreviation of remove. As the name suggests, it means to delete. When using rm to delete a file, if you do not add any parameters, it will prompt whether to delete the file, enter y to confirm, and enter n to cancel. Add the -f parameter to force deletion without prompting (use with caution)

insert image description here

  • To delete a directory (folder), you need to add the -r parameter, just like a file, add -f to delete it without prompting (no regrets), here are two parameters written together -rf

insert image description here

  1. cp and mv
name mean
cp The abbreviation of copy, as the name implies, means to copy
mv The abbreviation of move, as the name implies, means to move
命令格式:
cp 目录/源文件 目标目录
mv 目录/源文件 目标目录
  • In addition to the function of moving, mv also has the function of renaming, that is, when the file directory does not change, it is only renamed. Of course, the target directory also supports renaming.
  • cp practice effect

insert image description here

  • mv practice effect

insert image description here

8. Text editing (vi)

There are vim and nano for text editors under Linux, but only vim is included in the minimal installation of centos, so only vim is introduced. If you need to use nano, you need to install it separately.

On Linux systems, you can install using the command line nano:

  1. open terminal

Open a terminal in your Linux OS.

  1. Install nano using the package manager
  • Debian/Ubuntu distributions can use apt-getthe package manager:

    sudo apt-get update
    sudo apt-get install nano
    

    This will update the system package list and install nanothe editor.

  • CentOS/RHEL distributions can use yumthe package manager:

    sudo yum update
    sudo yum install nano
    

    This will update the system package list and install nanothe editor.

  • Arch Linux distributions can use pacmanthe package manager:

    sudo pacman -Sy nano
    

    This will update the system package list and install nanothe editor.

After the installation is successful, you can enter the command in the terminal nanoand press Enter to run nanothe editor.

vi format:

vi 文件名
  • Opens the file if the filename exists, or creates a new file in open mode if the filename does not exist.
  • After entering the open mode, you need to press i to enter the edit mode, and move the input cursor position by moving the direction keys.
  • Press the keyEsc to exit the editing mode, and save and exit operations can only be performed in the exiting editing mode.
  • Exiting the editing mode is the same as the mode just opened. In this mode, input :wqmeans saving and exiting vim, :means entering the command mode of vim wmeans writing (saving) qmeans exiting.

In vithe editor, you can use the following shortcuts to find, replace, and delete entire lines:

  1. find text

In vithe editor, you can use the /or ?commands to find text.

  • /Order

    In command mode, press /the key, then enter the text you want to find, and finally press Enterthe key. viThe editor looks for text below the current cursor position and positions the cursor on the first matching result.
    insert image description here

  • ?Order

    In command mode, press ?the key, then enter the text you want to find, and finally press Enterthe key. viThe editor looks for text above the current cursor position and positions the cursor on the first matching result.
    insert image description here

  1. replace text

In vithe editor, you can use :%sthe command to replace text.

  • :%s/old/new/gOrder

    In the command mode, enter :%sthe command, then enter oldreplace with new, and finally add gthe parameter to indicate global replacement. adFor example, if you want to replace all in the text with 12, you can execute the following command:

    :%s/ad/12/g
    

    This will search the text globally and replace all matches ad.
    insert image description here
    insert image description here

  1. delete entire line

In vithe editor, ddan entire line can be deleted with the command:

  • ddOrder

    In the command mode, press ddthe command to delete the line where the current cursor is located. If you want to delete multiple lines, you can ddadd the number of lines to be deleted before the command. For example, if you want to delete the current line and the following 3 lines, you can enter the following command:

    4dd
    

    This will delete the current line and the 3 lines below it.
    insert image description here
    insert image description here

Hope these commands can help you vifind, replace and delete text more efficiently in the editor.

9. Text viewing (cat, more, less)

cat test.txt

insert image description here

less test.txt

insert image description here
insert image description here

more test.txt

insert image description here

10. Text view (grep, find)

insert image description here

  • Display the lines containing asd in test.txt

Format:
grep Search content directory/file name
If no directory is added before the file name, it means it is a file in the current directory

In addition to searching the contents of files on the hard disk, grep can also filter and output (or specify output) the output of other programs on the terminal, but the pipe character " " is required |.
Pipeline: As the name implies, it means transmission and connection. Therefore, the su function of the pipe symbol in Linux is to pass the result output by the previous program to the subsequent application, that is, the processing object of the subsequent application comes from the result of the previous application. insert image description here
Use grep to filter the ip addr output and output only lines containing inet:
insert image description here

Format:
grep Search content directory/file name
If no directory is added before the file name, it means it is a file in the current directory

  • Search by full name of a file

insert image description here

  • Use wildcards *to search for matches, *which means multiple characters match, and wildcards mean single character matches

insert image description here

11. Packaging and decompression (tar)

  • Packaging and compression under Linux are two concepts, tar is just a packaging tool (package multiple files into one file), but you can call other compression tools in tar to compress while packaging
  • Format:

1. Pack only: tar –cvf 打包后的文件名 需要被打包的文件(夹)
2. Pack and call gz for compression:tar –czvf 打包后的文件名 需要被打包的文件(夹)

  • Note:
    gz has a dedicated command, if the file suffix is ​​not, .gzyou .tar.gzcannot use tar to decompress, you need to use gunzip. For more usage methods of gz, please search for "linux gz".
    In addition, in addition to gz compression, there are also bz2, tgz, z, etc. under linux, and of course there will be zip.
    The following only demonstrates tar packaging and tar calling gz for compression

1. First use ls -lsh to see what files and file sizes are in the directory

insert image description here
2. Use the -cf parameter to save only files ending with .repo in the current directory打包
insert image description here

3. Use the -czf parameter to copy all .repo files打包并压缩
insert image description here

insert image description here

Parameter explanation: -cmeans packaging, unpacking is -x. -tOnly list files inside the package. -fmust have. -vPrint out the file being manipulated (decompressed), optional.

12. Network settings (IP)

The IP configuration file of the network card is located /etc/sysconfig/network-scriptsin the directory and ifcfg-*exists in the form of the file name, where ifcfg- is fixed, *indicating the name of the network card interface. (The name of the network card interface of different machines is different, check it with ls before setting)

cd /etc/sysconfig/network-scripts
ls
cat ifcfg-ens160

insert image description here

  • This machine has set up the network (dhcp mode) when it is installed, so you can see more items (default), but not all items are necessary. The following demonstrates the mode of using a static IP address and keeps only necessary items.

insert image description here

  • TYPE=“Ethernet”
    Indicates that the network interface is an Ethernet interface, that is, a common wired network interface, and other types may include wireless interfaces, virtual interfaces, and so on. The setting of this line is important for the configuration and management of network connections, because different network interface types require different configuration parameters.

  • BOOTPROTO=“static”Indicates that the network interface will use a static IP address, which means that parameters such as the IP address, subnet mask, and gateway of the network interface need to be manually specified in the file. This is different from DHCP (Dynamic Host Configuration Protocol), which is a protocol for automatically assigning IP addresses, which will automatically assign parameters such as IP address, subnet mask, gateway, etc. to that network interface.

    Therefore, the setting of the BOOTPROTO="static" line tells the system not to leave the configuration of the network interface to DHCP
    , but to use manual configuration to specify network parameters.

  • Network card name ( DEVICE), boot enabled ( ONBOOT)

  • IP ( IPADDR), mask ( NETMASK), gateway ( GATEWAY)

After the configuration is complete, you need to restart the network card to take effect, restart the command

 systemctl restart network
报错Failed to restart network.service: Unit network.service not found.

This error means that the network service named network.service is not enabled in the current system, so the restart operation cannot be performed.

In CentOS 7 or higher , the network service has been migrated from NetworkManager to systemd-networkd and systemd-resolved. Therefore, if you want to restart network services, you should use the following command:

systemctl restart systemd-networkd.service

or:

systemctl restart NetworkManager.service

Among them, systemd-networkd.service is a network service in the systemd framework, which is suitable for use in a small network environment (such as a server). NetworkManager.service is a widely used network management program, which provides more network management functions, and is suitable for complex network environments that need to manage multiple network interfaces.

Therefore, if systemd-networkd.service or NetworkManager.service exists in your system, you can try to use the above command to restart the network service.

Single network card dual-line (2 IPs) and three-line (3 IPs) for IP settings.

Note: The multi-IP configurations discussed in this discussion are all configured on one network card, and all IPs are in the same subnet (commonly known as IPs in the same segment). Different IP segments of dual network cards need to configure system routing policies, which is beyond the scope of this discussion.

Configuration ideas:

Copy the original network card IP configuration file (ifcfg-*), and modify the file name to the name of the new interface, modify the DEVICE item, IPADDR, NETMASK item in the new configuration file, and cancel the GATEWAY item. For the system, it is a physical network card for two configuration files, but the two configuration files cannot have the same name, and the device name (DEVICE) cannot be the same.
insert image description here

Kind tips:

The above is the two-line configuration, and the three-line configuration is the same, and then copy one more copy, pay attention to the DEVICE name and file name.

As mentioned earlier, modifying the network card configuration file needs to restart the network card to take effect. Similarly, adding a new configuration file to ip also requires restarting the network card.
The command to restart the network card: systemctl restart network

Check if it works:

Method 1: Ping the IP you configured on another machine, and the general rule will take effect (if it fails, it may not necessarily not take effect. This involves network knowledge, which will be introduced later) Method 2: Use the ip addr command in the system to detect
and Ping the address of this machine on this machine (recommended method)
insert image description here

  • UP indicates that the network card is enabled
  • 1: lo is a loop interface, it must exist, and it is enabled by default
  • 2: eno16777736 is the physical network card just configured

Two IP addresses are configured. If it is a two-line computer room, you can understand one of them as the Telecom IP and the other as the Unicom IP.

Kind tips:

When you see UP, you can confirm that the configuration file has no syntax errors and has been successfully enabled. At the same time, you can ping your own IP on the machine, such as ping 192.168.14.128 or ping 172.16.0.128, and you can get normal return information.

13. Network settings (DNS)

  • The standard configuration file for DNS is in/etc/resolv.conf
  • Why is it a standard configuration file? Because some people configure DNS in the aforementioned network card configuration file.
  • Add the DNS server address to /etc/sysconfig/network-scriptsthe network card configuration file (ifcfg) located in the directory in the following format. For example
DNS1=xxx.xxx.xxx.xxx

It is recommended to use the standard configuration file.

  • Edit the resolv.conf file and add the DNS server address

Note: The modification of the DNS configuration takes effect immediately, so after configuration, immediately ping the domain name of a large website, such as 163.com or baidu.com, etc., if there is a return, the configuration is successful.
insert image description here

14. Firewall settings (firewalld)

Because the firewall involves too much network knowledge, and for the computer room, it is rarely touched, because even if it is a remote port, the system firewall is open by default, no need to set it. Therefore, the introduction of the firewall at this stage is only an introduction to the operation of the firewall service (start, stop, disable, and view the running status). Usually, for beginners, in order to reduce unnecessary troubles, it is recommended to disable the firewall first.
insert image description here

Check if the firewall is enabled

systemctl status firewalld
  • activesys means start
name mean
systemctl stop firewalld stop firewall
systemctl disable firewalld Disable the firewall, that is, it will not restart after the next restart (it will only take effect next time)
systemctl start firewalld start firewall
  • After deactivation:

insert image description here

15. Firewall settings Firewall settings (selinux)

getenforce
  • Check the status of selinux through getenforcethe command, Enforcing means it is enabled
vi /etc/selinux/config
  • Modify the configuration file of selinux
  • Change SELINUX=enforcing to SELINUX=disabled
SELINUX=disabled

Modified state:
insert image description here

After the modification is completed, the system needs to be restarted to take effect.

16. Commonly used commands - remote (ssh)

The character operation of Linux uses the ssh protocol for remote connection (the linux graphical desktop uses the vnc protocol for remote desktop connection, and the Microsoft windows uses the RDP protocol)
. The ssh client can log in to the host remotely. So there is not much introduction to the server, just introduce the client that supports the ssh protocol for your reference, such as:

  • Putty

  • Xshell

  • Bitvise SSH Client

  • Securecrt

VNC (Virtual Network Computing) and SSH (Secure Shell) protocols

  • Both VNC (Virtual Network Computing) and SSH (Secure
    Shell) protocols are commonly used remote access protocols in the computer field. They mainly differ in terms of security and functionality.

    The VNC protocol allows users to control another computer from a remote computer with a graphical interface. The way the VNC protocol works is that the screen image on the remote computer is transmitted to the local computer, and then the remote computer is controlled through mouse and keyboard input on the local computer. The main advantage of the VNC protocol is that it is easy to install and use, and is suitable for remote desktop support and management.

    However, the disadvantage of the VNC protocol is relatively poor security, since it can transmit all the user's screen images and mouse/keyboard input over the network. In addition, the graphics transmission speed of the VNC protocol may also be affected by network delays.

  • The SSH protocol is an encrypted network protocol used to create an encrypted channel between a local computer and a remote computer. It is mainly used for remote login and command execution, file transfer via SSH, and secure transfer of large amounts of data to remote computers/servers. The SSH protocol offers increased security as all transmitted information is encrypted. In addition, the use of the SSH protocol is fast and suitable for text-based system management.

    In short, the VNC protocol is suitable for environments that need to control the desktop and support a graphical interface, while the SSH protocol is suitable for environments that require higher security and text-based remote access. In actual use, these protocols can complement each other, and you can choose which protocol to use according to specific needs.

Xshell Common Commands - Restart/Shutdown

Shutdown command:

Order explain
shuthown -h The root user is required, you can enter directly as the root user
halt The most commonly used, it is actually equal to shutdown –p
init 0 init is a very important concept in linux, for more information, please search "linux init"

Restart command:

name mean
shutdown –r Permissions require root
reboot The most commonly used, it is actually equal to shutdown –r
init 1 For more information search for "linux init"

Summarize

For operation and maintenance engineers, the basic operations of linux are 验证系统版本, 验证硬件,, 配置网络etc., and configuring the network involves 文件的基本操作,查看、复制、编辑等.

Guess you like

Origin blog.csdn.net/weixin_43576565/article/details/131376844