ubuntu:networking:configuration:hostname
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
ubuntu:networking:configuration:hostname [2021/01/08 15:32] – peter | ubuntu:networking:configuration:hostname [2021/01/08 15:33] (current) – peter | ||
---|---|---|---|
Line 2: | Line 2: | ||
[[Ubuntu: | [[Ubuntu: | ||
- | |||
- | ===== Objective ===== | ||
- | |||
- | To persistently change the hostname of a Debian-based system. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Scenario ===== | ||
- | |||
- | Suppose that you have installed a copy of Debian on a machine without specifying a hostname, causing it to default to the name debian with no domain. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Method ===== | ||
- | |||
- | ==== Overview ==== | ||
- | |||
- | The method described here has three steps: | ||
- | |||
- | - Ensure that the new hostname can be resolved using /etc/hosts. | ||
- | - Alter the file / | ||
- | - Activate the new hostname. | ||
- | |||
- | |||
- | ==== Ensure that the new hostname can be resolved using /etc/hosts ==== | ||
- | |||
- | When a program needs to know the fully-qualified hostname of the machine it is running on, the usual method for obtaining that information is as follows: | ||
- | |||
- | - Obtain the hostname reported by the operating system (which may or may not be fully qualified); then | ||
- | - Canonicalise the hostname (thereby converting it into a fully-qualified domain name if that is possible). | ||
- | |||
- | For this to work, the unqualified hostname must be resolvable and must be an alias of the fully-qualified hostname. | ||
- | |||
- | Each line of /etc/hosts consists of an IP address, followed by the corresponding canonical domain name, followed by a list of aliases for that domain name. For the scenario described above, the file should therefore contain a line of the form: | ||
- | |||
- | <file bash / | ||
- | < | ||
- | </ | ||
- | |||
- | The remaining issue is how to choose the IP address. | ||
- | |||
- | <file bash / | ||
- | 203.0.113.1 mail.example.com mail | ||
- | </ | ||
- | |||
- | If the address is set dynamically (for example, using DHCP), or if there is no address because the host is not networked, then the best alternative is to choose an address that is within the loopback netblock (127.0.0.0/ | ||
- | |||
- | <file bash / | ||
- | 127.0.1.1 mail.example.com mail | ||
- | </ | ||
- | |||
- | If the host has several static IP addresses then there is some advantage to the canonical address being a public address (so that the host can be referred to by its canonical name from anywhere), but that is not the only consideration. | ||
- | |||
- | ==== Alter the file / | ||
- | |||
- | On Debian-based systems, the hostname is configured in the file **/ | ||
- | |||
- | (Both the manpage for the hostname command and the Debian Reference specify that it is the unqualified hostname that should appear in this file, as opposed to the fully-qualified hostname. | ||
- | |||
- | ==== Activate the new hostname ==== | ||
- | |||
- | The most portable way to activate the new hostname is to instruct the hostname command to re-read / | ||
- | |||
- | <code bash> | ||
- | hostname -F / | ||
- | </ | ||
- | |||
- | (Debian has a script / | ||
- | |||
- | ---- | ||
- | |||
- | ===== Testing ===== | ||
- | |||
- | You can check that the hostname has been correctly set using the hostname command. Executed with no options: | ||
- | |||
- | <code bash> | ||
- | hostname | ||
- | </ | ||
- | |||
- | it should print the hostname reported by the kernel, which should be the unqualified hostname: | ||
- | |||
- | <code bash> | ||
- | |||
- | </ | ||
- | |||
- | Executed again with the option **--fqdn**: | ||
- | |||
- | <code bash> | ||
- | hostname --fqdn | ||
- | </ | ||
- | |||
- | it should print the fully-qualified hostname: | ||
- | |||
- | <code bash> | ||
- | mail.example.com | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Troubleshooting ===== | ||
- | |||
- | If the reported hostname is not as intended then this usually due to it not being correctly canonicalised. | ||
- | |||
- | <code bash> | ||
- | getent hosts mail.example.com | ||
- | </ | ||
- | |||
- | This should display the relevant line from the hosts file, for example: | ||
- | |||
- | <code bash> | ||
- | 203.0.113.1 mail.example.com mail | ||
- | </ | ||
- | |||
- | Then, take the reported IP address and attempt to resolve it to a hostname: | ||
- | |||
- | <code bash> | ||
- | getent hosts 203.0.113.1 | ||
- | </ | ||
- | |||
- | This should give the same result. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Further reading ===== | ||
- | |||
- | * Osamu Aoki, The hostname, Debian Reference, March 2012 [http:// | ||
- | * hostname (Ubuntu manpage) [http:// | ||
- | |||
ubuntu/networking/configuration/hostname.1610119971.txt.gz · Last modified: 2021/01/08 15:32 by peter