To add or change a hostname on Linux, use sudo hostnamectl set-hostname new-name on most systemd-based distributions; the change is normally permanent and takes effect immediately. If you only need a temporary name, use sudo hostname new-name, which usually lasts until reboot. A hostname identifies the machine on a network, but it is not the same thing as a DNS record or an entry in your local hosts file.
What a Linux hostname does
A hostname is the system’s human-readable name. It appears in command prompts, logs, and network tools, and can help people distinguish machines such as web-01 and backup-01. Linux commonly distinguishes between a static hostname, a transient hostname, and a pretty hostname:
- Static hostname: The persistent system name, typically stored in
/etc/hostname. - Transient hostname: The runtime name, which may be supplied or changed by the system or network environment.
- Pretty hostname: An optional descriptive label, such as “Design Team Laptop”; it can include spaces and is not intended to serve as a conventional network name.
On systemd systems, hostnamectl manages these values. In many setups, setting the static hostname also updates the active hostname. Network managers, cloud-init, DHCP, or other provisioning software may later change it, so a name that reverts is often being managed elsewhere.
Set a permanent hostname with hostnamectl
Choose a short name and run the command with administrator privileges. For example:
sudo hostnamectl set-hostname workstation-07
Check the result with:
hostnamectl status
hostname
The first command shows hostname details; the second prints the active short hostname. You generally do not need to reboot. If an application or shell prompt still displays the old name, open a new session or check how that application obtains its prompt text.
To set a pretty hostname as well, use:
sudo hostnamectl set-hostname "Design Team Laptop" --pretty
The pretty label is for display and does not replace the conventional static hostname used by many network services. You can set the static name and pretty label separately.
Use a temporary hostname or a system without systemd
For a runtime-only change, run:
sudo hostname temporary-name
This normally changes the active kernel hostname without updating the persistent configuration. The name is typically lost at reboot, and a network or system management service may replace it sooner. Use this approach for a short test, not when you need a durable change.
Some Linux installations do not use systemd, and very minimal or container environments may not provide hostnamectl. On those systems, use the distribution’s documented hostname configuration method. A common traditional arrangement stores the name in /etc/hostname, but the file’s presence and the service that reads it depend on the distribution.
On a system where you have confirmed that /etc/hostname is the persistent source, an administrator can edit it, for example with sudoedit /etc/hostname, and put the desired short name on its own line. Then apply the change using the operating system’s hostname service or reboot if required. Do not assume that editing this file alone updates every running service or any network records.
Choose a valid name and update local name resolution
For the static hostname, a conservative choice is a short DNS-style label: letters, digits, and hyphens, with no spaces. Avoid beginning or ending the label with a hyphen. Use lowercase for consistency, and follow any naming rules imposed by your network or organization. Names containing dots can represent fully qualified domain names in some environments, but a machine’s hostname and its DNS domain are separate concepts; do not append a domain unless your setup expects one.
Changing the hostname does not automatically register it with a router, DNS provider, cloud control plane, or other computers. Whether other devices can resolve the name depends on network configuration, such as DNS, DHCP, or multicast DNS.
Some distributions keep a local mapping in /etc/hosts. Inspect the file before editing it:
cat /etc/hosts
A typical local entry might look like 127.0.1.1 workstation-07 on systems that use that convention. Other distributions use different arrangements, and servers may rely on DNS instead. Preserve essential entries such as localhost and IPv6 loopback mappings; do not replace the file wholesale. If the old hostname appears in a local mapping, update that specific entry only when appropriate for the system.
Common problems and checks
- Permission denied: Use
sudofor a system-wide change, if your account is permitted to administer the machine. - hostnamectl is unavailable: The system may not use systemd, or the utility may be absent. Identify the distribution and use its supported configuration method rather than installing or editing files at random.
- The name changes back: Look for cloud-init, DHCP settings, NetworkManager, configuration-management tools, or container orchestration that may set the hostname at startup.
- Other machines cannot find the name: The local hostname change is not proof of DNS registration. Check DNS or the network’s naming service, and test resolution with tools such as
getent hosts workstation-07. - Programs show a stale name: A process may have cached a value, or the display may come from a shell prompt configuration. Start a new session and compare with
hostnameandhostnamectl status.
In a container, changing the hostname may be restricted, temporary, or controlled by the container runtime. Set it through the container’s launch configuration when that is the intended source of truth. On a remote server, confirm the correct host and any operational naming requirements before changing it, since monitoring, scripts, and certificates may refer to the old name.
Frequently Asked Questions
How do I see my current hostname? Run hostname for the active short name, or hostnamectl status on a systemd machine for additional hostname details.
Do I need to reboot after changing the hostname? Usually not when using hostnamectl; it normally applies the change immediately. A particular service, prompt, or non-systemd distribution may need a new session or its own reload procedure.
What is the difference between hostname and domain name? The hostname identifies a machine, while a domain name identifies a DNS namespace or a name within it. A fully qualified domain name combines a host label and domain in some configurations, but setting a hostname does not create DNS records.
How do I add a hostname to /etc/hosts? Edit the file with administrator privileges only if a local mapping is needed, and preserve existing loopback entries. Add or update the relevant address-to-name line according to your distribution and network; this affects local resolution, not public DNS.
Why does my hostname revert after reboot? A startup or provisioning service may be assigning it again. Check the distribution’s hostname configuration and tools such as cloud-init or network management software to find the authoritative setting.