Change hostname and FQDN on debian

Hostname and FQDN on Debian

On Debian systems, the hostname is set during startup by the script:

/etc/init.d/hostname.sh

This script reads the hostname from the file:

/etc/hostname

You can use this file to update the system hostname as well as its FQDN (Fully Qualified Domain Name).


Important Note

In the /etc/hosts file, the order of hostname and FQDN must be correct.

The FQDN should appear first, followed by the short hostname.


Steps to Change Hostname and FQDN

1. Update the Hostname

Run:

echo "my-computer" > /etc/hostname

2. Update the Hosts File

Edit /etc/hosts:

echo "127.0.0.1 my-computer.my-domain.ext my-computer localhost" > /etc/hosts

Order explanation:

  1. FQDN → my-computer.my-domain.ext
  2. Short hostname → my-computer
  3. Local alias → localhost

3. Apply the Changes

Run:

/etc/init.d/hostname.sh

Verify the Changes

Check the hostname:

hostname

Output:

my-computer

Check the FQDN:

hostname --fqdn

Output:

my-computer.my-domain.ext

Note About Shell Prompt

The old hostname may still appear in your terminal prompt.

Simply log out and log in again to refresh it.


Name Resolution Explanation

Hostname resolution in Linux is controlled by the following file:

/etc/nsswitch.conf

You will usually see this configuration:

hosts: files dns

This means:

  1. The system first checks /etc/hosts
  2. If not found, it then queries DNS servers listed in /etc/resolv.conf

How FQDN Resolution Works

When you run:

hostname -f

The system attempts to resolve the FQDN by:

  1. Looking up the hostname in /etc/hosts
  2. If not found, querying the configured DNS servers

If the hostname is missing from both sources, the FQDN cannot be resolved.


References

http://www.rooot.net/en/geek-stuff/linux/13-change-hostname-fqdn-debian.html
https://unix.stackexchange.com/questions/239920/how-to-set-the-fully-qualified-hostname-on-centos-7-0


Semoga bermanfaat 👍


Komentar

Postingan populer dari blog ini

Cara restart / stop windows service (services.msc) dengan bat / cmd

Learning Haproxy Load Balancer with Podman and Go Backends

Konfigurasi ITNSA OpenVPN auth LDAP