天天看点

嵌入式Linux永久修改MAC和IP(特别适用多个网卡设计)临时更改长期更改

板子上根据设计需求设计了四个网口

其中三个是从USB hub扩展来的

使用了USB转网口的芯片

因为没有USB转网口电路中EEPROM的烧写工具,所以没有办法直接固化到里面MAC地址和IP之类的信息

所以要通过Linux的ifconfig进行配置

先介绍一种临时更改的方法

临时更改

所谓临时更改,是指在重新启动系统后就失效的短期更改方法,较简单。

Linux网卡的物理地址设置是在网络设置时进行的。

首先确定你在使用root用户。

假设对eth0进行设置。

对要修改MAC的网卡先“关闭”

ifconfig eth0 down
           

更换物理地址,把粗体部分更换为你要换的MAC地址:

ifconfig eth0 hw ether AA:BB:CC:DD:EE:FF
           

更换IP

启用eth0

ifconfig eth0 up
           
嵌入式Linux永久修改MAC和IP(特别适用多个网卡设计)临时更改长期更改

如果需要,请自行添加加入路由的命令。

注意:

如果还要设置IP等其它信息,一定要把hw ether 物理地址紧跟在设备名(如eth0)后面。

长期更改

这里指重启后仍能保留更改。

需要文本编辑器配置文件。

这里针对的Linux系统是嵌入式中的linux4.1.15,不是ubuntu或者centos等分发

所以对于文件的修改,与网络中大部分文章提供的都不相同

嵌入式Linux永久修改MAC和IP(特别适用多个网卡设计)临时更改长期更改

有个最简单的方法,找一个USB转网卡的转接头,然后插入到板子的USB,重启开机

嵌入式Linux永久修改MAC和IP(特别适用多个网卡设计)临时更改长期更改

会发现有以下的信息打印

嵌入式Linux永久修改MAC和IP(特别适用多个网卡设计)临时更改长期更改

所以答案都在

/etc/rc.local

文件中

嵌入式Linux永久修改MAC和IP(特别适用多个网卡设计)临时更改长期更改

/etc

文件夹下,打开

nano rc.local
           

主要修改的位置就是跟eth有关系的

添加如下的代码

遵循的思路也是先"关闭"网卡,然后修改mac和ip

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.


echo 30000 >  /proc/sys/vm/min_free_kbytes

ifconfig eth1 down
ifconfig eth1 hw ether AA:BB:CC:DD:EE:F1
ifconfig eth1 up

ifconfig eth2 down
ifconfig eth2 hw ether AA:BB:CC:DD:EE:F2
ifconfig eth2 up

ifconfig eth3 down
ifconfig eth3 hw ether AA:BB:CC:DD:EE:F3
ifconfig eth3 up


ifconfig eth0 192.168.0.232
ifconfig eth1 192.168.1.232
ifconfig eth2 192.168.2.232
ifconfig eth3 192.168.3.232

chmod 600 /usr/local/etc/ssh_host_dsa_key
chmod 600 /usr/local/etc/ssh_host_ecdsa_key
chmod 600 /usr/local/etc/ssh_host_ed25519_key
chmod 600 /usr/local/etc/ssh_host_rsa_key

/usr/local/sbin/sshd

exit 0

           

保存后退出

这个修改不会立刻改变

需要重启,然后执行

ifconfig
           
嵌入式Linux永久修改MAC和IP(特别适用多个网卡设计)临时更改长期更改

修改成功!

继续阅读