VPS Hosting with Ubuntu: Manage Hostnames and Hosts File for Seamless Connections

VPS Hosting with Ubuntu

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

When you are running a VPS hosting with Ubuntu, configuring hostnames and the host file correctly is important for smooth networking, remote access, and internal name resolution. So whether you are setting up a remote connection or customizing your hostname, these tasks are essential for all. 

In this guide, we will learn about how to manage host files, change the hostname, and understand the VPS hosting with Ubuntu. 

Understanding the /etc/hosts File in Ubuntu

The /etc/hosts file in Ubuntu is a local configuration file that maps IP addresses to hostnames. It is the first place that a system looks while resolving the domain names, before reaching out to the DNS servers. This makes it super useful for overriding DNS, setting up testing environments, or resolving names on the local networks. 

This file generally includes entries like 127.0.0.1 localhost, and you can add custom entries as needed for your use case.

Example:

127.0.0.1   localhost 

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

127.0.1.1   ubuntu-vps                                                                           

192.168.1.50   dev.local

In the above example:

  • localhost resolves to 127.0.0.1 (loopback).
  • ubuntu-vps is a custom hostname for your machine.
  • dev.local will resolve to a local IP for internal testing—useful for simulating DNS on a dev environment.

This file is critical for scenarios like:

  • Mapping domains in the local development (e.g., myapp.test to 127.0.0.1)
  • Blocking specific domains (e.g., redirecting them to 0.0.0.0)
  • Ensuring internal network machines are accessible by hostname

Related Article: KVM VPS Hosting: What It Is, How It Works, and Why It Matters

How to Edit the Hosts File in Ubuntu

To edit the /etc/hosts file, you need to have root permissions. You can open it with a text editor like nano using the following command:

sudo nano /etc/hosts

Once open, you can add new lines following the format:

<IP address>    <hostname>    [aliases]

For example:

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

192.168.1.50    test.local

After making all these changes, you can save the file and exit the command line interface. The edits will take place immediately, you do not need to reboot. 

How to Change the Hostname in Ubuntu

Ubuntu uses a system hostname to identify the machine on a network. You can see what your recent current hostname using the command: 

hostname

You can change it temporarily until the next reboot using: 

sudo hostname new-hostname

For a more permanent change, you should also edit the /etc/hostname and /etc/hosts files manually or use the hostnamectl command:

sudo hostnamectl set-hostname new-hostname

Can You Set Host Entries Per User in Ubuntu?

In Ubuntu, the /etc/hosts file applies to the entire system. This generally means that any changes that you make to the system will affect all users, not just one. Ubuntu does not have a built-in way to set the host entries for individual users. 

However, some of the applications might allow you to set custom DNS or proxy settings for one user. If you need a separate host mapping for different users, you can always use tools like the Docker or custom DNS setups to create isolated environments. 

Setting a Hostname for Remote Desktop Connections in Ubuntu

When you are connected to an Ubuntu system using the Remote Desktop (e.g. RDP or VNC), the client will usually display the system’s hostname. You can do so by: 

sudo hostnamectl set-hostname your-desired-name

Make sure that the name is reflected in your /etc/hostname and /etc/hosts files. Furthermore, if you are accessing Ubuntu over a network, you can assign it a static IP and set a DNS record or host file entry on the client machine to resolve the hostname easily. 

Best Practices for Managing Host and DNS Settings on a VPS

To make sure that you make the most out of VPS hosting with Ubuntu, keep a track of these best practices. 

  1. Always use the hostnamectl set-hostname yourhostname to change the system’s host name as it ensures persistence across reboots and updates the related files. 
  2. Before making changes backup critical files using
    sudo cp /etc/hosts /etc/hosts.bak

sudo cp /etc/hostname /etc/hostname.bak

  1. Use FQDNs like server.example.com for hostnames, especially for systems that are exposed to external networks or used in enterprise environments. 
  2. After updating the hostname, verify and align it in:
  • /etc/hostname
  • /etc/hosts
  • hostnamectl output
  • Any DNS or firewall configurations
  1. Avoid hardcoding values and IP addresses. 
  2. Use resolvectl or cat /etc/resolv.conf to check DNS resolver settings. Ensure nameservers are reachable.
  3. For more advanced setups, leverage systemd-resolved to manage split DNS, search domains, and fallback servers.
  4. Add entries to /etc/hosts only when DNS resolution is not an option or for development/test purposes:
    127.0.0.1   local.test myapp.local

Troubleshooting Common Hostname and Hosts File Issues in Ubuntu

IssuePossible CauseSolutionExample
Hostname not updating after rebootHostname was changed using hostname command only (temporary change)Use hostnamectl set-hostname newname for permanent changehostnamectl set-hostname myserver
Hostname still shows old name in shell promptShell session wasn’t restarted or /etc/hosts still uses the old nameRestart shell or update /etc/hosts to reflect the new hostnameUpdate 127.0.1.1 line in /etc/hosts with new hostname
Cannot resolve local domain/etc/hosts missing entry or incorrect IP mappingAdd proper entry in /etc/hosts192.168.1.100 dev.local
Ping to hostname failsDNS fallback fails and hostname isn’t listed in /etc/hostsAdd hostname-IP pair in /etc/hosts or ensure DNS is configuredAdd myserver.local to /etc/hosts or check /etc/resolv.conf
Permission denied when editing /etc/hostsEditing as non-root userUse sudo to open file in text editorsudo nano /etc/hosts
Remote desktop uses wrong nameHostname isn’t set correctly or DNS maps to wrong nameSet hostname properly and check DNS mappinghostnamectl set-hostname rdp-server and verify with hostname
Conflicting hostnames on LANMultiple machines using the same hostnameEnsure unique hostnames on the networkRename one using hostnamectl and update /etc/hosts
Custom domain doesn’t load in browserEntry missing in /etc/hosts, or browser caches DNSAdd entry and clear browser DNS cache127.0.0.1 myapp.local and restart browser

Conclusion: Managing VPS Hosting With Ubuntu For Networking the Right Way

Properly managing hostnames and DNS settings on your Ubuntu VPS is crucial for remote access, internal resolution, and smooth application performance. By following the best practices and troubleshooting smartly, you can ensure that your VPS hosting with Ubuntu yields the right results. 

FAQs

What should I do if changes to the hosts file don’t work?

Ensure the file syntax is correct, clear your DNS cache, and verify that no external DNS settings are overriding your local configuration.

How do I change the hostname of my Ubuntu VPS?

You can change the hostname using the hostnamectl command:
sudo hostnamectl set-hostname new-hostname

How do I edit the hosts file in Ubuntu?

You can edit the /etc/hosts file using a text editor like nano or vim with root privileges:
sudo nano /etc/hosts

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!