深色模式
网络基础命令 ip/ss/ping
摘要:网络不通是最高频的故障。本文讲清怎么看自己的 IP、谁在监听、能不能连到对端,是排障的第一步。
一、查看网卡与 IP
bash
ip addr # 所有网卡与 IP(替代旧 ifconfig)
ip -br addr # 简洁一行展示
ip route # 看路由表/默认网关1
2
3
2
3
二、查看端口监听
bash
ss -lntp # 监听中的 TCP 端口 + 进程
ss -tnp # 已建立的 TCP 连接
ss -s # 连接统计汇总1
2
3
2
3
三、测试连通性
bash
ping -c 4 8.8.8.8 # 测 IP 通不通
ping -c 4 www.example.com # 顺带验证 DNS
traceroute www.example.com # 看每一跳(路径)
mtr -n www.example.com # 持续探测丢包(更直观)1
2
3
4
2
3
4
四、临时改 IP(实验用)
bash
sudo ip addr add 192.168.1.100/24 dev eth0
sudo ip link set eth0 up1
2
2
ip 替代 ifconfig
ifconfig 已被弃用,新系统默认可能没有。统一用 ip 命令族。
验证
- [ ]
ip addr能看到你的 IP - [ ]
ss -lntp能看到 sshd 监听 22 - [ ]
ping -c 4 8.8.8.8有回包
常见坑
- ping 不通但服务能访问:可能对方禁 ICMP,不代表端口不通,用
telnet/nc测具体端口。 - IP 配了不生效:没
ip link set up或路由缺默认网关。 - ss 看不到进程:非 root 看不到其他用户的进程名,加
sudo。