How to Find the Hostname in Linux: A Step‑by‑Step Guide
In Linux, the hostname is the unique name assigned to a computer on a network. It helps identify the system among other devices and is essential for services like DNS, SSH, and email routing. Whether you are setting up a new server, troubleshooting network issues, or simply curious about your machine’s identity, knowing how to retrieve the hostname is a fundamental skill.
Introduction
The hostname can be queried using several built‑in utilities. The most common are the hostname command, the newer hostnamectl tool, and reading the static file /etc/hostname. Worth adding: each method reveals different pieces of information—some display the short name, others the fully qualified domain name (FQDN), and a few provide IP addresses or DNS domain details. This article walks you through every reliable technique, explains what each output means, and offers tips for using the information in scripts or system administration tasks.
The Basic hostname Command
The hostname utility is the original way to view or set a Linux host name. It comes pre‑installed on virtually all distributions Most people skip this — try not to..
How to Display the Current Hostname
hostname
Running this command prints the short hostname (e.Practically speaking, , server01). Now, it does not include the domain part. g.This is the value most applications use when they need a simple identifier And it works..
Viewing the Fully Qualified Domain Name (FQDN)
To obtain the FQDN, which includes the domain, use the -f flag:
hostname -f
The output might look like server01.example.com. The domain portion is often defined in /etc/hosts or configured via DHCP/Static IP settings Less friction, more output..
Retrieving the Short Hostname with -s
If you need to ensure you get only the short name (especially useful in scripts), use:
hostname -s
Both hostname and hostname -s return the same result on most systems.
The Modern hostnamectl Utility
Introduced with systemd, hostnamectl provides a richer view of system information, including the static hostname, the pretty hostname (for display), and the chassis type Small thing, real impact. But it adds up..
Displaying All Hostname Information
hostnamectl status
Typical output:
Static hostname: server01
Pretty hostname: Server One
Icon name: computer-laptop
Chassis: laptop
Machine ID: 1234567890...
Boot ID: abcdef1234...
Virtualization: none
Site: office
Domain: example.com
- Static hostname – the value stored in
/etc/hostname. - Pretty hostname – a human‑readable name that can contain spaces and special characters.
- Domain – the DNS domain suffix (if set).
If you only need the static hostname, pipe the output:
hostnamectl status --static
This returns just server01 Most people skip this — try not to. That's the whole idea..
Setting a New Hostname
You can change the static hostname with:
sudo hostnamectl set-hostname newhost
After changing, reboot or run sudo systemctl restart systemd-hostnamed for the change to propagate fully.
Reading the /etc/hostname File
The file /etc/hostname contains the static hostname that was set at system installation or via hostnamectl set-hostname. It is the source for the short hostname And it works..
Viewing the File
cat /etc/hostname
The file typically contains a single line, e.g.:
server01
If the system also writes the FQDN there (some distributions prepend the domain), you might see:
server01.example.com
This file is read by various network services during boot, so keeping it accurate is important.
Extracting Additional Network Details
Linux provides several flags to the hostname command that reveal IP addresses, aliases, and DNS information. These are handy for scripting and network diagnostics That's the whole idea..
| Flag | Description | Example Output |
|---|---|---|
-i or --ip-address |
Lists all active IPv4 addresses | 192.In real terms, 10 |
-6 |
Shows IPv6 addresses only | 2001:db8::1 |
-b |
Displays the broadcast address (rarely used) | 192. Also, 0. 168.168.Because of that, 168. On top of that, 10 10. Here's the thing — 255 |
-c |
Shows the canonical name (if DNS resolves) | server01. 0.Which means 1. 1.com |
-v |
Shows the version of the hostname utility | GNU coreutils 9.In real terms, com |
-d |
Displays the DNS domain name | example. In practice, 5 |
-4 |
Shows IPv4 addresses only | 192. Here's the thing — 0. But example. Still, 1. 1 |
-w |
Prints the working domain (if set) | example.example.example.com |
-x |
Displays the X (extended) hostname (some distros) | server01 |
-y |
Shows the Y (yet another) hostname (rare) | server01 |
-z |
Lists all zero addresses (loopback) | 127.That's why com |
-n |
Lists network node names (short) | server01 |
-p |
Prints the ping name (same as -n on most systems) |
server01 |
-r |
Shows the real hostname (FQDN) | server01. com |
-s |
Short hostname (same as default) | server01 |
-t |
Lists the top‑level domain (TLD) | com |
-u |
Displays the FQDN (same as -f) |
`server01.0. |
Example usage – to see all IP addresses associated with the host:
hostname -i
Or, for a quick DNS lookup of the domain:
hostname -d
These flags are especially useful when writing Bash scripts that need to capture specific parts of the host identity without parsing text Simple, but easy to overlook. Turns out it matters..
Practical Examples
1. Script to Capture the Static Hostname
#!/bin/bash
# get_static_hostname.sh
STATIC=$(hostnamectl status --static)
echo "Static hostname: $STATIC"
Running ./get_static_hostname.sh prints Static hostname: server01.
2.
2. Script to Gather Full Host Identity
#!/bin/bash
# gather_host_info.sh
SHORT=$(hostname -s)
FQDN=$(hostname -f)
DOMAIN=$(hostname -d)
IP=$(hostname -I | awk '{print $1}')
echo "Host Identity Report"
echo "--------------------"
echo "Short name : $SHORT"
echo "FQDN : $FQDN"
echo "Domain : $DOMAIN"
echo "Primary IP : $IP"
Running this consolidates several hostname flags into one readable snapshot, which is useful for inventory or troubleshooting scripts
Advanced Flag Combinations
When you need more granular information, the hostname utility can be combined with other shell options or external commands to extract exactly what you require. Below are several patterns that demonstrate how to put to work multiple flags at once.
1. Collect IPv4 and IPv6 addresses together
# Output both families on separate lines
hostname -I -4 | tr ' ' '\n' # IPv4 only
hostname -6 # IPv6 only
A compact one‑liner that prints all six families on a single screen is also possible:
hostname -I -4 -6 | sort -u
The -I flag expands to “all IPs”, so this command will list every address type present on the machine.
2. Resolve DNS while preserving the original FQDN
HOSTNAME=$(hostname -f) # short name
DNS_NAME=$(hostname -d) # fully qualified domain
echo "$HOSTNAME -> $DNS_NAME"
If the system uses an alias that differs from its canonical name, the two values may diverge—useful for debugging naming mismatches.
3. Filter by a specific network interface
To see which address belongs to a particular interface (e.g., eth0), pipe the result through grep:
hostname -i | grep '^10.0.0' # IPv4 of interface 10.x.x.x
hostname -6 | grep '^2001:' # IPv6 of interface 2001::/64
This pattern works well inside larger monitoring scripts where you want to isolate traffic per NIC.
4. Combine with other utilities for richer diagnostics
- SSH connection testing – After gathering the static hostname, you can immediately verify reachability:
STATIC=$(hostname -s)
if hostname -I | awk '{print $1}' | grep -q "$STATIC"; then
echo "Static hostname '$STATIC' appears on the system."
fi
- Log aggregation – Append captured data to a log file for later analysis:
{
echo "$(date '+%Y-%m-%d %H:%M') – Host identity:"
hostname -I -4 -6
} >> /var/log/host_info.log
These techniques illustrate how flexible hostname can become when paired with standard POSIX tools.
Cross‑Platform Considerations
Most Linux distributions expose the same set of flags documented above, but subtle differences exist across major families:
| Distro / OS | Notable quirks |
|---|---|
| Debian/Ubuntu | Uses hostnamectl status --static for a reliable static name; -w reflects the working domain configured in /etc/hosts. Relying solely on -s, -f, and -I remains safe. |
| Alpine Linux | The hostname binary is minimal; some flags (-b, -x) are unavailable. So |
| RHEL/CentOS | The -c flag returns the canonical name; it may differ from -f if the system has a custom hostname override. |
| macOS (BSD) | Flags behave differently: -a shows all addresses instead of -i; IPv6 support requires the ipv6 kernel module. |
Before embedding these commands in production scripts, test them against the target platform to avoid silent failures caused by missing utilities It's one of those things that adds up..
Security & Privacy Tips
-
Avoid exposing internal identifiers in logs unless necessary.
If you storehostname -p(the ping name) alongside system credentials, consider stripping it out of audit trails That's the whole idea.. -
Use absolute paths for temporary files created inside loops.
A script like:for ip in $(hostname -I); do echo "/tmp/$(basename "$ip") - $ip" donecould inadvertently create world‑writable files if
$ipcontains spaces or special characters. -
Sanitize output before piping to external processes.
When feedinghostnameresults into another tool (e.g.,awk), escape any embedded delimiters to prevent injection attacks Small thing, real impact..
Summary
The hostname command is far more than a simple identity lookup. By mastering its extensive flag set—from listing individual IP families (`-I