Quick take: The hostname command shows or sets the system's hostname. hostname prints it, hostname -f shows the fully qualified name, and hostname -I lists IP addresses. To change it permanently, use sudo hostnamectl set-hostname newname.
Introduction
The hostname command displays and temporarily sets the name that identifies a machine on a network. It is useful for quick checks and scripts, but because changes made with it do not survive a reboot, permanent changes are made with hostnamectl on modern systemd-based distributions.
Syntax
The basic syntax of the hostname command is:
hostname [OPTIONS] [NEW_NAME]Common Options and Parameters
The most useful options and parameters for the hostname command:
| Option | Description |
|---|---|
| (default) | Print the current short hostname. |
| -f | Print the fully qualified domain name (FQDN). |
| -i / -I | Print the IP address(es) of the host. |
| -d | Print the DNS domain name. |
| -s | Print the short hostname (up to the first dot). |
| hostnamectl set-hostname NAME | Set the hostname permanently (systemd). |
Practical Examples
Real hostname commands you can run today:
# Show the hostname
hostname
# Show the fully qualified name
hostname -f
# Show the machine's IP addresses
hostname -I
# Set the hostname for this session only
sudo hostname temp-name
# Set it permanently
sudo hostnamectl set-hostname web01
# View full host details
hostnamectlTips and Best Practices
- Changes made with
hostname nameare temporary; usehostnamectl set-hostnamefor a permanent change that persists across reboots. hostname -Iis a quick way to list the machine's IP addresses without parsingip a.- After changing the hostname, update
/etc/hostsso the new name resolves locally and tools do not complain.
Final Thoughts
hostname shows and temporarily sets a machine's network name, with handy flags for the FQDN (-f) and IP addresses (-I). For changes that stick, use hostnamectl set-hostname on systemd systems and update /etc/hosts to match. It is a small but frequently used tool for identifying and configuring servers.
FAQ: hostname Command in Linux
How do I check my hostname in Linux?+
Run hostname to print the short name, hostname -f for the fully qualified domain name, or hostnamectl for full details including the OS and kernel.
How do I change the hostname permanently?+
Use sudo hostnamectl set-hostname newname on systemd-based systems. This updates the configuration so the change survives reboots, unlike the temporary hostname command.
How do I find my IP address with hostname?+
Use hostname -I, which prints all of the machine's IP addresses on one line — a quick alternative to reading ip a.
Why does my hostname change revert after reboot?+
Setting it with hostname name is temporary and lives only in memory. Use hostnamectl set-hostname to write it to configuration so it persists.
What is the difference between hostname and FQDN?+
The hostname is the short name (like web01), while the fully qualified domain name (FQDN) includes the domain (web01.example.com). hostname -f shows the FQDN.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support