🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

hostname Command

Beginner System Information man(1)

Show or set the system hostname

👁 10 views 📅 Updated: Mar 15, 2026
SYNTAX
hostname [OPTION]...

What Does hostname Do?

hostname displays or sets the system hostname. The hostname identifies the machine on a network and is used in shell prompts, log files, and network communications.

hostname can show the short hostname, the fully qualified domain name (FQDN), and the IP addresses associated with the hostname. Setting the hostname requires root privileges.

On systemd-based systems, hostnamectl is the preferred tool for permanently changing the hostname, as hostname changes are temporary and lost on reboot.

Options & Flags

OptionDescriptionExample
-f Show fully qualified domain name (FQDN) hostname -f
-I Show all IP addresses hostname -I
-i Show IP address for the hostname hostname -i
-s Show short hostname hostname -s
-d Show domain name hostname -d
NAME Set hostname temporarily sudo hostname newname

Practical Examples

#1 Show hostname

Displays the current system hostname.
$ hostname
Output: webserver01

#2 Show FQDN

Shows the fully qualified domain name.
$ hostname -f
Output: webserver01.example.com

#3 Show IP addresses

Shows all network IP addresses.
$ hostname -I
Output: 192.168.1.100 10.0.0.5

#4 Set hostname permanently

Permanently changes the hostname (systemd systems).
$ sudo hostnamectl set-hostname newname

#5 Set hostname temporarily

Changes hostname until next reboot.
$ sudo hostname tempname

#6 Short hostname

Shows just the hostname without the domain.
$ hostname -s
Output: webserver01

Tips & Best Practices

Use hostnamectl for permanent changes: hostname changes are temporary. Use sudo hostnamectl set-hostname name for permanent changes on systemd systems.
hostname -I for scripts: hostname -I returns IP addresses without DNS resolution — faster and more reliable for scripts than hostname -i.
Update /etc/hosts too: After changing hostname, update /etc/hosts to map the new hostname to 127.0.1.1 to avoid DNS resolution issues.

Frequently Asked Questions

How do I change the hostname?
Use sudo hostnamectl set-hostname newname for permanent changes. Also update /etc/hosts.
How do I find my server IP address?
hostname -I shows all IP addresses. ip addr show also works with more detail.
What is the difference between hostname and hostnamectl?
hostname shows/sets temporarily. hostnamectl (systemd) shows/sets permanently and manages static, pretty, and transient hostnames.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →