0

On my test system, hostname --fqdn returns the Fully Qualified Domain Name (FQDN) which is foo.example.com.

I need this information in several C++ apps, and I get it as:

   char array[DNSDOMAIN_LEN];
   gethostname(array, DNSDOMAIN_LEN);

This has historically always returned foo.example.com on this test system. However, I have one new program where it's returning simply foo. The libc docs on this appear clear: if the system participates in the DNS (it does; it's web-accessible) then the returned value is the FQDN, aka the "host name".

Under what circumstances could gethostname actually return foo, ie. the "hostname", rather than the FQDN?

I'm on Ubuntu 22.04, with libc 2.35. The code was compiled by gcc 11.4.0:

me@foo:/var/log# ldd --version
ldd (Ubuntu GLIBC 2.35-0ubuntu3.8) 2.35
6
  • if the system participates in the DNS (it does; it's web-accessible) This is a wrong guess. The condition means: if the system is running a DNS service. Commented Dec 22, 2024 at 16:56
  • Do the previous programs still provide the fully qualified name and only the new program does not? Could there be some configuration change that is causing the problem such as an update to Ubuntu that changed a file? Commented Dec 22, 2024 at 17:10
  • 1
    @RichardChambers: so, I just added some new debug, and it turns out that it has always failed; only the new program made it obvious. Sorry, seem to be wasting everyone's time... Commented Dec 22, 2024 at 21:12
  • @stark - it prints (none) - back to the drawing board! Commented Dec 22, 2024 at 21:13
  • 1
    NIS/YP isn't set up on this system, so domainname returns (none). This also means that the uname syscall doesn't help. dnsdomainname is correct. I've checked the source for hostname --fqdn (thanks @barmar), and this runs gethostbyname. The returned h_name field correctly returns the FQDN. So, in short, hostname --fqdn works because my /etc/hosts contains 127.0.0.1 foo.example.com foo. Commented Dec 23, 2024 at 10:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.