在Linux系统中,可以使用ifconfig命令(传统方式)或更现代的ip命令来配置虚拟网卡(也称为网络别名接口)。以下是详细步骤和说明:
ifconfig(传统方式,部分新系统可能已弃用)虚拟网卡通常以 eth0:0, eth0:1 等形式命名(基于物理网卡 eth0 的别名)。
sudo ifconfig eth0:0 192.168.1.100 netmask 255.255.255.0 up
eth0:0:虚拟网卡名称(主网卡是 eth0)。192.168.1.100:分配给虚拟网卡的IP地址。up:立即激活接口。ifconfig -a | grep eth0:
或直接运行:
ifconfig
查看是否出现 eth0:0 的配置。
sudo ifconfig eth0:0 down
ip 命令(推荐新方式)现代Linux系统推荐使用 ip 命令(属于 iproute2 工具包)。
sudo ip addr add 192.168.1.100/24 dev eth0 label eth0:0
192.168.1.100/24:IP地址和子网掩码(CIDR格式)。label eth0:0:为虚拟接口设置别名。sudo ip link set eth0:0 up
ip addr show eth0
或:
ip addr show | grep eth0:
sudo ip addr del 192.168.1.100/24 dev eth0:0
临时配置重启后会失效,需通过系统网络配置文件永久生效(不同Linux发行版路径可能不同):
编辑 /etc/network/interfaces:
auto eth0:0
iface eth0:0 inet static
address 192.168.1.100
netmask 255.255.255.0
然后重启网络服务:
sudo systemctl restart networking
在 /etc/sysconfig/network-scripts/ 下创建文件 ifcfg-eth0:0:
DEVICE=eth0:0
IPADDR=192.168.1.100
NETMASK=255.255.255.0
ONBOOT=yes
然后重启网络:
sudo systemctl restart network
sudo 或以 root 用户操作。ifconfig 默认可能未安装(需安装 net-tools 包)。ip 命令。如果有其他需求(如配置IPv6或绑定MAC地址),可进一步调整参数。