> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Change the Hostname on Linux (Ubuntu, Debian, AlmaLinux)

> Change the hostname on Linux with sudo hostnamectl set-hostname server1.example.com, update /etc/hosts, then log in again. No reboot needed. Works on all distros.

To change the hostname on Linux, run `sudo hostnamectl set-hostname server1.example.com`, then edit `/etc/hosts` so the new name points to the server (on Ubuntu and Debian, the `127.0.1.1` line). The change applies at once, with no reboot; open a new SSH session to see it in your prompt. Check it with `hostnamectl`. This works the same on Ubuntu, Debian, AlmaLinux and Rocky Linux.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 5 minutes | Ubuntu 22.04 / 24.04, Debian 12 / 13, AlmaLinux / Rocky 9 / 10 | sudo or root access |

## Before you start

* [Connect with SSH](https://help.freakhosting.com/portals/other/how-to-connect-to-a-linux-server-with-ssh.md) as a [sudo user](https://help.freakhosting.com/portals/other/how-to-create-a-sudo-user-on-linux.md). Root can leave out `sudo`.
* A **hostname** is the name the server uses for itself. Use a full domain name you control (an FQDN), such as `server1.example.com`, especially if the server sends email. Allowed characters are letters, digits and hyphens, with dots between parts.
* On a VPS, the name shown in the VPS Panel is separate. See [rename your VPS and set a hostname](https://help.freakhosting.com/portals/vps-panel/how-to-rename-your-vps-and-set-a-hostname.md): a hostname set there is only applied when the server is rebuilt, so change it inside the server as below.

## How to check the current hostname

```bash theme={null}
hostnamectl
```

Example output (first lines):

```text theme={null}
 Static hostname: vps-12345
       Icon name: computer-vm
         Chassis: vm
```

`hostname` prints just the short name; `hostname -f` prints the full name.

## How to change the hostname

**1. Set the new hostname**

```bash theme={null}
sudo hostnamectl set-hostname server1.example.com
```

This updates `/etc/hostname` and the running system. It prints nothing when it succeeds.

**2. Update /etc/hosts**

```bash theme={null}
sudo nano /etc/hosts
```

> [tab] Ubuntu / Debian
>
> Change the `127.0.1.1` line (add it if missing):
>
> ```text theme={null}
> 127.0.0.1   localhost
> 127.0.1.1   server1.example.com server1
> ```

> [tab] AlmaLinux / Rocky
>
> Add a line with your server's public IP address:
>
> ```text theme={null}
> 127.0.0.1     localhost localhost.localdomain
> 203.0.113.10  server1.example.com server1
> ```

Leave the `localhost` lines as they are. Save with `Ctrl+O`, `Enter`, `Ctrl+X`.

**3. Open a new session**

Log out and back in. Your prompt now shows `alex@server1`.

> [!NOTE]
>
> If `sudo` prints `unable to resolve host server1.example.com`, the new name is missing from `/etc/hosts`. The command still runs; fix `/etc/hosts` as in step 2 and the message disappears.

## Why does my hostname change back after a reboot?

Many cloud and VPS images use **cloud-init**, which can set the hostname again at every boot from the platform's data. To keep your own hostname, tell cloud-init to leave it alone:

**1. Check whether cloud-init is installed**

```bash theme={null}
cloud-init status
```

If the command isn't found, cloud-init isn't the cause.

**2. Set preserve\_hostname**

```bash theme={null}
sudo nano /etc/cloud/cloud.cfg.d/99-hostname.cfg
```

Add:

```yaml theme={null}
preserve_hostname: true
```

**3. Reboot and check**

```bash theme={null}
sudo reboot
```

After reconnecting, run `hostnamectl` again.

## How to set reverse DNS to match

For a mail server, the IP address should resolve back to the same name (reverse DNS, or PTR record), and the name should resolve to the IP (an `A` record at your DNS provider). On a VPS, set the PTR record in the panel: [configure reverse DNS](https://help.freakhosting.com/portals/vps-panel/how-to-configure-reverse-dns.md). For a dedicated server, [open a ticket](https://freakhosting.com/clientarea/submitticket.php). To create the `A` record, see [point a domain to your server](https://help.freakhosting.com/portals/other/how-to-point-a-domain-to-your-server.md).

## How to check it worked

```bash theme={null}
hostnamectl | grep "Static hostname"
hostname -f
getent hosts server1.example.com
```

Expected output on Ubuntu/Debian:

```text theme={null}
 Static hostname: server1.example.com
server1.example.com
127.0.1.1       server1.example.com server1
```

## Frequently asked questions

### Do I need to reboot after changing the hostname?

No. `hostnamectl set-hostname` applies at once. Some services read the hostname only at start-up, such as a mail server (Postfix `myhostname`) or monitoring agents; restart those.

### What is the difference between static, transient and pretty hostnames?

The **static** hostname is stored in `/etc/hostname` and used at boot. The **transient** hostname is the one the kernel uses right now, normally the same. The **pretty** hostname is a free-text label such as "Web server 1" (`sudo hostnamectl set-hostname --pretty "Web server 1"`).

### Can I just edit /etc/hostname?

Yes, but the running system keeps the old name until you reboot or run `sudo hostname server1.example.com`. `hostnamectl` does both in one step.

### Does changing the hostname break anything?

Usually not. Check anything that has the old name in its config, for example `/etc/postfix/main.cf`, SSL certificates issued for the old name, or monitoring tools.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Rename your VPS](https://help.freakhosting.com/portals/vps-panel/how-to-rename-your-vps-and-set-a-hostname.md)
>
> The panel name and hostname.

> [card] [Configure reverse DNS](https://help.freakhosting.com/portals/vps-panel/how-to-configure-reverse-dns.md)
>
> Match your IP to the hostname.

> [card] [Check Linux version and system info](https://help.freakhosting.com/portals/other/how-to-check-your-linux-version-and-system-info.md)
>
> hostnamectl and more.

> [card] [Set the time zone](https://help.freakhosting.com/portals/other/how-to-set-the-timezone-and-time-sync-on-linux.md)
>
> Another first-day setting.

***

> [!NOTE]
>
> **Checked against the official [hostnamectl manual](https://www.freedesktop.org/software/systemd/man/latest/hostnamectl.html) and the [cloud-init documentation](https://docs.cloud-init.io/en/25.1/reference/yaml_examples/update_hostname.html)** on 4 November 2026. Our test container doesn't run systemd, so `hostnamectl` was not run there. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.