28

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?

28

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
  • does this work for AWS or other VPS? – Thufir Jan 3 '17 at 7:52
  • 7
    Both those conf files say DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN on my server (same version). – Walf May 1 '17 at 1:26
  • 5
    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 6 '18 at 23:24
  • 3
    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 4 '18 at 3:42
  • 1
    You could update the answer with hostnamectl – Ring Ø Aug 25 '18 at 5:10
2

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.