日常运维常用命令工具1

一、w/uptime查看负载

w:可以查看系统负载情况

[root@node5 ~]# w
 19:10:19 up  6:26,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.10.1     18:56    3.00s  0.12s  0.01s w
root     pts/1    192.168.10.1     19:07    2:35   0.02s  0.02s -bash
[root@node5 ~]# 

uptime:查看负载情况

[root@node5 ~]# uptime
 19:04:42 up  6:21,  1 user,  load average: 0.00, 0.01, 0.05
[root@node5 ~]# 

参数解析

19:06:10 :当前系统时间

up 6:22:已经开机多长时间

1 user:有几个用户在线

load average: 0.00, 0.01, 0.05:负载情况,单位时间内使用cpu活动的进程个数(平均值,不一定是整数)。3个数字分别是1分钟、 5分钟 、15分钟内的负载情况。

cat /proc/cpuinfo:查看cpu信息

[root@node5 ~]# cat /proc/cpuinfo 
processor    : 0
vendor_id    : GenuineIntel
cpu family    : 6
model        : 60
model name    : Intel(R) Core(TM) i3-4000M CPU @ 2.40GHz
stepping    : 3
microcode    : 0x17
cpu MHz        : 2394.490
cache size    : 3072 KB
physical id    : 0
siblings    : 2
core id        : 0
cpu cores    : 2
apicid        : 0
initial apicid    : 0
fpu        : yes
fpu_exception    : yes
cpuid level    : 13
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt arat
bogomips    : 4788.98
clflush size    : 64
cache_alignment    : 64
address sizes    : 43 bits physical, 48 bits virtual
power management:

processor    : 1
vendor_id    : GenuineIntel
cpu family    : 6
model        : 60
model name    : Intel(R) Core(TM) i3-4000M CPU @ 2.40GHz
stepping    : 3
microcode    : 0x17
cpu MHz        : 2394.490
cache size    : 3072 KB
physical id    : 0
siblings    : 2
core id        : 1
cpu cores    : 2
apicid        : 1
initial apicid    : 1
fpu        : yes
fpu_exception    : yes
cpuid level    : 13
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt arat
bogomips    : 4788.98
clflush size    : 64
cache_alignment    : 64
address sizes    : 43 bits physical, 48 bits virtual
power management:

processor    : 2
vendor_id    : GenuineIntel
cpu family    : 6
model        : 60
model name    : Intel(R) Core(TM) i3-4000M CPU @ 2.40GHz
stepping    : 3
microcode    : 0x17
cpu MHz        : 2394.490
cache size    : 3072 KB
physical id    : 1
siblings    : 2
core id        : 0
cpu cores    : 2
apicid        : 2
initial apicid    : 2
fpu        : yes
fpu_exception    : yes
cpuid level    : 13
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt arat
bogomips    : 4788.98
clflush size    : 64
cache_alignment    : 64
address sizes    : 43 bits physical, 48 bits virtual
power management:

processor    : 3
vendor_id    : GenuineIntel
cpu family    : 6
model        : 60
model name    : Intel(R) Core(TM) i3-4000M CPU @ 2.40GHz
stepping    : 3
microcode    : 0x17
cpu MHz        : 2394.490
cache size    : 3072 KB
physical id    : 1
siblings    : 2
core id        : 1
cpu cores    : 2
apicid        : 3
initial apicid    : 3
fpu        : yes
fpu_exception    : yes
cpuid level    : 13
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt arat
bogomips    : 4788.98
clflush size    : 64
cache_alignment    : 64
address sizes    : 43 bits physical, 48 bits virtual
power management:

[root@node5 ~]# 

processor:cpu核数(逻辑核数),0表示1核,1表示2核。。。

二、vmstat命令

vmstat:Report virtual memory statistics,查看虚拟内存状态。

语法:vmstat(选项)(参数)

选项:

-a:显示活动内页

-f:显示启动后创建的进程总数

-m:显示slab信息

-n:头信息仅显示一次

-s:以表格方式显示事件计数器和内存状态

-d:报告磁盘状态

-p:显示指定的硬盘分区状态

-S:输出信息的单位。

参数: 数字1 数字2

数字1:每隔多少秒刷新一下

数字2:显示多少次

[root@node5 ~]# 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   3072 141752      0 1582184    0    0    27   124  151   96  3  2 95  0  0
[root@node5 ~]# 

procs(进程):

r:run,表示有多少个进程处于运行状态

b:block,等待IO的进程数量

memory(内存):

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

swap部分:

si: 表示有多少kb的数据从swap进入到内存中,单位kb

so: 表示有多少kb的数据从内存写入到交换分区swap中,单位kb

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

io部分:

bi: 表示有多少kb的数据从磁盘读取到内存中,单位kb

bo: 表示有多少kb的数据从内存写入到磁盘中,单位kb

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

system部分:

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

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

cpu部分:

 us: 用户进程执行时间百分比(user time),us的值比较高时,说明用户进程消耗的CPU时间多,但是如果长期超50%的使用,那么我们就该考虑优化程序算法或者进行加速。

sy: 内核系统进程执行时间百分比(system time),sy的值高时,说明系统内核消耗的CPU资源多,这并不是良性表现,我们应该检查原因。

wa: IO等待时间百分比,wa的值高时,说明IO等待比较严重,这可能由于磁盘大量作随机访问造成,也有可能磁盘出现瓶颈(块操作)。

 id: 空闲时间百分比

三、top命令

top:查看进程使用资源情况

top - 19:47:01 up  7:03,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 186 total,   1 running, 185 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2030172 total,   141652 free,   306148 used,  1582372 buff/cache
KiB Swap:  2097148 total,  2094076 free,     3072 used.  1436804 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                               
  1111 root      20   0  161972   2296   1576 R   0.3  0.1   0:00.35 top                                   
  5159 root      20   0       0      0      0 S   0.3  0.0   0:20.58 kworker/0:0                           
 25729 mysql     20   0 2056252  94192   7320 S   0.3  4.6   0:18.36 mysqld                                
     1 root      20   0  128020   5788   3296 S   0.0  0.3   0:20.98 systemd      

选项解释:

第1行:

top - 19:47:01:当前系统时间

up 7:03:开机多长时间

2 users:几个用户在线

load average: 0.00, 0.01, 0.05:负载情况,单位时间内使用cpu活动的进程个数(平均值,不一定是整数)。3个数字分别是1分钟、 5分钟 、15分钟内的负载情况。

第2行:

Tasks: 186 total, 1 running, 185 sleeping, 0 stopped, 0 zombie:进程总数,各种状态的的进程数

第3行:

0.0 us:用户空间占用CPU的百分比

0.1 sy:内核空间占用CPU的百分比

0.0 ni:改变过优先级的进程占用CPU的百分比

99.9 id:cpu空闲百分比

0.0 wa:IO等待占用CPU的百分比

0.0 hi: 硬中断(Hardware IRQ)占用CPU的百分比

0.0 si:软中断(Software Interrupts)占用CPU的百分比

0.0 st:被偷走的cpu百分比,比如虚拟化相关使用了cpu

第4行:

2030172 total:物理内存总量

141652 free:空闲内存总量

306148 used:使用中的内存总量

1582372 buff/cache:缓冲和缓存大小

第5行:

2097148 total:交换分区总量

2094076 free:空闲的交换分区总量

3072 used:使用的交换分区总量

1436016 avail Mem:可用的总量

进程信息部分:

PID:进程id

USER :进程所有者

PR:优先级

NI :nice值,负数表示高优先级,正数表示低优先级

PRI,即进程的优先级,或者通俗点说就是程序被CPU执行的先后顺序,此值越小进程的优先级别越高。那NI呢?就是我们所要说的nice值了,其表示进程可被执行的优先级的修正数值。如前面所说,PRI值越小越快被执行,那么加入nice值后,将会使得PRI变为:PRI(new)=PRI(old)+nice。由此看出,PR是根据NICE排序的,规则是NICE越小PR越前(小,优先权更大),即其优先级会变高,则其越快被执行。如果NICE相同则进程uid是root的优先权更大。

VIRT: 进程使用的虚拟内容总量,单位kb,VIRT=SWAP+RES

RES:进程使用的、未被换出的物理内存大小

SHR:共享内存大小,单位kb

S :进程状态,D=不可中断的睡眠状态,R=运行,S=睡眠,T=跟踪/停止,Z=僵尸进程,该进程已经终止,但是其父进程无法正常终止它。其他状态:

<:表示进程运存在高优先级上

N:表示进程运存在低优先级上

L:表示进程有页面锁定在内存中

s:表示进程是控制进程

l:表示进程是多线程的

+:表示当前进程运行在前台

%CPU:上次更新到现在的CPU时间占用百分比

%MEM:进程使用的物理内存百分比

TIME+:进程使用的CPU时间总计,单位1/100秒

COMMAND: 进程名称(命令名/命令行)

top命令操作:

top -c:显示详细的进程信息,如下所示:

Tasks: 180 total,   1 running, 179 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.1 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2030172 total,  1548544 free,   271768 used,   209860 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1563148 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                               
  2059 root      20   0  162000   2380   1664 R   0.7  0.1   0:00.04 top -c                                
  1281 mysql     20   0 2056252 113428   7284 S   0.3  5.6   0:16.90 /usr/local/mysql/bin/mysqld --basedir+
     1 root      20   0  193616   6676   4164 S   0.0  0.3   0:09.34 /usr/lib/systemd/systemd --switched-r+
     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.75 [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.58 [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:01.50 [rcu_sched]                           
    10 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 [lru-add-drain]                       
    11 root      rt   0       0      0      0 S   0.0  0.0   0:01.02 [watchdog/0]                          
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.13 [watchdog/1]                          
    13 root      rt   0       0      0      0 S   0.0  0.0   0:01.13 [migration/1]    

top -bn1:静态显示所有进程

top默认按%CPU从大到小排序。

按键盘大写M则按%MEM从大到小排序,如下所示:

Tasks: 179 total,   2 running, 177 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.1 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2030172 total,  1548576 free,   271752 used,   209844 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1563180 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                               
  1281 mysql     20   0 2056252 113428   7284 S   0.4  5.6   0:16.96 mysqld                                
   777 root      20   0  357960  28928   7000 S   0.0  1.4   0:05.45 firewalld                             
  1097 root      20   0  573848  17016   6036 S   0.0  0.8   0:08.45 tuned                                 
  1101 root      20   0  222716  15556   3460 S   0.0  0.8   0:03.52 rsyslogd                              
   753 polkitd   20   0  539204  11900   4924 S   0.0  0.6   0:01.90 polkitd                               
   793 root      20   0  476312  11232   6792 S   0.0  0.6   0:03.54 NetworkManager                        
     1 root      20   0  193616   6676   4164 S   0.0  0.3   0:09.34 systemd                               
   618 root      20   0   48672   6240   2856 S   0.0  0.3   0:01.73 systemd-udevd                         
   754 root      20   0   99652   6108   4504 S   0.0  0.3   0:01.70 VGAuthS

按大写p,按cpu排序

按数字1,列出所有cpu使用情况,如下所示:

%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 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
%Cpu2  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

按q,退出top

四、sar命令

1、使用sar命令,得安装sysstat包:

[root@node5 ~]# yum install -y sysstat

2、sar命令使用:

直接输入sar:

[root@node5 ~]# sar
Cannot open /var/log/sa/sa07: No such file or directory

这是因为,sar不加具体参数会调用系统的历史文件。因为刚安装,所以没有历史文件,会报错。每个10分钟会生成sa文件。

2.1 sar -n DEV:查看网卡流量

[root@node5 ~]# sar -n DEV 1 3
Linux 3.10.0-862.el7.x86_64 (node5)     06/07/2018  _x86_64_    (4 CPU)

09:15:16 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
09:15:17 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
09:15:17 PM     ens33      1.00      1.00      0.06      0.18      0.00      0.00      0.00

09:15:17 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
09:15:18 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
09:15:18 PM     ens33      1.98      1.98      0.16      0.44      0.00      0.00      0.00

09:15:18 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
09:15:19 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
09:15:19 PM     ens33      0.99      0.99      0.06      0.38      0.00      0.00      0.00

Average:        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
Average:           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:        ens33      1.32      1.32      0.09      0.33      0.00      0.00      0.00
[root@node5 ~]# 

sar -n DEV 1 3:1表示每1秒显示一次,3就是总共显示3次

参数解释:

IFACE:LAN接口

rxpck/s:每秒钟接收的数据包,这个数据一般几千之内算正常,如果过万就可能不正常了。

txpck/s:每秒钟发送的数据包

rxkB/s : 每秒接受的字节数

txkB/s : 每秒发送的字节数

rxcmp/s:每秒钟接收的压缩数据包

txcmp/s:每秒钟发送的压缩数据包

rxmcst/s:每秒钟接收的多播数据包

查看历史文件:

[root@node5 ~]# sar -n DEV -f /var/log/sa/sa07 
Linux 3.10.0-862.el7.x86_64 (node5)     06/07/2018  _x86_64_    (4 CPU)
[root@node5 ~]# 

除了/var/log/sa/sa07 文件,还有/var/log/sa/sar07,因为sar才刚安装,所以这个文件得明天才会生成。前者是二进制文件,后者是普通文件,可以直接看的。

2.2 sar -q :系统负载

[root@node5 ~]# sar -q 1 2
Linux 3.10.0-862.el7.x86_64 (node5)     06/07/2018  _x86_64_    (4 CPU)

09:24:15 PM   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
09:24:16 PM         0       228      0.00      0.01      0.05         0
09:24:17 PM         0       228      0.00      0.01      0.05         0
Average:            0       228      0.00      0.01      0.05         0
[root@node5 ~]# 

sar -q 1 2:1表示每一秒显示一次,2表示总共显示2次。

查看历史文件:

[root@node5 ~]# sar -q -f /var/log/sa/sa07 
Linux 3.10.0-862.el7.x86_64 (node5)     06/07/2018  _x86_64_    (4 CPU)
[root@node5 ~]# 

因为才安装sar,所以历史文件内容为空

2.3 sar -b:磁盘读写

[root@node5 ~]# sar -b 1 2
Linux 3.10.0-862.el7.x86_64 (node5)     06/07/2018  _x86_64_    (4 CPU)

09:29:43 PM       tps      rtps      wtps   bread/s   bwrtn/s
09:29:44 PM      0.00      0.00      0.00      0.00      0.00
09:29:45 PM      0.00      0.00      0.00      0.00      0.00
Average:         0.00      0.00      0.00      0.00      0.00
[root@node5 ~]# 

五、nload命令

nload命令:查看网卡流量。

安装nload包:

[root@node5 ~]# yum install -y epel-release
[root@node5 ~]# yum install -y nload 

直接运行nload命令:

动态显示网卡流量。第一行:网卡名 ip 网卡个数,按键盘的向左、向右键可以切换网卡。

按q退出。通常服务器买的带宽是指出去的流量。

下面的是我的vps的网卡流量图:

nload 默认分为上下两块:

Incoming:也就是进入网卡的流量,

Outgoing:也就是从这块网卡出去的流量,

当前流量(Curr)

平均流量(Avg)

最小流量(Min)

最大流量(Max)

总和流量(Ttl)

六、监控io性能

iostat -x:磁盘使用

[root@node5 ~]# iostat -x
Linux 3.10.0-862.el7.x86_64 (node5)     06/07/2018  _x86_64_    (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.07    0.00    0.19    0.08    0.00   99.66

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda               0.00     0.06    1.03    0.34    40.85    15.93    82.84     0.03   25.71   29.32   14.72   8.69   1.19
dm-0              0.00     0.00    0.85    0.39    39.20    15.60    88.18     0.04   30.31   35.43   19.26   9.37   1.16
dm-1              0.00     0.00    0.02    0.00     0.36     0.00    47.40     0.00   14.78   14.78    0.00  10.95   0.02

[root@node5 ~]# 

rrqm/s:每秒进行merge的读操作数目。即delta(rmerge)/s
wrqm/s:每秒进行merge的写操作数目。即delta(wmerge)/s
r/s:每秒完成的读I/O设备次数。即delta(rio)/s
w/s:每秒完成的写I/0设备次数。即delta(wio)/s
rKB/s:每秒读K字节数。是rsec/s的一半,因为每扇区大小为512字节
wKB/s:每秒写K字节数。是wsec/s的一半
avgrq-sz:平均每次设备I/O操作的数据大小(扇区)。即delta(rsect+wsect)/delta(rio+wio)
avgqu-sz:平均I/O队列长度。即delta(aveq)/s/1000(因为aveq的单位为毫秒)
await:平均每次设备I/O操作的等待时间(毫秒)。即delta(ruse+wuse)/delta(rio+wio)
svctm:平均每次设备I/O操作的服务时间(毫秒)。即delta(use)/delta(rio+wio)
%util:一秒中有百分之多少的时间用于I/O操作,或者说一秒中有多少时间I/O队列是非空的。即delta(usr)/s/1000(因为use的单位为毫秒)

如果%util接近100%,说明产生的I/O请求太多,I/O系统已经满负载,该磁盘可能存在瓶颈。

iotop:磁盘使用

iotop:动态显示磁盘使用情况

安装iotop:

[root@node5 ~]# yum install -y iotop

直接运行iotop:

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                                                                                                     
   512 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [xfsaild/dm-0]
     1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switched-root --system --deserialize 22
     2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
     3 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
  1540 be/4 mysql    0.00 B/s    0.00 B/s  0.00 %  0.00 % mysqld --basedir=/usr/local/mysql --datadir=/mydata/mar~/mariadb/node5.pid --socket=/tmp/mysql.sock --port=3306
     5 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H]
  1542 be/4 mysql    0.00 B/s    0.00 B/s  0.00 %  0.00 % mysqld --basedir=/usr/local/mysql --datadir=/mydata/mar~/mariadb/node5.pid --socket=/tmp/mysql.sock --port=3306
     7 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
     8 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_bh]
     9 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_sched]
    10 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [lru-add-drain]
    11 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/0]
    12 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/1]
    13 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/1]
    14 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/1]
    16 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/1:0H]

按q退出。

七、free命令

free命令:查看内存情况

[root@node5 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        2030172      278408     1358324        9728      393440     1547376
Swap:       2097148           0     2097148
[root@node5 ~]# 

使用-m选项,结果用Mb显示,使用-h选项(human),结果使用人类可以识别的格式显示。

[root@node5 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1982         271        1326           9         384        1511
Swap:          2047           0        2047
[root@node5 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G        272M        1.3G        9.5M        384M        1.5G
Swap:          2.0G          0B        2.0G
[root@node5 ~]# 

Linux中会把内存预分配一部分给buff/cache。因为cpu和磁盘的读写速度差别很大,所以通过内存来平衡两者的差距,因为内存读写速度较快。内存得预留一些空间给buff/cache。

cache:缓冲,数据从磁盘读出交给cpu,中间需要经过内存,这部分空间叫cache。

buff:缓存,cpu处理好之后,把数据返回给磁盘,也经过内存,这部分叫buff。

vailable = free + buff/cache剩余的部分

total = used + free + buff/cache

swap:交换分区,也就是虚拟内存

八、ps命令

ps命令:查看系统进程

用法:ps aux、ps -elf

ps aux:

a:显示其他用户启动的进程

u:启动这个进程的用户和它启动的时间

x:查看系统中属于自己的进程

[root@node5 ~]# ps -aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.1  0.3 193616  6684 ?        Ss   20:03   0:09 /usr/lib/systemd/systemd --switched-root -
root          2  0.0  0.0      0     0 ?        S    20:03   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    20:03   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   20:03   0:00 [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    20:03   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    20:03   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        S    20:03   0:02 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S<   20:03   0:00 [lru-add-drain]
root         11  0.0  0.0      0     0 ?        S    20:03   0:01 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S    20:03   0:00 [watchdog/1]
root         13  0.0  0.0      0     0 ?        S    20:03   0:01 [migration/1]
root         14  0.0  0.0      0     0 ?        S    20:03   0:00 [ksoftirqd/1]
root         16  0.0  0.0      0     0 ?        S<   20:03   0:00 [kworker/1:0H]
root         17  0.0  0.0      0     0 ?        S    20:03   0:00 [watchdog/2]
root         18  0.0  0.0      0     0 ?        S    20:03   0:01 [migration/2]
root         19  0.0  0.0      0     0 ?        S    20:03   0:01 [ksoftirqd/2]
root         21  0.0  0.0      0     0 ?        S<   20:03   0:00 [kworker/2:0H]

参数解释:

USER:进程运行的用户

PID:进程id

%cpu:占用cpu的使用率

%mem:内存使用率

vsz:虚拟内存

rss:物理内存

tty:终端

stat:状态,D:不可中断的睡眠状态,R:运行,S:睡眠,T:跟踪/停止,Z:僵尸进程,该进程已经终止,但是其父进程无法正常终止它。

<:表示进程运存在高优先级上

N:表示进程运存在低优先级上

L:表示进程有页面锁定在内存中

s:表示进程是控制进程

l:表示进程是多线程的

+:表示当前进程运行在前台

start:进程启动的时间

time:使用cpu多长时间

command:命令

ps -elf:

e:显示所有进程

l:长格式

f:全格式

[root@node5 ~]# ps -elf
F S UID         PID   PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root          1      0  0  80   0 - 48404 ep_pol 20:03 ?        00:00:09 /usr/lib/systemd/systemd --swit
1 S root          2      0  0  80   0 -     0 kthrea 20:03 ?        00:00:00 [kthreadd]
1 S root          3      2  0  80   0 -     0 smpboo 20:03 ?        00:00:00 [ksoftirqd/0]
1 S root          5      2  0  60 -20 -     0 worker 20:03 ?        00:00:00 [kworker/0:0H]
1 S root          7      2  0 -40   - -     0 smpboo 20:03 ?        00:00:00 [migration/0]
1 S root          8      2  0  80   0 -     0 rcu_gp 20:03 ?        00:00:00 [rcu_bh]
1 S root          9      2  0  80   0 -     0 rcu_gp 20:03 ?        00:00:02 [rcu_sched]
1 S root         10      2  0  60 -20 -     0 rescue 20:03 ?        00:00:00 [lru-add-drain]
5 S root         11      2  0 -40   - -     0 smpboo 20:03 ?        00:00:01 [watchdog/0]
5 S root         12      2  0 -40   - -     0 smpboo 20:03 ?        00:00:00 [watchdog/1]
1 S root         13      2  0 -40   - -     0 smpboo 20:03 ?        00:00:01 [migration/1]
1 S root         14      2  0  80   0 -     0 smpboo 20:03 ?        00:00:00 [ksoftirqd/1]

--no-heading选项

[root@localhost ~]#ps --no-heading -C nginx 
 2785 ?        00:00:00 nginx
 3106 ?        00:00:14 nginx
[root@localhost ~]#ps --no-heading -C nginx | wc -l
2
[root@localhost ~]#

--no-headers:

[root@localhost ~]#ps -C nginx --no-headers
 2785 ?        00:00:00 nginx
 3106 ?        00:00:14 nginx
[root@localhost ~]#

九、查看网络状态

netstat:查看网络状态

[root@node5 ~]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0     52 node5:ssh               192.168.10.1:49416      ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    12061    /run/systemd/shutdownd
unix  3      [ ]         DGRAM                    9373     /run/systemd/notify
unix  2      [ ]         DGRAM                    9375     /run/systemd/cgroups-agent
unix  5      [ ]         DGRAM                    9390     /run/systemd/journal/socket
unix  13     [ ]         DGRAM                    9392     /dev/log
unix  3      [ ]         STREAM     CONNECTED     22313    
unix  2      [ ]         DGRAM                    17003    
unix  3      [ ]         STREAM     CONNECTED     22304    
unix  3      [ ]         STREAM     CONNECTED     22325    
unix  3      [ ]         STREAM     CONNECTED     17250    
unix  2      [ ]         DGRAM                    18239    
unix  3      [ ]         STREAM     CONNECTED     17005    
unix  3      [ ]         STREAM     CONNECTED     17004    
unix  3      [ ]         STREAM   

netstat -lnp:查看监听端口,l:监听,n:直接使用ip地址,p:显示正在使用socket的程序

[root@node5 ~]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1094/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1338/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1281/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1094/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1338/master         
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     22297    1338/master          private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     22300    1338/master          private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     22303    1338/master          private/defer
unix  2      [ ACC ]     STREAM     LISTENING     22306    1338/master          private/trace
unix  2      [ ACC ]     STREAM     LISTENING     22309    1338/master          private/verify
unix  2      [ ACC ]     STREAM     LISTENING     22315    1338/master          private/proxymap
unix  2      [ ACC ]     STREAM     LISTENING     22318    1338/master          private/proxywrite
unix  2      [ ACC ]     STREAM     LISTENING     22321    1338/master          private/smtp
unix  2      [ ACC ]     STREAM     LISTENING     22324    1338/master          private/relay

netstat -an:查看系统网络连接状态,a:全部

[root@node5 ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0     52 192.168.10.205:22       192.168.10.1:49416      ESTABLISHED
tcp6       0      0 :::3306                 :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     22297    private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     22300    private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     22303    private/defer
unix  2      [ ACC ]     STREAM     LISTENING     22306    private/trace
unix  2      [ ACC ]     STREAM     LISTENING     22309    private/verify
unix  2      [ ACC ]     STREAM     LISTENING     22315    private/proxymap

netstat -lntp:只看tcp,不包含socket,t:tcp

[root@node5 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1094/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1338/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1281/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1094/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1338/master         
[root@node5 ~]# 

ss -an和netstat异曲同工

[root@node5 ~]# ss -tnl
State      Recv-Q Send-Q         Local Address:Port                        Peer Address:Port              
LISTEN     0      128                        *:22                                     *:*                  
LISTEN     0      100                127.0.0.1:25                                     *:*                  
LISTEN     0      80                        :::3306                                  :::*                  
LISTEN     0      128                       :::22                                    :::*                  
LISTEN     0      100                      ::1:25                                    :::*                  
[root@node5 ~]# 

查看每种状态的连接数:

[root@node5 ~]# netstat -an | awk '/^tcp/ {++stat[$NF]} END {for(key in stat) print key,"\t",stat[key]}'
LISTEN      8
ESTABLISHED      1
TIME_WAIT      4
[root@node5 ~]# 

十、抓包

10.1 tcpdump

安装tcpdump工具

[root@node5 ~]# yum install -y tcpdump

用法:tcpdump -nn

-n:对地址以数字方式显式,否则显式为主机名,也就是说-n选项不做主机名解析。

-nn:除了-n的作用外,还把端口显示为数值,否则显示端口服务名。

tcpdump -nn -i ens33: -i,指定网卡名

23:01:41.046209 IP 192.168.10.1.49416 > 192.168.10.205.22: Flags [.], ack 415792, win 254, length 0
23:01:41.048079 IP 192.168.10.205.22 > 192.168.10.1.49416: Flags [P.], seq 415972:416248, ack 53, win 251, le
23:01:41.049698 IP 192.168.10.1.49416 > 192.168.10.205.22: Flags [.], ack 416248, win 253, length 0
23:01:41.049877 IP 192.168.10.205.22 > 192.168.10.1.49416: Flags [P.], seq 416248:416428, ack 53, win 251, le
23:01:41.050492 IP 192.168.10.205.22 > 192.168.10.1.49416: Flags [P.], seq 416428:416704, ack 53, win 251, le
23:01:41.051378 IP 192.168.10.1.49416 > 192.168.10.205.22: Flags [.], ack 416704, win 251, length 0
23:01:41.051889 IP 192.168.10.205.22 > 192.168.10.1.49416: Flags [P.], seq 416704:416884, ack 53, win 251, le
23:01:41.053522 IP 192.168.10.205.22 > 192.168.10.1.49416: Flags [P.], seq 416884:417160, ack 53, win 251, le
23:01:41.053729 IP 192.168.10.1.49416 > 192.168.10.205.22: Flags [P.], seq 53:105, ack 416884, win 256, lengt
^C
2709 packets captured
2712 packets received by filter
0 packets dropped by kernel
[root@node5 ~]# 

时间 、源ip:端口、目标ip:端口、数据包信息

使用举例:

tcpdump -nn -i ens33 port 80:指定某个网卡的端口

tcpdump -nn -i ens33 not port 80:排除某个端口

tcpdump -nn -i ens33 not port 80 and host 192

tcpdump -nn -i ens33 -c 100 -w 1.cap:-c指定抓包个数,-w保存到文件,1.cap文件名

[root@node5 ~]# tcpdump -nn -i ens33 -c 10 -w 1.cap 
tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
10 packets captured
23 packets received by filter
0 packets dropped by kernel
[root@node5 ~]# 

抓包保存的文件为1.cap,但这个文件是不能直接看的。得使用tcpdump -r查看:

[root@node5 ~]# tcpdump -r 1.cap 
reading from file 1.cap, link-type EN10MB (Ethernet)
23:17:54.342496 IP node5.ssh > 192.168.10.1.49416: Flags [P.], seq 3712966503:3712966651, ack 385333990, win 251, length 148
23:17:54.342766 IP 192.168.10.1.49416 > node5.ssh: Flags [.], ack 148, win 252, length 0
23:17:56.788933 ARP, Request who-has node5 (00:0c:29:d0:a0:1b (oui Unknown)) tell 192.168.10.1, length 46
23:17:56.788961 ARP, Reply node5 is-at 00:0c:29:d0:a0:1b (oui Unknown), length 28
23:17:58.289008 IP 192.168.10.1.60065 > node5.http: Flags [S], seq 943579038, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
23:17:58.289094 IP node5.http > 192.168.10.1.60065: Flags [S.], seq 3837498471, ack 943579039, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
23:17:58.289321 IP 192.168.10.1.60065 > node5.http: Flags [.], ack 1, win 256, length 0
23:17:58.377315 IP 192.168.10.1.60065 > node5.http: Flags [P.], seq 1:395, ack 1, win 256, length 394: HTTP: GET / HTTP/1.1
23:17:58.377382 IP node5.http > 192.168.10.1.60065: Flags [.], ack 395, win 237, length 0
23:17:58.387338 IP node5.http > 192.168.10.1.60065: Flags [.], seq 1:2921, ack 395, win 237, length 2920: HTTP: HTTP/1.1 200 OK
[root@node5 ~]# 

10.2 tshark命令:

安装wireshart包:

[root@node5 ~]# yum install -y wireshark
[root@node5 ~]# tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"

十一、网络相关配置

11.1 ifconfig命令

要使用该命令得安装net-tools包

[root@node5 ~]# yum install -y net-tools

查看ip:

[root@node5 ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.205  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fed0:a01b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d0:a0:1b  txqueuelen 1000  (Ethernet)
        RX packets 25923  bytes 24521258 (23.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14709  bytes 5480098 (5.2 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 1000  (Local Loopback)
        RX packets 217  bytes 15316 (14.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 217  bytes 15316 (14.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@node5 ~]# 

网卡配置文件:

[root@node5 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.10.205
PREFIX=24
GATEWAY=192.168.10.2
DNS1=119.29.29.29
DNS2=182.254.116.116

11.2 ifup/ifdown命令

ifdown 网卡名:停用网卡

[root@node5 ~]# ifdown ens33

ifup 网卡名:启用网卡

[root@node5 ~]# ifup ens33

11.3 mii-tool命令

mii-tool 网卡名:  查看网卡是否连接

[root@node5 ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok
[root@node5 ~]# 

11.4 ethtool 网卡名:查看网卡是否连接

[root@node5 ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok
[root@node5 ~]# 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
    Supported FEC modes: Not reported
    Advertised link modes:  10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Advertised FEC modes: Not reported
    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
[root@node5 ~]# 

11.5 设置虚拟网卡

虚拟网卡在lvs中会用到,关于lvs内容,请参考我的另外文章:

https://my.oschina.net/u/3851637/blog/1819351

复制已经有的网卡配置文件,文件名为ifcfg-ens33:0

[root@node5 ~]# cd /etc/sysconfig/network-scripts/
[root@node5 network-scripts]# ls
ifcfg-ens33  ifdown-ippp  ifdown-routes    ifup          ifup-ipv6   ifup-ppp       ifup-tunnel
ifcfg-lo     ifdown-ipv6  ifdown-sit       ifup-aliases  ifup-isdn   ifup-routes    ifup-wireless
ifdown       ifdown-isdn  ifdown-Team      ifup-bnep     ifup-plip   ifup-sit       init.ipv6-global
ifdown-bnep  ifdown-post  ifdown-TeamPort  ifup-eth      ifup-plusb  ifup-Team      network-functions
ifdown-eth   ifdown-ppp   ifdown-tunnel    ifup-ippp     ifup-post   ifup-TeamPort  network-functions-ipv6
[root@node5 network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@node5 network-scripts]# 

ifcfg-ens33配置文件内容如下:

[root@node5 network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.10.205
PREFIX=24
GATEWAY=192.168.10.2
DNS1=119.29.29.29
DNS2=182.254.116.116

修改ifcfg-ens33:0文件,内容如下:

[root@node5 network-scripts]# vim ifcfg-ens33:0
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.10.206
PREFIX=24

重启网络后,查看ip:

[root@node5 network-scripts]# systemctl restart network
[root@node5 network-scripts]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.205  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fed0:a01b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d0:a0:1b  txqueuelen 1000  (Ethernet)
        RX packets 26936  bytes 24821309 (23.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 15464  bytes 5579393 (5.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.206  netmask 255.255.255.0  broadcast 192.168.10.255
        ether 00:0c:29:d0:a0:1b  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 1000  (Local Loopback)
        RX packets 217  bytes 15316 (14.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 217  bytes 15316 (14.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@node5 network-scripts]# 

11.6 修改主机名

修改主机名有2种方法:

方法1:centos6.x版本不支持。

[root@node5 ~]# hostnamectl set-hostname node

方法2:输出重定向到配置文件

[root@node5 ~]# echo haha > /etc/hostname 

11.7 修改DNS

DNS可以在网卡配置文件中指定。也可以修改dns配置文件:

[root@node5 ~]# vim /etc/resolv.conf 
# Generated by NetworkManager
nameserver 119.29.29.29
nameserver 182.254.116.116

永久修改dns:修改网卡配置文件

临时修改dns:修改/etc/resolv.conf 文件

11.8 hosts文件

此文件Linux、Windows都有,主要用作本机域名解析。文件内容格式为:

ip 域名1 域名2…

如果一个域名配置多个ip,则以最后一个为主。例如:

192.168.10.205 haha.com

192.168.10.206 haha.com

则,ping haha.com得到的是192.168.10.206

只在本机生效

ip命令使用:

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

3. 停止开启网卡
ip link set eth0 down
ip linke set eth0 up

更改网卡名字
ip link set ens37 name eth1

 

转载于:https://my.oschina.net/logmm/blog/1824091

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值