How to change Debian Linux domainname

1

1

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!

xpt

Posted 2017-05-30T00:13:24.120

Reputation: 5 548

2

Don't worry about domainname unless you're a time traveler from the 90s. I also generally wouldn't worry about dnsdomainname - it's generally unimportant and should not affect network performance.

– Bob – 2017-05-30T00:23:12.627

No, domainname is part of the FQDN (Fully Qualified Domain Name) that my sendmail mail server depends on. – xpt – 2017-05-30T03:52:59.980

2domainname has nothing to do with a FQDN. Its only relevance is an old pre-LDAP system that hasn't been used in 20 years. While dnsdomainname is more relevant, it still should not result in any performance issues. If name resolution is taking a long time, that's more likely because of misconfigured or slow resolvers (/etc/resolv.conf) or possibly a lacking entry for resolving your own hostname. – Bob – 2017-05-30T05:06:19.437

@Bob, my OP updated, showing that /etc/resolv.conf is not the problem and there is no DNS resolve delay. Again my sendmail mail server depends on FQDN, and when it is not matching with what system thinks, it will cause extensive delay. It has always been the case for me. And this is a home Linux system that do not use LDAP but pure DNS server. – xpt – 2017-05-30T12:53:18.060

@Bob is very relevant: curl -v verysecuresite.com fetch data because DNS server receive "verysecuresite.com.utopia.net". On a systemd distro sysctl kernel.domainname return (none) and hostname -d nothing. systemd-resolve --status return utopia.net on field DNS Domain:, on Global and on an interface. The utopia thing comes from an ISP hijacked router (OPTION: 15 ( 10) Domainname utopia.net on dhcpdump). I removed from the interface (systemd-resolve --set-domain=local --interface=eth0) but not yet from Global.

– Pablo A – 2019-09-25T21:12:45.567

Answers

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

JakeGould

Posted 2017-05-30T00:13:24.120

Reputation: 38 217

So basically you put FQDN in /etc/hostname, or you only have a short host name but not the DNS domain name in your hostname -f output? – xpt – 2017-05-31T20:30:08.460

so are you trying to answer my specific question then? – xpt – 2017-06-01T01:13:42.490

1

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.

ben

Posted 2017-05-30T00:13:24.120

Reputation: 373

No, actually /etc/hostname should only contain hostname. The domain name is set somewhere else, not there. – xpt – 2017-05-30T12:44:07.170

man hostname: [...]Nowadays most software is able to cope with a full FQDN here[...] so yes, you can do that (Debian deals correct with it). You can as well alias the host name to the FQDN in /etc/hosts, I will add that as an option. – ben – 2017-05-31T11:38:28.310

Well, "it works" doesn't means "it is correct". man hostname also 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." I'd rather do it the correct way instead of hacky way. The problem is that the correct way is not working. I know that, and want to fix it if possible. – xpt – 2017-05-31T16:12:22.803

Oh, sorry just saw your update -- the problem is that I *am* using the alias in the host name to the FQDN in /etc/hosts: localhost.my.domain.org. See OP. – xpt – 2017-05-31T16:22:26.937

Rather use 127.0.0.1 localhost coral.my.domain.org, which works with debian stretch and fedora 25. – ben – 2017-06-01T05:39:30.580

I explained why I didn't put my host name (coral) in /etc/hosts in my OP. You missed that. – xpt – 2017-06-01T12:51:49.650

Ok, my failure. This works as well (Debian Stretch): 127.0.0.1 localhost localhost.my.domain.org – ben – 2017-06-02T06:25:27.077

Works with Fedora 25, too. I'll update my answer. – ben – 2017-06-02T06:37:18.543