运维日常1

一、使用w查看系统负载

w命令用于显示目前登入系统的用户信息。执行这项指令可得知目前登入系统的用户有哪些人,以及他们正在执行的程序。单独执行 w 指令会显示所有的用户,您也可指定用户名称,仅显示某位用户的相关信息

1、w命令格式:w [选项] [用户名称]

选项
-f  开启或关闭显示用户从何处登入系统
-h  不显示各栏位的标题信息列
-l  使用详细格式列表,此为预设值
-s  使用简洁格式列表,不显示用户登入时间,终端机阶段作业和程序所耗费的CPU时间
-u  忽略执行程序的名称,以及该程序耗费CPU时间的信息
-V  显示版本信息

具体操作

[root@localhost ~]# w (查看系统负载)
 14:03:33 up  3:01,  2 users,  load average: 0.00, 0.01, 0.05  (系统时间和登陆时间,登录用户的个数,后边的内容为最重要的,表示1分钟,5分钟,15分钟内的使用cpu活动进程有多少个)
USER TTY    FROM         LOGIN@   IDLE    JCPU   PCPU  WHAT
root tty1                11:0     33:00m  0.03s  0.03s -bash
root pts/0  192.168.1.1  11:0     35.00s  0.12s  0.03s w
[root@localhost ~]# date (可以使用此命令查看时间)
2018年 06月 03日 星期日 14:04:16 CST
[root@localhost ~]# cat /proc/cpuinfo  (查看系统CPU个数,看第一行数字,0表示1颗cpu,一次类推,表示逻辑CPU)
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 58
......(中间省略)
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management: 
[root@localhost ~]# uptime (uptime与w命令效果一样)
 14:21:35 up  3:19,  2 users,  load average: 0.00, 0.01, 0.05

字段说明:

User:  登录用户名    
TTY:   登录后系统分配的终端号   
From:  远程主机名,即从哪登录的   
login@:何时登录   
IDLE:  用户空闲时间。这是个计时器,一旦用户执行任何操作,改计时器就会被重置   
JCPU:  和终端连接的所有进程占用时间。包括当前正在运行的后台作业占用时间   
PCPU:  当前进程所占用时间   
WHAT:  当前正在运行进程的命令行

二、vmstat命令

vmstat命令的含义为显示虚拟内存状态(“Viryual Memor Statics”),但是它可以报告关于进程、内存、I/O等系统整体运行状态

1、vmstat命令格式:vmstat [选项] [参数]

选项
-a:显示活动内页
-f:显示启动后创建的进程总数
-m:显示slab信息
-n:头信息仅显示一次
-s:以表格方式显示事件计数器和内存状态
-d:报告磁盘状态
-p:显示指定的硬盘分区状态
-S:输出信息的单位

具体操作(1)

[root@localhost ~]# vmstat  (显示系统进程)
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   sobibo   in   cs us sy id wa st
 0  0  0 742892   2076 14373600 8 1   30   27  0  0 100  0  0
[root@localhost ~]# vmstat 1  (加1表示每一秒显示一次,按Ctrl+c结束)
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   sobibo   in   cs us sy id wa st
 1  0  0 742736   2076 14376800 8 1   30   27  0  0 100  0  0
 0  0  0 742736   2076 14376800 016   66   56  0  0 100  0  0
 0  0  0 742736   2076 14376800 0 0   58   47  0  0 100  0  0
 0  0  0 742736   2076 14376800 0 0   72   54  0  0 100  0  0
 0  0  0 742736   2076 14376800 0 0   46   37  0  0 100  0  0
 0  0  0 742736   2076 14376800 0 0   63   52  0  0 100  0  0
 0  0  0 742736   2076 14376800 0 0   62   45  0  1 100  0  0
 0  0  0 742736   2076 14376800 0 0   50   46  0  0 100  0  0
 0  0  0 742736   2076 14376800 0 0   53   42  0  0 100  0  0
^C
[root@localhost ~]# vmstat 1 5  (表示每一秒显示一次,只显示五次)
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   sobibo   in   cs us sy id wa st
 1  0  0 743108   2076 14376800 8 1   30   27  0  0 100  0  0
 0  0  0 743108   2076 14376800 0 0   65   49  0  0 100  0  0
 0  0  0 743108   2076 14376800 0 0   46   40  0  0 100  0  0
 0  0  0 743108   2076 14376800 0 0   53   43  0  0 100  0  0
 0  0  0 743108   2076 14376800 0 0   42   38  0  0 100  0  0

字段说明:

1)Procs(进程)

r: 运行队列中进程数量,这个值也可以判断是否需要增加CPU。(长期大于1)
b: 等待IO的进程数量

2)Memory(内存)

swpd: 使用虚拟内存大小,如果swpd的值不为0,但是SI,SO的值长期为0,这种情况不会影响系统性能
free: 空闲物理内存大小
buff: 用作缓冲的内存大小
cache: 用作缓存的内存大小,如果cache的值大的时候,说明cache处的文件数多,如果频繁访问到的文件都能被cache处,那么磁盘的读IO bi会非常小

3)Swap

si: 每秒从交换区写到内存的大小,由磁盘调入内存
so: 每秒写入交换区的内存大小,由内存调入磁盘

注意:内存够用的时候,这2个值都是0,如果这2个值长期大于0时,系统性能会受到影响,磁盘IO和CPU资源都会被消耗。有些朋友看到空闲内存(free)很少的或接近于0时,就认为内存不够用了,不能光看这一点,还要结合si和so,如果free很少,但是si和so也很少(大多时候是0),那么不用担心,系统性能这时不会受到影响的。

4)IO

bi: 每秒读取的块数
bo: 每秒写入的块数

注意:随机磁盘读写的时候,这2个值越大(如超出1024k),能看到CPU在IO等待的值也会越大。

5)system(系统)

in: 每秒中断数,包括时钟中断 cs: 每秒上下文切换数

注意:上面2个值越大,会看到由内核消耗的CPU时间会越大。

6)CPU(以百分比表示)

us: 用户进程执行时间百分比(user time)
sy: 内核系统进程执行时间百分比(system time)
wa: IO等待时间百分比
id: 空闲时间百分比

wa的值高时,说明IO等待比较严重,这可能由于磁盘大量作随机访问造成,也有可能磁盘出现瓶颈(块操作)。
sy的值高时,说明系统内核消耗的CPU资源多,这并不是良性表现,我们应该检查原因。
us的值比较高时,说明用户进程消耗的CPU时间多,但是如果长期超50%的使用,那么我们就该考虑优化程序算法或者进行加速。

三、top命令

top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息监测系统性能和运行信息的实用工具。通过top命令所提供的互动式界面,用热键可以管理

1、top命令格式:top [选项]

选项
-b:以批处理模式操作
-c:显示完整的治命令
-d:屏幕刷新间隔时间
-I:忽略失效过程
-s:保密模式
-S:累积模式
-i:设置间隔时间
-u:指定用户名
-p:指定进程
-n:循环显示的次数

具体操作(1)

[root@localhost ~]# top  (查看系统进程)
top - 14:48:42 up  3:46,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  93 total,   1 running,  92 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1008176 total,   742008 free,   119936 used,   146232 buff/cache
KiB Swap:  2047996 total,  2047996 free,0 used.   724776 avail Mem

14:48:42(当前系统时间);3:46(系统已经运行了16天);2 users(个用户当前登录);load average: 0.00, 0.01, 0.05(系统负载,即任务队列的平均长度)
Tasks: 93 total(总进程数);1 running(正在运行的进程数);92 sleeping(睡眠的进程数);0 stopped(停止的进程数);0 zombie(冻结进程数)
Cpu(s): 0.0%us(用户空间占用CPU百分比);0.2%sy(内核空间占用CPU百分比);0.0%ni(用户进程空间内改变过优先级的进程占用CPU百分比);99.8%id(空闲CPU百分比);0.0%wa(等待输入输出的CPU时间百分比)
Mem: 1008176k total(物理内存总量);742008k used(使用的物理内存总量);119936k free(空闲内存总量);146232k buffers(用作内核缓存的内存量)
Swap:  2047996K total(交换区总量);0 used(使用的交换区总量);2047996k free(空闲交换区总量);724776k cached(缓冲的交换区总量) 

  PID USER  PR  NIVIRTRESSHR S  %CPU %MEM TIME+ COMMAND
  544 root  20   0  305408   6380   4924 S   0.3  0.6   0:21.80 vmtoolsd   
 1268 root  20   0  157624   2160   1536 R   0.3  0.2   0:00.02 top
1 root  20   0  128208   6844   4080 S   0.0  0.7   0:01.57 systemd
2 root  20   0   0  0  0 S   0.0  0.0   0:00.02 kthreadd   
3 root  20   0   0  0  0 S   0.0  0.0   0:00.04 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:00.02 migration/0
8 root  20   0   0  0  0 S   0.0  0.0   0:00.00 rcu_bh 
9 root  20   0   0  0  0 S   0.0  0.0   0:02.35 rcu_sched  
   10 root  rt   0   0  0  0 S   0.0  0.0   0:00.10 watchdog/0 

按M会根据内存大小排序
  PID USER  PR  NIVIRTRESSHR S  %CPU %MEM TIME+ COMMAND
  577 root  20   0  334276  29016   6896 S   0.0  2.9   0:02.79 firewalld  
  900 root  20   0  562432  16596   5916 S   0.0  1.6   0:02.66 tuned  
  538 polkitd   20   0  534936  11824   4868 S   0.0  1.2   0:00.11 polkitd
  592 root  20   0  548048   9236   6904 S   0.0  0.9   0:01.02 NetworkManager 
1 root  20   0  128208   6844   4080 S   0.0  0.7   0:01.57 systemd
  544 root  20   0  305408   6380   4924 S   0.0  0.6   0:21.83 vmtoolsd
按1会列出所有CPU的使用情况
Tasks:  93 total,   1 running,  92 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1008176 total,   742016 free,   119936 used,   146224 buff/cache
[root@localhost ~]# top -c  (可以查看命令的全局状态)
top - 14:57:07 up  3:54,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  92 total,   1 running,  91 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1008176 total,   742024 free,   119920 used,   146232 buff/cache
KiB Swap:  2047996 total,  2047996 free,0 used.   724792 avail Mem 

  PID USER  PR  NIVIRTRESSHR S  %CPU %MEM TIME+ COMMAND
  542 root  20   0   21664   1284972 S   0.3  0.1   0:05.52 /usr/sbin/irqbalance --foreground  
1 root  20   0  128208   6844   4080 S   0.0  0.7   0:01.57 /usr/lib/systemd/systemd --switched-root --system --deserialize 21 
2 root  20   0   0  0  0 S   0.0  0.0   0:00.03 [kthreadd] 
3 root  20   0   0  0  0 S   0.0  0.0   0:00.05 [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:00.02 [migration/0]  
8 root  20   0   0  0  0 S   0.0  0.0   0:00.00 [rcu_bh]   
9 root  20   0   0  0  0 S   0.0  0.0   0:02.99 [rcu_sched]
   10 root  rt   0   0  0  0 S   0.0  0.0   0:00.10 [watchdog/0]   
   11 root  rt   0   0  0  0 S   0.0  0.0   0:00.10 [watchdog/1]
[root@localhost ~]# top -bn1  (静态显示,一次性显示全部,可用于shell脚本)
top - 14:58:20 up  3:56,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  92 total,   1 running,  91 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1008176 total,   742404 free,   119544 used,   146228 buff/cache
KiB Swap:  2047996 total,  2047996 free,0 used.   725172 avail Mem 

  PID USER  PR  NIVIRTRESSHR S  %CPU %MEM TIME+ COMMAND
 1273 root  20   0  157624   2048   1504 R   6.7  0.2   0:00.01 top
1 root  20   0  128208   6844   4080 S   0.0  0.7   0:01.57 systemd
2 root  20   0   0  0  0 S   0.0  0.0   0:00.03 kthreadd
3 root  20   0   0  0  0 S   0.0  0.0   0:00.05 ksoftirqd/0
......(中间省略)
 1269 root  20   0   0  0  0 S   0.0  0.0   0:00.01 kworker/0:1
 1272 root  20   0   0  0  0 S   0.0  0.0   0:00.00 kworker/0:2

四、sar命令

sar命令是Linux下系统运行状态统计工具,它将指定的操作系统状态计数器显示到标准输出设备,被戏称为Linux的“瑞士×××”,可见其有强大的功能

1、sar命令格式:sar [选项] [参数]

选项
-A:显示所有的报告信息
-b:显示I/O速率
-B:显示换页状态
-c:显示进程创建活动
-d:显示每个块设备的状态
-e:设置显示报告的结束时间
-f:从指定文件提取报告
-i:设状态信息刷新的间隔时间
-P:报告每个CPU的状态
-R:显示内存状态
-u:显示CPU利用率
-v:显示索引节点,文件和其他内核表的状态
-w:显示交换分区状态
-x:显示给定进程的状态

具体操作(1)

[root@localhost ~]# sar
-bash: sar: 未找到命令
[root@localhost ~]# yum install -y sysstat  (首先需要安装sar命令)
已加载插件:fastestmirror
base 
......(中间省略)
[root@localhost ~]# sar (sar不加任何选项,会默认调用历史系统软件)
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月03日     _x86_64_    (2 CPU)
[root@localhost ~]# sar -n DEV 1 5  (查看系统流量使用,每一秒显示一次,显示5次)
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月03日     _x86_64_    (2 CPU)

15时22分06秒 IFACE   rxpck/s   txpck/srxkB/stxkB/s   rxcmp/s   txcmp/s  rxmcst/s
15时22分07秒lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
15时22分07秒 ens33  0.00  0.00  0.00  0.00  0.00  0.00  0.00

15时22分07秒 IFACE   rxpck/s   txpck/srxkB/stxkB/s   rxcmp/s   txcmp/s  rxmcst/s
15时22分08秒lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
15时22分08秒 ens33  1.00  1.00  0.06  0.38  0.00  0.00  0.00

15时22分08秒 IFACE   rxpck/s   txpck/srxkB/stxkB/s   rxcmp/s   txcmp/s  rxmcst/s
15时22分09秒lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
15时22分09秒 ens33  1.00  1.00  0.06  0.38  0.00  0.00  0.00

15时22分09秒 IFACE   rxpck/s   txpck/srxkB/stxkB/s   rxcmp/s   txcmp/s  rxmcst/s
15时22分10秒lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
15时22分10秒 ens33  1.00  1.00  0.06  0.38  0.00  0.00  0.00

15时22分10秒 IFACE   rxpck/s   txpck/srxkB/stxkB/s   rxcmp/s   txcmp/s  rxmcst/s
15时22分11秒lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
15时22分11秒 ens33  1.00  1.00  0.06  0.38  0.00  0.00  0.00

平均时间: IFACE   rxpck/s   txpck/srxkB/stxkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均时间:lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
平均时间: ens33  0.80  0.80  0.05  0.31  0.00  0.00  0.00

字段介绍
第一列:系统时间
第二列:网卡名称
第三列:接收的数据包数量 (几千为一个合适值)
第四列:发送的数据包数量
第五列:接收的数据量
第六列:发送的数据量
后边的不会对系统运行有很大影响
[root@localhost ~]# sar -q  (查看系统负载)
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月03日     _x86_64_    (2 CPU)

15时10分02秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
15时20分01秒 0   111  0.00  0.01  0.05 0
平均时间: 0   111  0.00  0.01  0.05 0
[root@localhost ~]# sar -q -f /var/log/sa/sa03 (查看系统负载以及系统历史文件)
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月03日     _x86_64_    (2 CPU)

15时10分02秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
15时20分01秒 0   111  0.00  0.01  0.05 0
平均时间: 0   111  0.00  0.01  0.05 0
[root@localhost ~]# sar -n DEV  -f /var/log/sa/sa03
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月03日     _x86_64_    (2 CPU)

15时10分02秒 IFACE   rxpck/s   txpck/srxkB/stxkB/s   rxcmp/s   txcmp/s  rxmcst/s
15时20分01秒lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
15时20分01秒 ens33  0.06  0.06  0.00  0.00  0.00  0.00  0.00
平均时间:lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
平均时间: ens33  0.06  0.06  0.00  0.00  0.00  0.00  0.00
[root@localhost ~]# sar -b  (产看磁盘负载)
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月03日     _x86_64_    (2 CPU)

15时10分02秒   tps  rtps  wtps   bread/s   bwrtn/s
15时20分01秒  0.04  0.00  0.04  0.00  0.56
平均时间:  0.04  0.00  0.04  0.00  0.56
[root@localhost ~]# ls /var/log/sa
sa03  (sa03不是一个二进制文件,不可以用cat查看)
[root@localhost ~]# ls /var/log/sa/sar18 (还会有sar18文件,会在明年生成)

五、nload命令

nload命令用来即时监看网路状态和各ip所使用的频宽

1、nload命令格式:nload [选项]

nload默认的是eth0网卡,如果你想监测eth1网卡的流量,需要输入:nload eth1

选项
-a:这个好像是全部数据的刷新时间周期,单位是秒,默认是300
-i:进入网卡的流量图的显示比例最大值设置,默认10240 kBit/s
-m:不显示流量图,只显示统计数据
-o:出去网卡的流量图的显示比例最大值设置,默认10240 kBit/s
-t:显示数据的刷新时间间隔,单位是毫秒,默认500
-u:设置右边Curr、Avg、Min、Max的数据单位,默认是自动变的.注意大小写单位不同
-U:设置右边Ttl的数据单位,默认是自动变的.注意大小写单位不同(与-u相同)

具体操作

[root@localhost ~]# nload (可以查看,第一行显示网卡名称,括号内显示说明有两个网卡,可以按左右的方向键控制查看)
Device ens33 [192.168.1.131] (1/2):
============================================================================================================================================================================================
Incoming:


   Curr: 944.00 Bit/s
   Avg: 968.00 Bit/s
   Min: 880.00 Bit/s
   Max: 1.84 kBit/s
   Ttl: 7.50 MByte
Outgoing:


   Curr: 9.54 kBit/s
   Avg: 9.52 kBit/s
   Min: 3.75 kBit/s
   Max: 10.23 kBit/s
   Ttl: 2.12 MByte

六、监控io性能

iosatat和iotop命令:监控磁盘

1、iostat -x使用方法

使用iostat命令,首先要进行安装,安装sar命令的时候的安装包也是iostat命令的安装包
[root@localhost ~]# iostat -x
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain)     2018年06月03日     _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
   0.040.000.090.050.00   99.81

Device: rrqm/s   wrqm/s r/s w/srkB/swkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda   0.00  0.02  0.32  0.21 10.93 5.65   61.56   0.01  17.9  57.54   33.78   3.04   0.16
sdb   0.00  0.00  0.00  0.00 0.12  0.00   47.27   0.00  0.17  0.17  0.00   0.15   0.00
scd0  0.00  0.00  0.00  0.00 0.06  0.00   114.22  0.00  0.56  0.56  0.00   0.33   0.00
主要关注:%util列,这一列表示磁盘有多少时间使用CPU的百分比,如果数字很大磁盘很忙

2、iotop使用方法

[root@localhost ~]# iotop 
-bash: iotop: 未找到命令
[root@localhost ~]# yum install -y iotop  (首先要安装该命令)
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.sohu.com
 * epel: mirrors.ustc.edu.cn
......(中间省略)
已安装:
  iotop.noarch 0:0.6-2.el7  

完毕!
[root@localhost ~]# iotop  (会动态显示磁盘使用情况,io会按照大小排序)
Total DISK READ :   0.00 B/s | Total DISK WRITE :   0.00 B/s
Actual DISK READ:   0.00 B/s | Actual DISK WRITE:   0.00 B/s
  TID  PRIO  USER DISK READ  DISK WRITE  SWAPIN IO>COMMAND  
 1024 be/4 root0.00 B/s0.00 B/s  0.00 %  0.00 % -bash
1 be/4 root0.00 B/s0.00 B/s  0.00 %  0.00 % systemd --switched-root --system --deserialize 21
2 be/4 root0.00 B/s0.00 B/s  0.00 %  0.00 % [kthreadd]

七、free命令

free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区

1、free命令格式:free [选项]

选项
-b  以Byte为单位显示内存使用情况
-k  以KB为单位显示内存使用情况
-m  以MB为单位显示内存使用情况
-o  不显示缓冲区调节列
-s  持续观察内存使用状况
-t  显示内存总和列
-V  显示版本信息

2、具体操作

[root@localhost ~]# free  (查看内存情况)
        total    used    free     shared  buff/cache   available
Mem:    1008176  121420  562760   6892    323996       702944
Swap:   2047996  0       2047996
[root@localhost ~]# free -m  (以MB为单位显示内存使用情况)
        total    used    free     shared  buff/cache   available
Mem:    984      118     549       6      316          686
Swap:   1999     0       1999
[root@localhost ~]# free -h  (自动计算以合适的单位显示内存大小)
        total    used    free     shared  buff/cache   available
Mem:    984M     118M    549M     6.7M    316M         686M
Swap:   2.0G     0B      2.0G

字段解释

total:         内存总数
used:          已经使用的内存数
free:          空闲的内存数
shared:        当前已经废弃不用
buffers Buffer:缓存内存数
cached Page:   缓存内存数

知识点

1)数据(磁盘)-->内存(cache)-->cpu (CPU从磁盘中读取数据,会很浪费时间,所以会首先将磁盘中的数据放入到内存中,此内存为:cache称之为:缓存)

2)cpu-->内存(buffer)-->磁盘 (将CPU已经运行完成的数据存放磁盘,也会同样经过内存在进入磁盘,此内存为:buffer,称之为:缓冲)

3)

total=used+free+buff/cache
available包含free和buff/cache剩余的空间

八、ps命令

ps命令用于报告当前系统的进程状态。可以搭配kill指令随时中断、删除不必要的程序

1、命令格式:ps [选项]

选项
-a :显示所有的容器,包括未运行的
-f :根据条件过滤显示的内容
-l :显示最近创建的容器
-n :列出最近创建的n个容器
-q :静默模式,只显示容器编号
-s :显示总的文件大小
--format :   指定返回值的模板文件
--no-trunc : 不截断输出

具体操作

[root@localhost ~]# ps aux (可以查看所有进程)
USER   PID %CPU %MEMVSZ   RSS TTY  STAT START   TIME COMMAND
root 1  0.0  0.6 128208  6856 ?Ss   11:02   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
......(中间省略)
root  1557  4.0  0.1 151104  1844 pts/0R+   17:53   0:00 ps aux
[root@localhost ~]# ps aux |grep nginx  (查看某个进程是否正在运行)
root  1559  0.0  0.0 112720   980 pts/0S+   17:53   0:00 grep --color=auto nginx
[root@localhost ~]# ps -elf  (与ps aux的效果一样,都可以查看全部进程)
F S UIDPID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY  TIME CMD
4 S root 1 0  0  80   0 - 32052 ep_pol 11:02 ?00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
......(中间省略)
1 R root  1556 2  0  80   0 - 0 -  17:50 ?00:00:00 [kworker/1:2]
0 R root  1561  1045  0  80   0 - 37776 -  17:53 pts/000:00:00 ps -elf

ps aux命令下的字段介绍

USER:该进程属于哪个使用者账号的  
PID :该进程的号码  (此号码可以用于杀死一个进程:kill 进程PID或者查看一个非法进程的目录:ls -l /proc/进程PID)
%CPU:该进程使用掉的 CPU 资源百分比  
%MEM:该进程所占用的物理内存百分比  
VSZ :该进程使用掉的虚拟内存量 (Kbytes)  
RSS :该进程占用的固定的内存量 (Kbytes)  
TTY :该进程是在哪个终端机上面运作,若与终端机无关,则显示 ?,另外, tty1-tty6 是本机上面的登入者程序,若为 pts/0 的,则表示为由网络连接进主机的程序。  
STAT:该程序目前的状态,主要的状态有
START:该进程被触发启动的时间  
TIME :该进程实际使用 CPU 运作的时间  
COMMAND:该程序的实际指令

知识点:1、STAT的主要的状态有

1)D :不能中断的进程,该进程会影响cpu负载

2)R :该程序目前正在运作,或者是可被运作

3)T :该程序目前正在侦测或者是停止了

[root@localhost ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   sobibo   in   cs us sy id wa st
 2  0  0 562792   2076 32206400 4 2   27   23  0  0 100  0  0
 0  0  0 562668   2076 32206400 0 0   54   44  0  0 100  0  0
 0  0  0 562652   2076 32206400 0 0   51   42  0  1 100  0  0
^Z
[1]+  已停止   vmstat 1 
[root@localhost ~]# ps aux |grep vmstat  (此种进程不会耗费过多的CPU资源)
root  1585  0.2  0.1 148356  1356 pts/0T18:05   0:00 vmstat 1
root  1587  0.0  0.0 112720   984 pts/0S+   18:05   0:00 grep --color=auto vmstat

4)S :sleep状态的进程

5)Z :僵尸进程 (僵尸进程过多会增加过多的负载,应该杀死)

6)< :高优先级进程

7)N :低优先级进程

8)L :内存中被锁的内存分页

9)l :多线程进程

10)s :主进程

11)+ :前台进程

2、-C和--no-heading的用法

[root@ma-1 ~]# ps -C sshd  (-C后跟进程名称)
  PID TTY  TIME CMD
  904 ?00:00:00 sshd
 1047 ?00:00:00 sshd
[root@ma-1 ~]# ps --no-heading -C  sshd (--no-heading可以去除表头信息)
  904 ?00:00:00 sshd
 1047 ?00:00:00 sshd
[root@ma-1 ~]# ps --no-heading -C  sshd | wc -l (在shell脚本中可以使用)
2

九、查看网络状态

netstat命令是用来查看网络状态的

1、命令格式:netstat [选项]

选项
-a或--all:显示所有连线中的Socket
-A<网络类型>或--<网络类型>:列出该网络类型连线中的相关地址
-c或--continuous:持续列出网络状态
-C或--cache:显示路由器配置的快取信息
-e或--extend:显示网络其他相关信息
-F或--fib:显示FIB
-g或--groups:显示多重广播功能群组组员名单
-h或--help:在线帮助
-i或--interfaces:显示网络界面信息表单
-l或--listening:显示监控中的服务器的Socket
-M或--masquerade:显示伪装的网络连线
-n或--numeric:直接使用ip地址,而不通过域名服务器
-N或--netlink或--symbolic:显示网络硬件外围设备的符号连接名称
-o或--timers:显示计时器
-p或--programs:显示正在使用Socket的程序识别码和程序名称
-r或--route:显示Routing Table
-s或--statistice:显示网络工作信息统计表
-t或--tcp:显示TCP传输协议的连线状况
-u或--udp:显示UDP传输协议的连线状况
-v或--verbose:显示指令执行过程
-V或--version:显示版本信息
-w或--raw:显示RAW传输协议的连线状况
-x或--unix:此参数的效果和指定"-A unix"参数相同
--ip或--inet:此参数的效果和指定"-A inet"参数相同

具体操作

[root@localhost ~]# netstat -lnp  (查看监听端口)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address   Foreign Address State   PID/Program name
tcp0  0 0.0.0.0:22  0.0.0.0:*   LISTEN  901/sshd
tcp0  0 127.0.0.1:250.0.0.0:*   LISTEN  1008/master 
tcp6   0  0 :::22   :::*LISTEN  901/sshd
tcp6   0  0 ::1:25  :::*LISTEN  1008/master 
udp0  0 127.0.0.1:323   0.0.0.0:*   552/chronyd 
udp6   0  0 ::1:323 :::*552/chronyd 
raw6   0  0 :::58   :::*7   592/NetworkManager  
......(中间省略)
unix  2  [ ACC ] STREAM LISTENING 171051008/master  public/pickup
unix  2  [ ACC ] STREAM LISTENING 171091008/master  public/cleanup
[root@localhost ~]# netstat -an   (查看系统的网络连接状况)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address   Foreign Address State  
tcp0  0 0.0.0.0:22  0.0.0.0:*   LISTEN 
tcp0  0 127.0.0.1:250.0.0.0:*   LISTEN 
......(中间省略)
unix  3  [ ] DGRAM12251
unix  3  [ ] STREAM CONNECTED 17132
[root@localhost ~]# netstat -ltunp  (只查看tcp和udp的监听端口)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address   Foreign Address State   PID/Program name
tcp0  0 0.0.0.0:22  0.0.0.0:*   LISTEN  901/sshd
tcp0  0 127.0.0.1:250.0.0.0:*   LISTEN  1008/master 
tcp6   0  0 :::22   :::*LISTEN  901/sshd
tcp6   0  0 ::1:25  :::*LISTEN  1008/master 
udp0  0 127.0.0.1:323   0.0.0.0:*   552/chronyd 
udp6   0  0 ::1:323 :::*552/chronyd 
[root@localhost ~]# ss -an  (此命令和netstat -lnp命令效果相差不多,可以相互使用)
Netid  State  Recv-Q Send-Q  Local Address:Port Peer Address:Port  
nl UNCONN 0  0   0:0*   
nl UNCONN 0  0   0:-662699440   *   
......(中间省略)  
tcpLISTEN 0  128:::22 :::*  
tcpLISTEN 0  100   ::1:25 :::*  
[root@localhost ~]# netstat -lnp |grep -i lis  (此命令可以查看以lis开头的文件。-i是为了区分大小写)
tcp0  0 0.0.0.0:22  0.0.0.0:*   LISTEN  901/sshd
tcp0  0 127.0.0.1:250.0.0.0:*   LISTEN  1008/master 
tcp6   0  0 :::22   :::*LISTEN  901/sshd
tcp6   0  0 ::1:25  :::*LISTEN  1008/master 
unix  2  [ ACC ] STREAM LISTENING 8233 1/systemd/run/systemd/journal/stdout
......(中间省略)
unix  2  [ ACC ] STREAM LISTENING 171051008/master  public/pickup
unix  2  [ ACC ] STREAM LISTENING 171091008/master  public/cleanup
[root@localhost ~]# netstat -an |awk '/^tcp/ {++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}' (查看各种文件的数量)
LISTEN   4
ESTABLISHED      1  (该文件表示有多少客户端在于服务端通信,该数值越大表明服务器越繁忙)

知识点

1)如果在没有监听端口的情况下,是没有办法和其他机器通信的

2)tcp中ip的三次握手和四次挥手

https://blog.csdn.net/qq_34022469/article/details/52728304
https://blog.csdn.net/a987073381/article/details/52206215

3)socket文件可以是进程之间通信,但必须是同一台服务器

4)其他一些常用命令

列出所有端口 (包括监听和未监听的)

netstat -a   列出所有端口
netstat -at  列出所有tcp端口
netstat -au  列出所有udp端口

列出所有处于监听状态的 Sockets

netstat -l     只显示监听端口
netstat -lt    只列出所有监听 tcp 端口
netstat -lu    只列出所有监听 udp 端口
netstat -lx    只列出所有监听 UNIX 端口

显示每个协议的统计信息

netstat -s    显示所有端口的统计信息
netstat -st   显示TCP端口的统计信息
netstat -su   显示UDP端口的统计信息

在netstat输出中显示 PID 和进程名称

netstat -pt

4)ip命令使用

1. 设置和删除IP

ip  addr add 192.168.10.10/24  dev eth0
ip  addr show eth0
ip addr del 192.168.10.10/24  dev eth0

2. 路由相关设置

ip route show
ip route add default  via 192.168.10.1
ip route add 192.168.5.0/24 dev eth0
ip route del 192.168.10.1

另外用route命令来管理路由

(a)、网络路由
# route add -net 192.168.1.0/24 gw 172.16.1.106
(b)、主机路由
# route add -host 192.168.1.110 gw 172.16.1.106
(c)、默认路由
# route add default gw 172.16.1.106
或
# route add -net 0.0.0.0 gw 172.16.1.106
(d)、删除路由
# route del -host 192.168.1.110
# route del -net 192.168.1.0/24

3. 显示网络统计数据

ip -s link
ip -s -s link ls eth0

4. 停止开启网卡

ip link set eth0 down
ip linke set eth0 up

5.更改网卡名字

ip link set ens37 name eth1

十、Linux下抓包

抓包工具:tcpdump

1、tcpdump命令格式:tcpdump [选项]

选项
-a:           尝试将网络和广播地址转换成名称
-c<数据包数目>:收到指定的数据包数目后,就停止进行倾倒操作
-d:           把编译过的数据包编码转换成可阅读的格式,并倾倒到标准输出
-dd:          把编译过的数据包编码转换成C语言的格式,并倾倒到标准输出
-ddd:         把编译过的数据包编码转换成十进制数字的格式,并倾倒到标准输出
-e:           在每列倾倒资料上显示连接层级的文件头
-f:           用数字显示网际网络地址
-F<表达文件>:  指定内含表达方式的文件
-i<网络界面>:  使用指定的网络截面送出数据包
-l:           使用标准输出列的缓冲区
-n:           不把主机的网络地址转换成名字
-N:           不列出域名
-O:           不将数据包编码最佳化
-p:           不让网络界面进入混杂模式
-q:           快速输出,仅列出少数的传输协议信息
-r<数据包文件>:从指定的文件读取数据包数据
-s<数据包大小>:设置每个数据包的大小
-S:           用绝对而非相对数值列出TCP关联数
-t:           在每列倾倒资料上不显示时间戳记
-tt:          在每列倾倒资料上显示未经格式化的时间戳记
-T<数据包类型>:强制将表达方式所指定的数据包转译成设置的数据包类型
-v:           详细显示指令执行过程
-vv:          更详细显示指令执行过程
-x:           用十六进制字码列出数据包资料
-w<数据包文件>:把数据包数据写入指定的文件

具体操作

[root@localhost ~]# yum install -y tcpdump (安装tcpdump命令)
[root@localhost ~]# tcpdump -nn -i ens33  (对网卡名为:ens33的网卡进行抓包)
20:25:22.943507 IP 192.168.1.1.49289 > 192.168.1.131.22: Flags [.], ack 2039140, win 7720, length 0
......(中间省略)
20:25:22.944119 IP 192.168.1.131.22 > 192.168.1.1.49289: Flags [P.], seq 2039728:2039988, ack 365, win 291, length 260
第一列:系统时间
第二列:IP
第三列:原IP和原端口,后跟指定方向
第四列:进入的IP和端口
第五列:数据包信息
[root@localhost ~]# tcpdump -i ens33  (在不加-nn的情况下显示的不再是IP而是主机名)
20:31:20.246608 IP 192.168.1.1.49289 > localhost.localdomain.ssh: Flags [.], ack 1795676, win 11338, length 0
......(中间省略)
20:31:20.246769 IP 192.168.1.1.49289 > localhost.localdomain.ssh: Flags [P.], seq 313:365, ack 1795676, win 11338, length 52
[root@localhost ~]# tcpdump -nn -i ens33 port 80  (指定只需要抓该网卡下的80端口数据包)
[root@localhost ~]# tcpdump -nn -i ens33 not port 22 (指定不需要抓该网卡下的22端口数据包)
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
^C
0 packets captured
0 packets received by filter
0 packets dropped by kernel
[root@localhost ~]# tcpdump -nn -i ens33 not port 22 and host 192.168.1.1  (指定不需要抓该网卡下的22端口数据包并且抓该192.168.1.1的数据包)
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
^[[Aqu20:34:04.806349 ARP, Request who-has 192.168.1.131 (00:0c:29:ec:44:82) tell 192.168.1.1, length 46
20:34:04.806377 ARP, Reply 192.168.1.131 is-at 00:0c:29:ec:44:82, length 28
iqqqquia^H^Z
[2]+  已停止   tcpdump -nn -i ens33 not port 22 and host 192.168.1.1
[root@localhost ~]# tcpdump -i ens33 -c 100 -w /tmp/1.cap  (-c指定抓包的数量,-w指定抓包的路径)
[root@localhost ~]# yum install -y wireshark  (安装tshark命令)
[root@localhost ~]# tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e

知识点

1)存放数据包的文件不能用cat查看,会出现乱码,应该用:tcpdump -r 路径 查看

2)tshark相关知识点

https://blog.csdn.net/github_39069288/article/details/75670401

十一、Linux网络相关

1、ifconfig查看网关

[root@localhost ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 192.168.1.131  netmask 255.255.255.0  broadcast 192.168.1.255
inet6 fe80::6aac:3e4d:6b3:73ee  prefixlen 64  scopeid 0x20<link>
ether 00:0c:29:ec:44:82  txqueuelen 1000  (Ethernet)
RX packets 33800  bytes 27620379 (26.3 MiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 29772  bytes 7768190 (7.4 MiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
inet 127.0.0.1  netmask 255.0.0.0
inet6 ::1  prefixlen 128  scopeid 0x10<host>
loop  txqueuelen 1  (Local Loopback)
RX packets 72  bytes 6256 (6.1 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 72  bytes 6256 (6.1 KiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

当网卡无IP时不显示,用下边命令

[root@localhost ~]# ifconfig  -a

ifdown ens33为断开网卡,ifup ens33重新连接网卡

[root@localhost ~]# ifdown ens33 && ifup ens33 
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3)

2、设定虚拟网卡

[root@localhost ~]# cd /etc/sysconfig/network-scripts/  (进入网卡配置文件目录下)
[root@localhost network-scripts]# ls
ifcfg-ens33  ifdown-bnep  ifdown-ipv6  ifdown-ppp ifdown-Team  ifup  ifup-eth   ifup-isdn   ifup-postifup-sit   ifup-tunnel   network-functions
ifcfg-lo ifdown-eth   ifdown-isdn  ifdown-routes  ifdown-TeamPort  ifup-aliases  ifup-ippp  ifup-plip   ifup-ppp ifup-Team  ifup-wireless network-functions-ipv6
ifdown   ifdown-ippp  ifdown-post  ifdown-sit ifdown-tunnelifup-bnep ifup-ipv6  ifup-plusb  ifup-routes  ifup-TeamPort  init.ipv6-global
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:1  (拷贝一份配置文件,命名为:1,加\是为了脱义:)
[root@localhost network-scripts]# vi !$  (修改配置文件)
vi ifcfg-ens33\:1

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:1  (修改NAME、DEVICE字段值,分别加上:1)
UUID=84285fdd-1036-410d-94b7-01c5a5b909a3
DEVICE=ens33:1
IPADDR=192.168.1.145
NETMASK=255.255.255.0
GATEWAY=192.168.1.2
[root@localhost network-scripts]# ifdown ens33 && ifup ens33  (断开并重新连接网卡)
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/8)
[root@localhost network-scripts]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 192.168.1.131  netmask 255.255.255.0  broadcast 192.168.1.255
inet6 fe80::6aac:3e4d:6b3:73ee  prefixlen 64  scopeid 0x20<link>
ether 00:0c:29:ec:44:82  txqueuelen 1000  (Ethernet)
RX packets 34586  bytes 27689389 (26.4 MiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 30389  bytes 7863035 (7.4 MiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500  (会发现多了一个虚拟网卡ens33:1)
inet 192.168.1.145  netmask 255.255.255.0  broadcast 192.168.1.255 (此虚拟网卡可以在Windowsping通)
ether 00:0c:29:ec:44:82  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
inet 127.0.0.1  netmask 255.0.0.0
inet6 ::1  prefixlen 128  scopeid 0x10<host>
loop  txqueuelen 1  (Local Loopback)
RX packets 96  bytes 8320 (8.1 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 96  bytes 8320 (8.1 KiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3、查看网卡是否连接网络

[root@localhost network-scripts]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok (当不显示link ok时,用ethtool命令)
[root@localhost network-scripts]# ethtool ens33
Settings for ens33:
Supported ports: [ TP ]
Supported link modes:   10baseT/Half 10baseT/Full 
100baseT/Half 100baseT/Full 
1000baseT/Full 
Supported pause frame use: No
Supports auto-negotiation: Yes
Advertised link modes:  10baseT/Half 10baseT/Full 
100baseT/Half 100baseT/Full 
1000baseT/Full 
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
MDI-X: off (auto)
Supports Wake-on: d
Wake-on: d
Current message level: 0x00000007 (7)
           drv probe link
Link detected: yes  (显示为yes)

4、更改主机名

[root@localhost network-scripts]# hostnamectl set-hostname ma-1  (更改主机名命令)
[root@localhost network-scripts]# cd ~
[root@localhost ~]# bash  (更改完成在当前终端不会显示,断开或者才进入子shell发现变更)
[root@ma-1 ~]# cat /etc/hostname   (主机名配置文件)
ma-1

5、dns配置文件

[root@ma-1 ~]# cat /etc/resolv.conf   (dns配置文件)
# Generated by NetworkManager
nameserver 119.29.29.29
[root@ma-1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33  (更改网卡配置文件)

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=84285fdd-1036-410d-94b7-01c5a5b909a3
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.1.131
NETMASK=255.255.255.0
GATEWAY=192.168.1.2
DNS1=119.29.29.29
DNS2=8.8.8.8   (添加一个新的dns)
[root@ma-1 ~]# ifdown ens33 && ifup ens33  (重新启动网卡)
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/9)
[root@ma-1 ~]# cat /etc/resolv.conf   (查看已添加一个新的dns)
# Generated by NetworkManager
nameserver 119.29.29.29
nameserver 8.8.8.8
[root@ma-1 ~]# vim /etc/resolv.conf(临时更改dns文件配置)

# Generated by NetworkManager
nameserver 119.29.29.29
nameserver 8.8.8.8

六、配置文件 /etc/hosts

[root@ma-1 ~]# cat /etc/hosts  (查看该配置文件))
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@ma-1 ~]# vi !$
vi /etc/hosts
[root@ma-1 ~]# ping www.qq123.com   (ping该网址,IP是202.91.250.93)
PING www.qq123.com (202.91.250.93) 56(84) bytes of data.
64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=1 ttl=128 time=50.9 ms
64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=3 ttl=128 time=50.8 ms
^C
--- www.qq123.com ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 2004ms
rtt min/avg/max/mdev = 50.880/50.890/50.901/0.225 ms
[root@ma-1 ~]# vi /etc/hosts  (修改该网址的访问IP)
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.131 www.qq123.com www.qq.com www.baidu.com  (一个IP支持多个域名)
127.0.0.1  www.qq.com
[root@ma-1 ~]# ping www.qq123.com  (IP已被更改)
PING www.qq123.com (192.168.1.131) 56(84) bytes of data.
64 bytes from www.qq123.com (192.168.1.131): icmp_seq=1 ttl=64 time=0.067 ms
64 bytes from www.qq123.com (192.168.1.131): icmp_seq=2 ttl=64 time=0.083 ms
64 bytes from www.qq123.com (192.168.1.131): icmp_seq=3 ttl=64 time=0.379 ms
^C
--- www.qq123.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.067/0.176/0.379/0.143 ms
[root@ma-1 ~]# ping www.baidu.com   (次访问的域名也是IP)
PING www.qq123.com (192.168.1.131) 56(84) bytes of data.
64 bytes from www.qq123.com (192.168.1.131): icmp_seq=1 ttl=64 time=0.441 ms
64 bytes from www.qq123.com (192.168.1.131): icmp_seq=2 ttl=64 time=0.086 ms
^C
--- www.qq123.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.086/0.263/0.441/0.178 ms
[root@ma-1 ~]# ping www.qq.com  (当一个IP对应多个IP以最后一个为准)
PING www.qq123.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.087 ms
^C
--- www.qq123.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.076/0.081/0.087/0.010 ms![image](https://note.youdao.com/favicon.ico)


猜你喜欢

转载自blog.51cto.com/13750987/2127910