{"id":50760,"date":"2026-03-22T23:29:53","date_gmt":"2020-04-10T12:16:31","guid":{"rendered":"https:\/\/computingforgeeks.com\/?p=50760"},"modified":"2026-03-22T23:29:54","modified_gmt":"2026-03-22T20:29:54","slug":"how-to-change-hostname-on-linux-using-nmcli","status":"publish","type":"post","link":"https:\/\/computingforgeeks.com\/how-to-change-hostname-on-linux-using-nmcli\/","title":{"rendered":"Change Hostname on Linux"},"content":{"rendered":"\n<p>Every Linux system has a hostname &#8211; a label that identifies the machine on a network. You need to change it when provisioning new servers, renaming machines to match your naming convention, or fixing a default hostname assigned by a cloud provider. Linux gives you several ways to do this, from the recommended <code>hostnamectl<\/code> command to <code>nmcli<\/code> and direct file edits.<\/p>\n\n\n\n<p>This guide covers all methods to change the hostname on Linux systems including RHEL 10, Rocky Linux 10, AlmaLinux 10, Ubuntu 24.04, Debian 13, and Fedora 42. Each method works on any modern Linux distribution running <a href=\"https:\/\/www.freedesktop.org\/software\/systemd\/man\/latest\/hostnamectl.html\" target=\"_blank\" rel=\"noreferrer noopener\">systemd<\/a>, which covers nearly every server and desktop distribution in active use today.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Linux server or workstation (RHEL 10, Rocky Linux 10, AlmaLinux 10, Ubuntu 24.04, Debian 13, or Fedora 42)<\/li>\n\n<li>Root or sudo access<\/li>\n\n<li>SSH access if configuring a remote server<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Check the Current Hostname<\/h2>\n\n\n\n<p>Before making any changes, check what hostname your system currently uses. The <code>hostnamectl<\/code> command shows all hostname types along with system details.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostnamectl<\/code><\/pre>\n\n\n\n<p>The output displays the static hostname, icon name, chassis type, and OS information:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Static hostname: localhost.localdomain\n       Icon name: computer-vm\n         Chassis: vm\n      Machine ID: 3f47d43533514bb6bcc03d51b7468459\n         Boot ID: a8b2c1d9ef124567890abcdef1234567\n  Virtualization: kvm\nOperating System: Rocky Linux 10.0 (Smoky Quartz)\n     CPE OS Name: cpe:\/o:rocky:rocky:10\n          Kernel: Linux 6.12.0-55.el10.x86_64\n    Architecture: x86-64<\/code><\/pre>\n\n\n\n<p>You can also use the <code>hostname<\/code> command for a quick one-line check:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostname<\/code><\/pre>\n\n\n\n<p>This returns just the hostname without any extra system details:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>localhost.localdomain<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Change Hostname with hostnamectl (Recommended)<\/h2>\n\n\n\n<p>The <code>hostnamectl<\/code> command is the standard way to set the hostname on any Linux system running systemd. It updates the static hostname in <code>\/etc\/hostname<\/code> and notifies all running services of the change &#8211; no reboot required.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostnamectl set-hostname server01.example.com<\/code><\/pre>\n\n\n\n<p>Confirm the change took effect immediately:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostnamectl<\/code><\/pre>\n\n\n\n<p>The static hostname line should now show your new hostname:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Static hostname: server01.example.com\n       Icon name: computer-vm\n         Chassis: vm<\/code><\/pre>\n\n\n\n<p>Linux actually maintains three types of hostnames. The <code>hostnamectl set-hostname<\/code> command sets all three at once, but you can target each one individually:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Static hostname<\/strong> &#8211; stored in <code>\/etc\/hostname<\/code>, persists across reboots. This is the one you usually care about<\/li>\n\n<li><strong>Transient hostname<\/strong> &#8211; assigned at runtime by DHCP or mDNS, resets on reboot<\/li>\n\n<li><strong>Pretty hostname<\/strong> &#8211; a free-form UTF-8 label for display purposes (can include spaces and special characters)<\/li>\n<\/ul>\n\n\n\n<p>To set each type separately:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostnamectl set-hostname server01.example.com --static\nsudo hostnamectl set-hostname server01.example.com --transient\nsudo hostnamectl set-hostname \"Production Web Server 01\" --pretty<\/code><\/pre>\n\n\n\n<p>After setting the hostname, update <code>\/etc\/hosts<\/code> to match so that local name resolution works correctly. If you have <a href=\"https:\/\/computingforgeeks.com\/configure-static-ip-on-rocky-almalinux\/\" target=\"_blank\" rel=\"noreferrer noopener\">static IP configured on your server<\/a>, use that IP. Otherwise use the loopback address.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vi \/etc\/hosts<\/code><\/pre>\n\n\n\n<p>Add or update the line that maps your IP to the new hostname:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>127.0.0.1   localhost\n127.0.1.1   server01.example.com server01\n::1         localhost ip6-localhost ip6-loopback<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Change Hostname with nmcli<\/h2>\n\n\n\n<p>NetworkManager&#8217;s <a href=\"https:\/\/networkmanager.dev\/docs\/api\/latest\/nmcli.html\" target=\"_blank\" rel=\"noreferrer noopener\">nmcli<\/a> command line tool can also set the system hostname. This method is useful on systems where <code>hostnamectl<\/code> doesn&#8217;t persist the hostname across reboots &#8211; something that happens on Red Hat CoreOS (RHCOS) and Fedora CoreOS (FCOS) systems where the <code>\/etc\/hostname<\/code> file gets overwritten during updates.<\/p>\n\n\n\n<p>Set the hostname with <code>nmcli<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nmcli general hostname server01.example.com<\/code><\/pre>\n\n\n\n<p>Verify the hostname was set by querying NetworkManager:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nmcli general hostname<\/code><\/pre>\n\n\n\n<p>The command should return your new hostname:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server01.example.com<\/code><\/pre>\n\n\n\n<p>After changing the hostname with <code>nmcli<\/code>, restart the systemd-hostnamed service so other tools pick up the change:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart systemd-hostnamed<\/code><\/pre>\n\n\n\n<p>This method writes the hostname to <code>\/etc\/hostname<\/code> through NetworkManager&#8217;s D-Bus interface, which makes it persistent across reboots. On <a href=\"https:\/\/computingforgeeks.com\/how-to-set-static-ip-address-on-rhcos-fcos-machine\/\" target=\"_blank\" rel=\"noreferrer noopener\">RHCOS and FCOS systems<\/a>, nmcli is the preferred method because it integrates with the immutable OS configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Change Hostname via \/etc\/hostname and \/etc\/hosts<\/h2>\n\n\n\n<p>On older systems without systemd or when you need to script hostname changes without installing extra tools, you can edit the configuration files directly. This method works on every Linux distribution.<\/p>\n\n\n\n<p>Open the hostname file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vi \/etc\/hostname<\/code><\/pre>\n\n\n\n<p>Replace the entire contents with your new hostname &#8211; the file should contain a single line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server01.example.com<\/code><\/pre>\n\n\n\n<p>Next, update <code>\/etc\/hosts<\/code> to map the hostname to your server&#8217;s IP address:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vi \/etc\/hosts<\/code><\/pre>\n\n\n\n<p>Ensure your hostname appears on the 127.0.1.1 line (or your server&#8217;s static IP):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>127.0.0.1   localhost\n127.0.1.1   server01.example.com server01\n::1         localhost ip6-localhost ip6-loopback<\/code><\/pre>\n\n\n\n<p>Apply the change without rebooting by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostname server01.example.com<\/code><\/pre>\n\n\n\n<p>The <code>hostname<\/code> command sets the transient hostname for the current session. Combined with the <code>\/etc\/hostname<\/code> edit, the change is both immediate and persistent.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Change Hostname Temporarily<\/h2>\n\n\n\n<p>Sometimes you need a temporary hostname change that reverts on reboot &#8211; for testing, debugging, or short-lived environments. The <code>hostname<\/code> command sets only the transient hostname without touching <code>\/etc\/hostname<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostname temp-debug-server<\/code><\/pre>\n\n\n\n<p>Verify the temporary change:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostname<\/code><\/pre>\n\n\n\n<p>The output shows the temporary hostname:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>temp-debug-server<\/code><\/pre>\n\n\n\n<p>You can also use <code>hostnamectl<\/code> with the <code>--transient<\/code> flag for the same effect:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostnamectl set-hostname temp-debug-server --transient<\/code><\/pre>\n\n\n\n<p>After the next reboot, the system reverts to the static hostname stored in <code>\/etc\/hostname<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 6: Change Hostname on Cloud Instances (AWS, Azure, GCP)<\/h2>\n\n\n\n<p>Cloud instances have an extra layer of hostname management. Cloud providers assign hostnames through metadata services, and cloud-init can overwrite your changes on reboot. Here is how to make hostname changes stick on each major platform.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">AWS EC2<\/h3>\n\n\n\n<p>On AWS EC2 instances, cloud-init resets the hostname on every boot by default. Set the hostname first, then tell cloud-init to preserve it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostnamectl set-hostname web-prod-01.example.com<\/code><\/pre>\n\n\n\n<p>Edit the cloud-init configuration to prevent hostname resets:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vi \/etc\/cloud\/cloud.cfg<\/code><\/pre>\n\n\n\n<p>Find the <code>preserve_hostname<\/code> setting and change it to <code>true<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>preserve_hostname: true<\/code><\/pre>\n\n\n\n<p>This prevents cloud-init from overwriting your <code>\/etc\/hostname<\/code> on subsequent boots. If you also set a Name tag on the EC2 instance through the AWS console, the hostname and tag will stay consistent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Azure<\/h3>\n\n\n\n<p>Azure Linux VMs use the waagent (Azure Linux Agent) alongside cloud-init. Set the hostname and update the cloud-init config the same way:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostnamectl set-hostname app-server-01.example.com<\/code><\/pre>\n\n\n\n<p>Then disable cloud-init hostname management:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vi \/etc\/cloud\/cloud.cfg<\/code><\/pre>\n\n\n\n<p>Set the preserve_hostname directive:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>preserve_hostname: true<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Google Cloud Platform (GCP)<\/h3>\n\n\n\n<p>GCP instances use Google&#8217;s guest agent to manage hostnames. The process is the same &#8211; set the hostname and disable cloud-init&#8217;s hostname module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostnamectl set-hostname db-primary-01.example.com<\/code><\/pre>\n\n\n\n<p>Prevent cloud-init from resetting it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vi \/etc\/cloud\/cloud.cfg<\/code><\/pre>\n\n\n\n<p>Change the setting:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>preserve_hostname: true<\/code><\/pre>\n\n\n\n<p>For a detailed walkthrough on managing hostnames in cloud environments, see the guide on <a href=\"https:\/\/computingforgeeks.com\/set-or-update-vm-hostname-in-aws-openstack-digitalocean\/\" target=\"_blank\" rel=\"noreferrer noopener\">changing server hostname in EC2, OpenStack, and Azure instances<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 7: Hostname Naming Conventions<\/h2>\n\n\n\n<p>Choosing a good hostname matters for server management at scale. A clear naming scheme makes it easy to identify machines in logs, monitoring dashboards, and SSH sessions.<\/p>\n\n\n\n<p>Follow these rules for valid and practical hostnames:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Allowed characters<\/strong> &#8211; lowercase letters (a-z), digits (0-9), and hyphens (-). No underscores, spaces, or special characters<\/li>\n\n<li><strong>Length<\/strong> &#8211; each label (between dots) can be up to 63 characters. The full FQDN must be under 253 characters<\/li>\n\n<li><strong>No leading or trailing hyphens<\/strong> &#8211; <code>-web-01<\/code> and <code>web-01-<\/code> are invalid<\/li>\n\n<li><strong>Use FQDNs for servers<\/strong> &#8211; <code>web-prod-01.example.com<\/code> is better than just <code>web-prod-01<\/code> for DNS resolution and SSL certificates<\/li>\n<\/ul>\n\n\n\n<p>A practical naming pattern for servers is <code>role-environment-number.domain<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Hostname<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td>web-prod-01.example.com<\/td><td>Production web server<\/td><\/tr><tr><td>db-staging-02.example.com<\/td><td>Staging database server<\/td><\/tr><tr><td>k8s-worker-03.example.com<\/td><td>Kubernetes worker node<\/td><\/tr><tr><td>mon-prod-01.example.com<\/td><td>Production monitoring server<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Avoid creative names like &#8220;thor&#8221; or &#8220;gandalf&#8221; for servers &#8211; they are fun at first but impossible to manage at scale. When you have 50 servers, you need to know what each one does from the hostname alone. If you run a <a href=\"https:\/\/computingforgeeks.com\/how-to-change-hostname-on-centos-fedora\/\" target=\"_blank\" rel=\"noreferrer noopener\">CentOS, RHEL, Rocky, or Alma Linux system<\/a>, the same naming rules apply.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 8: Verify the Hostname Change<\/h2>\n\n\n\n<p>After changing the hostname with any method, run through these verification steps to confirm everything is consistent.<\/p>\n\n\n\n<p>Check the static hostname:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostnamectl --static<\/code><\/pre>\n\n\n\n<p>This should return your new static hostname:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server01.example.com<\/code><\/pre>\n\n\n\n<p>Check the transient hostname to make sure it matches:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostnamectl --transient<\/code><\/pre>\n\n\n\n<p>Verify the hostname resolves correctly through <code>\/etc\/hosts<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>getent hosts server01.example.com<\/code><\/pre>\n\n\n\n<p>The output should show your server&#8217;s IP mapped to the hostname:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>127.0.1.1       server01.example.com<\/code><\/pre>\n\n\n\n<p>Check that the <code>\/etc\/hostname<\/code> file contains the correct value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/hostname<\/code><\/pre>\n\n\n\n<p>The file should contain a single line with your new hostname:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server01.example.com<\/code><\/pre>\n\n\n\n<p>Open a new terminal session or SSH connection to see the updated hostname in your shell prompt. The current session may still show the old hostname in the prompt until you log out and back in. If you use <a href=\"https:\/\/computingforgeeks.com\/install-and-use-networkmanager-nmcli-on-ubuntu-debian\/\" target=\"_blank\" rel=\"noreferrer noopener\">NetworkManager with nmcli on Ubuntu and Debian<\/a>, verify with <code>nmcli general hostname<\/code> as well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>You now know how to change the hostname on Linux using <code>hostnamectl<\/code>, <code>nmcli<\/code>, and direct file edits. The <code>hostnamectl<\/code> method is recommended for most systems since it updates all hostname types at once and persists across reboots without extra steps. For cloud instances, always set <code>preserve_hostname: true<\/code> in cloud-init to prevent your changes from being overwritten.<\/p>\n\n\n\n<p>After changing the hostname, remember to update DNS records, monitoring systems, and any configuration management tools (Ansible, Puppet) that reference the old hostname. For servers in production, plan hostname changes during a maintenance window since some services cache the hostname at startup and need a restart to pick up the new value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every Linux system has a hostname &#8211; a label that identifies the machine on a network. You need to change it when provisioning new servers, renaming machines to match your naming convention, or fixing a default hostname assigned by a cloud provider. Linux gives you several ways to do this, from the recommended hostnamectl command &#8230; <a title=\"Change Hostname on Linux\" class=\"read-more\" href=\"https:\/\/computingforgeeks.com\/how-to-change-hostname-on-linux-using-nmcli\/\" aria-label=\"Read more about Change Hostname on Linux\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":50793,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[299,50],"tags":[254,307],"class_list":["post-50760","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","category-linux-tutorials","tag-networking","tag-nmcli"],"_links":{"self":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/50760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/comments?post=50760"}],"version-history":[{"count":1,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/50760\/revisions"}],"predecessor-version":[{"id":163657,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/50760\/revisions\/163657"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media\/50793"}],"wp:attachment":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media?parent=50760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/categories?post=50760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/tags?post=50760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}