Configure and Change the Hostname on Linux
Learn how to inspect, temporarily change, and permanently configure a Linux hostname using hostname, /etc/hostname, /etc/hosts, and hostnamectl.
A hostname is the name assigned to a Linux machine for local and network identification. It can appear in the shell prompt, system logs, administrative tools, terminal output, and network-related messages.
Linux uses an active hostname held by the running operating system. It also needs a persistent hostname saved in configuration so the name can be restored after a reboot. Changing only the active hostname changes the current runtime state; it may return to the previous value when the machine starts again.
Choose a Suitable Hostname
Use a concise, unique name made from letters, digits, and hyphens. For example, devbox, web-01, and backup2 are suitable short hostnames.
- Avoid spaces.
- Avoid underscores in host labels because they are not valid in ordinary DNS hostnames.
- Do not choose a name already used by another local machine or DNS record.
- Use a short hostname when a simple machine label is sufficient.
- Use a fully qualified domain name (FQDN) when the host must be identified within a domain, such as
node1.example.com.
DNS is the network naming system that translates names into addresses. A local hostname can work without DNS when it is listed in /etc/hosts.
Inspect the Current Hostname
Run hostname to display the active hostname held by the running system:
hostname
On a system using systemd, hostnamectl provides additional hostname information, including static and transient values when available:
hostnamectl
Before making changes, inspect both the active value and the traditional persistent configuration:
hostname
cat /etc/hostname
The file may not exist on every system, and some distributions use different startup or provisioning mechanisms. Treat the output of these commands as a description of the current system rather than a guarantee that every distribution stores the name in exactly the same way.
Temporarily Change the Active Hostname
The hostname command can change the active hostname for the current running system:
sudo hostname devbox
Verify the change:
hostname
This approach normally changes only the runtime value. Unless persistent configuration is also updated, the old hostname can return after a reboot. Administrative privileges are generally required.
Persist the Hostname with /etc/hostname
Many traditional Linux installations save the persistent local hostname in /etc/hostname. Edit the file with an editor such as nano:
sudo nano /etc/hostname
Make the file contain the intended hostname, for example:
devbox
The file normally contains one hostname entry and no surrounding explanation, labels, or competing names. Save the file and exit the editor. A reboot, a service refresh, or a suitable hostname-management command may be needed before all running components use the new value.
Update Local Resolution in /etc/hosts
/etc/hosts is a local static name-resolution file. It associates IP addresses with names and can be consulted before or alongside DNS, depending on the system's name-service configuration.
A common layout is:
127.0.0.1 localhost
127.0.1.1 devbox
127.0.0.1 is the usual loopback address for localhost. The name localhost refers back to the current computer. Some Linux distributions separately use 127.0.1.1, another loopback-style address, for the machine's local hostname.
To edit the file:
sudo nano /etc/hosts
On distributions that use the 127.0.1.1 convention, change the hostname on that mapping while preserving the existing localhost entry and unrelated aliases. The exact layout varies, so do not replace the entire file blindly or remove addresses and aliases that serve other purposes.
Use hostnamectl on systemd Systems
On many modern systemd-based distributions, hostnamectl set-hostname is the preferred persistent method:
sudo hostnamectl set-hostname devbox
This command normally updates the active hostname and the system's persistent hostname configuration. Check the result with:
hostname
hostnamectl
cat /etc/hostname
Older or minimal distributions may not provide hostnamectl. They may rely directly on /etc/hostname, distribution-specific startup tooling, network configuration, or an init system other than systemd.
Temporary and Persistent Methods Compared
Hostname-Related Files and Addresses
Restart and Validate the Configuration
A restart is a straightforward way to ensure persistent settings are loaded by boot-time services:
sudo reboot
After the system starts, verify the active name:
hostname
hostnamectl
Test local resolution with ping:
ping -c 1 devbox
A successful result shows that the name resolved to an address and that the local system responded. The exact address may be 127.0.0.1, 127.0.1.1, another local address, or an address returned by DNS, depending on the configuration.
Troubleshoot Hostname Changes
The hostname returns to its old value after reboot
- Only the active runtime hostname was changed.
/etc/hostnamewas not updated, or the distribution uses another persistent source.- A hostname-management service, cloud-init process, or other provisioning automation is overriding the setting.
Check both hostname and cat /etc/hostname. On a systemd system, use hostnamectl. If the values are correct but the name still changes during startup, inspect environment-specific automation and distribution startup configuration.
Ping cannot resolve the new hostname
/etc/hostsstill contains the previous hostname.- The name is being resolved through DNS instead of the intended local mapping.
- The hosts file contains invalid formatting or conflicting entries.
Review /etc/hosts, preserve the localhost entry, correct the applicable local hostname mapping, and check the system's name-service ordering if local entries are being ignored.
Applications or sudo report that the host cannot be resolved
The active hostname may not match a name available through /etc/hosts or DNS. Add or correct the required local mapping, check spelling, remove contradictory duplicate entries, and repeat the lookup test.
hostnamectl is unavailable
The machine may not use systemd, or it may be a minimal installation. Use hostname for runtime changes and configure persistence through /etc/hostname or the distribution-specific network and init configuration.
Permission is denied
Changing the hostname and editing files under /etc normally require administrative access. Run the command with sudo or obtain authorized administrator access.
Exam-Relevant Notes
- Active hostname: the name currently held by the running operating system.
- Persistent hostname: the saved name restored during startup.
hostnamecan change the runtime name, but the change is generally temporary./etc/hostnametraditionally stores one persistent hostname value./etc/hostsprovides local static resolution and is separate from DNS.127.0.0.1 localhostand a possible127.0.1.1 devboxmapping have different purposes.hostnamectl set-hostnameis commonly preferred on systemd-based distributions, but it is not universal.
For related Linux command-line topics, see Linux administration guides and Bourne Again Shell (Bash).