Click the blue text above to follow us

Overview
This article discusses the relationship and role of the system kernel and the Bash interpreter, how to correctly execute Linux commands, and common troubleshooting methods.
Experienced operations and maintenance personnel can appropriately combine commands and parameters to make Linux character commands more flexible while relatively reducing system resource consumption.
Powerful and Useful SHELL
Computer hardware consists of components such as the arithmetic unit, controller, memory, and input/output devices. The system kernel is what allows these various devices inside the case to perform their respective functions. The kernel is responsible for driving hardware, managing activities, and allocating/managing hardware resources. Thus, the system kernel is indeed crucial for computers, so it cannot be directly operated by users.
Since users cannot directly control hardware or operate the kernel, programs/services developed based on the “system call interface” are needed to meet users’ daily work requirements.
First, it must be acknowledged that in Red Hat RHEL7, some graphical tools, such as Logical Volume Manager (LVM), are very useful and reduce the chances of operational errors for maintenance personnel, which is commendable. However, Linux operations and maintenance personnel have always preferred to write scripts using commands, as graphical tools are less flexible and comparatively consume more system resources.
In fact, many graphical tools also call scripts to work, but their functions are “neutered” and lack flexibility. Therefore, some operations and maintenance personnel may not even install a graphical interface on the Linux system; they simply connect remotely when work is needed, which is indeed quite efficient.
“Shell”—also known as “the shell”—serves as the translator between the user and the kernel (hardware). Users “tell” Shell some commands, and it will call the corresponding program services to perform tasks. Currently, many popular Linux systems, including Red Hat, default to using Bash (Bourne-Again SHell) as their character shell.
As Bash is the default character interpreter for most Linux systems, it is essential to learn it well! Advantages of Bash:
-
1. It automatically saves command history (can be viewed using the up and down keys)
-
2. Commands can be partially entered, and the tab key can complete them (RHEL7 even supports parameter completion)
-
3. Powerful batch processing scripts
-
4. Useful environment variables
Executing Commands and Viewing Help with man
Since there is such a useful “translator,” it is necessary to learn how to communicate with it more efficiently.
To accurately and efficiently complete work, one cannot rely solely on the command itself, but should also combine various command selections and command parameters according to the actual situation:
Command Name [Command Parameters] [Command Object]
Note: Please separate the command name, command parameters, and command object with spaces.
The command object is generally the target to be processed (ordinary files/directory files/users, etc.), while the command parameters can be more troublesome for beginners, as this value varies depending on the command and environmental conditions. Therefore, it requires long-term experience accumulation to select and match parameters correctly.
Command parameters can be specified in long format (full option name) or short format (abbreviated single letter), prefixed with “–” and “-” respectively.
Long format: man --help Short format: man -h
When encountering an unfamiliar command, how can one know what available parameters it has?
In this case, the man command can be used.
The available help documents for the man command are categorized:
Generally speaking, help files are long and extensive. If you want to browse them, you need to understand the directory structure and operational methods of the help documentation:
Common System Work Commands
1. echo
The echo command is used to display strings or variables in the terminal. The format is: echo [string | variable]
Output the string of the echo command to the terminal:
[root@xgj~]# echo xgjxgj
Use the echo command to view the value of the SHELL variable (preceded by the $ symbol):
[root@xgj~]# echo $SHELL/bin/bash
View the hostname of this machine:
[root@xgj~]# echo $HOSTNAME xgj
2. date
The date command is used to display/set the system’s time or date. The format is: date [options] [+specified format]
The powerful date command can display the system’s time or date in a specified format by simply typing a string prefixed with a “+”.
The detailed format is as follows:
View the current system time:
[root@xgj ~]# date Mon Aug 24 16:11:23 CST 2016
In the format of “year-month-day hour:minute:second”:
[root@xgj ~]# date "+%Y-%m-%d %H:%M:%S" 2017-05-04 16:29:12
Set the system time to 8:30 on May 1, 2017:
[root@xgj ~]# date -s "20170501 8:30:00" Mon May 1 08:30:00 CST 2015
View the local system timezone:
[root@xgj ~]# date "+%Z" CST
Check which day of the week it is:
[root@xgj ~]# date "+%A" Tuesday
Input whether it is AM or PM:
[root@xgj Desktop]# date "+%p" AM
Determine what day of the year today is:
[root@xgj ~]# date "+%j" 244
3. reboot
The reboot command is used to restart the system (only root users can use it).
The format is: “reboot” to restart the computer:
[root@xgj ~]# reboot
4. wget
The wget command is used to download files from the web using the command line. The format is: wget [options] download address
System Status Check Commands
A qualified operations and maintenance personnel must have the ability to quickly check the system status.
1. ifconfig
The ifconfig command is used to obtain information about network card configuration and network status.
The format is: ifconfig [network device] [parameters] to view the current network card configuration and network status of this machine:
[root@xgj ~]# ifconfig............
2. uname
The uname command is used to view system kernel version and other information.
The format is: “uname [-a]” to view the system’s kernel name, kernel release, kernel version, node name, hardware name, hardware platform, processor type, operating system, and other information:
[root@xgj ~]# uname -a
By the way, if you want to see detailed version information of the system, check the redhat-release file:
[root@xgj ~]# cat /etc/redhat-release
3. uptime Check System Load
The uptime command is used to check the system load.
The format is: “uptime”
I also often use “watch -n 1 uptime” to refresh and obtain the current system load every second, where the output includes the current system time, system uptime, current online users, and average load values.
The average load is divided into the system load for the last 1 minute, 5 minutes, and 15 minutes, with lower load values being better (less than 1 is normal).
Get the current system status information:
[root@xgj ~]# uptime 22:49:55 up 10 min, 2 users, load average: 0.01, 0.19, 0.18
4. free
The free command is used to display the current memory usage in the system.
The format is: free [-m/-g]
Display the current memory usage in the system in MB:
[root@xgj ~]# free -m
5. who
[root@xgj ~]# wh
6. last
The last command is used to view all login records in the system.
The format is: “last [parameters]”
[root@xgj ~]# last root pts/0 :0 Mon Aug 24 17:52 still logged in root :0 :0 Mon Aug 24 17:52 still logged in (unknown :0 :0 Mon Aug 24 17:50 - 17:52 (00:02) reboot system boot 3.10.0-123.el7.x Tue Aug 25 01:49 - 18:17 (-7:-32) root pts/0 :0 Mon Aug 24 15:40 - 08:54 (7+17:14) root pts/0 :0 Fri Jul 10 10:49 - 15:37 (45+04:47)
7. history
The history command is used to display previously executed commands.
The format is: “history [-c]”
Historical commands are saved in the user’s home directory in the “.bash_history” file. In Linux systems, files starting with a dot (.) are hidden files, generally system files.
Clear the history of commands executed by this user on this machine:
[root@xgj ~]# history -c
The history command saves the last 1000 executed commands by default. If you want to modify this, you can directly edit the HISTSIZE value in the /etc/profile file.
8. sosreport
The sosreport command is used to collect system configuration and diagnostic information and output a conclusion document.
The format is: “sosreport”
When our Red Hat system fails and we need to contact Red Hat support or other technical support, in most cases, we need to provide this command.
Collect system local configuration information and diagnose:
[root@xgj ~]# sosreport
Working Directory Switching Commands
pwd
The pwd command is used to display the current working directory.
The format is: “pwd [options]”
cd
The cd command is used to switch the working directory.
The format is: “cd [directory name]”
Switch to the /etc directory:
[root@xgj ~]# cd /etc
Switch to the /bin directory:
[root@xgj etc]# cd /bin
Return to the previous directory (i.e., /etc directory):
[root@xgj bin]# cd -/etc
Return to the user’s home directory:
[root@xgj etc]# cd ~[root@xgj ~]#
ls
The ls command is used to view the files in a directory.
The format is: ls [options] [files] to view the files in the current directory (long format):
View the files in the /etc directory:
[root@xgj ~]# ls /etc………省略部分文件………
View the permissions and attributes of the /etc directory:
[root@xgj ~]# ls -ld /etc drwxr-xr-x. 132 root root 8192 Jul 10 10:48 /etc
Append the -h parameter to display capacity in K/M/G:
[root@xgj ~]# ls -ldh /etc drwxr-xr-x. 132 root root 8.0K Jul 10 10:48 /etc