在 Linux 系统中,联网通常指的是通过网络接口(如 eth0、wlan0 等)与互联网进行通信。以下是一些常见的 Linux 网络操作命令和方法,用于检查、配置和测试网络连接:
✅ 一、检查网络状态
1. 检查网络接口状态
ip a
# 或
ifconfig
作用:显示所有网络接口的状态(如 UP、DOWN、inet、inet6 等)。
2. 检查网络接口的 IP 地址
ip addr show
# 或
ifconfig
作用:显示当前网络接口的 IP 地址、子网掩码、网关等信息。
3. 检查网络连接状态
ping 8.8.8.8
# 或
ping -c 4 8.8.8.8
作用:测试与 Google 的连接是否正常。
✅ 二、配置网络(常用方式)
1. 使用 netplan(适用于 Ubuntu/Debian)
sudo nano /etc/netplan/01-network-manager-config.yaml
配置示例:
network:
version: 2
ethernets:
enp0s3:
dhcp4: yes
保存并退出后执行:
sudo netplan apply
2. 使用 ifconfig 或 ip 命令配置网络
sudo ip addr add 192.168.1.10/24 dev eth0
sudo ip link set eth0 up
✅ 三、测试网络连通性
1. 使用 traceroute 检查路由
traceroute google.com
2. 使用 curl 测试网页访问
curl -v https://www.google.com
3. 使用 nslookup 检查 DNS 解析
nslookup google.com
✅ 四、网络管理工具(推荐)
1. nmap 进行网络扫描
nmap -sP 192.168.1.0/24
2. ss 查看监听端口
ss -tuln
✅ 五、网络配置文件(/etc/network/interfaces 或 netplan)
- 适用于 Debian/Ubuntu:使用
netplan(推荐)。 - 适用于 older Debian/Ubuntu:使用
/etc/network/interfaces。
✅ 六、网络接口名称(常见)
eth0:以太网接口wlan0:无线接口veth0:虚拟接口lo:本地回环接口
✅ 七、网络服务(如 SSH、HTTP)
1. 启动 SSH 服务
sudo systemctl start ssh
sudo systemctl enable ssh
2. 启动 HTTP 服务
sudo systemctl start apache2
sudo systemctl enable apache2
✅ 八、网络管理工具(如 ifupdown、systemd)
systemd是现代 Linux 的网络管理工具,推荐使用。
✅ 九、网络故障排查
- 检查
iptables规则:sudo iptables -L -n - 检查
dhcpcd或systemd-networkd的状态:sudo systemctl status dhcpcd
✅ 十、网络接口的动态 IP(DHCP)
sudo dhclient eth0
✅ 总结
| 命令/工具 | 用途 |
|---|---|
ip a |
查看网络接口状态 |
ping |
测试网络连通性 |
traceroute |
检查路由 |
curl |
测试网页访问 |
netplan |
网络配置(推荐) |
ifconfig |
网络配置(旧版) |
如你有具体的网络配置需求(如设置静态 IP、配置 DNS、防火墙规则等),可以进一步提供信息,我可以为你定制解决方案。

