Linux笔记之网络工具与排错

Linux提供了一系列强大的网络工具,用于排查网络问题、监测网络性能和带宽。本章将详细讲解常用网络工具,包括pingtraceroutenslookupnetstat等,以及性能监测工具如iftopiperf,并结合实际命令与输出结果说明。


一、网络工具

1、ping工具

基本概念

ping通过ICMP协议检测网络连通性和延迟。它会向目标地址发送数据包,并记录回传时间。

常用命令
  • 测试目标主机是否可达:

    ping google.com
    
  • 限制发送次数:

    ping -c 4 google.com
    
实际操作

运行命令:

ping -c 4 google.com

输出结果示例

PING google.com (142.250.190.78) 56(84) bytes of data.
64 bytes from lhr48s08-in-f14.1e100.net (142.250.190.78): icmp_seq=1 ttl=116 time=9.36 ms
64 bytes from lhr48s08-in-f14.1e100.net (142.250.190.78): icmp_seq=2 ttl=116 time=9.25 ms
64 bytes from lhr48s08-in-f14.1e100.net (142.250.190.78): icmp_seq=3 ttl=116 time=9.18 ms
64 bytes from lhr48s08-in-f14.1e100.net (142.250.190.78): icmp_seq=4 ttl=116 time=9.29 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 9.181/9.272/9.362/0.077 ms

2、traceroute工具

基本概念

traceroute显示数据包从源主机到目标主机的路径及其每一跳的延迟。

常用命令
  • 跟踪到目标主机的路由:
    traceroute google.com
    
实际操作

运行命令:

traceroute google.com

输出结果示例

traceroute to google.com (142.250.190.78), 30 hops max, 60 byte packets
 1  192.168.1.1 (192.168.1.1)  1.123 ms  1.091 ms  1.076 ms
 2  * * *
 3  10.0.0.1 (10.0.0.1)  20.455 ms  20.423 ms  20.391 ms
 4  lhr14s44-in-f14.1e100.net (142.250.190.78)  30.522 ms  30.488 ms  30.455 ms

3、nslookup工具

基本概念

nslookup用于查询域名解析记录。

常用命令
  • 查询域名的A记录:

    nslookup google.com
    
  • 查询指定DNS服务器:

    nslookup google.com 8.8.8.8
    
实际操作

运行命令:

nslookup google.com

输出结果示例

Server:         192.168.1.1
Address:        192.168.1.1#53

Non-authoritative answer:
Name:   google.com
Address: 142.250.190.78

4、netstat工具

基本概念

netstat显示网络连接、端口状态及网络统计信息。

常用命令
  • 显示所有网络连接:

    netstat -a
    
  • 查看网络统计信息:

    netstat -s
    
实际操作

运行命令:

netstat -a

输出结果示例

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 192.168.1.100:22        192.168.1.200:54578     ESTABLISHED

二、网络带宽与性能监测

1、iftop工具

基本概念

iftop实时显示网络接口的带宽使用情况。

常用命令
  • 监测指定接口流量:

    sudo iftop -i eth0
    
  • 显示发送和接收带宽:

    sudo iftop -n
    
实际操作

运行命令:

sudo iftop -i eth0

输出结果示例(实时变化):

            192.168.1.100                           =>     8.8.8.8        2.01Kb  1.89Kb  1.56Kb
                                                 <=                            2.45Kb  2.31Kb  2.01Kb

2、iperf工具

基本概念

iperf用于测试网络带宽性能。

常用命令
  • 在服务器端启动:

    iperf -s
    
  • 在客户端测试带宽:

    iperf -c <server_ip>
    
实际操作

服务器端运行:

iperf -s

客户端运行:

iperf -c 192.168.1.100

输出结果示例

[  3] local 192.168.1.200 port 5001 connected with 192.168.1.100 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec  112 MBytes  93.7 Mbits/sec

三、小结

通过本章学习,你已经掌握以下内容:

  1. 网络工具

    • 使用ping测试连通性。
    • 使用traceroute跟踪路由。
    • 使用nslookup查询域名解析。
    • 使用netstat查看网络连接。
  2. 网络带宽与性能监测

    • 使用iftop实时监控带宽使用。
    • 使用iperf测试网络带宽性能。

这些工具为Linux网络排错和优化提供了强大支持。在实际运维中,灵活运用这些工具可以快速定位网络问题并优化网络性能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人间酒中仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值