On most Linux systems that use systemd, change the hostname permanently with sudo hostnamectl set-hostname new-name, replacing new-name with your chosen name. Then check the relevant entry in /etc/hosts and verify the result with hostnamectl; other Linux distributions may use different tools or configuration files.
What a Linux hostname does
A hostname is the name a computer uses to identify itself on a network and in many local system displays. It may appear in a terminal prompt, system logs, network administration tools, and remote-access software. A hostname is not the same thing as a domain name, an IP address, or a Linux user name. Changing it does not automatically rename user accounts, alter an IP address, or register a new name in DNS.
Linux systems can expose more than one form of hostname. On systemd-based distributions, hostnamectl distinguishes a static hostname saved in the system configuration, a transient hostname that can be supplied dynamically by the network, and an optional pretty hostname intended for display. The static hostname is usually what people mean when they ask how to rename a Linux machine.
Hostnames are useful even on a computer that is not publicly reachable. A clear name helps distinguish machines in terminal sessions, monitoring dashboards, local networks, and configuration files. For example, build-node-2 is more informative than a generic default such as localhost.
Choose a hostname that works well
Linux allows hostname values that may not work consistently with every network service. For broad compatibility, use a short name made of lowercase letters, numbers, and hyphens. Keep it easy to read and avoid spaces, underscores, and punctuation. A DNS-style name label should start and end with a letter or number; each label is limited to 63 characters. If using a fully qualified domain name, separate labels with periods, such as web-01.example.net.
Consider where the name will be used before changing it. A laptop might use maya-laptop, while a server could use a role and number, such as db-03. Avoid names that duplicate another device on the same network. If your organization assigns hostnames through an inventory system, cloud platform, or DHCP service, follow that system’s naming rules rather than choosing a local name in isolation.
Change the hostname with hostnamectl
On most current Ubuntu, Debian, Fedora, Red Hat Enterprise Linux, and other systemd-based installations, hostnamectl is the simplest method. Open a terminal and run:
sudo hostnamectl set-hostname new-name
For example, to name a machine lab-pc-4, use:
sudo hostnamectl set-hostname lab-pc-4
Enter your account password if prompted. The command generally updates the system’s static hostname and makes the new name active without requiring a reboot. You do not need to include the old name, and there is no special quoting required for a conventional hostname without spaces.
You can inspect the system’s hostname information with:
hostnamectl status
To see only the name currently returned by the hostname command, run hostname. If hostnamectl is unavailable, that does not necessarily mean Linux cannot change the name; the distribution may not use systemd, or the command may not be installed in a minimal environment. Use the distribution-specific approach described below.
Check /etc/hosts after renaming
The hostname setting and the local hosts file serve different purposes. The file /etc/hosts maps names to IP addresses on the machine itself. A stale entry can cause local programs to look up the old name, so inspect the file after changing the hostname:
cat /etc/hosts
On some Debian and Ubuntu installations, you may see a line like 127.0.1.1 old-name. If so, edit the file with an editor, for example sudo nano /etc/hosts, and replace the old machine name on that line with the new one. A server might instead have its hostname associated with an address on a different line; check the existing configuration before editing.
Keep the standard localhost mappings, commonly 127.0.0.1 localhost and ::1 localhost. Do not replace those entries with the machine’s new hostname. The exact file contents differ by distribution and setup, so preserve unrelated aliases and address mappings. Save the file, then check that the new hostname is recognized locally with getent hosts new-name if an appropriate mapping is present.
Change it on systems without systemd
Not every Linux distribution uses systemd. On systems such as Alpine Linux, the persistent hostname is commonly stored in /etc/hostname, but the precise procedure depends on the init system and release. A typical approach is to edit that file as an administrator, set the desired name, and apply it for the current session with the system’s hostname command or service. Consult the distribution’s documentation before changing startup configuration.
The command sudo hostname new-name can set the active kernel hostname on many Linux systems, but by itself it is often temporary: the name may revert at reboot if the persistent configuration was not updated. On a non-systemd system, determine which file or service sets the hostname during startup, update that source, and then verify both the current name and the saved setting. Avoid blindly editing files from instructions written for a different distribution.
Temporary and permanent hostname changes
A temporary change is useful for testing or for a short-lived environment. On many systems, sudo hostname test-name changes the active hostname until a reboot or until another service changes it. A permanent change updates the configuration used at startup, as hostnamectl set-hostname usually does on systemd distributions.
Network configuration can complicate this distinction. A DHCP server, cloud agent, or network-management service may provide a transient hostname. On systemd systems, a transient name can be different from the static name saved locally, and software may display whichever value it obtains. If the name changes back or differs between commands, inspect hostnamectl status and the network or cloud configuration rather than repeatedly setting it by hand.
There is also a separate pretty hostname, which can contain spaces and is intended for display rather than use as a conventional network name. You can set one on a systemd machine with sudo hostnamectl set-hostname "Maya's Work Laptop" --pretty. Keep the static hostname simple and compatible even if you choose a more descriptive display name.
Verify the change and decide whether to reboot
After updating the hostname and any relevant hosts-file entry, verify the result:
- Run
hostnamectl statusto review static, transient, and pretty hostname information where supported. - Run
hostnameto check the active name reported by the command. - Review
/etc/hostnameon systems that use it for persistent configuration. - Open a new terminal or start a new login session if the shell prompt still shows the old name.
A reboot is usually unnecessary after using hostnamectl, but it can be a useful final test when you need to confirm that the name survives startup. Some applications read the hostname only when they start, and an existing terminal prompt may not refresh automatically. Restart only the relevant application or service if it has cached the old value; do not assume the hostname change itself requires restarting the whole computer.
Special cases: servers, containers, and cloud machines
On a server, changing the local hostname does not automatically update DNS records, certificates, monitoring systems, SSH known-host entries, or configuration-management inventories. If clients connect using a DNS name, update the authoritative DNS record or the system that manages it separately. A certificate’s names also remain unchanged unless you replace or reissue the certificate. The hostname change alone does not invalidate SSH keys, though a client may display a changed host label or report a mismatch if the address or identity it relies on has changed for another reason.
In a container, the hostname may be set by the container runtime or orchestration platform. Editing a file inside a running container may not persist after the container is replaced, and the container may not have permission to change its hostname. Set the name through the runtime or deployment configuration when that is the intended source of truth.
Cloud images can also receive their hostname from cloud-init, a provider’s metadata service, or an instance-management agent. A manual change might be overwritten during reboot or provisioning. Before changing a cloud server, check whether its hostname is meant to match an instance name, private DNS record, or provider-managed value. In managed environments, update the central configuration so that the desired name persists.
Common problems and safe troubleshooting
- Permission denied: Use
sudofor commands that change system settings, or log in with an account authorized to administer the machine. - Hostname returns to the old value: Check for DHCP, cloud-init, container settings, or another startup service that supplies a dynamic name.
- The prompt still shows the old name: Start a new shell or login session. The prompt may have been generated earlier or customized in shell configuration.
- Programs cannot resolve the new name: Check the relevant
/etc/hostsmapping and DNS configuration. Setting a hostname does not create a DNS record. - The name contains spaces or punctuation: Use a simple static hostname with letters, digits, and hyphens; reserve a descriptive phrase for the optional pretty hostname.
Changing a hostname is normally a low-risk administrative task, but make a note of the old value and inspect any scripts or services that depend on it. On production systems, coordinate the change with the people responsible for DNS, monitoring, backups, and configuration management. That avoids a machine being renamed locally while the rest of the environment continues to expect the previous name.
Frequently Asked Questions
Do I need to reboot after changing a Linux hostname?
Usually not when you use hostnamectl on a systemd-based machine. The active name normally changes immediately. A reboot can confirm that the saved setting persists, while a new terminal session may be enough to refresh a prompt.
How do I check my current Linux hostname?
Run hostname for the active hostname. On a systemd system, run hostnamectl status to see additional information, including the static and, when set, pretty hostname.
Does changing the hostname change my IP address?
No. A hostname and an IP address are separate. Changing the local hostname does not reconfigure network interfaces or update DNS automatically.
Why does my hostname change back after restarting?
The persistent setting may not have been updated, or a DHCP client, cloud-init, container runtime, or other management service may be setting the name at startup. Check which component controls the hostname and change its configuration.
Can I use spaces in a Linux hostname?
Use a simple network-compatible static hostname without spaces. On systemd systems, you can set a separate pretty hostname with spaces for display, while keeping the static hostname suitable for network use.