How to Set Hostname and Configure /etc/hosts on Ubuntu

Last updated: May 1, 2025 | by AlphaDev Team

Setting the hostname and configuring the /etc/hosts file are essential tasks for system identification and local network communication. This step-by-step guide is tailored for Ubuntu users who want to configure their systems properly.

1. Check the Current Hostname

Use this command to view the current hostname:

hostname

2. Set a New Hostname

Replace new-hostname with your desired hostname:

sudo hostnamectl set-hostname new-hostname

3. Edit the /etc/hosts File

To map your hostname locally:

127.0.1.1 new-hostname

4. Test the Configuration

Use hostname or ping new-hostname to verify everything works correctly.

5. Reboot the System (Optional)

Reboot to apply changes:

sudo reboot

Conclusion

With your hostname updated and /etc/hosts configured, your Ubuntu system is now better prepared for local networking and system management.