How to get the IPv6 IP address in Linux

I have a Linux server and I want to find the main IPv6 address via one single command line.

My command so far:

$ ip addr show dev eth0 | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d'

which shows:

2001:410:0:39:221:28ff:fe46:eef4
fe80::221:28ff:fe46:eef4

But I want only one occurrence, so that the output reads:

2001:410:0:39:221:28ff:fe46:eef4

Solution 1:

ip -6 addr

will show your IPv6 addresses.

Solution 2:

/sbin/ip -6 addr | grep inet6 | awk -F '[ \t]+|/' '{print $3}' | grep -v ^::1 | grep -v ^fe80

The output of /sbin/ip -6 addr | grep inet6 looks something like this:

inet6 ::1/128 scope host 
inet6 2001:123:456:55f::1/64 scope global 
inet6 fe80::62eb:69ff:fed2:d2a6/64 scope link 

awk -F '[ \t]+|/' '{print $3}' splits each line setting delimiters to be either one or more white spaces or a forward slash. The part we need is the $3.

grep -v ^::1 | grep -v ^fe80 to exclude any line starting with ::1 or fe80.

Solution 3:

$ /sbin/ifconfig | grep inet6
      inet6 addr: fe80::2ff:19ff:fe60:1a00/64 Scope:Link
      inet6 addr: fe80::211:22ff:fe33:4455/64 Scope:Link
      inet6 addr: ::1/128 Scope:Host

If you want a random selection of one IPv6 address

$ /sbin/ifconfig | grep inet6 | head -n 1
      inet6 addr: fe80::2ff:19ff:fe60:1a00/64 Scope:Link

If you want the IPv6 address of the most common name for the first Ethernet adapter

$ /sbin/ifconfig eth0 | grep inet6
      inet6 addr: fe80::2ff:19ff:fe60:1a00/64 Scope:Link

If you just want the address

$ /sbin/ifconfig eth0 |  awk '/inet6/{print $3}'
fe80::2ff:19ff:fe60:1a00/64

Solution 4:

Use the following command to view your IP address on Linux :

ifconfig

Normally, Ipv6 address looks like 2001:5c0:9168::/48 . If you are facing any conflict in your IP, follow the below steps to set an IP address again:

  1. To assign IPv6 IPs, make sure you have the iproute2 tools installed.
  2. Using them, let's start to assign your IPs.
  3. Make sure that the ipv6 module installed or not.

Then, use the following command to add new ip:

ip -f inet6 addr add 2001:5c0:9168::2/64 dev eth0

Afterwards, add default ip via

ip -f inet6 ro add default via 2001:5c0:9168::1 dev eth0

After completing your installation, just reconfigure/restart your IPv6 enabled services, like Apache, SSH etc.

Solution 5:

First, you need to remember that with IPv6 any machine may have several IPv6 addresses, and they may be on separate networks, and any of them might be used, depending on where you want to reach.

So, before you can answer the question of what is your source IP address you have to decide where you're sending the traffic. Then you can just ask Linux to tell you which IPv6 address will be the source when you send traffic to that destination.

If you're sending it to "the Internet" then just pick a global IPv6 address at random, e.g. Google's Public DNS address.

ip r get to 2001:4860:4860::8888 | perl -ne '/src ([\w:]+)/ && print "$1\n"'
2001:db8:f387:c818:5:2:0:1000

This asks Linux for the route to that destination. Perl parses the result looking for src and then prints the next field.

By providing a different destination, you may receive a different source address:

ip r get to ::1 | perl -ne '/src ([\w:]+)/ && print "$1\n"'
::1