Home / Linux / How to Set Hostname and Edit /etc/hosts

How to Set Hostname and Edit /etc/hosts

Set a server hostname correctly and map names in /etc/hosts, with verification commands and common pitfalls.

Views: 23 Unique: 20 Updated: 2026-03-22

What this is

This guide explains how to set the Linux hostname and edit /etc/hosts for local name resolution.

What it is for

  • Give your VPS a clean, recognizable name
  • Fix issues where services require a resolvable hostname
  • Map internal names to IPs for convenience

Prerequisites

  • SSH access + sudo

Step-by-step

Step 1) Check current hostname

hostname
hostnamectl

Step 2) Set a new hostname (recommended way)

sudo hostnamectl set-hostname vps-01

Expected output: Usually silent.

Step 3) Edit /etc/hosts

sudo nano /etc/hosts

Add or adjust a line like:

127.0.1.1 vps-01

Why: Prevents local resolution problems on some distributions.

Step 4) Verify

hostnamectl
getent hosts vps-01

Warnings & notes

  • /etc/hosts does not change public DNS; it only affects the local machine.
  • Pick a hostname using letters, numbers, and hyphens (no spaces).

Conclusion

Your server now has a consistent hostname and local mapping. This helps with administration and avoids some service warnings.

Back to category