在 Linux 系统上设置上网(网络配置)通常涉及以下几个方面:
一、查看网络接口状态
ip a # 显示所有网络接口的状态
ifconfig # 显示网络接口信息(适用于旧版系统)
二、配置网络接口(ifconfig 或 ip 命令)
1. 使用 ip 命令(推荐)
sudo ip a add <IP地址>/<子网掩码> dev <接口名>
sudo ip link set <接口名> up
示例:
sudo ip a add 192.168.1.10/24 dev eth0
sudo ip link set eth0 up
2. 使用 ifconfig(适用于旧系统)
sudo ifconfig eth0 up
sudo ifconfig eth0 192.168.1.10 netmask 255.255.255.0
三、配置静态 IP(/etc/network/interfaces 或 netplan)
1. 使用 netplan(适用于 Ubuntu/Debian)
编辑 /etc/netplan/01-network-manager-default.yaml:
network:
version: 2
renderer: network-manager
ethernets:
eth0:
dhcp4: no
addresses: [192.168.1.10/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
应用配置:
sudo netplan apply
2. 使用 /etc/network/interfaces(适用于 Debian/Ubuntu 旧版)
auto eth0
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
nameserver 8.8.8.8
应用配置:
sudo ifup eth0
四、配置 DNS
1. 修改 /etc/resolv.conf(推荐)
sudo echo "nameserver 8.8.8.8" > /etc/resolv.conf
2. 使用 networkmanager(推荐)
sudo systemctl enable NetworkManager
sudo systemctl start NetworkManager
五、测试网络连接
ping 8.8.8.8
六、配置代理(可选)
sudo nano /etc/environment
在 PATH 中添加:
export http_proxy=http://your-proxy:port
export https_proxy=http://your-proxy:port
或使用 proxychains 工具。
七、查看网络接口信息
ip a show
八、网络管理工具(推荐)
- NetworkManager:图形化界面管理网络
- ifupdown:旧版系统中用于管理网络的工具
九、防火墙设置(iptables 或 ufw)
sudo ufw enable
sudo ufw allow ssh
十、网络状态监控
nmcli dev status # NetworkManager 的状态
ip a show # 显示所有网络接口状态
总结
| 操作 | 命令/工具 |
|---|---|
| 查看网络接口 | ip a / ifconfig |
| 配置静态 IP | ip a add / netplan / /etc/network/interfaces |
| 配置 DNS | /etc/resolv.conf / networkmanager |
| 配置代理 | proxychains / environment |
| 管理防火墙 | ufw / iptables |
| 测试网络 | ping |
如果你有特定的 Linux 发行版(如 Ubuntu、Debian、CentOS、Fedora),我可以提供更具体的配置方法。需要我帮你配置某个发行版吗?

