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
(none)- back to the drawing board!domainnamereturns(none). This also means that theunamesyscall doesn't help.dnsdomainnameis correct. I've checked the source forhostname --fqdn(thanks @barmar), and this runsgethostbyname. The returnedh_namefield correctly returns the FQDN. So, in short,hostname --fqdnworks because my/etc/hostscontains127.0.0.1 foo.example.com foo.