Ubuntu – How to correctly set hostname and domain name

domainhostnameUbuntu

I am renting a server, running Ubuntu 16.04 at a company, let's name it company.org.

Currently, my server is configured like this:

  • hostname: server737263
  • domain name: company.org

Here's my FQDN:

user@server737263:~ $ hostname --fqdn
server737263.company.org

This is not surprising.

I am also renting a domain name, let's name it domain.org. What I would like to do would be to rename my server as server1.domain.org.

This means configuring my hostname as server1 and my domain name as domain.org.

How can I do it correctly?

Indeed, the manpage for hostname is not clear. To me at least:

HOSTNAME(1)

[…]

SET NAME

  • When called with one argument or with the –file option, the commands set the host name or the NIS/YP domain name. hostname uses
    the sethostname(2) function, while all of the three domainname,
    ypdomainname and nisdomainname use setdomainname(2). Note, that this
    is effective only until the next reboot. Edit /etc/hostname for
    permanent change.

[…]

THE FQDN

  • You cannot change the FQDN with hostname or dnsdomainname.

[…]

So it seems that editing /etc/hostname is not enough? Because if it really changed the hostname, it would have changed the FQDN. There's also a trick I read to change the hostname with the command sysctl kernel.hostname=server1, but nothing says whether this is the correct way or an ugly trick.

So:

  1. What is the correct way to set the hostname?

  2. What is the correct way to set the domain name?

Best Answer

Setting your hostname:

  • You'll want to edit /etc/hostname with your new hostname.

  • Then, run sudo hostname $(cat /etc/hostname).

Setting your domain:

  • Then, in /etc/resolvconf/resolv.conf.d/head, you'll add then line domain your.domain.name (not your FQDN, just the domainname).

  • Then, run sudo resolvconf -u to update your /etc/resolv.conf (alternatively, just reproduce the previous change into your /etc/resolv.conf).

Both:

Finally, update your /etc/hosts file. There should be at least one line starting with one of your IP (loopback or not), your FQDN and your hostname. grepping out ipv6 addresses, your hosts file could look like this:

127.0.0.1 localhost
1.2.3.4 service.domain.com service
Related Question