How do I enable IPv6 in RHEL 7.4 on Amazon EC2

Solution 1:

To configure DHCPv6 on RHEL 7.4 or CentOS 7

  1. Connect to your instance using the instance's public IPv4 address.
  2. Using a text editor of your choice, create a custom file, for example:

    /etc/cloud/cloud.cfg.d/99-custom-networking.cfg

  3. Add the following lines to your file, and save your changes:

    network:
      version: 1
      config:
      - type: physical
        name: eth0
        subnets:
          - type: dhcp6
    
  4. Reboot your instance.

  5. Reconnect to your instance and use the ifconfig command to verify that the IPv6 address is configured on the network interface.

Source: https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-migrate-ipv6.html#ipv6-dhcpv6-rhel

Solution 2:

I found a good solution to this. I've been using terraform to launch and do initial configuration of my instances using cloud-init user-data, and the following EC2 user-data file works well for me:

#cloud-config
preserve_hostname: false
hostname: centos-01
fqdn: centos-01.example.com
manage_etc_hosts: true
write_files:
  - path: /etc/cloud/cloud.cfg.d/99-custom-networking.cfg
    owner: root:root
    permissions: 0600
    content: |
      network:
        version: 1
        config:
        - type: physical
          name: eth0
          subnets:
            - type: dhcp
            - type: dhcp6
power_state:
  mode: reboot
  delay: now
  message: Rebooting post-config
  timeout: 30
  condition: True

The important parts of this config are the write_files section, which installs the appropriate cloud-init config to enable IPv6, and then power_state, which triggers an immediate post-config reboot, which then applies the network config changes.

After reboot, this is what the network config looks like:

[centos@centos-01 ~]$ cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=dhcp
DEVICE=eth0
DHCPV6C=yes
IPV6INIT=yes
IPV6_AUTOCONF=no
ONBOOT=yes
TYPE=Ethernet
USERCTL=no

I'm happy with the solution, although I'm not entirely sure why I couldn't simply specify the network config in my user-data directly rather than having to write it to a file and reboot. At any rate, this works and is rather painless.