Your hostname is basically your computer’s identity on a network.
It’s how other systems recognize your machine especially useful when managing servers, connecting through SSH, or organizing multiple devices.
If you’ve ever set up a fresh Linux installation, you might have noticed your system name appears in the terminal prompt something like:
pratham@ubuntu:~$
Here, ubuntu
is your hostname.
Changing it is simple, but depending on your distribution and version, there are a few different methods. Let’s go step-by-step.
1. Check Your Current Hostname
Before you change it, see what your hostname is right now.
Use either of these commands:
hostname
or
hostnamectl
Example output:
Static hostname: ubuntu
Pretty hostname: (none)
Icon name: computer-laptop
Chassis: laptop
Machine ID: 1234567890abcd
Boot ID: 9876543210abcd
Operating System: Ubuntu 22.04.3 LTS
Kernel: Linux 5.15.0-91-generic
Architecture: x86-64
The “Static hostname” is what your system currently uses.
2. Set a New Hostname Using hostnamectl
(Recommended)
The simplest and most modern method is with hostnamectl
.
Command:
sudo hostnamectl set-hostname new-hostname
Example:
sudo hostnamectl set-hostname prats-server
Now, check the change:
hostnamectl
Output:
Static hostname: prats-server
Why this method is best:
- Works on all modern distros (Ubuntu, Fedora, CentOS, Debian).
- Updates both the hostname and related system records automatically.
- Doesn’t require editing system files manually.
Note: You might need to log out and log back in (or restart the terminal) to see the new hostname reflected in your shell prompt.
3. Temporary Hostname Change (Until Reboot)
If you only want to change the hostname temporarily — maybe for testing or local use — you can use the hostname
command directly.
Command:
sudo hostname new-hostname
Example:
sudo hostname temp-server
Limitation:
This change lasts only until the next reboot. After restarting, your system will revert to the previous hostname.
When to use it:
Useful for temporary testing environments, container setups, or scripts that don’t need permanent changes.
4. Permanently Change Hostname by Editing /etc/hostname
You can also manually edit the hostname configuration file.
Steps:
- Open the file with root privileges:
sudo nano /etc/hostname
- Replace the existing name with your new hostname.
- Save and exit (Ctrl + O, Enter, Ctrl + X).
Example:
If it says:
ubuntu
Change it to:
prats-server
Then also update your hosts file:
sudo nano /etc/hosts
Change this line:
127.0.1.1 ubuntu
to
127.0.1.1 prats-server
Finally, apply the change without rebooting:
sudo hostnamectl set-hostname prats-server
Why it matters:
Manually editing these files ensures system consistency — especially on older Linux versions that don’t use systemd
.
5. Change Hostname on Older Systems (Without hostnamectl
)
Some older Linux distributions (like CentOS 6 or Debian 7) don’t support hostnamectl
.
In that case, use the legacy method.
a. Edit /etc/sysconfig/network
(for CentOS/RHEL 6)
sudo nano /etc/sysconfig/network
Find the line:
HOSTNAME=oldname
and replace it with:
HOSTNAME=new-hostname
Save the file and reboot.
b. Edit /etc/hostname
(for Debian/Ubuntu older versions)
sudo nano /etc/hostname
Replace the old name, then run:
sudo service hostname restart
6. Apply the New Hostname Without Reboot
If you’ve edited files manually, apply the change immediately with:
sudo systemctl restart systemd-logind.service
or simply:
sudo hostnamectl
to refresh the hostname configuration.
Alternative method:
exec bash
This reloads your shell so the prompt updates instantly.
7. Verify the Change
Run any of these commands to confirm your hostname is updated:
hostname
or
hostnamectl
You can also ping your hostname:
ping -c 2 $(hostname)
If everything’s configured properly, it’ll respond to itself.
8. Bonus: Set Pretty Hostname (Optional)
Linux supports a “pretty” hostname — a more readable name that can include spaces and special characters.
To set it:
sudo hostnamectl set-hostname "Prats Linux Workstation" --pretty
Why it’s used:
It’s mainly for display purposes in graphical interfaces (like login screens or system info tools), not in network configurations.
9. Example: Setting Hostname for a Server
Let’s say you’re setting up a web server.
You want your server to have a unique identity like web-prod-01
.
Steps:
sudo hostnamectl set-hostname web-prod-01
sudo nano /etc/hosts
Edit:
127.0.1.1 web-prod-01
Then verify:
hostnamectl
Output:
Static hostname: web-prod-01
Done your server now has a professional, network-friendly hostname.
Common Hostname Naming Tips
When setting hostnames:
- Use lowercase letters, numbers, and hyphens only.
- Avoid spaces or underscores.
- Keep it short (under 63 characters).
- Choose something meaningful — e.g.:
web-server-1
dev-machine
db-prod-node
These conventions make your infrastructure easier to manage and identify — especially if you handle multiple systems.
10. Quick Comparison of Methods
Method | Command | Permanent | Recommended For |
---|---|---|---|
hostnamectl set-hostname | ✅ | Yes | All modern Linux distros |
hostname newname | ⚠️ | No | Temporary testing |
Edit /etc/hostname | ✅ | Yes | Manual setups |
Edit /etc/sysconfig/network | ✅ | Yes | Older RHEL/CentOS |
GUI Settings | ✅ | Yes | Desktop environments |
11. GUI Method (For Desktop Users)
If you’re using a Linux desktop like Ubuntu or Fedora, you can also change the hostname through settings:
On Ubuntu (GNOME):
- Open Settings → About
- Click Device Name
- Enter your new hostname and save
On Fedora:
- Open Settings → About
- Edit Device Name
The system updates the hostname automatically without any command-line work.
Why Setting the Hostname Matters
Your hostname isn’t just cosmetic it’s crucial for:
- Network identification
- SSH access and remote management
- Logging and monitoring clarity
- Automation scripts and Ansible playbooks
In servers, having consistent and clear hostnames helps avoid confusion when managing multiple environments (production, staging, test).
Conclusion
Changing your hostname in Linux is a simple but essential task for personalization, network clarity, and system management.
The easiest and most reliable method is:
sudo hostnamectl set-hostname new-hostname
It works across modern distributions and updates your system instantly.
Whether you’re customizing your desktop or naming your new cloud server, setting the right hostname keeps things organized and professional.