Linux日常运维管理技巧(一)


监控系统状态

  • w/uptime查看系统负载

[root@zyshanlinux-01 ~]# w
##当前系统时间,启动至今时间,目前登录用户数,系统负载:1分钟,5分钟,15分钟内系统负载是多少(单位时间内cpu使用的活动进程有多少个,不一定是整数,平均值)
 12:08:10 up 44 min,  2 users,  load average: 0.06, 0.04, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      11:47   20:58   0.03s  0.03s -bash  ##本地登录的用户
root     pts/0    192.168.106.1    11:34    2.00s  0.05s  0.02s w  ##远程登录的用户
##登录的是谁,登录的终端(从网络远程登录来的就是pts/0,从本地登录就是tty1),从哪里登录来的(远程就是ip),用户登录的时间点,空闲了多少时间,使用CPU的时间,命令。
[root@zyshanlinux-01 ~]# date
2018年 06月 10日 星期日 11:41:41 CST

[root@zyshanlinux-01 ~]# uptime  ##和w命令是一样的。
 12:08:27 up 45 min,  2 users,  load average: 0.04, 0.04, 0.05

##当系统中只有一个逻辑CPU的时候,load average: 0.00, 0.01, 0.05中在1分钟的时候数值为1是最理想的,不闲也没压力,即每个逻辑CPU有一个进程在占用它。第2、3个数值分别体现在5分钟和15分钟内逻辑CPU中进程占有量;1分钟的是最能体现当前系统的负载量的参数。
12:08:10UP 44 MIN2 USERSLOAD AVERAGE0.060.040.05
当前系统时间启动至今时间目前登录用户数系统负载1分钟内系统负5分钟内系统负载15分钟内系统负载

USERTTYFROMLOGIN@IDLEJCPUPCPUWHAT
登录的用户登录的终端,tty1是本地登录,pts/0是远程终端登录登录来源(网络远程登录就是ip)用户登录的时间节点空闲了多久JCPU使用的时间PCPU使用的时间使用的命令

  • cat /proc/cpuinfo查看cpu核数

[root@zyshanlinux-01 ~]# cat /proc/cpuinfo  ##查看系统有多少个逻辑CPU
processor   : 0  ##数字0代表有1颗逻辑CPU,数字1代表2颗逻辑CPU。
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
stepping    : 3
microcode   : 0x22
cpu MHz     : 2393.631
cache size  : 6144 KB
physical id : 0
siblings    : 1
core id     : 0
cpu cores   : 1
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 dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm epb fsgsbase smep xsaveopt dtherm ida arat pln pts
bogomips    : 4788.92
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

  • vmstat监控系统状态

[root@zyshanlinux-01 ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 1580752   2076 163592    0    0  1232    50  301  608  3  7 82  9  0
  • 用法vmstat 1

[root@zyshanlinux-01 ~]# vmstat 1  ##动态每秒显示一次
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 1580768   2076 163664    0    0  1094    45  281  555  2  6 84  8  0
 0  0      0 1580752   2076 163664    0    0     0     0  111  130  0  0 100  0  0
 0  0      0 1580752   2076 163664    0    0     0     0  100  123  0  0 100  0  0
 0  0      0 1580752   2076 163664    0    0     0     0   80  107  0  1 99  0  0
  • 关键的几列: r , b , swpd , si , so , bi , bo , us , wa

参数解析:

注:系统负载很高,us%可以很低或0(可能在等待或者不运行);us%很高,系统负载肯定会很高。

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

[root@zyshanlinux-01 ~]# top
top - 17:07:16 up  5:43,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  88 total,   2 running,  86 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st  ##st被偷走的CPU
KiB Mem :  1875480 total,  1542804 free,   145232 used,   187444 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1545540 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                
    1 root      20   0  128208   6844   4084 S  0.0  0.4   0:01.77 systemd                                
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                               
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.24 ksoftirqd/0                            
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                           
    6 root      20   0       0      0      0 S  0.0  0.0   0:02.64 kworker/u128:0 

PID号可以通过命令来杀死对应PID号的进程。

参数解析:

TASKSTOTALRUNNINGSLEEPINGSTOPPEDZOMBIE
共多少进程正在运行的进程休眠的进程数停止进程僵尸进程,由于主进程意外终止,遗留下的子进程没人管,自生自灭

  • top -c显示详细的进程信息

  • top -bn1静态显示所有进程,适合写脚本的时候用

  • q退出,数字1显示所有核cpu,大写字母M按内存使用排序

  • 大写字母P按cpu使用排序

监控网卡流量

  • yum install -y sysstat

[root@zyshanlinux-01 ~]# sar  ##没有这个命令,就需要安装一个包
-bash: sar: 未找到命令
[root@zyshanlinux-01 ~]# yum install -y sysstat
已加载插件:fastestmirror
base                                                                                | 3.6 kB  00:00:00 
[root@zyshanlinux-01 ~]# sar  ##sar不加具体的选项或参数,它就会去调用它的历史文件
无法打开 /var/log/sa/sa10: 没有那个文件或目录  ##sar特性,每次正点10分钟去抓取系统状态保存在该目录的文件里

  • sar -n DEV 网卡流量

[root@zyshanlinux-01 ~]# sar -n DEV 2 3  ##每隔2秒显示一次,仅显示3次
Linux 3.10.0-693.el7.x86_64 (zyshanlinux-01)    2018年06月10日     _x86_64_    (1 CPU)
##时间         网卡名字  接收数据包  发送数据包 接收数据量 发送数据量
17时45分33秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
17时45分35秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
17时45分35秒     ens33      0.50      0.50      0.03      0.10      0.00      0.00      0.00

17时45分35秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
17时45分37秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
17时45分37秒     ens33      0.51      0.51      0.03      0.20      0.00      0.00      0.00

17时45分37秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
17时45分39秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
17时45分39秒     ens33      0.51      0.51      0.03      0.20      0.00      0.00      0.00

平均时间:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均时间:        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均时间:     ens33      0.50      0.50      0.03      0.17      0.00      0.00      0.00

注:接收数据包几千算是正常,上万或几十万的话就不正常,被攻击了。可以通过抓包工具验证是否被攻击。

[root@zyshanlinux-01 ~]# sar
Linux 3.10.0-693.el7.x86_64 (zyshanlinux-01)    2018年06月10日     _x86_64_    (1 CPU)

17时40分01秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
17时50分01秒     all      0.03      0.00      0.11      0.00      0.00     99.86
18时00分01秒     all      0.03      0.00      0.10      0.00      0.00     99.86
18时10分01秒     all      0.04      0.00      0.13      0.00      0.00     99.82
  • sar -f /var/log/sa/saxx 历史文件

[root@zyshanlinux-01 ~]# ls /var/log/sa/
sa10

[root@zyshanlinux-01 ~]#  sar -n DEV -f /var/log/sa/sa10
Linux 3.10.0-693.el7.x86_64 (zyshanlinux-01)    2018年06月10日     _x86_64_    (1 CPU)

17时40分01秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
17时50分01秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
17时50分01秒     ens33      0.22      0.04      0.01      0.01      0.00      0.00      0.00

注:当天/var/log/sa/里面不仅有一个sa10的文件还有sar10的文件,但只有到明天这个文件才会出现,两者有什么区别呢,sa10是二进制文件不能用cat直接看只能用sar -n DEV查看,sar10可以用cat直接查看。

  • sar -q 系统负载

[root@zyshanlinux-01 ~]# sar -q 1 3  ##每秒显示1次,仅显示3次
Linux 3.10.0-693.el7.x86_64 (zyshanlinux-01)    2018年06月10日     _x86_64_    (1 CPU)

23时17分11秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
23时17分12秒         0       105      0.02      0.07      0.05         0
23时17分13秒         0       105      0.02      0.07      0.05         0
23时17分14秒         0       105      0.02      0.07      0.05         0
平均时间:         0       105      0.02      0.07      0.05         0

  • sar -b 磁盘读写

[root@zyshanlinux-01 ~]# sar -b 1 3  ##每秒显示1次,仅显示3次
Linux 3.10.0-693.el7.x86_64 (zyshanlinux-01)    2018年06月10日     _x86_64_    (1 CPU)

23时18分37秒       tps      rtps      wtps   bread/s   bwrtn/s
23时18分38秒      0.00      0.00      0.00      0.00      0.00
23时18分39秒      0.00      0.00      0.00      0.00      0.00
23时18分40秒      0.00      0.00      0.00      0.00      0.00
平均时间:      0.00      0.00      0.00      0.00      0.00

  • nload命令

第一次用这个命令,会出现没有命令的错误,需要安装2个包才可以执行这个命令

  • yum install -y epel -release

  • yum install -y nload

[root@zyshanlinux-01 ~]# nload

Device ens33 [192.168.106.128] (1/2):
===========================================================================================================
Incoming:  ##进来的流量,被攻击的话,这个流量就会变得很大











                                                                         Curr: 944.00 Bit/s
                                                                         Avg: 1.05 kBit/s
                                                                         Min: 944.00 Bit/s
                                                                         Max: 1.87 kBit/s
                                                                         Ttl: 7.73 MByte
Outgoing:  ##出去的流量,买了100的带宽跑到70多了还是正常,跑满的就证明带宽不够了












                                                                         Curr: 8.29 kBit/s
                                                                         Avg: 8.30 kBit/s
                                                                         Min: 4.27 kBit/s
                                                                         Max: 8.78 kBit/s
                                                                         Ttl: 3.33 MByte

监控IO性能

  • iostat -x 磁盘使用

iostat和sar是同一个安装包的,都是sysstat,安装了这个包后,2个命令都可以用了。

[root@zyshanlinux-01 ~]# iostat -x 1 1  ##每秒显示1次,仅显示1次
Linux 3.10.0-693.el7.x86_64 (zyshanlinux-01)    2018年06月10日     _x86_64_    (1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.08    0.00    0.18    0.06    0.00   99.67

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util  ##%util重点参数,CPU等待IO硬盘时间的百分比,越大硬盘越差,硬盘太慢。这个参数和硬盘的读写参数是相关联的,这个百分比大读写数据也大,如果读写几乎为0,这个百分比仍然很大,硬盘异常
sdb               0.00     0.00    0.01    0.00     0.10     0.00    28.99     0.00    2.75    2.75    0.00   2.05   0.00
sda               0.00     0.01    0.16    0.09     6.86     3.25    80.63     0.02   74.62   14.22  177.01  11.07   0.28
scd0              0.00     0.00    0.00    0.00     0.03     0.00   114.22     0.00    0.83    0.83    0.00   0.72   0.00
dm-0              0.00     0.00    0.00    0.00     0.03     0.00    48.19     0.00    6.42    6.42    0.00   5.63   0.00

  • iotop 磁盘使用,查看占用磁盘最高的是哪个进程

[root@zyshanlinux-01 ~]# iotop  ##命令没有,需要安装下面的包才可以执行这个命令
-bash: iotop: 未找到命令
[root@zyshanlinux-01 ~]# yum install -y iotop

[root@zyshanlinux-01 ~]# 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                                     
    1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switched-root~system --deserialize 21
    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]
    5 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H]
    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 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/0]
   12 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kdevtmpfs]
   13 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [netns]
   14 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khungtaskd]
   15 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [writeback]
   16 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kintegrityd]
   17 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [bioset]
   18 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kblockd]
   19 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [md]
   25 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kswapd0]
   26 be/5 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksmd]
   27 be/7 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [khugepaged]
   28 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [crypto]
  547 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd-logind
   36 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthrotld]
  549 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % VGAuthService -s
  550 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % vmtoolsd
   39 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kpsmoused]
   41 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ipv6_addrconf]
  557 be/4 chrony      0.00 B/s    0.00 B/s  0.00 %  0.00 % chronyd
  564 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % rsyslogd -n [in:imjournal]
  565 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % rsyslogd -n [rs:main Q:Reg]
  566 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kdmflush]
  567 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [bioset]
   60 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [deferwq]
  522 be/3 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % auditd

  • free查看内存使用情况

[root@zyshanlinux-01 ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1875480      145684     1363624        8800      366172     1537304
Swap:       2097148           0     2097148

  • free -m / -g / -h

[root@zyshanlinux-01 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1831         142        1331           8         357        1501
Swap:          2047           0        2047
[root@zyshanlinux-01 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        142M        1.3G        8.6M        357M        1.5G
Swap:          2.0G          0B        2.0G
[root@zyshanlinux-01 ~]# free -g
              total        used        free      shared  buff/cache   available
Mem:              1           0           1           0           0           1
Swap:             1           0           1

  • buffer/cache区别

缓冲/缓存,cpu运算速度快,磁盘很慢,所以需要内存进行缓冲,以下就是数据不同流向的区别
[root@zyshanlinux-01 ~]# 0000(磁盘)-->内存(cache)-->cpu        ^C
[root@zyshanlinux-01 ~]# cpu(00000)  -->内存(buffer)--> 磁盘     ^C

  • 公式:total=used+free+buff/cache

  • availabel(真正的剩余物理内存)包含free和buffer/cache剩余部分

查看系统进程

  • ps查看系统进程

把你当前进程的快照打印出来,ps是一次性静态的把系统进程列出来,top则是实时动态的。

  • 用法:ps aux、ps -elf

[root@zyshanlinux-01 ~]#  ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.3 128208  6844 ?        Ss   23:08   0:01 /usr/lib/systemd/systemd --switched-root -
root         2  0.0  0.0      0     0 ?        S    23:08   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    23:08   0:00 [ksoftirqd/0]
root         4  0.2  0.0      0     0 ?        S    23:08   0:01 [kworker/0:0]
root         5  0.0  0.0      0     0 ?        S<   23:08   0:00 [kworker/0:0H]
root         6  0.0  0.0      0     0 ?        S    23:08   0:00 [kworker/u128:0]
root         7  0.0  0.0      0     0 ?        S    23:08   0:00 [migration/0]

[root@zyshanlinux-01 ~]# 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 - 32052 ep_pol 23:08 ?        00:00:01 /usr/lib/systemd/systemd --switc
1 S root         2     0  0  80   0 -     0 kthrea 23:08 ?        00:00:00 [kthreadd]
1 S root         3     2  0  80   0 -     0 smpboo 23:08 ?        00:00:00 [ksoftirqd/0]
1 S root         4     2  0  80   0 -     0 worker 23:08 ?        00:00:01 [kworker/0:0]
1 S root         5     2  0  60 -20 -     0 worker 23:08 ?        00:00:00 [kworker/0:0H]

[root@zyshanlinux-01 ~]# ps aux |grep mysql  ##查看该进程是否在运行
root      1298  0.0  0.0 112720   972 pts/0    R+   23:22   0:00 grep --color=auto mysql
[root@zyshanlinux-01 ~]# kill 1132  ##用PID号可以杀死该进程
[root@zyshanlinux-01 ~]# ps aux |grep qmgr  ##确认该进程确实是被杀死
postfix   1157  0.0  0.2  89756  4048 ?        S    23:08   0:00 qmgr -l -t unix -u
root      1300  0.0  0.0 112720   972 pts/0    R+   23:23   0:00 grep --color=auto qmgr

[root@zyshanlinux-01 ~]# ls -l /proc/521/  ##proc加上数字的文件夹就是进程的PID号
总用量 0
dr-xr-xr-x. 2 root root 0 6月  10 23:08 attr
-rw-r--r--. 1 root root 0 6月  10 23:23 autogroup
-r--------. 1 root root 0 6月  10 23:23 auxv

  • STAT部分说明

  • D 不能中断的进程

  • R run状态的进程

  • S sleep状态的进程

[root@zyshanlinux-01 ~]# ps aux |grep vmstat
root      1307  0.0  0.0 148356  1352 pts/0    S+   23:27   0:00 vmstat 1
root      1309  0.0  0.0 112720   972 pts/1    R+   23:27   0:00 grep --color=auto vmstat

  • T 暂停的进程

[root@zyshanlinux-01 ~]# ps aux |grep vmstat
root      1307  0.0  0.0 148356  1352 pts/0    T    23:27   0:00 vmstat 1
root      1311  0.0  0.0 112720   968 pts/1    R+   23:27   0:00 grep --color=auto vmstat

  • Z僵尸进程

数量较少,有就用命令杀死它。

  • < 高优先级进程

  • N 低优先级进程

  • L 内存中被锁了内存分页

  • s主进程

  • l 多线程进程

  • + 前台进程,都是在终端中显示

[root@zyshanlinux-01 ~]# ps aux |grep vmstat
root      1307  0.0  0.0 148356  1352 pts/0    S+   23:27   0:00 vmstat 1
root      1313  0.0  0.0 112720   968 pts/1    R+   23:28   0:00 grep --color=auto vmstat

查看网络状态

  • netstat 查看网络状态

  • netstat -lnp 查看监听端口

[root@zyshanlinux-01 ~]# 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      912/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1130/master         
tcp6       0      0 :::22                   :::*                    LISTEN      912/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1130/master         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           557/chronyd         
udp6       0      0 ::1:323                 :::*                                557/chronyd         
raw6       0      0 :::58                   :::*                    7           610/NetworkManager  
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path

  • netstat -an 查看系统tcp的网络连接状况

[root@zyshanlinux-01 ~]# 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.106.128:22      192.168.106.1:14605     ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
udp        0      0 127.0.0.1:323           0.0.0.0:*                          
udp6       0      0 ::1:323                 :::*                               
raw6       0      0 :::58                   :::*                    7          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     15871    /var/run/vmware/guestServicePipe
unix  2      [ ACC ]     STREAM     LISTENING     18867    private/proxymap

  • nestat -lntp 只看出tcp的,不包含socket

unix  2      [ ACC ]     STREAM     LISTENING     11901    1/systemd            /run/lvm/lvmpolld.socket
unix  2      [ ACC ]     STREAM     LISTENING     11915    1/systemd            /run/lvm/lvmetad.socket
[root@zyshanlinux-01 ~]# netstat -ltnp
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      912/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1130/master         
tcp6       0      0 :::22                   :::*                    LISTEN      912/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1130/master         
[root@zyshanlinux-01 ~]# netstat -ltunp
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      912/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1130/master         
tcp6       0      0 :::22                   :::*                    LISTEN      912/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1130/master         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           557/chronyd         
udp6       0      0 ::1:323                 :::* 

  • ss -an 和nestat 异曲同工,不足是不会显示进程的名字

    [root@zyshanlinux-01 ~]# ss -an |grep -i listen
    u_str  LISTEN     0      32     /var/run/vmware/guestServicePipe 15871                 * 0                  
    u_str  LISTEN     0      100    private/proxymap 18867                 * 0                  
    u_str  LISTEN     0      100    private/proxywrite 18870                 * 0                  
    u_seq  LISTEN     0      128    /run/udev/control 12110                 * 0                  
    u_str  LISTEN     0      128    /run/systemd/private 11855                 * 0                  
    u_str  LISTEN     0      128    /var/run/dbus/system_bus_socket 14716                 * 0 
    u_str  LISTEN     0      128    /run/lvm/lvmpolld.socket 11901                 * 0                  
    u_str  LISTEN     0      128    /run/lvm/lvmetad.socket 11915                 * 0                  
    u_str  LISTEN     0      100    public/pickup 18835                 * 0                  
    u_str  LISTEN     0      100    public/cleanup 18839                 * 0                  
    u_str  LISTEN     0      100    public/qmgr 18842                 * 0                  
    u_str  LISTEN     0      100    public/flush 18864                 * 0                  
    u_str  LISTEN     0      100    public/showq 18879                 * 0                  
    u_str  LISTEN     0      100    private/smtp 18873                 * 0                  
    u_str  LISTEN     0      100    private/tlsmgr 18846                 * 0                  
    u_str  LISTEN     0      100    private/rewrite 18849                 * 0                  
    u_str  LISTEN     0      100    private/bounce 18852                 * 0                  
    u_str  LISTEN     0      100    private/defer 18855                 * 0                  
    u_str  LISTEN     0      100    private/trace 18858                 * 0                  
    u_str  LISTEN     0      100    private/verify 18861                 * 0                  
    u_str  LISTEN     0      100    private/relay 18876                 * 0                  
    u_str  LISTEN     0      100    private/error 18882                 * 0                  
    u_str  LISTEN     0      100    private/retry 18885                 * 0                  
    u_str  LISTEN     0      100    private/discard 18888                 * 0                  
    u_str  LISTEN     0      100    private/local 18891                 * 0                  
    u_str  LISTEN     0      100    private/virtual 18894                 * 0                  
    u_str  LISTEN     0      100    private/lmtp 18897                 * 0                  
    u_str  LISTEN     0      100    private/anvil 18900                 * 0                  
    u_str  LISTEN     0      100    private/scache 18903                 * 0                  
    u_str  LISTEN     0      128    /run/systemd/journal/stdout 7665                  * 0                  
    tcp    LISTEN     0      128       *:22                    *:*                  
    tcp    LISTEN     0      100    127.0.0.1:25                    *:*                  
    tcp    LISTEN     0      128      :::22                   :::*                  
    tcp    LISTEN     0      100     ::1:25                   :::*         

    一个小技巧:直接查看以下数据的并发量。

  • netstat -an |awk '/^tcp/{++sta[$NF]} END {for(key in sta) print key,"\t",sta[key]}'

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

Linux下抓包

  • 抓包工具tcpdump

[root@zyshanlinux-01 ~]# yum install -y tcpdump  ##第一次运行这个命令要安装这个包

  • 用法:tcpdump -nn

  • tcpdump -nn -i ens33

[root@zyshanlinux-01 ~]# tcpdump -nn -i ens33  ##特殊的设备需要加上网卡名
08:41:31.158670 IP 192.168.106.1.1512 > 192.168.106.128.22: Flags [.], ack 802176, win 2048, length 0
08:41:31.158683 IP 192.168.106.128.22 > 192.168.106.1.1512: Flags [P.], seq 802356:802536, ack 157, win 251, length 180
##得出的结果都是数据包的流向,成对出现,还要关注一个重要参数length 180,长度参数。有种攻击是DDos,即udp flood洪水攻击,可以发出几百G流量攻击,只能用防攻击的设备或服务。

  • tcpdump -nn port 80

[root@zyshanlinux-01 ~]# tcpdump -nn -i ens33 port 22  ##指定22端口的
[root@zyshanlinux-01 ~]# tcpdump -nn -i ens33 not port 22  ##排除22端口的

  • tcpdump -nn not port 22 and host 192.168.0.100

[root@zyshanlinux-01 ~]# tcpdump -nn -i ens33 not port 22 and host 192.168.106.1  ##排除22端口,只要192.168.106.1 ip的

  • tcpdump -nn -c 100 -w 1.cap

[root@zyshanlinux-01 ~]# tcpdump -nn -i ens33 -c 10  ##只抓前10条

[root@zyshanlinux-01 ~]# tcpdump -nn -i ens33 -c 10 -w /tmp/1.cap  ##抓10个包写入1.cap里面,单开一个终端时这条命令可能会卡住,因为没有数据流动,所以没产生包,命令就卡住,可以重新开个终端运行写top或vmstat 1的命令产生些包,这条命令就会很快完成抓包任务。
tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
10 packets captured
10 packets received by filter
0 packets dropped by kerne

[root@zyshanlinux-01 ~]# file /tmp/1.cap  ##可以用file查看
/tmp/1.cap: tcpdump capture file (little-endian) - version 2.4 (Ethernet, capture length 262144)
[root@zyshanlinux-01 ~]# cat /tmp/1.cap  ##因为是抓的数据包,包的内容,cat你是看不懂得

[root@zyshanlinux-01 ~]# tcpdump -r /tmp/1.cap  ##可以用-r来看,就是读数据流;但实际上1.cap中是真真正正得写入数据包
reading from file /tmp/1.cap, link-type EN10MB (Ethernet)
08:59:45.166244 IP zyshanlinux-01.ssh > 192.168.106.1.wins: Flags [P.], seq 3728788926:3728789074, ack 2081729664, win 251, length 148
08:59:45.166661 IP 192.168.106.1.wins > zyshanlinux-01.ssh: Flags [.], ack 148, win 2047, length 0
08:59:51.222568 IP 192.168.106.1.49249 > 239.255.255.250.ssdp: UDP, length 129
08:59:51.323252 IP 192.168.106.1.49249 > 239.255.255.250.ssdp: UDP, length 129
08:59:52.388207 IP 192.168.106.1.wins > zyshanlinux-01.ssh: Flags [P.], seq 1:53, ack 148, win 2047, length 52
08:59:52.388769 IP zyshanlinux-01.ssh > 192.168.106.1.wins: Flags [P.], seq 148:200, ack 53, win 251, length 52
08:59:52.430645 IP 192.168.106.1.wins > zyshanlinux-01.ssh: Flags [.], ack 200, win 2053, length 0
08:59:52.508157 IP 192.168.106.1.wins > zyshanlinux-01.ssh: Flags [P.], seq 53:105, ack 200, win 2053, length 52
08:59:52.508666 IP zyshanlinux-01.ssh > 192.168.106.1.wins: Flags [P.], seq 200:252, ack 105, win 251, length 52
08:59:52.549433 IP 192.168.106.1.wins > zyshanlinux-01.ssh: Flags [.], ack 252, win 2052, length 0
  • yum install -y wireshark

    [root@zyshanlinux-01 ~]# yum install -y wireshark  ##第1次运行该命令需要安装

  • 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"

[root@zyshanlinux-01 ~]# 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"  ##需要线上的系统才能抓到,
tshark: -R without -2 is deprecated. For single-pass filtering use -Y.
Running as user "root" and group "root". This could be dangerous.
Capturing on 'nflog'
^C0 packets captured

下图就是线上服务器抓到的结果。可以知道有什么人访问哪个域名,访问了哪个链接等。

Linux网络相关

  • ifconfig 查看网卡ip(yum install net-tools)

[root@zyshanlinux-01 ~]# ifconfig -a  ##加-a选项,如果网卡宕机或者没有ip的时候是不显示的。

  • ifup ens33/ifdown ens33 启动或关闭ens33网卡,关闭网卡终端就会断开,需要到本地再次启动才行。应用场景:当某个网卡需要更改配置,就要重启服务,但又不想重启所有的网卡时,就可以用该命令指定网卡重启。

如果你的机房在国外,你单独执行关闭网卡命令,你就要去国外重启网卡;这时你就应该关闭和重启服务一起执行。

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

  • 设定虚拟网卡ens33:1

[root@zyshanlinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@zyshanlinux-01 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@zyshanlinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0
[root@zyshanlinux-01 network-scripts]# vi !$
vi ifcfg-ens33\:0
[root@zyshanlinux-01 network-scripts]# ifdown ens33;ifup ens33
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/8)
[root@zyshanlinux-01 network-scripts]# ifconfig  ##多出新的虚拟网卡ens33:0:
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.106.128  netmask 255.255.255.0  broadcast 192.168.106.255
        inet6 fe80::8fc3:bbdf:ba89:22a7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a1:d4:eb  txqueuelen 1000  (Ethernet)
        RX packets 54409  bytes 25525816 (24.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 209646  bytes 52121081 (49.7 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.106.150  netmask 255.255.255.0  broadcast 192.168.106.255
        ether 00:0c:29:a1:d4:eb  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 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  • mii-tool ens33 查看网卡是否连接

[root@zyshanlinux-01 network-scripts]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok  ##ok正常连接网线

  • ethtool ens33 也可以查看网卡是否连接

[root@zyshanlinux-01 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正常连接网线

  • 更改主机名 hostnamectl set-hostname aminglinux

[root@zyshanlinux-01 ~]# hostnamectl set-hostname zyshanlinux-001  
[root@zyshanlinux-01 ~]# hostname  ##更改主机名需要退出再重新登录,或者用hostname命令查看
zyshanlinux-001
[root@zyshanlinux-01 ~]# bash  ##用子shell也可查看
[root@zyshanlinux-001 ~]# exit
exit
[root@zyshanlinux-01 ~]# cat /etc/hostname  ##主机名的配置文件
zyshanlinux-001

  • DNS配置文件/etc/resolv.conf

[root@zyshanlinux-01 ~]# cat /etc/resolv.conf  ##查看DNS
# Generated by NetworkManager
nameserver 119.29.29.29
[root@zyshanlinux-01 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33  ##网卡的配置文件
##增加谷歌的DNS2=8.8.8.8
[root@zyshanlinux-01 ~]# ifdown ens33;ifup ens33
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/9)
[root@zyshanlinux-01 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
nameserver 8.8.8.8  ##可以看到新增的DNS
[root@zyshanlinux-01 ~]# vim /etc/resolv.conf  ##可以临时更改DNS,但重启后会被网卡的DNS配置文件覆盖

  • /etc/hosts文件

[root@zyshanlinux-01 ~]# cat /etc/hosts  ##查看配置文件
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@zyshanlinux-01 ~]# ping www.qq123.com  ##公网IP
PING www.qq.com (140.206.160.207) 56(84) bytes of data.
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=1 ttl=128 time=54.0 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=2 ttl=128 time=115 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=3 ttl=128 time=109 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=4 ttl=128 time=51.3 ms
^C
--- www.qq.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 51.387/82.591/115.483/29.951 ms
[root@zyshanlinux-01 ~]# vi /etc/hosts  ##可以指定自定义IP,多个域名指向一个IP空格隔开,多个IP指向一个域名只执行最后一个IP,以行为单位,不能换行。
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.106.150 www.qq123.com www.19.com www.zyshan.com
127.0.0.1 www.19.com

[root@zyshanlinux-01 ~]# ping www.qq123.com
PING www.qq123.com (192.168.106.150) 56(84) bytes of data.
64 bytes from www.qq123.com (192.168.106.150): icmp_seq=1 ttl=64 time=0.084 ms
64 bytes from www.qq123.com (192.168.106.150): icmp_seq=2 ttl=64 time=0.102 ms
64 bytes from www.qq123.com (192.168.106.150): icmp_seq=3 ttl=64 time=0.105 ms
64 bytes from www.qq123.com (192.168.106.150): icmp_seq=4 ttl=64 time=0.104 ms
64 bytes from www.qq123.com (192.168.106.150): icmp_seq=5 ttl=64 time=0.102 ms
^C
--- www.qq123.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.084/0.099/0.105/0.011 ms
[root@zyshanlinux-01 ~]# ping www.19.com  ##只执行最后一个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.044 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.098 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.097 ms
^C
--- www.qq123.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.044/0.079/0.098/0.027 ms

在window下不生效,只在linux本机下才能生效

C:\Users\zhengyushan>ping www.qq123.com

正在 Ping www.qq123.com [211.155.235.108] 具有 32 字节的数据:
来自 211.155.235.108 的回复: 字节=32 时间=49ms TTL=226
来自 211.155.235.108 的回复: 字节=32 时间=57ms TTL=226
来自 211.155.235.108 的回复: 字节=32 时间=55ms TTL=226
来自 211.155.235.108 的回复: 字节=32 时间=54ms TTL=226

211.155.235.108 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 49ms,最长 = 57ms,平均 = 53ms

C:\Users\zhengyushan>www.19.com
'www.19.com' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

拓展阅读:

tcp三次握手四次挥手(重点) http://www.doc88.com/p-9913773324388.html

tshark几个用法 http://ask.apelearn.com/question/995

延伸阅读 :

TCP 三次握手和四次挥手https://blog.csdn.net/qq_25677349/article/details/80468409

TCP 之三次握手和四次挥手http://baijiahao.baidu.com/s?id=1600656878316112806&wfr=spider&for=pc

TCP、UDP以及HTTP的简单讲解https://www.cnblogs.com/xiohao/p/4439164.html

认识cpu、核、进程与线程——备忘篇https://blog.csdn.net/zhengyshan/article/details/80641770

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值