54

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?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
titsou
  • 543
  • 1
  • 4
  • 4

5 Answers5

49

Setting your hostname:

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

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

Setting your domain, assuming you have a resolvconf binary:

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

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

If you do not have resolvconf, just edit /etc/resolv.conf, adding the domain your.domain.name line.

Either way:

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

In response to hostnamectl suggestions piling up in comments: it is not mandatory, nor exhaustive.

It can be used as a replacement for step 1 & 2, IF you OS ships with systemd. Whereas the steps given above are valid regardless of systemd being present (pclinuxos, devuan, ...).

SYN
  • 2,793
  • 12
  • 19
  • does this work for AWS or other VPS? – Thufir Jan 03 '17 at 07:52
  • Running on EC2, note that you may want to set some DHCP options to your VPC, pushing the proper domain name - or disable DHCP client from your AMIs - to prevent `/etc/resolv.conf` from being rewritten. – SYN Jan 04 '17 at 01:04
  • 8
    Both those conf files say `DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN` on my server (same version). – Walf May 01 '17 at 01:26
  • 11
    Don't be fooled by the fact that the "DO NOT EDIT" warning appears in `/etc/resolvconf/resolv.conf.d/head`. Everything in the head file is prepended to the resulting `/etc/resolv.conf` output file, so that's why the warning is in there, so that it shows up in the final result. Threw me for a loop at first. – njbair Jan 06 '18 at 23:24
  • 7
    This doesn't seem to apply to 18.04 LTS - there's no such file `/etc/resolvconf/resolv.conf.d/head` and there's no such utility `resolvconf`. – inopinatus Jun 04 '18 at 03:42
  • 4
    You could update the answer with `hostnamectl` – Déjà vu Aug 25 '18 at 05:10
  • how do i set the fqdn in the prompt? nm : `\H` – chovy Dec 02 '18 at 15:51
  • It seems suboptimal to hardcode the IP address in /etc/hosts, in case the server is moved later. Would it be better to put the fqdn on the 127.0.0.1 line, as recommended by @Richard Westby-Nunn? – nealmcb Jun 04 '19 at 23:25
  • 3
    Note also that if you use AWS you'll also need to preserve the hostname after reboots - `sudo nano /etc/cloud/cloud.cfg` update `preserve_hostname: true` (default is `false`). – B. Shea Jun 24 '19 at 23:55
  • @inopinatus `sudo apt install resolvconf` to edit it in 18.04 – Adam Plocher Jan 07 '20 at 18:06
  • 2
    -1, this answer is becoming increasingly obsolete. – BobHy Feb 24 '20 at 02:12
  • Feel free to edit then. The above steps still reflect how I'ld configure my hostname, on debian-based systems, though I didn't deploy Ubuntu in ages... hostnamectl did exist with Ubuntu Trusty and Debian Jessie already, while I initially answered. It's still not a part of Devuan though. Thus, not a standard with debian-like systems, as far as I'm concerned. – SYN Feb 24 '20 at 08:17
  • Once again StackExchange's obsolete accepted answers take precedence over current information while questions for newer versions are being deleted as duplicates. – jbarlow May 09 '21 at 08:48
  • Lacking any edit suggestion, here you go: I'm adding mentions to hostnamectl, which is not exhaustive, nor mandatory. Also missing from some Linux distros / not portable – SYN May 10 '21 at 07:03
  • the question here is about Ubuntu 16.04 - and its tagged Ubuntu. – Alexander Stohr Mar 03 '22 at 12:42
15

sudo nano /etc/hostname

hostname.domain.com

sudo nano /etc/hosts

127.0.0.1   hostname.domain.com hostname localhost

REBOOT!

MUST HAVE SINGLE HOSTNAME after FQDN in /etc/hosts file. Works fine on Ubuntu 18.04.1 and all other versions. On EC2 and elsewhere.

Didn't mess with resolve file or anything else.

That shows hostname in shell and then has the FQDN when you need it.

  • best answer IMO – Mike Ciffone Nov 18 '21 at 23:09
  • @MikeCiffone Technically, hostname should not contain the domain name -- i.e. should not be FQDN. Many deployments do it, but it should not be done that way. Note that in-kernel hostname under Linux after 1.0 is restricted to 64 characters (see `man 2 sethostname`) because that's the value of `HOST_NAME_MAX`. (Until 1.0 it was mere 8 bytes.) – Ivan Vučica Jan 05 '22 at 14:52
  • @IvanVučica Oh that's interesting, I wasn't aware. – Mike Ciffone Jan 05 '22 at 21:03
  • @IvanVučica why shouldn’t it be done that way? – bfontaine Feb 28 '22 at 18:53
  • 1
    @bfontaine You don't have to care about any of the reasons I'll state -- most of my hosts were configured with hostname=fqdn most of the time, and they worked. One reason is that your FQDN, not shortname, is then limited to 63ch (`HOST_NAME_MAX`). Another is that `molly-guard` will require you to type fqdn, not the shortname. Another is that `hostname` CLI prints out the full FQDN, not just `hostname -f`. Another is that some software might print out FQDN in place of shortname, or append the domain a second time to the FQDN (i.e. `foo.example.org.example.org` instead of `foo.example.org`). – Ivan Vučica Mar 01 '22 at 19:42
  • 1
    You can, of course, dismiss all of these as hypotheticals -- but I feel correctness of having the hostname contain just, well, the hostname is more correct and professional. – Ivan Vučica Mar 01 '22 at 19:42
  • Thank you for your detailed response @IvanVučica! – bfontaine Mar 01 '22 at 21:03
  • Additional example: Ceph explicitly says "use shortname" in config: https://docs.ceph.com/en/quincy/rados/configuration/common/ – Ivan Vučica Mar 08 '22 at 19:02
9

Instructions written against Ubuntu 18.04.3 LTS (bionic)

Change the hostname:

sudo hostnamectl set-hostname server1

Check result by running hostnamectl:

root@www:/# hostnamectl
   Static hostname: server1       <-- Check this value
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 202c4264b06d49e48cfe72599781a798
           Boot ID: 43654fe8bdbf4387a0013ab30a155872
    Virtualization: xen
  Operating System: Ubuntu 18.04.3 LTS
            Kernel: Linux 4.15.0-65-generic
      Architecture: x86-64

Change the domain via new network manager, Netplan, by editing /etc/netplan/01-netcfg.yaml and changing the search parameter:

sudoedit /etc/netplan/01-netcfg.yaml

Sample configuration:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes
      nameservers:
          search: [ domain.org ]

Test changes by logging in a second time, and running sudo netplan try in one of the sessions and checking settings in the other:

# netplan try
Do you want to keep these settings?


Press ENTER before the timeout to accept the new configuration


Changes will revert in  97 seconds
Configuration accepted.
# systemd-resolve --status
...
Link 2 (eth0)
      Current Scopes: DNS
       LLMNR setting: yes
MulticastDNS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
         DNS Servers: 8.8.8.8
                      8.8.4.4
          DNS Domain: domain.org      <-- Check this value
# cat /etc/resolv.conf
...
nameserver 127.0.0.53
options edns0
search domain.org    <-- Check this value
# hostname -f
server1.domain.org

It all is well, press ENTER at the sudo netplan try prompt to make things permanent.

RobM
  • 482
  • 4
  • 9
5

~$ man hostname

[…]
       You cannot change the FQDN with hostname or dnsdomainname.

       The  recommended  method  of  setting the FQDN is to make the hostname be an alias for the fully qualified name using /etc/hosts,
       DNS, or NIS. For example, if the hostname was "ursula", one might have a line in /etc/hosts which reads

              127.0.1.1    ursula.example.com ursula

       Technically: The FQDN is the name getaddrinfo(3) returns for the host name returned by gethostname(2).  The DNS  domain  name  is
       the part after the first dot.

       Therefore  it  depends  on the configuration of the resolver (usually in /etc/host.conf) how you can change it. Usually the hosts
       file is parsed before DNS or NIS, so it is most common to change the FQDN in /etc/hosts.

       If a machine has multiple network interfaces/addresses or is used in a mobile environment,  then  it  may  either  have  multiple
       FQDNs/domain  names  or  none at all. Therefore avoid using hostname --fqdn, hostname --domain and dnsdomainname.  hostname --ip-
       address is subject to the same limitations so it should be avoided as well.

[…]

This was kindly pointed out by poige in another thread and is exactly what Lutz proposed here.

You should not put your fqdn into /etc/hostname.

karlsebal
  • 795
  • 8
  • 20
0

I tried to change my domain entry from myhome.local to myhome.lan I had to edit the /etc/hosts file and the /etc/network/interfaces file. My /etc/hosts file now looks like:

127.0.0.1   localhost
192.168.3.2 server.myhome.lan   server

and my /etc/network/interfaces file now looks like:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp2s0
iface enp2s0 inet static
    address 192.168.3.2
    netmask 255.255.255.0
    network 192.168.3.0
    broadcast 192.168.3.255
    gateway 192.168.3.1
    # dns-* options are implemented by the resolvconf package, if installed
    dns-nameservers 192.168.3.1
    dns-search myhome.lan

It works fine for me.

Lutz
  • 1