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:
- FQDN →
my-computer.my-domain.ext - Short hostname →
my-computer - 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:
- The system first checks
/etc/hosts - 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:
- Looking up the hostname in
/etc/hosts - 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