# set MAC address at boot



## bigearsbilly (May 16, 2012)

I cannot figure out how to set MAC at boot.

Any help please.

I've all sorts in rc.conf:


```
#ifconfig_em0="link be:ef:be:ef:be:ef"
#ifconfig_em0="inet 193.35.157.168  netmask 255.255.255.0"
#ifconfig_em0="inet 193.35.157.168 netmask 255.255.255.0 link 2b:3b:4b:5b:6b:7b"
```
*C*an't set link and inet at the same time it seems.


----------



## acheron (May 16, 2012)

You can create a file named /etc/start_if.em0 with the following content:

```
lladdr be:ef:be:ef:be:ef
```


----------



## generic (May 16, 2012)

What about:


```
ifconfig_em0="inet 10.0.0.2 netmask 255.255.255.0 ether aa:bb:cc:dd:ee:ff"
```


----------



## bigearsbilly (May 16, 2012)

generic said:
			
		

> What about:
> 
> 
> ```
> ...



No, I tried that, it says:


```
sudo ifconfig em0 inet 193.35.157.168 netmask 255.255.255.0 link 2b:3b:4b:5b:6b:7bifconfig: link: bad value
```

I tried doing over two lines in rc.conf but that didn't work.

We have a customer blowing up the phone! I'm not a real sysadmin you see, I am supposed to be a programmer!

I will try the next suggestion.


----------



## acheron (May 16, 2012)

What about this:

```
ifconfig_em0="ether be:ef:be:ef:be:ef"
ipv4_addrs_em0="193.35.157.168/24"
```


----------



## SirDice (May 16, 2012)

Why does your client want to change the MAC address?


----------



## DutchDaemon (May 16, 2012)

bigearsbilly said:
			
		

> ```
> #ifconfig_em0="inet 193.35.157.168 netmask 255.255.255.0 link 2b:3b:4b:5b:6b:7b"
> ```



Try setting the link in an alias, not in the same ifconfig line:


```
ifconfig_em0="inet 193.35.157.168 netmask 255.255.255.0"
ifconfig_em0_alias0="link 2b:3b:4b:5b:6b:7b"
```


----------



## bigearsbilly (May 16, 2012)

acheron said:
			
		

> You can create a file named /etc/start_if.em0 with the following content :
> 
> ```
> lladdr be:ef:be:ef:be:ef
> ```



nope, that didn't work either I'm afraid.

I did this which worked:



```
[billy@penguin:0]$ cat /etc/start_if.em0 
ifconfig em0 ether be:ef:be:ef:be:ef
```

Though it's moot now as I've abandoned this line of action.
Still it was interesting.


----------



## bigearsbilly (May 16, 2012)

DutchDaemon said:
			
		

> Try setting the link in an alias, not in the same ifconfig line:
> 
> 
> ```
> ...



This appeared to work OK but the machine was a bit odd. The *ifconfig* showed it worked ok, it looked fine sitting on the console but strangely on the same network segment I could not ssh on to it. I could ssh _from_ it but not _to_ it. I think I'll go home and have a beer.

edit: I tried reversing them, doing the link first and the address as the alias. Same result.


----------



## phoenix (May 16, 2012)

You need to clear the ARP table of whatever machine you are trying to SSH *from*, so that it picks up the new MAC address.


----------



## ecazamir (May 19, 2012)

bigearsbilly said:
			
		

> I did this which worked:
> 
> 
> 
> ...



This is the proper way. /etc/start_if.name should contain full commands.
It should work this way too:

```
/etc/rc.conf
# up is placed after ether/lladdr/link
ifconfig_em0="ether 00:11:22:33:44:55 up"
# assign IP addresses with ipv4_addrs_ifname
ipv4_addrs_em0="192.0.2.1/24 192.0.2.2/32 ..."
```
_link_, _lladdr_ and _ether_ have the same meaning for /sbin/ifconfig.
Older FreeBSD versions did run /etc/rc.d/rc.early if it existed, at an early boot stage. Current versions should use /etc/start_if.name or a custom script placed in /etc/rc.d, aware of the ordering mechanism of rc(8).


----------



## bigearsbilly (May 20, 2012)

Thank you I will try this on Monday.

I looked in the handbook and the Absolute Free*BSD* book but couldn't find a reference for this.
Thanks for the help though everybody.


----------



## UNIXgod (May 20, 2012)

ecazamir said:
			
		

> This is the proper way. /etc/start_if.name should contain full commands.
> It should work this way too:
> 
> ```
> ...



startif files can contain $1 which triggers the device name after the dot.

example:

```
/sbin/ifconfig $1 alias ...
```

This makes the files portable to being used on other machines with different NICs (the device name after the dot will need to be changed though).

I like this way as well vs using rc.conf as it's not only portable but also easy scripted.

One example is: I have a partially automated script that creates a jail, creates and adds an alias to startif for persistence as well as setting up the jail in a pf NAT all in one step.


----------



## bigearsbilly (May 21, 2012)

I think next time somebody asks me to mess about with a MAC address I will tell them to bog off!


----------



## kpa (May 21, 2012)

Can you reveal why you were asked to change the MAC address? I can't think of that many real world uses for changing it.


----------



## bigearsbilly (May 21, 2012)

Well, we swapped a lower spec machine out and swapped IP address to avoid reconfiguration.

After which port 80 stopped working. It was fine on the old box. (Both FreeBSD 8.2 set up by me this year).

We don't own the router so someone suggested it would possibly be the MAC address IP combination, so I thought what the heck try it.

I never expected it to work but I was interested in trying it as an exercise.
I'm not really an admin, I'm a hacker really but we are a small company and I have to do it all.


----------



## kpa (May 21, 2012)

Well yes, that's the usual ARP cache problem when you replace a network card and use the same IP address as before. The upstream router refuses to deal with the (new MAC, IP address) -pair until the old entry has expired.


----------



## bigearsbilly (May 21, 2012)

Hmm something like that maybe. It was up and running quite a while though, more than a week before it came to light.
The router's connected to some sort of leased line we have no control over it.
So I'm told anyway.


----------



## SirDice (May 21, 2012)

Then the issue had nothing to do with the MAC address.


----------



## Uniballer (May 21, 2012)

kpa said:
			
		

> Well yes, that's the usual ARP cache problem when you replace a network card and use the same IP address as before. The upstream router refuses to deal with the (new MAC, IP address) -pair until the old entry has expired.



And that is one of the reasons why a node should perform a gratuitous ARP for its own address when it starts: the router should learn the new address from that event (detecting multiple uses of an IP address is not usually as important).


----------

