How to set hostname on Linux system
When setting up a Linux computer, one of the first things you’re likely to do is assign the correct hostname. Host names help identify computers on the network or even locally, making management and troubleshooting easier. In this guide, I will show you how to set a hostname on Linux using the following command hostnamectl
Order.
What is a hostname?
one host name It is the unique identification code of the machine on the network. Think of it as the “name” of the system. Hostnames are typically used for:
- Internet communication
- System management
- Identify machines in logs
There are three types of host names:
- static hostname: The primary, persistent host name of the system.
- nice hostname: A descriptive name that can contain spaces or special characters (such as “My Cool Server”).
- Temporary hostname: Temporary host name to use for the current session (reset after reboot).
How to set the hostname
The easiest way to set the hostname on a Linux system is to use hostnamectl
Order. The tool is systemd
and allows you to easily manage hostnames.
Step 1: Check current hostname
First, check the current hostname by executing the following command:
hostnamectl
Output example:
Static hostname: (unset)
Transient hostname: localhost
Icon name: computer-server
Chassis: server
Machine ID: 2c9a10b57b9248e7bdf1c78b0f9ea8e1
Boot ID: 8e4bb01e03ef4147bbeb2c5bc74f8a6f
Operating System: Linux
Kernel: Linux 6.2.0-43-generic
Architecture: x86-64
Step 2: Set a new static hostname
To set a permanent static hostname, execute:
sudo hostnamectl set-hostname workstation06
This will update the static hostname in the system profile.
Step 3: Set a nice hostname (optional)
If you want a more descriptive or user-friendly hostname for display purposes, use:
sudo hostnamectl set-hostname "My Awesome Server" --pretty
Pretty hostnames are purely decorative and do not affect network communications.
Step 4: Set temporary host name (optional)
To set a hostname that lasts only the current session:
sudo hostnamectl set-hostname temporary-hostname --transient
Temporary host names are often used for testing or debugging.
Step 5: Verify the new hostname
After setting the host name, confirm the changes:
hostnamectl
Output example:
Static hostname: workstation06
Pretty hostname: My Awesome Server
Icon name: computer-server
Chassis: server
Machine ID: 2c9a10b57b9248e7bdf1c78b0f9ea8e1
Boot ID: 8e4bb01e03ef4147bbeb2c5bc74f8a6f
Operating System: Linux
Kernel: Linux 6.2.0-43-generic
Architecture: x86-64
Update hosts file
Changing hostname does not update automatically /etc/hosts
document. Edit it manually to avoid local name resolution issues:
sudo nano /etc/hosts
Make sure it contains an entry like this:
127.0.1.1 workstation06
Save and close the file.
Extra tips
-
Use a valid hostname: The host name can only contain alphanumeric characters, hyphens (
-
) and point (.
). Avoid starting or ending with a hyphen. - Restart network service: Some services may require a restart to recognize the new hostname:
sudo systemctl restart networking
- Host name restrictions: Prevent multiple machines on the same network from using the same host name to prevent conflicts.
in conclusion
Setting a hostname on Linux is quick and easy using the following command hostnamectl
Order. Whether you’re setting up a server, developing a computer, or just experimenting, this guide covers everything you need to know to manage hostnames effectively.
If you have questions or tips about managing hostnames in Linux, feel free to leave a comment below!
What do you think? Did this guide help you with your hostname related tasks? Let me know! 😊