How to correctly set hostname and domain name?
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 linedomain 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
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.
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.