Linux – In Linux, how can I automatically assign an IPv6 address to an interface based upon its IPv4 address at boot time

ip addressipv6linuxlinux-networking

On a CentOS 6 machine, I have a server with 2 NICs, eth0 and eth1. eth0 has address 192.168.0.1.

I would like to create a script that parses the IP address of eth0 and assigns a similar-looking IPv6 address to eth1 at boot time.

In this case, I would like eth1 to have address fc00::192:168:0:1/64.

I know that in order to assign this address, I have to run the command

ip address add fc00::192:168:0:1/64 dev eth1

However, I'm trying to find a way to calculate the IP to be assigned to eth1 based upon the IP of eth0 and have this script run at boot-time.

The reason I'm doing this instead of statically assigning the addresses to both interfaces is that I'm attempting to turn this server into a VM template and automatically deploy it with new IP settings. Unfortunately the tool that I am using does not really have any support for IPv6.

So I guess my question has two parts:

  • How do I calculate the IPv6 address to assign?
  • How do I have this address calculated and assigned to eth1 at boot time?

Best Answer

Not sure this is even a good idea, but throwing caution to the wind, i'd do something like:

hexip='fe80::'
ip=$(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d '/' -f1 | sed 's/\./:/g')
hexip="${hexip}${ip}"

ip address add "$hexip/64" dev eth1 

You need it in some startup file that runs after runlevel 3. I think /etc/rc.local would do that ?