在Linux系统中,网卡配置主要通过命令行工具或配置文件实现,以下是常用的配置方法和命令:
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up # 配置IP和子网掩码
sudo ifconfig eth0 down # 禁用网卡
sudo ifconfig eth0 up # 启用网卡
sudo ip addr add 192.168.1.100/24 dev eth0 # 添加IP地址
sudo ip link set eth0 up # 启用网卡
sudo ip link set eth0 down # 禁用网卡
sudo ip route add default via 192.168.1.1 # 设置默认网关
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf # 临时修改DNS
/etc/network/interfaces
)sudo nano /etc/network/interfaces
示例配置:
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8
生效命令:
sudo systemctl restart networking # 重启网络服务
/etc/sysconfig/network-scripts/
)配置文件示例(ifcfg-eth0
):
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
生效命令:
sudo nmcli c reload # NetworkManager重载配置
sudo systemctl restart network # 传统方式重启网络
sudo nmcli con add con-name "my-eth0" ifname eth0 type ethernet ip4 192.168.1.100/24 gw4 192.168.1.1
sudo nmcli con mod "my-eth0" ipv4.dns "8.8.8.8"
sudo nmcli con up "my-eth0"
查看网卡信息
ip addr show # 查看所有网卡IP
ip link show # 查看网卡链路状态
ethtool eth0 # 查看网卡详细信息(速度、双工模式等)
测试网络连通性
ping 8.8.8.8
traceroute google.com
刷新DHCP
sudo dhclient -r eth0 # 释放IP
sudo dhclient eth0 # 重新获取IP
eth0
、ens33
、enp0s3
等)。systemd-networkd
的系统(如较新Ubuntu)需编辑/etc/systemd/network/
中的配置文件。cloud-init
配置。如果需要针对特定发行版或场景的详细配置,请提供更多信息!