Debian Change Hostname: Step-by-Step Guide
To change the hostname on a Debian system, you need to edit the /etc/hostname file and the /etc/hosts file, then restart the hostname service or reboot. This guide covers both the immediate and permanent methods, along with common pitfalls and verification steps.
What a Hostname Is and Why Change It
A hostname is the unique name that identifies your Debian machine on a network. It appears in your terminal prompt, is used by services like DHCP and DNS, and helps other devices refer to your system. You might want to change it when you repurpose a machine, set up a new server, or simply want a more descriptive name. The hostname is stored in /etc/hostname and is read at boot time.
Quick Method: Change Hostname Without Rebooting
You can change the hostname immediately without a reboot using the hostnamectl command (on systems with systemd) or the hostname command (on older SysVinit systems). This is useful when you want to avoid downtime.
- Open a terminal as root or use
sudo. - Run
hostnamectl set-hostname new-hostname(systemd) orhostname new-hostname(older systems). - Verify with
hostnameorhostnamectl.
Note that this change is temporary until you also update the permanent files, as explained next.
Permanent Change: Edit /etc/hostname and /etc/hosts
To make the change survive a reboot, you must edit the configuration files. Use a text editor like nano or vim.
- Edit
/etc/hostnameand replace the old name with your new hostname (e.g.,webserver). Save the file. - Edit
/etc/hosts. Find the line that maps your IP address to the old hostname, typically127.0.1.1 old-hostname. Change it to127.0.1.1 new-hostname. This line is important for some services like sudo and mail. - Apply the change immediately with
hostnamectl set-hostname new-hostnameor/etc/init.d/hostname.sh start(older systems).
After a reboot, the new hostname will be active.
Choosing a Valid Hostname
Hostnames must follow RFC 1178 guidelines: use letters (a-z), digits (0-9), and hyphens (-), but not start or end with a hyphen. Underscores are not allowed. The maximum length is 63 characters per label, and the entire hostname (FQDN) can be up to 253 characters. Avoid using spaces or special characters. For example, my-server-01 is valid; my_server is not.
Common Problems and How to Fix Them
Sometimes the change doesn’t take effect as expected. Here are typical issues:
- Hostname reverts after reboot: You forgot to edit
/etc/hostnameor/etc/hosts. Double-check both files. - sudo complains “unable to resolve host”: This is usually because the hostname in
/etc/hostsdoesn’t match the one in/etc/hostname. Ensure the line127.0.1.1 new-hostnameis present. - Hostname shows as localhost: If you see
localhostinstead of your desired name, thehostnamecommand may be reading from a different source. Check/etc/hostnameand runhostname -F /etc/hostnameto force it. - Changes don’t propagate to services: Some services like Postfix or Apache cache the hostname. Restart them after the change.
Verifying the Change
After making the change, verify it with the following commands:
hostname– shows the short hostname.hostname -f– shows the fully qualified domain name (FQDN).hostnamectl– displays detailed hostname info on systemd systems.cat /etc/hostname– confirms the file content.
If you use a static IP, ensure your DNS records are updated accordingly, but that’s outside the scope of this system change.
Changing Hostname on Older Debian Versions (SysVinit)
Debian 7 and earlier used SysVinit instead of systemd. The process is similar but uses the hostname command and the /etc/init.d/hostname.sh script. Edit /etc/hostname and /etc/hosts as above, then run /etc/init.d/hostname.sh start to apply immediately. The hostnamectl command is not available on these systems.
Frequently Asked Questions
Do I need to reboot after changing the hostname?
No, you can apply the change immediately with hostnamectl set-hostname or by running the hostname script. However, some services may not pick up the change until they are restarted, and a reboot ensures all processes use the new name.
What is the difference between /etc/hostname and /etc/hosts?
/etc/hostname stores the hostname itself, while /etc/hosts maps IP addresses to hostnames. The line in /etc/hosts with your hostname is used for local name resolution. Both must be updated for a consistent system.
Can I use uppercase letters in a hostname?
Yes, but it’s not recommended. Hostnames are case-insensitive, but the convention is to use lowercase to avoid confusion. The system will typically convert to lowercase automatically.
Why does my terminal prompt still show the old hostname?
The prompt may be cached in your shell session. Open a new terminal or run exec bash to reload the shell. If it still persists, check that the hostname files are correct and restart the shell.
How do I change the hostname on a Raspberry Pi running Debian?
The process is identical to any Debian system. Use hostnamectl set-hostname and edit the files as described. For Raspberry Pi OS, you can also use the raspi-config tool under “Network Options” > “Hostname”.
What happens if I only change /etc/hostname and not /etc/hosts?
Your system will have a new hostname, but the old one may still be used for local resolution, causing errors like “unable to resolve host” when using sudo or other services. Always update both files.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.