Linux hostname Command: Check and Change Hostnames
To check a Linux computer’s hostname, run:
hostname
On most modern Linux systems, you can see more detail with:
hostnamectl
To change the hostname permanently on a system that uses systemd, run:
sudo hostnamectl set-hostname web01
That is the short answer. Before renaming a work server, though, confirm the naming standard, DNS records, monitoring, certificates, and application dependencies. Changing the label on the machine is easy. Finding the automation that still expects the old label is the part that creates tickets.
What a Linux hostname is
A hostname is the local name a Linux system uses to identify itself. You will see it in shell prompts, logs, monitoring tools, SSH warnings, inventory systems, and commands such as hostnamectl.
For example, a web server might be named:
web01
A fully qualified domain name, or FQDN, adds the DNS domain:
web01.example.com
Those values are related, but they are not automatically the same thing. The machine can have a local hostname of web01 while DNS maps web01.example.com to its IP address. Renaming the local system does not automatically update your DNS provider, DHCP server, Active Directory, cloud inventory, monitoring platform, TLS certificates, or documentation.
That distinction matters in helpdesk work. A user saying “the server name is wrong” could mean the shell prompt, a DNS record, an inventory label, or the name shown in a management portal. Confirm which identity is actually wrong before changing anything.
Check the current hostname
The simplest command is:
hostname
Example output:
web01
This prints the current hostname known by the running kernel. It is useful in scripts and quick ticket checks because the output is clean.
If you also need the distribution and kernel version, use the steps in How to Check Linux Version from the Command Line.
Use hostnamectl for a complete summary
On Ubuntu, Debian, Fedora, Rocky Linux, and other systemd-based distributions, run:
hostnamectl
Example output:
Static hostname: web01
Icon name: computer-vm
Chassis: vm
Machine ID: 2a2f74e2c7834f92a31f43ea5b4a7710
Boot ID: 927cf556144a4e4db57acc1b1a6f594a
Virtualization: kvm
Operating System: Ubuntu 24.04.2 LTS
Kernel: Linux 6.8.0-60-generic
Architecture: x86-64
The most useful line for this task is Static hostname. The operating system, kernel, architecture, and virtualization fields are useful context when you are documenting a system or checking whether you connected to the correct machine.
For script-friendly output, ask for the static hostname directly:
hostnamectl --static
Linux can track three hostname types:
| Type | Purpose |
|---|---|
| Static | Persistent system hostname, normally stored in /etc/hostname |
| Transient | Temporary name supplied by the kernel, DHCP, or another network service |
| Pretty | Optional human-friendly description that may contain spaces |
Most server work is concerned with the static hostname.
Check the persistent hostname file
Many Linux distributions store the persistent name in:
cat /etc/hostname
Example:
web01
This file usually contains one hostname on one line. On a systemd machine, hostnamectl set-hostname updates the correct system settings for you, so editing this file manually should not be your first choice.
Still, checking /etc/hostname helps when the live hostname and persistent configuration disagree. That can happen after an incomplete manual change, an image-building mistake, or automation that modified only one layer.
Compare the running and stored values:
printf 'Running: %s\n' "$(hostname)"
printf 'Persistent: %s\n' "$(cat /etc/hostname)"
If they differ, investigate why before forcing them to match. DHCP, cloud-init, a configuration-management agent, or a provisioning script may own the value.
Change the hostname permanently with hostnamectl
On a systemd-based system, the preferred command is:
sudo hostnamectl set-hostname web01
Replace web01 with the approved name. Then verify:
hostname
hostnamectl --static
cat /etc/hostname
All three should normally report the new static name.
You usually do not need to reboot just to update the kernel hostname. New shell prompts, applications, or management agents may not notice the change until you start a new session or restart the affected service. Do not reboot a production server merely because the current SSH prompt still displays the old name.
Open a second session when possible and verify the system before closing your working connection. A rename should not normally break SSH connectivity by itself, but DNS, host keys, access tooling, or jump-host configuration may use the old name.
Use a valid, boring hostname
A practical server hostname should use lowercase letters, numbers, and hyphens. A safe example is:
web-prod-01
Avoid spaces, underscores, punctuation, and names that depend on someone remembering an inside joke six months from now. Your organization may have a format such as location-role-environment-number. Follow it.
A hostname label is commonly limited to 63 characters. Shorter is easier to read in prompts, logs, dashboards, and ticket notes.
Update /etc/hosts when required
After a hostname change, check:
cat /etc/hosts
A Debian or Ubuntu system might contain:
127.0.0.1 localhost
127.0.1.1 web01
A server with a static address might instead map its real IP to an FQDN and short name:
192.0.2.25 web01.example.com web01
If the old hostname appears in /etc/hosts, update it according to your organization’s standard. Use sudoedit rather than making the file broadly writable:
sudoedit /etc/hosts
Do not blindly replace every occurrence of the old name. Loopback mappings, cluster aliases, service names, and comments may have specific purposes. Make the smallest justified change.
A stale /etc/hosts entry can cause errors such as:
sudo: unable to resolve host web01
It can also make local name lookups return confusing results. Test the current lookup with:
getent hosts "$(hostname)"
getent checks the system’s configured name-service sources rather than only reading one file.
Changing /etc/hostname manually
If hostnamectl is unavailable on a non-systemd distribution or minimal system, you may need to update /etc/hostname manually:
sudoedit /etc/hostname
Put the approved hostname on one line, save the file, and update any matching /etc/hosts entry if your distribution requires it.
To change the running hostname until the next boot, older systems may support:
sudo hostname web01
That command changes the active kernel hostname but may not make the change persistent. If you run only that command, the old name can return after reboot.
The exact persistent mechanism varies on older distributions. Check the distribution’s documentation rather than guessing which network configuration file owns the name.
Temporary hostname changes
For a short-lived lab or troubleshooting test, this command changes the running name:
sudo hostname temporary-test
Verify it with:
hostname
The change normally disappears after reboot or when a network/provisioning service reapplies its configuration.
Temporary renames are rarely useful on managed production systems. Logs, monitoring data, and audit trails may split one machine into two identities. If you only need to label a terminal session for yourself, update your notes instead of renaming the server.
Hostname changes in cloud VMs and containers
Cloud instances can behave differently because another system may own the hostname.
Cloud-init
On a cloud VM, cloud-init may set the hostname during boot. Check its reported value:
sudo cloud-init query local_hostname 2>/dev/null
If cloud-init owns the value, a direct rename can work now and revert on the next boot. Review the image and provider documentation, then change the intended provisioning source rather than broadly disabling cloud-init.
Containers
A container’s hostname is commonly assigned by Docker, Podman, Kubernetes, or another runtime. Changing it from inside may be temporary or irrelevant after replacement. Set the name in the container definition instead.
If the “server” disappears and comes back from an image, fix the image or deployment configuration. Editing the running instance is a sticky note on a machine designed to be replaced.
DNS is a separate change
After setting the local hostname, this command may still fail:
getent hosts web01.example.com
That is expected if DNS has not been updated. Local hostname configuration does not create an A record, AAAA record, or PTR record.
Before renaming a managed server, check whether you need to update:
- Forward DNS records
- Reverse DNS or PTR records
- DHCP reservations
- Monitoring and alerting targets
- Backup jobs
- Configuration-management inventory
- SSH client aliases and known-host records
- TLS certificates
- Application allowlists
- Cluster membership or service discovery
- Documentation and asset inventory
A service may bind to an IP address and not care about the hostname. Another may include its FQDN in certificates, URLs, database records, or licenses. Search the application configuration before assuming the rename is cosmetic.
For basic network checks, see Linux Networking Commands for Beginner Sysadmins.
A safe helpdesk hostname-change workflow
Use this sequence when a ticket requests a rename.
1. Confirm the target
Record the current hostname, IP addresses, operating system, and machine or VM ID:
hostnamectl
ip -brief address
Make sure the ticket refers to this exact system. Renaming the wrong VM is an impressive way to turn a five-minute task into a meeting.
2. Confirm the approved name
Check the organization’s naming standard and whether the short hostname or FQDN was requested. Verify that the new name is not already in use in DNS, inventory, or monitoring.
3. Identify dependencies
Search relevant application configs and automation for the old name. Check DNS, certificates, backups, monitoring, orchestration, and access paths.
4. Capture the current configuration
Capture the current values in an approved working note:
hostname
hostnamectl --static
cat /etc/hostname
getent hosts "$(hostname)"
5. Set the new hostname
On systemd:
sudo hostnamectl set-hostname web-prod-01
Update /etc/hosts only if the existing local mapping requires it.
6. Verify locally
hostname
hostnamectl
cat /etc/hostname
getent hosts "$(hostname)"
Start a new shell so the prompt refreshes. Check journalctl or the affected service logs if a management agent reports errors.
7. Verify externally
From another approved system, test the expected DNS name, SSH path, monitoring target, and application health.
8. Document the outcome
Record the old name, new name, command used, related DNS or inventory changes, validation performed, and whether a reboot or service restart was needed.
Common beginner mistakes
Changing only the running hostname
sudo hostname newname may work immediately but revert after reboot. Update the persistent configuration through hostnamectl or the distribution’s supported method.
Editing only /etc/hostname
The stored value can change while the running kernel, shell prompt, or /etc/hosts still uses the old one. Use the supported tool and verify every relevant layer.
Assuming hostnamectl updates DNS
It does not. DNS, DHCP, certificates, inventory, and application settings are separate systems.
Renaming a managed cloud instance by hand
Provisioning tools may restore the old value. Find the actual source of truth first.
Rebooting before validating dependencies
A reboot can make the persistent name take effect everywhere at once and remove your easiest rollback window. Validate locally and externally, and reboot only when the operating procedure or affected software requires it.
Forgetting the SSH known_hosts impact
SSH associates host keys with names and addresses. If a new name points to an existing machine, clients may see a host-key warning. Verify the fingerprint through an approved channel instead of deleting warnings until the connection works.
Using a clever hostname
Names such as final-final-server, batcave, or bob-fixed-this age badly. Use the naming standard. Future on-call staff already have enough mysteries.
Practice Linux system-identification commands
Hostname work is a good beginner exercise because it connects a simple command to DNS, service configuration, cloud provisioning, and change control. Practice checking identity and network state before you make changes on a real server.
Practice Linux commands in Shell Samurai. It gives you a safe place to build command-line habits without discovering the naming policy during an outage call.
FAQ
How do I check the hostname in Linux?
Run hostname for the current name. On a systemd machine, run hostnamectl for the static hostname plus operating system, kernel, architecture, and virtualization details.
How do I permanently change a Linux hostname?
On most modern distributions, run sudo hostnamectl set-hostname newname. Verify with hostname, hostnamectl --static, and cat /etc/hostname.
Do I need to reboot after changing the hostname?
Usually not. hostnamectl updates the active and persistent hostname. A new shell or service restart may be needed for individual programs to display the new name. Reboot only when required by your platform or change procedure.
Why did my hostname change back after reboot?
You may have changed only the running hostname, or cloud-init, DHCP, configuration management, or another provisioning tool may own the value. Find and update the real source of truth.
Does changing the hostname update DNS?
No. Local hostname settings do not create or modify forward DNS, reverse DNS, DHCP, certificates, monitoring, or inventory records.
What is the difference between a hostname and an FQDN?
A hostname is the local short name, such as web01. An FQDN includes the DNS domain, such as web01.example.com. DNS and local system configuration must be managed consistently, but one does not automatically update the other.
Practice This in a Real Terminal
Shell Samurai gives you safe Linux missions so the commands actually stick. Chapter 1 is free; the full practice path is a one-time purchase, not another subscription.