(Add this to your favorites to enhance your Linux skills)
Author: Shouwang (This article is contributed by the author, see the end for a brief introduction)
Introduction
Among the commonly used Linux commands, some commands can be used to check the system’s status. Understanding the current state of the system can help us better maintain the system or troubleshoot issues. This article will briefly introduce these commands.
Check System Uptime — uptime
Sometimes we want to know when the system was last rebooted or how long the system has been running. We can obtain this information using the uptime command:
uptime
20:47:06 up 50 min, 1 user, load average: 0.75, 0.84, 0.93
The information displayed from left to right is: current time, uptime, number of logged-in users, and the average load over the last 1, 5, and 15 minutes.
Check Logged-in Users — who
who -a
hyb tty7 2018-09-30 19:57 (:0)
The who command allows you to see which users are logged into this host and from which IP address.
Check System Version Information — uname
For example:
uname -a
Linux ubuntu16.04 4.15.0-34-generic #37~16.04.1-Ubuntu SMP Tue Aug 28 10:44:06 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
From the result, we can see the operating system version (ubuntu16.04), CPU type (x86_64), and other information.
Check Current Environment Variables — export
Environment variables affect program execution, so sometimes it is necessary to check the current environment variables:
export
Check Directory and File Space Usage — du
The du command directly displays the space occupied by each directory and its files in the current directory. The –max-depth parameter can be used to specify the directory level to display.
For example, if you only want to display the total space occupied by each directory in the current directory, you can use:
du -h --max-depth=1
19M ./python
9.0M ./git
321M ./hexo
17M ./lua
28K ./vim
1.4M ./shell
81M ./redis
316M ./books
48M ./c
810M .
du -sh # Only count the total size of the current directory
810M
Here, -h indicates displaying sizes in a human-readable format, i.e., M, and –max-depth=1 indicates the directory level. From the command results, we can see the space occupied by each subdirectory in the current directory, as well as the total space size.
Check Space on Each Mount Point — df
For example:
df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 786M 9.5M 776M 2% /run
/dev/sda10 24G 9.8G 13G 45% /
tmpfs 3.9G 43M 3.8G 2% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda11 454M 274M 153M 65% /boot
/dev/sda15 55G 5.2G 47G 10% /home
/dev/sda1 256M 31M 226M 12% /boot/efi
/dev/sda14 4.6G 9.9M 4.4G 1% /tmp
tmpfs 786M 60K 786M 1% /run/user/1000
From the results, we can see the total space and available space for each mount point. When disk files occupy a large amount of space, it may cause the system to run slowly, so we can perform corresponding disk cleanup based on this result.
Check Available Memory — free
The free command can be used to display the available physical memory, swap memory, and the buffer used by the kernel:
free -h
total used free shared buff/cache available
Mem: 7.7G 2.3G 2.6G 268M 2.8G 4.7G
Swap: 7.6G 0B 7.6G
From the execution result, we can see the used and free memory. Similarly, when memory is insufficient, it may cause program execution anomalies or system lag.
Check Process Memory Usage — pmap
The pmap command is used to view memory-related information of a process:
pmap pid # pid is the process id, which can be obtained using the ps command
6030: ./main
0000000000400000 4K r-x-- main
0000000000600000 4K r---- main
0000000000601000 4K rw--- main
00000000017d2000 132K rw--- [ anon ]
00007fc3843e8000 1792K r-x-- libc-2.23.so
00007fc3845a8000 2048K ----- libc-2.23.so
00007fc3847a8000 16K r---- libc-2.23.so
00007fc3847ac000 8K rw--- libc-2.23.so
00007fc3847ae000 16K rw--- [ anon ]
00007fc3847b2000 152K r-x-- ld-2.23.so
00007fc3849b9000 12K rw--- [ anon ]
00007fc3849d7000 4K r---- ld-2.23.so
00007fc3849d8000 4K rw--- ld-2.23.so
00007fc3849d9000 4K rw--- [ anon ]
00007ffcf2018000 132K rw--- [ stack ]
00007ffcf2175000 12K r---- [ anon ]
00007ffcf2178000 8K r-x-- [ anon ]
ffffffffff600000 4K r-x-- [ anon ]
total 4356K
We can optimize our program based on the space occupied by different parts of the process.
Check Virtual Memory Statistics — vmstat
vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 2214960 407828 2572148 0 0 126 57 377 1006 8 2 88 2 0
The output values of vmstat can help us understand the current system performance. For example, if si (the amount of memory written from swap to memory per second) and so (the amount of memory written to swap per second) are frequently greater than 0, it indicates that memory may be insufficient, thus requiring the use of swap space. Similarly, if us (the percentage of CPU time spent on user processes) is high, it indicates that user processes are consuming a lot of CPU resources. If wa (the percentage of time spent waiting for I/O) is high, it indicates serious I/O wait conditions.
Check IP Address — ip
Sometimes using the ifconfig command may not show all IP addresses, so you can use:
ip addr
Check Network Connection Status — netstat
The netstat command can be used to check the network connection status, usually for diagnosing network-related issues. For example, to check if a port is occupied, whether a connection has been released, or if a service is listening, etc.
Check Process Relationships — pstree
The pstree command allows you to see the parent-child relationships between processes, displaying this information in a tree structure:
pstree
systemd─┬─ModemManager─┬─{gdbus}
│ └─{gmain}
├─NetworkManager─┬─dhclient
│ ├─dnsmasq
│ ├─{gdbus}
│ └─{gmain}
├─accounts-daemon─┬─{gdbus}
│ └─{gmain}
├─acpid
├─atd
├─avahi-daemon───avahi-daemon
├─bluetoothd
├─colord─┬─{gdbus}
│ └─{gmain}
├─cron
├─cups-browsed─┬─{gdbus}
│ └─{gmain}
├─cupsd───3*[dbus]
├─dbus-daemon
├─gnome-keyring-d─┬─{gdbus}
│ ├─{gmain}
│ └─{timer}
(only showing part of the content)
Conclusion
In fact, many of the command information mentioned above is obtained from system files, such as /proc/meminfo which stores memory-related information, and /proc/net/dev which stores network traffic-related information. However, these are all static values, but we can use the watch command to dynamically observe this information, for example:
watch -n 1 cat /proc/meminfo
watch -n 1 cat /proc/net/dev
The above commands indicate that cat /proc/meminfo or cat /proc/net/dev will be executed every second, allowing us to see memory information or network card traffic information being refreshed in real-time.
This article only introduces the classic usage of these commands. More usage can be viewed through the man command. With these built-in system commands, we can conveniently monitor the system status, thus helping us maintain the system or troubleshoot issues. Feel free to leave comments for additions or corrections.
[Author of this article]
Shouwang: A developer who loves literature and technology. I persist in sharing original technical articles on my personal public account “Programming Gems” and look forward to exchanging and learning together.
Recommended Reading
(Click the title to jump to read)
Common Linux Commands: Development and Debugging
Common Linux Commands: Text Viewing
How to Find and Delete Duplicate Files in Linux
Did you gain something from this article? Please share it with more people.
Follow “Linux Enthusiasts” and add it to your favorites to enhance your Linux skills.