How to change Debian Linux domainname

17,040

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.

Share:
17,040

Related videos on Youtube

xpt
Author by

xpt

Updated on September 18, 2022

Comments

  • xpt
    xpt over 1 year

    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
    xpt almost 7 years
    No, actually /etc/hostname should only contain hostname. The domain name is set somewhere else, not there.
  • ben
    ben almost 7 years
    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.
  • xpt
    xpt almost 7 years
    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
    xpt almost 7 years
    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
    xpt almost 7 years
    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
    xpt almost 7 years
    so are you trying to answer my specific question then?
  • ben
    ben almost 7 years
    Rather use 127.0.0.1 localhost coral.my.domain.org, which works with debian stretch and fedora 25.
  • xpt
    xpt almost 7 years
    I explained why I didn't put my host name (coral) in /etc/hosts in my OP. You missed that.
  • ben
    ben almost 7 years
    Ok, my failure. This works as well (Debian Stretch): 127.0.0.1 localhost localhost.my.domain.org
  • ben
    ben almost 7 years
    Works with Fedora 25, too. I'll update my answer.