Although it seems to be an FAQ, but I had never been able find a proper way to change hostname with domainname on Debian/Ubuntu.
First of all, many "answers" or writings are just wrong by putting hostname with domainname (FQDN) into /etc/hostname
, like this, this and this, because
The Debian reference says the hostname should not use the FQDN:
3.5.5. The hostname
The kernel maintains the system hostname. The init script in runlevel S which is symlinked to "/etc/init.d/hostname.sh" sets the system hostname at boot time (using the hostname command) to the name stored in "/etc/hostname". This file should contain only the system hostname, not a fully qualified domain name.
Also, from man hostname
, it says,
"
/etc/hosts
Usually, this is where one sets the domain name by aliasing the host name to the FQDN." and "A FQDN consists of a short host name and the DNS domain name. Unless you are using bind or NIS for host lookups you can change the FQDN and the DNS domain name (which is part of the FQDN) in the /etc/hosts file."
OK. The above is what I should do, but this is what I'm getting:
$ cat /etc/hostname
coral
$ head -1 /etc/hosts
127.0.0.1 coral.my.domain.org localhost
$ dnsdomainname
ht.home
$ cat /etc/resolv.conf
domain ht.home
search ht.home
nameserver 192.168.0.1
# after I change it to --
$ cat /etc/resolv.conf
# Fixed resolv.conf file
domain my.domain.org
search my.domain.org
nameserver 192.168.0.1
# everything just gone wrong --
$ dnsdomainname
dnsdomainname: No address associated with hostname
$ hostname -f
hostname: No address associated with hostname
$ hostname -d
hostname: No address associated with hostname
Update
I did a trace on hostname -f
, and it seems the "No address associated with hostname" error comes from libresolv.so
:
$ strace -o /tmp/strace.log hostname -f
hostname: No address associated with hostname
$ grep -E 'openat|close|No address' /tmp/strace.log
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(3) = 0
close(3) = 0
openat(AT_FDCWD, "/etc/nsswitch.conf", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
openat(AT_FDCWD, "/etc/host.conf", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
openat(AT_FDCWD, "/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libnss_files.so.2", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
openat(AT_FDCWD, "/etc/hosts", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libnss_dns.so.2", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libresolv.so.2", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(3) = 0
close(3) = 0
write(2, "No address associated with hostn"..., 35) = 35
Any help appreciated.
$ uname -rm
5.10.0-6-amd64 x86_64
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux bullseye/sid
Release: testing
Codename: bullseye