Foreword
Linux common commands Some commands can be used to view the status of the system, with the current state of understanding of the system, it can help us to better maintain the system or locate the problem. This article will briefly explain these commands.
View system uptime, load --uptime
Sometimes we want to know the system is reset at what time or how long the system has been running, we can get this information by the uptime command:
uptime
20:47:06 up 50 min, 1 user, load average: 0.75, 0.84, 0.93
Information displayed in the order from left to right are: the current time, time has run, the number of users log in, the average load of 1 minute, 5 minutes and 15 minutes of the system.
View the system logged-in user --who
who -a
hyb tty7 2018-09-30 19:57 (:0)
Who can see through the command to which users log on to the host through which this ip.
Display system version information --uname
E.g:
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
We can see the operating system version (ubuntu16.04), CPU type (x86_64) and other information from the results.
View the current environment variables --export
Environment variables affect the operation of the program, so sometimes need to see the environment variables for the current environment:
export
Check the directory and file space --du
du commands directly display the current directory for each directory and file space. Combined --max-depth parameter to specify the directory hierarchy display.
For example, if you only want to display the directories of the total space 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 #仅统计当前目录总大小
810M
Wherein -h expressed in units readable display size, i.e. M, - max-depth = 1 indicates that the directory hierarchy. By command results, we can see the subdirectories take up space, and the total space in the current directory.
Mount View each point in space --df
E.g:
df -h
文件系统 容量 已用 可用 已用% 挂载点
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 that each mount point total space and available space. When the disk files take up more space, it may cause the system to run slowly, therefore, can make the appropriate disk cleanup based on the result.
Check availability --free memory
command can be used free physical memory, buffer memory and swap display system kernel available:
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 results we can see the situation has been memory with memory and free. Likewise, when there is insufficient memory to run the program may result in abnormal or system Caton.
View memory usage process --pmap
pmap command is used to view process memory-related information:
pmap pid #pid为进程id,可通过ps命令获取
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 each part of the process space situation, to continue to optimize our program.
View 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
Vmstat output values can help us understand the performance of the current system. For example, if si (per second write to memory size from swap) and so (written per second memory size of the swap area) is often greater than the value of 0, indicating that memory might not be enough, hence the need to use swap space. Suppose, for example, when the value of us (the percentage of user process execution time) is relatively high, indicating more CPU resources consumed by user process. The wa (IO wait time percentage) when a high value, indicating IO wait in serious condition.
View the current external IP hosts
curl ifconfig.me
Check interface information -ifconfig
ifconfig ip address can be used to view, Ethernet port and other information:
ifconfig -a
Check ip address --ip
Sometimes using the ifconfig command do not necessarily see all the ip address, you can use:
ip addr
View network connection status --netstat
netstat command to view the network connection status, commonly used to diagnose network-related issues. For example, to see if the port is occupied, whether the connection has been released, the service is listening on, and so on. In particular the section " must know the network commands -netstat ."
View CPU utilization -top
top command can be used to view the system's CPU utilization, memory usage, etc.:
top
top - 22:06:11 up 12 min, 1 user, load average: 0.87, 1.12, 1.02
Tasks: 235 total, 1 running, 180 sleeping, 0 stopped, 0 zombie
%Cpu(s): 6.9 us, 2.1 sy, 0.0 ni, 90.8 id, 0.1 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 8039576 total, 3106072 free, 2486744 used, 2446760 buff/cache
KiB Swap: 8002556 total, 8002556 free, 0 used. 4804820 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3389 hyb 20 0 1243184 108880 68152 S 6.0 1.4 0:28.70 compiz
6414 hyb 20 0 1299428 209808 84204 S 6.0 2.6 0:07.48 chrome
4879 hyb 20 0 1670760 270444 127124 S 5.6 3.4 0:32.89 chrome
View inter-process relationships --pstree
You can see the parent-child relationship between processes by pstree command, it displays the 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}
(仅显示部分内容)
to sum up
In fact, many related commands mentioned information is obtained from the file system, for example, / proc / meminfo save a memory-related information, / proc / net / dev save network traffic related information. But, they are static values, but we can combine 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 command, said every second to perform a cat / proc / meminfo or cat / proc / net / dev, so we can see the memory card information or traffic information in real-time refresh. More in the section " in Linux treasure trove of information not to be missed ."