Get local IP Address in Linux using GO [4 Methods]


Written By - admin
Advertisement

Different methods to get local IP Address in Linux using golang. Here we will explore some of the possible ways to get both IPv4 and IPv6 addresses on all available interfaces on your local Linux setup with examples:

  1. Using net.InterfaceAddrs()
  2. Using net.Interfaces()
  3. Using net.LookupHost()
  4. Using net.Dial()

 

Method-1: Using net.InterfaceAddrs() function

We can use the net.InterfaceAddrs() function from the net package to. Here's an example to get both IPv4 and IPv6 addresses only:

package main

import (
	"fmt"
	"net"
)

func main() {
	// get list of available addresses
	addr, err := net.InterfaceAddrs()
	if err != nil {
		fmt.Println(err)
		return
	}

	for _, addr := range addr {
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			// check if IPv4 or IPv6 is not nil
			if ipnet.IP.To4() != nil || ipnet.IP.To16 != nil {
				// print available addresses
				fmt.Println(ipnet.IP.String())
			}
		}
	}
}

Here we are using ipnet.IP.To4() != nil to check if the address is IPv4 and ipnet.IP.To16() to check if it is IPv6. Also to ignore the loopback address we are using ipnet.IP.IsLoopback() function.

Output:

# go run main.go 
10.39.251.148
10.39.251.169
192.168.138.16
172.17.0.1
192.168.49.1
2a00:8a00:4000:6191::a
fe80::f816:3eff:fee0:4d4c
2a00:8a00:4000:6009::b:cf91
fe80::f816:3eff:fe6c:23e3
fe80::f816:3eff:fece:84ee
fe80::1476:a5ff:fe58:55cb

 

Method-2: Using net.Interfaces() function

We can also use the net.Interfaces() function from the net package to get a list of all network interfaces, and then use the Addrs() method on each interface to get its addresses.

package main

import (
	"fmt"
	"net"
)

func main() {
	intfaces, err := net.Interfaces()
	if err != nil {
		fmt.Println(err)
		return
	}

	// get list of interfaces
	for _, i := range intfaces {
		addrs, err := i.Addrs()
		if err != nil {
			fmt.Println(err)
			continue
		}

		// get both IPv4 and IPv6 address per interface
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPAddr:
				ip = v.IP
			case *net.IPNet:
				ip = v.IP
			default:
				continue
			}
			// print the available ip addresses
			fmt.Println(ip.String())
		}
	}
}

Output:

# go run main.go 
127.0.0.1
::1
10.39.251.148
2a00:8a00:4000:6191::a
fe80::f816:3eff:fee0:4d4c
10.39.251.169
2a00:8a00:4000:6009::b:cf91
fe80::f816:3eff:fe6c:23e3
192.168.138.16
fe80::f816:3eff:fe64:3d5
172.17.0.1
fe80::42:9cff:fe4e:169a
192.168.49.1
fe80::42:c3ff:fea7:e4cb
fe80::1476:a5ff:fe58:55cb

Here we are not checking for loopback addresses but if you want you can check the same using IsLoopback() method in the same code.

 

Method-3: Using os.Hostname() and net.LookupHost() function

Next we will use os.Hostname() function to get the hostname of the machine, and then use the net.LookupHost() function to lookup the IP addresses associated with the hostname. But you have to understand that this method relies on DNS resolution so either need a working DNS server or you can setup your /etc/resolv.conf to perform localhost lookup.

Advertisement
package main

import (
	"fmt"
	"net"
	"os"
)

func main() {
	// get hostname
	hostname, err := os.Hostname()
	if err != nil {
		panic(err)

	}

	// perform lookup of the hostname
	addrs, err := net.LookupHost(hostname)
	if err != nil {
		panic(err)
	}

	// fetch ip address details
	for _, addr := range addrs {
		fmt.Println(addr)
	}
}

Output:

# go run main.go 
fe80::f816:3eff:fee0:4d4c%eth0
fe80::f816:3eff:fe6c:23e3%eth1
fe80::f816:3eff:fece:84ee%eth2
fe80::f816:3eff:fe64:3d5%eth3
fe80::42:9cff:fe4e:169a%docker0
fe80::42:c3ff:fea7:e4cb%br-40e59d9a39a3
fe80::1476:a5ff:fe58:55cb%vethcc9c196
2a00:8a00:4000:6191::a%eth0
2a00:8a00:4000:6009::b:cf91%eth1
10.39.251.148
10.39.251.169
192.168.138.16
172.17.0.1
192.168.49.1

 

Method-4: Using net.InterfaceByName() and Addrs() function

We will useĀ net.InterfaceByName() along with Addrs() method on the interface to get its addresses. Here's an example for printing both the interface name and IP address:

package main

import (
	"fmt"
	"net"
)

func main() {
	interfaces, err := net.Interfaces()
	if err != nil {
		panic(err)
	}

	// get list of available interfaces
	for _, i := range interfaces {
		fmt.Println("Following addresses configured for ", i.Name)
		// get list of addresses mapped to each interface
		// using Addrs() method on the interface
		addrs, err := i.Addrs()
		if err != nil {
			fmt.Println(err)
			continue
		}

		// iterate through the interface list
		for _, addr := range addrs {
			var ip net.IP
			// check the type of the address and 
			// assign it to the variable ip of type net.IP
			switch v := addr.(type) {
			case *net.IPAddr:
				ip = v.IP
			case *net.IPNet:
				ip = v.IP
			default:
				continue
			}
			// Print the IP address
			fmt.Println("\t", ip.String())
		}
	}
}

Output

# go run main.go 
Following addresses configured for  lo
         127.0.0.1
         ::1
Following addresses configured for  eth0
         10.39.251.148
         2a00:8a00:4000:6191::a
         fe80::f816:3eff:fee0:4d4c
Following addresses configured for  eth1
         10.39.251.169
         2a00:8a00:4000:6009::b:cf91
         fe80::f816:3eff:fe6c:23e3
Following addresses configured for  eth2
Following addresses configured for  eth3
         192.168.138.16
         fe80::f816:3eff:fe64:3d5
Following addresses configured for  docker0
         172.17.0.1
         fe80::42:9cff:fe4e:169a
Following addresses configured for  br-40e59d9a39a3
         192.168.49.1
         fe80::42:c3ff:fea7:e4cb
Following addresses configured for  vethcc9c196
         fe80::1476:a5ff:fe58:55cb

 

Summary

There are several ways to get the IP addresses of the localhost in Linux using Go, the above methods are some of the most common ways. In this tutorial we have used net.LookupHost(), net.Interfaces(), net.InterfaceAddrs() and net.InterfaceAddrs() to get IP address of the Linux machine. We can get both IPv4 and IPv6 address along with the interface name. You also have an option to skip loopback address specifically while you are fetching the IP Address.

 

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment