Ubuntu中常用的网络命令指南
Ubuntu中常用的网络命令指南
在Ubuntu系统中,网络管理是日常运维和故障排查的核心技能。
🛠️ 基础网络诊断
-
ping
- 测试网络连通性ping google.com # 持续测试 ping -c 4 google.com # 发送4个包后停止
-
traceroute
/tracepath
- 追踪数据包路径traceroute github.com tracepath github.com # 无需root权限
-
mtr
- 实时网络质量分析(结合ping+traceroute)mtr -rw github.com # 生成报告并退出
🔍 网络配置查看
-
ip
- 全能网络工具(取代过时的ifconfig
)ip addr show # 查看所有接口IP ip route # 显示路由表 ip -s link # 查看接口统计信息
-
nmcli
- NetworkManager命令行控制nmcli device status # 查看设备状态 nmcli connection show # 显示所有连接
-
ss
- 查看套接字信息(替代netstat
)ss -tuln # 查看所有监听端口 ss -s # 统计摘要
📡 网络扫描与探测
-
nmap
- 端口扫描神器sudo nmap -sS 192.168.1.0/24 # 扫描局域网 sudo nmap -p 80,443 google.com # 指定端口扫描
-
dig
- DNS查询工具dig google.com A # 查询A记录 dig +short google.com # 简化输出
-
host
- 简易DNS查询host github.com host 140.82.121.3 # 反向DNS解析
🌐 网络连接测试
-
curl
- 数据传输工具curl -I https://ubuntu.com # 仅显示HTTP头 curl -o file.zip http://example.com/file.zip # 下载文件
-
wget
- 文件下载wget -c http://example.com/bigfile.iso # 断点续传
-
telnet
/nc
- 端口连通性测试telnet google.com 80 nc -zv google.com 443 # 快速测试端口
⚙️ 高级网络管理
-
tcpdump
- 网络抓包分析sudo tcpdump -i eth0 port 80 # 捕获eth0接口80端口流量 sudo tcpdump -w capture.pcap # 保存为pcap文件
-
iptables
- 防火墙配置sudo iptables -L # 列出规则 sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许SSH
-
ethtool
- 网卡诊断sudo ethtool eth0 # 查看网卡信息 sudo ethtool -s eth0 speed 1000 duplex full # 强制千兆全双工
🧩 其他实用工具
-
arp
- ARP缓存管理arp -n # 显示ARP表(禁用反向解析)
-
route
- 路由表管理(旧版,建议用ip route
)route -n # 显示数字格式路由表
-
hostname
- 主机名操作hostname -I # 显示所有IP地址
-
whois
- 域名注册信息查询whois ubuntu.com
-
ssh
- 远程登录ssh user@server -p 2222 # 指定端口连接
💡 使用技巧建议
- 组合命令示例:
ping -c 3 google.com && curl -I https://google.com
- 权限提示:涉及底层操作时需加
sudo
- 学习资源:
man ip # 查看命令手册 ip --help # 快速帮助
提示:网络故障排查标准流程:
ping
网关 → 2.ping
外部DNS → 3.nslookup
域名解析 → 4.traceroute
路径追踪
掌握这些命令后,你将能高效处理90%的Ubuntu网络问题。建议在测试环境中实操体验,逐步构建自己的网络工具箱!🚀