Updated: 4 November 2026 · Applies to: hostnamectl on Fedora 44 (systemd 259), Rocky Linux 9 (systemd 252, same as RHEL 9) and Ubuntu 24.04 (systemd 255); the same commands work on AlmaLinux, RHEL 10 and Debian 13
To change the hostname permanently in Linux, run sudo hostnamectl set-hostname server1.example.com. It writes the new name to /etc/hostname, so it survives a reboot, and changes the running name at the same time, so no reboot is needed. The command is the same on Fedora, Rocky Linux, AlmaLinux, RHEL 9 and 10, Ubuntu and Debian.
sudo hostnamectl set-hostname server1.example.com # set it (permanent) hostnamectl # check it
We tested the commands in containers on our test server. A container cannot change its running (kernel) hostname, so some outputs below come from containers started with the name already set; the commands behave the same on a normal server.
1. Set the hostname on Fedora, Rocky Linux, RHEL or Ubuntu
- See the current name. Look at the
Static hostnameline (the permanent one):hostnamectl
- Set the new name. On a server, use a fully qualified domain name (FQDN) such as
server1.example.com:sudo hostnamectl set-hostname server1.example.com
By default this sets all three names systemd knows: the static name (saved in
/etc/hostname), the transient name (the one the kernel uses now) and the pretty name. Newer systemd versions also accepthostnamectl hostname server1.example.com, the name the current systemd manual uses; systemd 252 (Rocky Linux 9) and 259 (Fedora 44) both accepted it. - Check the saved name:
$ cat /etc/hostname web1.example.com $ hostnamectl hostname --static web1.example.com
- Your shell prompt still shows the old name until you open a new session (log out and in, or run
exec bash).
Allowed names: lowercase letters, digits and hyphens, with dots between the parts, at most 64 characters (according to the systemd manual). Spaces and underscores are refused:
$ sudo hostnamectl set-hostname --static "web_1 test" Could not set static hostname: Invalid static hostname 'web_1 test'
Fedora 44 words it as Invalid hostname 'web_1 test'. If you want a display name with spaces, set it as the pretty name: sudo hostnamectl set-hostname --pretty "Web Server 1". When we passed "Web Server 1" without --pretty, systemd kept it as the pretty name and saved WebServer1 as the static name.
2. Set the domain name (FQDN)
Linux has no separate setting for the DNS domain. The domain is the part of the FQDN after the first dot, and hostname -f, hostname -d and dnsdomainname read it from /etc/hosts or DNS. To set it:
- Set the hostname to the full name, as in step 1.
- Add a line to
/etc/hostswith the server's IP address, the FQDN and the short name (use your own IP address):203.0.113.10 web1.example.com web1
- Check the result:
$ hostname web1.example.com $ hostname -f web1.example.com $ hostname -s web1 $ hostname -d example.com
Without the /etc/hosts line, hostname -f and hostname -d failed in our test with hostname: No address associated with hostname. Also create an A record for the name in DNS. If the server sends email, its reverse DNS (PTR) should match the name: your provider sets that.
Do not use the domainname command for this: it shows the NIS domain, an old directory service setting, and printed (none) in our test. The DNS search domain (the search line in /etc/resolv.conf) is a separate network setting.
3. Other ways to change the hostname
- nmcli (systems that use NetworkManager, such as Fedora and the RHEL family):
sudo nmcli general hostname server1.example.com. According to the NetworkManager manual it hands the name to NetworkManager, which stores it in/etc/hostnameon most distributions. - Editing /etc/hostname directly also makes the name permanent, but the file is read at boot, so the change takes effect only after a reboot.
- The hostname command (
sudo hostname newname) changes only the running name. It is lost at the next reboot.
4. The hostname changes back after a reboot
- cloud-init (used by many cloud images): it can set the name from its metadata at every boot. Ubuntu 24.04 with cloud-init 26.1 has
preserve_hostname: falsein/etc/cloud/cloud.cfg. Create/etc/cloud/cloud.cfg.d/99-keep-hostname.cfgwith the linepreserve_hostname: true. Ifmanage_etc_hosts: trueis set, cloud-init also rewrites/etc/hostsat boot from a template in/etc/cloud/templates/, so edit the template instead. - Ucartz VPS: change the hostname in the Virtualizor panel as well, so both match: see how to change your VPS hostname in Virtualizor.
- cPanel servers: use WHM, Networking Setup > Change Hostname, so cPanel updates its own configuration. According to cPanel, the name must be an FQDN of at most 60 characters that no cPanel account on the server uses.
More on the hostname command and its options: Linux hostname command examples; a short hostnamectl overview: how to set or change the hostname with hostnamectl. For the older CentOS 7 steps, see how to set or change the hostname in CentOS 7.
Official documentation: hostnamectl manual, nmcli manual and cPanel Change Hostname.