Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?
share|improve this question
4  
This is an excellent idea, in my uneducated opinion. My biggest reason for waiting for other people to push me into ipv6 is that I can't fit a whole address in my memory. –  Basil May 7 at 20:16
    
@Basil: this isn't going to help you. The network prefix will be the same (you're on the same subnet) and it is much easier to e.g. just take the last part of the IPv4 address and configure that manually. One of my machines is 37.77.56.75, and the IPv6 subnet it is in is 2a00:8640:1:1::/64. This mechanism would give it 2a00:8640:1:1:37:77:56:75. I just give it 2a00:8640:1:1::75 manually. Much easier :) The example in the question uses fc00:: which is a non-existent subnet, which makes the idea look good. With real addresses this idea won't help that much, maybe even making it worse... –  Sander Steffann May 7 at 20:26
3  
Don't use fc00 that way. It is a reserved prefix, and the way you are using it is far from the way that prefix is intended to be used. The correct way to assign a unique local prefix is by taking one byte with value fd followed by five random bytes. The result could end up looking like fd94:951c:c5e4:: That prefix could be followed by a verbatim IPv4 address. fd94:951c:c5e4::192.0.2.42 is a perfectly valid IPv6 address. –  kasperd May 7 at 21:03
    
I am using an fd address -- I just used fc00 for illustrative purposes above. –  reedog117 May 7 at 21:19

3 Answers 3

up vote 3 down vote accepted

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 ?

share|improve this answer
    
This kinda works -- the awk statement up top grabs addr:192.168.0.1 so the first octet gets a little mangled since there's no separation between addr: and the address. I also changed the %x to %d since I actually want the decimal IP in the IPv6 address without any hex conversion. How do I separate the addr: from the IP? –  reedog117 May 7 at 20:15
    
Your comment makes this alot simpler, so i've edited my answer. Again, i've no idea if this is a good idea, nor really why you'd want to do it. I'd recommend just getting your DNS skills up so you dont need to be wrangling addresses by hand –  Sirex May 7 at 20:29
    
Excellent -- liking how streamlined this revised answer is! –  reedog117 May 7 at 20:33

Not an answer to your question, but some advice from someone with lots of IPv6 experience...

This idea might seem useful, but in practice it is much easier to e.g. just take the last part of the IPv4 address and configure that manually in ifcfg-eth0.

One of my machines is 83.247.10.7, and the IPv6 subnet it is in is 2001:9e0:803::/64. This mechanism would give it 2001:9e0:803:0:83:247:10:7. I just give it 2001:9e0:803::7 manually. Much easier :) Here is my ifcfg-eth0:

DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes

IPADDR=83.247.10.7
NETMASK=255.255.255.224

IPV6INIT=yes
IPV6ADDR=2001:9e0:803::7/64

Those two extra lines won't kill you, and are much better than some script that tries to do something this trivial automatically.

Your example in the question uses fc00:: which is a non-existent subnet which you won't see in real life, which does makes the idea look good. With real addresses this idea won't help that much, maybe even making it worse...

share|improve this answer
1  
I agree -- I do this in my network at home. I am doing this for another project, however, which may require a subnet bigger than a /24, so I figure preserving all the octets makes sense until I have a better handle of how large this project might become. I just used fc00:: in this example but am using something completely different in practice. –  reedog117 May 7 at 20:45

One suggestion for possible improvement.

Extract the subnet bits and host ID bits of the IPv4 address. Use the host ID bits as an offset for the IPv6 host; use the subnet bits as a lookup of some map for the appropriate IPv6 prefix to use. This way, you can have the flexibility of making your networking topology match.

But really this should all be part of an IPAM solution, and the VMs should be using DHCP and DHCPv6 to receive their addressing. But, as this is a virtual appliance, perhaps that is relying on infrastructure that you can't expect to have.

share|improve this answer
    
I think a later iteration of the project I'm working on may implement this. What makes things a bit difficult is that I don't know how many VMs (and IP addresses) I'm going to need, so I don't want to commit to a particular schema dependent upon subnet bits when I'm not sure how large an IPv4 subnet I'm going to need. –  reedog117 May 7 at 21:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.