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

27 篇文章 5 订阅

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

其中三个是从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

ifconfig eth0 192.168.0.232

启用eth0

ifconfig eth0 up

在这里插入图片描述

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

注意:

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

长期更改

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

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

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

所以对于文件的修改,与网络中大部分文章提供的都不相同
在这里插入图片描述

有个最简单的方法,找一个USB转网卡的转接头,然后插入到板子的USB,重启开机
在这里插入图片描述
会发现有以下的信息打印

在这里插入图片描述
所以答案都在 /etc/rc.local 文件中
在这里插入图片描述
/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

在这里插入图片描述
修改成功!

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值