How to change Debian Linux domainname

I have never been able to fully understand how to change Linux hostname and domainname, and now it comes back and bites me again -- my Linux domainname is blank (domainname gives (none)) and I just cannot change it back:

$ hostname
coral

$ hostname -f
coral

$ hostname -d

$ hostname -d | wc 
      0       0       0

$ domainname 
(none)

$ dnsdomainname

$ dnsdomainname | wc 
      0       0       0

$ cat /etc/hostname
coral

$ head -1 /etc/hosts 
127.0.0.1       localhost.my.domain.org localhost

$ cat /etc/resolv.conf
# Fixed resolv.conf file
domain my.domain.org
search my.domain.org
nameserver 192.168.2.102

$ time dig coral
;; ANSWER SECTION:
coral.                  0       IN      A       192.168.2.102
real    0m0.038s
user    0m0.032s
sys     0m0.004s

I've rebooted several times, and my domainname is still blank now. The above is after several reboots.

Adding more details.

  1. Using alias the host name in /etc/hosts to specific the FQDN seems to be the correct answer, however, I've already done that see the above localhost.my.domain.org entry. I didn't put my host name (coral) in /etc/hosts because I don't want my host name to resolve to 127.0.0.1 but to real IP instead, which I obtained from my DNS server.
  2. I did a trace on hostname -f, but can't figure any problem out of the log. I can put it here if anyone interested.

As a consequence, there is an extensive (network) delay to start and stop my sendmail server.

UPDATE:

Today, the hostname -f, hostname -d & dnsdomainname are all working fine now. I didn't do anything, except I traced again the hostname -f. I should have traced to a new file, but I didn't. So now it is working, and I still do not know why it is working now, and why it didn't work before. Moreover, even when everything is working, my sysctl kernel.domainname is still reporting (none). This is so strange and convoluted.

$ uptime 
 21:33:36 up 2 days,  1:36,  5 users,  load average: 0.22, 0.43, 0.29

$ sysctl kernel.domainname
kernel.domainname = (none)

And the above (network) delay to connecting to my sendmail server is gone too. So my conclusion is that all I was doing above was correct, but somehow it mystically take a great amount of time for my Ubuntu 17.04 to realized it.

Anyway, I'll accept JakeGould's answer as he has something new in how to use sysctl and kernel.hostname.
Thank you all who helped!


Solution 1:

The way I deal with it is like so; assuming your hostname is covfefe. First run this command:

sudo hostname covfefe

Then edit /etc/hostname and change the value in there:

sudo nano /etc/hostname

Finally check your hostname with sysctl like this:

sysctl kernel.hostname

And if the value is not covfefe, change it to covfefe like this:

sudo sysctl kernel.hostname=covfefe

Solution 2:

For setting the domain name in Debian, you can edit /etc/hostname. For a host hulefu in domain somewhere.co.uk you simply set /etc/hostname to hulefu.somewhere.co.uk. This should work for most other Linux distributions too.

Now a hostname -d should give somewhere.co.uk as an answer.

A similar question has been asked on serverfault.

Update: You can add instead a domain alias to /etc/hosts. Following the previous example, this would be the first line of /etc/hosts: 127.0.0.1 localhost localhost.somewhere.co.uk

IMHO directly setting kernel parameters should be the last resort.