hostname Command: Tutorial & Examples
Display and change the system hostname on Linux servers
The hostname
command is a fundamental Linux utility used to display or set the system's hostname — the unique identifier assigned to a device on a network.
The hostname plays an essential role in network communications, system identification, and service configuration. While the hostname
command can temporarily
change the hostname, making such changes permanent involves modifying system configuration files or using system tools.
This article explains how the hostname
command works, common parameters, practical usage examples, making hostname changes persistent, and potential issues
you might encounter on Linux servers and virtual machines.
What The hostname Command Does
The hostname command interacts with the Linux kernel and system configuration to retrieve or set the system's hostname. The hostname is a human-readable label that identifies the machine on a network and is used by various network protocols and services.
When run without arguments, hostname
displays the current hostname. When given a new name as an argument, it sets the hostname temporarily until the next
reboot or hostname change.
The hostname is stored in the kernel and can be queried or modified during runtime. However, to make the hostname persistent across reboots, system
configuration files like /etc/hostname
must be updated.
Why The hostname Is Important
The hostname uniquely identifies your server in a networked environment, which is critical for:
- Network communications and routing
- Logging and monitoring systems
- Security and authentication mechanisms
- Service configurations, such as web servers or databases that rely on hostname-based access control
Incorrect or inconsistent hostname configuration can lead to network failure, logging issues, or problems in distributed systems.
Common Parameters And Their Usage
The hostname
command supports several useful command-line options:
-s, --short
Display the short hostname (up to the first dot).-f, --fqdn, --long
Display the fully qualified domain name (FQDN).-d, --domain
Display the DNS domain name.-i, --ip-address
Display the IP address(es) associated with the hostname.-I
Display all network addresses of the host.-y, --yp
Display the NIS or YP domain name.-F filename
Read the hostname from a specified file.--help
Show help message.--version
Show version information.
How To Use hostname: Basic And Advanced Examples
Here are some practical examples illustrating the usage of the hostname
command.
Display the current hostname:
hostname
server01
Display the short hostname:
hostname -s
server01
Display the fully qualified domain name (FQDN):
hostname -f
server01.example.com
Display the domain name:
hostname -d
example.com
Display the IP address(es) associated with the hostname:
hostname -i
192.168.1.100
Set the hostname temporarily (effective until reboot):
sudo hostname new-hostname
# No output, but the hostname changes immediately
Verify the hostname change:
hostname
new-hostname
Set hostname permanently by modifying /etc/hostname:
echo "new-hostname" | sudo tee /etc/hostname
# You may need to reboot or restart hostname-related services for changes to take effect
Alternatively, on systemd-based systems, use hostnamectl:
sudo hostnamectl set-hostname new-hostname
# This modifies the persistent hostname and updates relevant services
Making Hostname Changes Persistent
While the hostname
command modifies the hostname immediately, these changes are temporary and lost after a reboot. To make the hostname change permanent,
update the appropriate system configuration files or use system-specific tools.
On most Linux distributions:
Edit the/etc/hostname
file and replace the existing hostname with the new one.On systemd-based systems (e.g., Ubuntu, CentOS 7+):
Use thehostnamectl
command to set the hostname persistently.Update
/etc/hosts
to reflect hostname changes:
It is often necessary to update the/etc/hosts
file to map the hostname to the loopback address or the server's IP address to avoid resolution issues.
Example /etc/hosts
entry:
127.0.0.1 localhost new-hostname
After making these changes, reboot the system or restart networking services to apply the new hostname fully.
Potential Problems And Pitfalls
Hostname Not Persisting After Reboot:
This occurs if the hostname change was made only via thehostname
command without updating/etc/hostname
or usinghostnamectl
.Hostname Resolution Issues:
If/etc/hosts
does not include the new hostname mapping, some services may fail to resolve the hostname, causing errors.Inconsistent Hostname Across Network:
Ensure DNS records and DHCP configurations correspond to the hostname to avoid conflicts or dns-issue.Permissions:
Changing the hostname requires superuser privileges. Usesudo
to run commands that modify the hostname.Effect on Running Services:
Some services may cache the hostname at startup; restarting these services or rebooting may be necessary after a hostname change.
Scripting And Automation With hostname
The hostname
command is useful in scripts and automation tasks, such as configuration management or cloud-init scripts.
Example: Print the current hostname in a script
#!/bin/bash
current_hostname=$(hostname)
echo "Current hostname is: $current_hostname"
Example: Change hostname in a script with persistence
#!/bin/bash
new_hostname="webserver01"
sudo hostnamectl set-hostname "$new_hostname"
sudo sed -i "s/^127\.0\.1\.1.*$/127.0.1.1 $new_hostname/" /etc/hosts
This script sets a new hostname and updates /etc/hosts
accordingly.
Related Commands And Tools
hostnamectl
: A systemd utility for managing the hostname and related settings.cat
: View contents of files like/etc/hostname
and/etc/hosts
.systemctl
: Manage system services that may need restarting after hostname changes.ip
: Display network interfaces and addresses.nslookup
: Diagnose DNS resolution issues.
See Also
Further Reading
- Linux for Networking Professionals by Rob Vandenbrink (partner link)
- Understanding Linux Network Internals by Christian Benvenuti (partner link)
- Linux Networking Cookbook by Carla Schroder (partner link)
As an Amazon Associate, I earn from qualifying purchases.