关于/etc/network/interface 多网卡设置dhcp和vlan

本文详细介绍了在米尔IMX6UL系统上如何配置四个网卡eth0, eth0:1, eth1, eth1:1,其中eth1作为DHCP客户端,其他为静态IP,并设置了VLAN和相应的网关及路由。特别提到了静态IP用于内网通信,避免影响DNS访问的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# Wired or wireless interfaces
auto eth0
iface eth0 inet static
    address 192.168.144.25
    netmask 255.255.255.0

auto eth0:1
iface eth0:1 inet static
    address 192.168.36.25
    netmask 255.255.255.0

auto eth1
iface eth1 inet dhcp

auto eth1:1
iface eth1:1 inet static
    address 192.168.72.25
    netmask 255.255.255.0

如上会产生eth0,eth0:1,eth1,eth1:1四个网卡,其中eth1为dhcp client,其他三个为静态ip,eth0:1和eth1:1为VLAN,同理可以有eth0:2,eth1:2...

需要配置网关和路由如下:

iface eth0 inet static
    address 192.168.79.25
    netmask 255.255.255.0
    network 192.168.79.0
    gateway 192.168.79.1

因为我的固定IP仅用于内网通信,为了避免填写网关后可能修改/etc/resolve.conf造成无法访问DNS服务器没填写。具体内部原理未考证。

auto应该是开机自动配置,没有auto开机不回配置,这块是猜的,具体没考证。

本设置在米尔imx6ul系统上测试成功。仅供参考。

### 创建虚拟网络接口并配置 VLAN 及自定义 MAC 地址 #### 配置环境准备 为了在 Debian 系统上创建虚拟网络接口并将该接口加入特定的 VLAN 中,同时设置自定义的 MAC 地址,需要编辑 `/etc/network/interfaces` 文件来定义新的网络接口。 对于基于 Debian 的系统来说,可以通过修改上述文件实现对物理网卡或已存在的逻辑网卡添加子接口用于 VLAN 支持[^2]。具体操作如下: #### 编辑 Network Interfaces 文件 打开终端命令行工具,使用文本编辑器(如 nano 或 vim)编辑 `/etc/network/interfaces` 文件,在其中增加相应的条目以声明新创建的 VLAN 接口及其属性。假设要为 `eth0` 物理网卡创建 ID 为 10 的 VLAN 子接口,并指定静态 IP 地址自定义 MAC 地址,则可以按照下面的方式编写配置项: ```bash auto lo iface lo inet loopback allow-hotplug eth0 iface eth0 inet dhcp # Define the VLAN interface on top of eth0 with VID=10. auto eth0.10 iface eth0.10 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1 hwaddress ether 00:11:22:33:44:55 # Customized MAC Address here vlan-raw-device eth0 # Specifies which physical device this VLAN is based upon ``` 保存更改后的文件并退出编辑器。此时已经完成了基本的配置工作;但是为了让这些改动生效还需要安装额外的支持软件包——`vlan` 工具集,它提供了管理 Linux 下 IEEE 802.1Q VLAN 所需的功能。 #### 安装 Vlan 软件包支持服务 执行以下指令完成必要的依赖关系安装: ```bash apt-get update && apt-get install vlan -y modprobe 8021q # Load kernel module for VLAN support immediately without rebooting echo "8021q" >> /etc/modules # Ensure that the 8021q module loads at boot time automatically ifdown eth0 # Bring down parent interface before bringing up child interfaces vconfig add eth0 10 # Create VLAN sub-interface using vconfig command line tool ifup eth0.10 # Activate newly created VLAN interface according to settings defined earlier in /etc/network/interfaces file ``` 以上步骤完成后,应该能够成功地在一个名为 `eth0.10` 的新 VLAN 接口中获得预期的 IP 地址分配以及所设定好的硬件地址了。 #### 使用 ip 命令替代 vconfig 值得注意的是,现代 Linux 发行版更倾向于推荐使用 `ip link` 来代替传统的 `vconfig` 方法来进行 VLAN 操作。如果希望采用这种方式的话,可以用下列命令序列取代上面提到的部分: ```bash ip link add link eth0 name eth0.10 type vlan id 10 # Add a VLAN tagged interface named eth0.10 over existing eth0 NIC ip addr flush dev eth0 # Remove any previous addresses from base interface just in case they interfere ip addr add 192.168.1.100/24 brd + dev eth0.10 # Assign desired IPv4 configuration directly via CLI instead of relying solely on config files ip link set dev eth0.10 address 00:11:22:33:44:55 # Set custom hardware (MAC) address for our new virtual network adapter ip link set dev eth0.10 up # Enable the newly configured VLAN interface so it can start communicating across the network ``` 这样做的好处是可以即时看到效果而不需要重新启动整个机器或者单独的服务进程即可让变更立即起作用。 #### 验证配置成果 最后一步是要验证所有的设置都正常运作。这通常涉及到检查当前活动状态下的所有网络适配器列表、确认它们各自的参数是否符合预期等动作。可利用 `ip a s` 查看详细的网络接口信息,确保刚刚建立起来的那个带有特殊 MAC 地址标记的新成员确实存在并且处于激活状态下运行良好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值