当前位置:首页 > linux > 正文

linux怎么检测网络连接

  • linux
  • 2024-05-14 10:50:37
  • 1936

检测互联网连接

ping www.google.com -c 1

- ping 命令向指定主机发送一组 ICMP 请求。
- -c 1 选项指定发送 1 个请求,这对于快速检查连接是否正常已足够。
检测特定网络接口的连接

ip addr show dev eth0 | grep inet

- ip addr show dev eth0 显示 eth0 网络接口的详细信息。
- grep inet 过滤输出,仅显示具有有效 IP 地址的接口。
检测路由表

route -n

- route -n 显示路由表,其中包含到各种网络的目标和网关的信息。
- 如果路由表为空或不包含到 Internet 的网关,则表明网络连接存在问题。
其他有用的命令
- ifconfig:显示所有网络接口的状态和配置。
- tcpdump:捕获和分析网络流量。
- netstat -an:列出所有网络连接。
示例输出
成功连接:

ping google.com (142.250.189.132) 56(84) bytes of data.
64 bytes from 142.250.189.132: icmp_seq=1 ttl=56 time=26.6 ms
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 26.607/26.607/26.607/0.000 ms

无连接:

ping google.com (142.250.189.132) 56(84) bytes of data.
From 10.0.2.15 icmp_seq=1 Destination Host Unreachable