2

How do you find the FQDN of the local host in Go?

BTW: net.LookupAddr() doesn't work on Windows. So that's not an option.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Kevin Bailey
  • 39
  • 1
  • 1
  • 2
  • Nope. That will not return the DNS suffix. – Kevin Bailey Oct 03 '15 at 08:52
  • How would you get the information from the OS? There's nothing specific to Go in this sense. Is the 'USERDNSDOMAIN' environment variable set? – JimB Oct 03 '15 at 11:13
  • That will not work. It is not commonly set. Where to retrieve the connection DNS suffix depends on the OS. There are api calls or an /etc file. I found that Go does not provide a common way across OSs to get it and you will have to write the function yourself for each OS. For Window I need to call the `IpAdapterAddresses` system call; on linux I will have to figure it out when I port the app to CentOS. – Kevin Bailey Oct 03 '15 at 21:50
  • 1
    That's what I'm saying. IIRC `USERDNSDOMAIN` is set when the host is logged into an AD domain, which might not be there, but is it what you really want if it is? Go has no way of knowing what else you might want besides what's configured in the kernel. On linux do you want a PTR record for the first network interface, or do you want the hostname plus a "domain" entry from `resolv.conf`, or do you want the first fqdn listed in `/etc/hosts` for a local address, or do you want to use `nswitch.conf` to lookup the host from some other directory? – JimB Oct 04 '15 at 15:00
  • It doesn't make sense. The computer often does not know its own FQDN or may not have any way to access the information. There are so many ways for this to fail. – Michael Hampton Aug 03 '18 at 21:46

3 Answers3

3

By default there is no short way.

os.Hostname() doesn't provide the Fully Qualified Domain Name by default.

cmd := exec.Command("/bin/hostname", "-f")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
    log.Errorf(err)
}
fqdn := out.String()
fqdn = fqdn[:len(fqdn)-1] // removing EOL

Rudu
  • 15,682
  • 4
  • 47
  • 63
3

According to the documentation, function os.Hostname() returns the system host name reported by kernel. So, if your computer is named computer1, os.Hostname() returns computer1. If your computer is named computer1.my.office, os.Hostname() returns computer1.my.office. On Windows, is the same. If you want the domain name (as referred to the Active Directory domain) you have four ways:

  1. Parse the result of this command: wmic computersystem get domain
  2. Parse the result of this command: systeminfo | findstr /B /C:"Domain"
  3. Assume the existence of the environment variable USERDNSDOMAIN and evaluate his value (note that: the value of this variable is referred at the domain which user is stored)
  4. Check if one of the ip's assigned to the workstation can be resolved via DNS (for this point, you can view this: https://github.com/Showmax/go-fqdn)
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Enrico Bianchi
  • 1,901
  • 1
  • 10
  • 8
1

You can perform some gymnastics using the net lib as demonstrated here.

quikchange
  • 564
  • 2
  • 6
  • 14