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
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:

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.
- 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.
- Before making changes backup critical files using
sudo cp /etc/hosts /etc/hosts.bak
sudo cp /etc/hostname /etc/hostname.bak
- Use FQDNs like server.example.com for hostnames, especially for systems that are exposed to external networks or used in enterprise environments.
- After updating the hostname, verify and align it in:
- /etc/hostname
- /etc/hosts
- hostnamectl output
- Any DNS or firewall configurations
- Avoid hardcoding values and IP addresses.
- Use resolvectl or cat /etc/resolv.conf to check DNS resolver settings. Ensure nameservers are reachable.
- For more advanced setups, leverage systemd-resolved to manage split DNS, search domains, and fallback servers.
- 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
Issue | Possible Cause | Solution | Example |
Hostname not updating after reboot | Hostname was changed using hostname command only (temporary change) | Use hostnamectl set-hostname newname for permanent change | hostnamectl set-hostname myserver |
Hostname still shows old name in shell prompt | Shell session wasn’t restarted or /etc/hosts still uses the old name | Restart shell or update /etc/hosts to reflect the new hostname | Update 127.0.1.1 line in /etc/hosts with new hostname |
Cannot resolve local domain | /etc/hosts missing entry or incorrect IP mapping | Add proper entry in /etc/hosts | 192.168.1.100 dev.local |
Ping to hostname fails | DNS fallback fails and hostname isn’t listed in /etc/hosts | Add hostname-IP pair in /etc/hosts or ensure DNS is configured | Add myserver.local to /etc/hosts or check /etc/resolv.conf |
Permission denied when editing /etc/hosts | Editing as non-root user | Use sudo to open file in text editor | sudo nano /etc/hosts |
Remote desktop uses wrong name | Hostname isn’t set correctly or DNS maps to wrong name | Set hostname properly and check DNS mapping | hostnamectl set-hostname rdp-server and verify with hostname |
Conflicting hostnames on LAN | Multiple machines using the same hostname | Ensure unique hostnames on the network | Rename one using hostnamectl and update /etc/hosts |
Custom domain doesn’t load in browser | Entry missing in /etc/hosts, or browser caches DNS | Add entry and clear browser DNS cache | 127.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