1. uptime
# uptime
14:57:31 up 163 days, 23:23, 2 users, load average: 0.44, 0.23, 0.21
-
14:57:31
:当前时间。(The current time.) -
up 163 days, 23:23
:距离上一次启动以来的系统运行时间。(How long the system has been running.) -
2 users
:正在登录的用户数。(How many users are currently logged on.) -
load average: 0.44, 0.23, 0.21
:过去1分钟、5分钟、15分钟的平均负。(The system load averages for the past 1, 5, and 15 minutes.)
平均负载
:是指单位时间内,系统处于可运行状态和不可中断状态的平均进程数。(System load averages is the average number of processes that are either in a runnable or uninterruptable state.)
可运行状态的进程
:是指正在使用 CPU 或者正在等待 CPU 的进程,也就是我们常用 ps 命令看到的,处于 R 状态(Running 或 Runnable)的进程。(A process in a runnable state is either using the CPU or waiting to use the CPU.)
不可中断状态的进程
:则是正处于内核态关键流程中的进程,并且这些流程是不可打断的,比如最常见的是等待硬件设备的 I/O 响应,也就是我们在 ps 命令中看到的 D 状态(Uninterruptible Sleep,也称为 Disk Sleep)的进程。(A process in uninterruptable state is waiting for some I/O access, eg waiting for disk.)
2. vmstat
# vmstat 1 1
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 386880 220644 3959584 0 0 11 34 1 1 5 2 93 0 0
-
r(Running or Runnable)
:就绪队列的长度,也就是正在运行和等待CPU的进程数。(The number of runnable processes (running or waiting for run time).) -
b(Blocked)
:就是不可中断状态的进程数。(The number of processes in uninterruptible sleep.) -
swpd
:使用的虚拟交换内存大小。(The amount of virtual memory used.) -
free
:空闲内存的大小。(The amount of idle memory.) -
buff
:buffer cache 的大小,表示直接读写块设备(比如磁盘)时的缓存((内核 --> 块设备))。(The amount of memory used as buffers.) -
cache
:page cache 的大小,表示通过文件系统(比如 xfs、ext4)读写文件时的缓存(内核 --> 文件系统 --> 块设备)。(The amount of memory used as cache.) -
si
:每秒从虚拟交换内存写到内存的大小。(Amount of memory swapped in from disk (/s).) -
so
:每秒写入虚拟交换内存的内存大小。(Amount of memory swapped to disk (/s).) -
bi
:表示块设备读取的大小,单位为块/秒。因为 Linux 中块的大小是 1KB,所以这个单位也就等价于 KB/s。(Blocks received from a block device (blocks/s).) -
bo
:表示块设备写入的大小,单位为块/秒。因为 Linux 中块的大小是 1KB,所以这个单位也就等价于 KB/s。(Blocks sent to a block device (blocks/s).) -
in(interrupt)
:则是每秒中断的次数。(The number of interrupts per second, including the clock.) -
cs(context switch)
:是每秒上下文切换的次数。(The number of context switches per second.) -
us(user)
:代表用户态 CPU 时间,包括 nice 时间。(Time spent running non-kernel code. (user time, including nice time)) -
ni(nice)
:代表低优先级用户态 CPU 时间,也就是进程的 nice 值被调整为 1-19 之间时的 CPU 时间。这里注意,nice 可取值范围是 -20 到 19,数值越大,优先级反而越低。 -
sy(system)
:代表内核态 CPU 时间。(Time spent running kernel code. (system time)) -
id(idle)
:代表 CPU 空闲时间。注意,Linux 2.5.4 版本后它不包括等待 I/O 的时间(iowait)(Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.) -
wa(iowait)
:代表等待 I/O 的 CPU 时间。(Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.) -
st(steal)
:代表当系统运行在虚拟机中的时候,被其他虚拟机占用的 CPU 时间。(Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.)
CPU 上下文切换说明:
在每个任务运行前,CPU 都需要知道任务从哪里加载、又从哪里开始运行,也就是说,需要系统事先帮它设置好 CPU 寄存器和程序计数器(Program Counter,PC)。
CPU 寄存器,是 CPU 内置的容量小、但速度极快的内存。而程序计数器,则是用来存储 CPU 正在执行的指令位置、或者即将执行的下一条指令位置。它们都是 CPU 在运行任何任务前,必须的依赖环境,因此也被叫做 CPU 上下文。
CPU 上下文切换,就是先把前一个任务的 CPU 上下文(也就是 CPU 寄存器和程序计数器)保存起来,然后加载新任务的上下文到这些寄存器和程序计数器,最后再跳转到程序计数器所指的新位置,运行新任务。
而这些保存下来的上下文,会存储在系统内核中,并在任务重新调度执行时再次加载进来。这样就能保证任务原来的状态不受影响,让任务看起来还是连续运行。
根据任务的不同,CPU 的上下文切换就可以分为几个不同的场景,也就是进程上下文切换、线程上下文切换以及中断上下文切换。
3. pidstat
# pidstat -w 1 1
Linux 3.10.0-957.el7.x86_64 (cnszkf02001) 12/01/2020 _x86_64_ (4 CPU)
01:23:41 AM UID PID cswch/s nvcswch/s Command
省略其他输出……
01:23:42 AM 99 27413 3.92 0.00 nginx
01:23:42 AM 0 32680 7.84 0.00 dockerd
Average: UID PID cswch/s nvcswch/s Command
省略其他输出……
Average: 99 27413 3.92 0.00 nginx
Average: 0 32680 7.84 0.00 dockerd
-
cswch/s
:表示每秒自愿上下文切换(voluntary context switches)的次数,指进程无法获取所需资源,导致的上下文切换。比如说, I/O、内存等系统资源不足时,就会发生自愿上下文切换。(Total number of voluntary context switches the task made per second. A voluntary context switch occurs when a task blocks because it requires a resource that is unavailable.) -
nvcswch/s
:表示每秒非自愿上下文切换(non voluntary context switches)的次数,是指进程由于时间片已到等原因,被系统强制调度,进而发生的上下文切换。比如说,大量进程都在争抢 CPU 时,就容易发生非自愿上下文切换。(Total number of non voluntary context switches the task made per second. A involuntary context switch takes place when a task executes for the duration of its time slice and then is forced to relinquish the processor.)
4. /proc/stat
# grep ^cpu /proc/stat
cpu 80938541 9246 25886506 24928033302 659223 0 1762342 0 0 0
cpu0 17610971 2217 5787558 6236972220 33302 0 479401 0 0 0
cpu1 21536748 2406 6738397 6230678777 43307 0 456602 0 0 0
cpu2 21547981 2261 6828093 6228696088 43968 0 463363 0 0 0
cpu3 20242841 2361 6532456 6231686215 538644 0 362975 0 0 0
-
cpu[N]
:CPU内核序号。(The specific CPU.) -
user(通常缩写为 us)
:代表用户态 CPU 时间。(Time spent in user mode.) -
nice(通常缩写为 ni)
:代表低优先级用户态 CPU 时间,也就是进程的 nice 值被调整为 1-19 之间时的 CPU 时间。这里注意,nice 可取值范围是 -20 到 19,数值越大,优先级反而越低。(Time spent in user mode with low priority (nice).) -
system(通常缩写为sy)
:代表内核态 CPU 时间。(Time spent in system mode.) -
idle(通常缩写为id)
:代表 CPU 空闲时间。注意,它不包括等待 I/O 的时间(iowait)。(Time spent in the idle task. This value should be USER_HZ(1/100 second) times the second entry in the /proc/uptime pseudo-file.) -
iowait
(通常缩写为 wa):代表等待 I/O 完成的 CPU 时间(不一定可靠,要配合其他参数一起来使用)。(Time waiting for I/O to complete.This value is not reliable
, for the following reasons: 1. The CPU will not wait for I/O to complete; iowait is the time that a task is waiting for I/O to complete. When a CPU goes into idle state for outstanding task I/O, another task will be scheduled on this CPU. 2. On a multi-core CPU, the task waiting for I/O to complete is not running on any CPU, so the iowait of each CPU is difficult to calculate. 3. The value in this field may decrease in certain conditions.) -
irq(通常缩写为 hi)
:代表处理硬中断的 CPU 时间。(Time servicing interrupts.) -
softirq(通常缩写为 si)
:代表处理软中断的 CPU 时间。(Time servicing softirqs.) -
steal(通常缩写为 st)
:代表当系统运行在虚拟机中的时候,被其他虚拟机占用的 CPU 时间。(Stolen time, which is the time spent in other operating systems when running in a virtualized environment.) -
guest(通常缩写为 guest)
:代表作为宿主机通过虚拟化运行其他操作系统的时间,也就是运行虚拟机的 CPU 时间。(Time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.) -
guest_nice(通常缩写为 gnice)
:代表以低优先级运行虚拟机的时间。(Time spent running a niced guest (virtual CPU for guest operating systems under the control of the Linux kernel).)
平均 CPU 使用率计算方式
5. ps aux
# ps aux | head -n 5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 191440 3504 ? Ss Feb11 1:40 /usr/lib/systemd/systemd --system --deserialize 22
root 2 0.0 0.0 0 0 ? S Feb11 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Feb11 29:56 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Feb11 0:00 [kworker/0:0H]
STAT 列说明:
-
R
:表示进程在 CPU 的就绪队列中,正在运行或者正在等待运行。(Running or runnable (on run queue)。) -
D
:是 Disk Sleep 的缩写,也就是不可中断状态睡眠(Uninterruptible Sleep),一般表示进程正在跟硬件交互,并且交互过程不允许被其他进程或中断打断。(Uninterruptible sleep (usually IO).) -
Z
: 是 Zombie 的缩写,表示僵尸进程,也就是进程实际上已经结束了,但是父进程还没有回收它的资源(比如进程的描述符、PID 等)。(Defunct ("zombie") process, terminated but not reaped by its parent.) -
S
:是 Interruptible Sleep 的缩写,也就是可中断状态睡眠,表示进程因为等待某个事件而被系统挂起。当进程等待的事件发生时,它会被唤醒并进入 R 状态。(Interruptible sleep (waiting for an event to complete).) -
I
:是 Idle 的缩写,也就是空闲状态,用在不可中断睡眠的内核线程上。前面说了,硬件交互导致的不可中断进程用 D 表示,但对某些内核线程来说,它们有可能实际上并没有任何负载,用 Idle 正是为了区分这种情况。要注意,D 状态的进程会导致平均负载升高, I 状态的进程却不会。 -
T
:也就是 Stopped 的缩写,表示进程处于暂停状态。(Stopped by job control signal.) -
t
:Traced 的缩写,表示进程处于跟踪状态。(Stopped by debugger during the tracing.) -
X
:也就是 Dead 的缩写,表示进程已经消亡。(dead (should never be seen).) -
s
:表示这个进程是一个会话的领导进程。(Is a session leader.) -
+
:表示前台进程组。(Is in the foreground process group.) -
<
:高优先级进程。(High-priority (not nice to other users).) -
N
:低优先级进程。(Low-priority (nice to other users).)
6. free
# free -m
total used free shared buff/cache available
Mem: 7982 1534 203 401 6244 5695
Swap: 3967 181 3786
-
total
:总内存大小。(Total installed memory (MemTotal and SwapTotal in /proc/meminfo).) -
used
:已使用内存的大小,包含了共享内存。(Used memory (calculated as total - free - buffers - cache).) -
free
:未使用内存的大小。(Unused memory (MemFree and SwapFree in /proc/meminfo).) -
shared
:共享内存的大小。(Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available on kernels 2.6.32, displayed as zero if not available).) -
buff
:buffer cache 的大小。(Memory used by kernel buffers (Buffers in /proc/meminfo).) -
cache
:page cache 的大小。(Memory used by the page cache and slabs (Cached and Slab in /proc/meminfo).) -
available
:新进程可用内存的大小。available不仅包含未使用内存,还包括了可回收的缓存,所以一般会比未使用内存更大。不过,并不是所有缓存都可以回收,因为有些缓存可能正在使用中。(Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free).)
Buffers/Cached 额外说明*:
Buffers
:是对原始磁盘块的临时存储,也就是用来缓存磁盘的数据,通常不会特别大(20MB左右)。这样,内核就可以把分散的写集中起来,统一优化磁盘的写入,比如可以把多次小的写合并成单次大的写等等。Buffer 既可以用作“将要写入磁盘数据的缓存”,也可以用作“从磁盘读取数据的缓存”。(Relatively temporary storage for raw disk blocks that shouldn't get tremendously large (20MB or so).
Cached
:是从磁盘读取文件的页缓存,也就是用来缓存从文件读取的数据。这样,下次访问这些文件数据时,就可以直接从内存中快速获取,而不需要再次访问缓慢的磁盘。Cache 既可以用作“从文件读取数据的页缓存”,也可以用作“写文件的页缓存”。(In-memory cache for files read from the disk (the page cache). Doesn't include SwapCached.)
关于磁盘和文件的区别,磁盘是一个块设备,可以划分为不同的分区;在分区之上再创建文件系统,挂载到某个目录,之后才可以在这个目录中读写文件。
其实 Linux 中“一切皆文件”,而文章中提到的“文件”是普通文件,磁盘是块设备文件,这些大家可以执行 "ls -l <路径>" 查看它们的区别(输出的含义如果不懂请 man ls 查询)。
在读写普通文件时,会经过文件系统,由文件系统负责与磁盘交互;而读写磁盘或者分区时,就会跳过文件系统,也就是所谓的“裸I/O“。这两种读写方式所使用的缓存是不同的,也就是文中所讲的 Cache 和 Buffer 区别。
7. top
# top
top - 17:45:55 up 165 days, 2:11, 1 user, load average: 0.16, 0.18, 0.16
Tasks: 130 total, 1 running, 129 sleeping, 0 stopped, 0 zombie
%Cpu0 : 2.7 us, 2.0 sy, 0.0 ni, 94.7 id, 0.7 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 4.0 us, 1.7 sy, 0.0 ni, 93.6 id, 0.7 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8010456 total, 365480 free, 3476532 used, 4168444 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 4154732 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
32688 root 20 0 746276 31548 7964 S 1.0 0.4 2847:59 containerd
3984 mysql 20 0 3656524 466328 6476 S 0.3 5.7 440:07.82 mysqld
7182 nobody 20 0 81524 9136 2340 S 0.3 0.1 0:20.79 nginx
23715 influxdb 20 0 1509916 93420 7364 S 0.3 1.1 1905:45 influxd
1 root 20 0 193820 5276 3076 S 0.0 0.1 359:19.74 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:17.44 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 31:44.71 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:35.39 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
-
us(user)
:代表用户态 CPU 时间,不包括 nice 时间。(Time running un-niced user processes.) -
sy(system)
:代表内核态 CPU 时间。(Time running kernel processes.) -
ni(nice)
:代表低优先级用户态 CPU 时间,也就是进程的 nice 值被调整为 1-19 之间时的 CPU 时间。这里注意,nice 可取值范围是 -20 到 19,数值越大,优先级反而越低。(Time running niced user processes.) -
id(idle)
:代表 CPU 空闲时间。(Time spent in the kernel idle handler.) -
wa(iowait)
:代表等待 I/O 的 CPU 时间。(Time waiting for I/O completion) -
hi
:代表处理硬中断的 CPU 时间。(Time spent servicing hardware interrupts.)
si
:代表处理软中断的 CPU 时间。(Time spent servicing software interrupts.) -
st
:代表当系统运行在虚拟机中的时候,被其他虚拟机占用的 CPU 时间。(Time stolen from this vm by the hypervisor.) -
PR
:代表进程的优先级。(Priority - The scheduling priority of the task. If you see `rt' in this field, it means the task is running under real time scheduling priority.) -
NI
:代表进程的 nice 值。值越小,进程优先级越高。值越大,进程优先级越低。其实就是控制进程占用 CPU 时间片的长短。(Nice Value - The nice value of the task. A negative nice value means higher priority, whereas a positive nice value means lower priority. Zero in this field simply means priority will not be adjusted in determining a task's dispatch-ability.) -
VIRT
:进程虚拟内存的大小,只要是进程申请过的内存,即便还没有真正分配物理内存,也会计算在内。(Virtual Memory Size (KiB) - The total amount of virtual memory used by the task. It includes all code, data and shared libraries plus pages that have been swapped out and pages that have been mapped but not used.) -
RES
:常驻内存的大小,也就是进程实际使用的物理内存大小,但不包括 Swap 内存。(Resident Memory Size (KiB) - The non-swapped physical memory a task is using.) -
SHR
:共享内存的大小,比如与其他进程共同使用的共享内存、加载的动态链接库以及程序的代码段等。(Shared Memory Size (KiB) - The amount of shared memory available to a task, not all of which is typically resident. It simply reflects memory that could be potentially shared with other processes.) -
%CPU
:进程的 CPU 使用率。( CPU Usage - The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.) -
%MEM
:进程使用物理内存占系统总内存的百分比。(Memory Usage (RES) - A task's currently used share of available physical memory.) -
TIME+
:进程启动后占用的 CPU 时间,格式为 minutes:seconds.hundredths 。(CPU Time, hundredths -Total CPU time the task has used since it started, but reflecting more granularity through hundredths of a second.)
8. iostat
# iostat -d -x 1
Linux 3.10.0-693.2.2.el7.x86_64 (cnszsc01053) 12/02/2020 _x86_64_ (2 CPU)
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
vda 0.00 0.33 0.01 0.50 0.24 3.60 15.32 0.00 3.57 4.07 3.56 0.76 0.04
vdb 0.00 0.01 0.00 0.15 0.02 32.97 432.26 0.01 61.38 3.15 61.44 1.22 0.02
vdc 0.00 0.01 0.00 0.15 0.77 46.82 612.77 0.02 125.15 16.21 127.39 2.29 0.04
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
vda 0.00 37.00 1.00 2.00 8.00 156.00 109.33 0.00 1.00 1.00 1.00 1.00 0.30
vdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
vdc 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
-
rrqm/s
:每秒合并的读请求数(记录的是合并了多少读请求,并不是对磁盘的实际操作)。(The number of read requests merged per second that were queued to the device.) -
wrqm/s
:每秒合并的写请求数(记录的是合并了多少写请求,并不是对磁盘的实际操作)。( The number of write requests merged per second that were queued to the device.) -
r/s
:每秒对磁盘的合并后的读请求数(记录的是对磁盘的实际读操作次数)。(The number (after merges) of read requests completed per second for the device.) -
w/s
:每秒对磁盘的合并后的写请求数(记录的是对磁盘的实际写操作次数)。(The number (after merges) of write requests completed per second for the device.) -
rkB/s
:每秒从磁盘读取的数据量。(The number of sectors (kilobytes, megabytes) read from the device per second.) -
wkB/s
:每秒向磁盘写入的数据量。(The number of sectors (kilobytes, megabytes) written to the device per second.) -
avgrq-sz
:平均每次设备 I/O 操作的数据大小 (扇区)。(The average size (in sectors) of the requests that were issued to the device.) -
avgqu-sz
:平均请求队列长度。(值越大,说明磁盘处理不过来?)(The average queue length of the requests that were issued to the device.) -
await
:平均每次读写请求处理完成等待时间,单位为毫秒。(The average time (in milliseconds) for I/O requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.) -
r_await
:平均每次读请求处理完成等待时间,单位为毫秒。(The average time (in milliseconds) for read requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.) -
w_await
:平均每次写请求处理完成完成等待时间,单位为毫秒。(The average time (in milliseconds) for write requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.) -
svctm
:处理 I/O 请求所需的平均时间(不包括等待时间),单位为毫秒。(The average service time (in milliseconds) for I/O requests that were issued to the device. Warning! Do not trust this field any more. This field will be removed in a future sysstat version.) -
%util
:磁盘处理 I/O的时间百分比。(Percentage of elapsed time during which I/O requests were issued to the device (bandwidth utilization for the device). Device saturation occurs when this value is close to 100%.)
9. sar
// -n 后面还可以接 DEV, EDEV, NFS, NFSD, SOCK, IP, EIP, ICMP, EICMP, TCP, ETCP, UDP, SOCK6, IP6, EIP6, ICMP6, EICMP6 and UDP6 等选项
# sar -n DEV 1
Linux 3.10.0-957.el7.x86_64 (cnszkf02001) 12/08/2020 _x86_64_ (4 CPU)
11:05:08 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
11:05:09 AM ens192 42.00 27.00 33.58 32.69 0.00 0.00 0.00
11:05:09 AM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
11:05:09 AM docker0 0.00 0.00 0.00 0.00 0.00 0.00 0.00
-
rxpck/s
:是每秒接收的数据包总数。(Total number of packets received per second.) -
txpck/s
: 是每秒发送的数据包总数。(Total number of packets transmitted per second.) -
rxkB/s
: 是接收的吞吐量,单位是KB/秒。(Total number of kilobytes received per second.) -
txkB/s
: 是发送的吞吐量,单位是KB/秒。(Total number of kilobytes transmitted per second.) -
rxcmp/s
: 是每秒收到的压缩包数。(Number of compressed packets received per second (for cslip etc.).) -
txcmp/s
: 每秒传输的压缩数据包数。(Number of compressed packets transmitted per second.) -
rxmcst/s
:每秒接收的多播数据包数。(Number of multicast packets received per second.)
10. route
# route -een
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface MSS Window irtt
0.0.0.0 10.0.2.1 0.0.0.0 UG 100 0 0 eth0 0 0 0
10.0.2.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0 0 0 0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker 0 0 0
172.20.37.192 10.0.2.156 255.255.255.192 UG 0 0 0 tunl0 0 0 0
172.20.133.128 10.0.2.154 255.255.255.192 UG 0 0 0 tunl0 0 0 0
172.20.171.0 0.0.0.0 255.255.255.192 U 0 0 0 * 0 0 0
172.20.171.1 0.0.0.0 255.255.255.255 UH 0 0 0 cali60 0 0 0
172.20.171.2 0.0.0.0 255.255.255.255 UH 0 0 0 cali37 0 0 0
172.20.184.64 10.0.2.152 255.255.255.192 UG 0 0 0 tunl0 0 0 0
172.20.222.0 10.0.2.153 255.255.255.192 UG 0 0 0 tunl0 0 0 0
-
Destination
:表示 目标网络地址 或者 目标主机地址。可以是一个网络标识符,一个 IP 地址,广播地址 或 多播地址等。也就是,当前主机去往的目的地。Destination 字段为 default 或者 0.0.0.0 时,表示默认路由。(The destination network or destination host.) -
Gateway
:表示网关地址(如果没有网关,用 * 或者 0.0.0.0 表示,即这是同网段的通信,不需要经过网关)。如果目标计算机的 IP 地址与子网掩码(Genmask)做与(AND)运算后结果等于 目标网络地址(Destination)的话,就会将数据包发送到 网关(Gateway)所示的 IP 地址。(The gateway address or '*' if none set.) -
Genmask
:表示目标网络的子网掩码(目标网络地址(Destination)为 主机 IP 的子网掩码为 255.255.255.255,目标网络地址(Destination)为默认路由的子网掩码为为 0.0.0.0)(The netmask for the destination net; '255.255.255.255' for a host destination and '0.0.0.0' for the default route.) -
Flags
:
U:表示路由是活动的(只有 U,没有 G,表示网络通信没有经过网关,在同网段内通信)。(route is up)
UH:表示目标是一台主机,即 Destination 的 IP 是一个主机 IP,不是一个网段。(target is a host)
UG:表示默认路由网关。(use gateway)
其中
Destination Gateway Genmask Flags Metric Ref Use Iface MSS Window irtt
0.0.0.0 10.0.2.1 0.0.0.0 UG 100 0 0 eth0 0 0 0
这是一条 默认路径 。表示当该主机发送数据包时,如果在路由表内找不到其他可以用来发送此数据包的路径时,该数据包会通过这条 默认路径 来发送,也就是说从 接口eth0 发出,然后送给网关 10.0.2.1 。
Destination Gateway Genmask Flags Metric Ref Use Iface MSS Window irtt
172.20.133.128 10.0.2.154 255.255.255.192 UG 0 0 0 tunl0 0 0 0
如果 要访问目标计算机的 IP 地址
与 子网掩码(Genmask)255.255.255.192
做与(AND)运算
后结果等于 目标网络地址(Destination)172.20.133.128
的话,就会将数据包从接口 tunl0 发送到网关(Gateway)10.0.2.154,由 网关(Gateway)10.0.2.154 进行转发处理。
参考:
网友评论