{"id":925,"date":"2026-10-25T18:00:00","date_gmt":"2026-10-25T18:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=925"},"modified":"2026-10-25T18:00:00","modified_gmt":"2026-10-25T18:00:00","slug":"change-hostname-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/25\/change-hostname-ubuntu-22-04\/","title":{"rendered":"How to Change Hostname on Ubuntu 22.04 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Change hostname on Ubuntu 22.04 with <code>hostnamectl<\/code>, a matching local hosts entry, and a short verification pass. You can finish in about five minutes without rebooting, then open a new SSH session to see the updated name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>Ubuntu Server 22.04 LTS (Jammy Jellyfish) running on your VPS.<\/li>\n\n\n<li>An SSH connection as <code>root<\/code>, or a normal account with working <code>sudo<\/code> privileges.<\/li>\n\n\n<li>A new short hostname such as <code>web01<\/code>, or an FQDN such as <code>web01.example.com<\/code>; use lowercase letters, numbers, hyphens, and dots.<\/li>\n\n\n<li>Access to your domain&#8217;s DNS records and the server&#8217;s public IP if the name must resolve outside this VPS.<\/li>\n\n\n<li>No special RAM or disk allocation is required for a hostname change; a <a href=\"https:\/\/vpslake.com\/buy-vps\">VPSLake Linux VPS<\/a> is suitable if you need an Ubuntu 22.04 server.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Record the current hostname<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check the active and saved values first so you can distinguish an old local setting from a name supplied by the image or network.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/os-release\nhostnamectl status --static\nhostnamectl status --pretty\nhostnamectl status --transient\nhostname\ncat \/etc\/hostname<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The release file should identify Ubuntu 22.04 (Jammy). Empty static or pretty values are possible on a freshly provisioned VPS and are not errors.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PRETTY_NAME=\"Ubuntu 22.04.5 LTS\"\nold-web.example.com\n\nold-web.example.com\nold-web.example.com<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The static hostname is the durable operating-system value. A transient name can be supplied by DHCP or provisioning, while a pretty hostname is only a human-readable label.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Set the persistent Ubuntu hostname<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>hostnamectl<\/code> so Ubuntu changes the running kernel name and <code>\/etc\/hostname<\/code> together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostnamectl set-hostname NEW_HOSTNAME\nhostnamectl status --static\ncat \/etc\/hostname<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>NEW_HOSTNAME<\/code> with <code>web01<\/code> or <code>web01.example.com<\/code>. Do not include spaces, underscores, or a final dot. The command normally prints no success message, so the two following checks are the useful confirmation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>web01.example.com\nweb01.example.com<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ubuntu&#8217;s <a href=\"https:\/\/manpages.ubuntu.com\/manpages\/jammy\/man1\/hostnamectl.1.html\" target=\"_blank\" rel=\"noopener\"><code>hostnamectl<\/code> manual<\/a> treats the static, transient, and pretty values separately. If you want an optional description for people, set it independently:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo hostnamectl set-hostname \"NEW_PRETTY_NAME\" --pretty\nhostnamectl status --pretty<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>NEW_PRETTY_NAME<\/code> with a label such as <code>Production web server<\/code>. Spaces are valid in this label, but it is not the hostname used for DNS or service identity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Update Ubuntu&#8217;s local hosts entry<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Changing the system value does not automatically make the new name resolve locally, so update <code>\/etc\/hosts<\/code> before testing the FQDN.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/hosts<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the existing <code>localhost<\/code> and IPv6 records. Replace the old machine-name line, or add one <code>127.0.1.1<\/code> line in this form:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>127.0.0.1 localhost\n127.0.1.1 NEW_HOSTNAME NEW_SHORT_NAME<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For <code>web01.example.com<\/code>, replace <code>NEW_SHORT_NAME<\/code> with <code>web01<\/code>. If you chose only <code>web01<\/code>, use <code>127.0.1.1 web01<\/code> instead. Keep one machine-name entry; leaving both the old and new lines can produce inconsistent lookups. Save with <code>Ctrl+O<\/code>, press Enter, and exit with <code>Ctrl+X<\/code> in nano.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Test the local resolver:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>getent hosts NEW_HOSTNAME\nhostname --fqdn\nhostname --short<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For an FQDN, successful output resembles:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>127.0.1.1       web01.example.com web01\nweb01.example.com\nweb01<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This mapping applies only to the VPS. Create an A record for IPv4 or an AAAA record for IPv6 if other systems need to resolve the name through public DNS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Check cloud-init before making its setting permanent<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Some Ubuntu VPS images run cloud-init during boot and can restore a provider-supplied hostname; do not install it just for this task.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if command -v cloud-init &gt;\/dev\/null 2&gt;&amp;1; then\n  cloud-init status --long\nelse\n  echo \"cloud-init is not installed\"\nfi<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the hostname remains correct after a reboot, no cloud-init change is needed. If cloud-init is installed and restores the previous name, add a drop-in that preserves the local value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo install -d -m 0755 \/etc\/cloud\/cloud.cfg.d\nprintf '%s\\n' 'preserve_hostname: true' | sudo tee \/etc\/cloud\/cloud.cfg.d\/99-preserve-hostname.cfg\nsudo hostnamectl set-hostname NEW_HOSTNAME<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>tee<\/code> command should echo the setting it saved:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>preserve_hostname: true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>NEW_HOSTNAME<\/code> with the same value used in Step 2. The <a href=\"https:\/\/cloudinit.readthedocs.io\/en\/latest\/reference\/modules.html\" target=\"_blank\" rel=\"noopener\">cloud-init module documentation<\/a> describes <code>preserve_hostname<\/code>; if provider metadata still wins at boot, update the hostname in that provisioning configuration too.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Refresh SSH and applications that cache the name<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The active hostname changes immediately, but an existing shell prompt or application may have read the old value only once at startup.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostname\nhostnamectl status --static\ncat \/etc\/hostname<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">All three lines should contain the new name:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>web01.example.com\nweb01.example.com\nweb01.example.com<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Open a second SSH session to refresh the prompt before closing the first one. Restart an individual application only if it caches the hostname at startup:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart YOUR_SERVICE<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>YOUR_SERVICE<\/code> with a real unit name. Changing the hostname does not require restarting networking or rebooting the VPS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the new hostname<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run this final group after editing <code>\/etc\/hosts<\/code> to check the persistent value, short form, FQDN, and resolver result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hostnamectl status --static\nhostname --short\nhostname --fqdn\ngetent hosts NEW_HOSTNAME<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For <code>web01.example.com<\/code>, the first three lines should be <code>web01.example.com<\/code>, <code>web01<\/code>, and <code>web01.example.com<\/code>. The last line should return the <code>127.0.1.1<\/code> entry or the expected DNS address. When those values agree, Ubuntu 22.04 is using the new hostname locally.<\/p>\n\n\n\n<aside class=\"vl-element vl-cta-banner\" aria-label=\"Give your server a clear identity\">\n\t<div class=\"vl-cta-banner__content\">\n\t\t<p class=\"vl-cta-banner__eyebrow\">Ubuntu VPS<\/p>\n\t\t<h3 class=\"vl-cta-banner__title\">Give your server a clear identity<\/h3>\n\t\t<p class=\"vl-cta-banner__text\">Deploy an Ubuntu VPS with root access for clean hostname and DNS configuration.<\/p>\n\t<\/div>\n\t<a class=\"vl-cta-banner__button\" href=\"\/buy-vps\" aria-label=\"View Linux VPS plans\">View Linux VPS plans<\/a>\n<\/aside>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><code>hostnamectl: invalid hostname<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The static value contains a space, underscore, trailing dot, or another unsupported character. Use lowercase letters, numbers, hyphens, and dots in <code>NEW_HOSTNAME<\/code>; put spaces only in <code>NEW_PRETTY_NAME<\/code>. Check that each FQDN label starts and ends with a letter or number.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>sudo: unable to resolve host<\/code> appears after the change<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The new static hostname and <code>\/etc\/hosts<\/code> disagree, so <code>sudo<\/code> cannot resolve the local machine name. Inspect the machine-name lines and leave one matching entry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -nE '127\\.0\\.1\\.1|OLD_HOSTNAME|NEW_HOSTNAME' \/etc\/hosts\ngetent hosts NEW_HOSTNAME<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace both uppercase placeholders with real values. Update the <code>127.0.1.1<\/code> line, then run the checks again. If only remote computers see an old answer, the issue is public DNS caching rather than Ubuntu&#8217;s local hostname.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The hostname changes back after a reboot<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Cloud-init, DHCP metadata, or the VPS image is applying a name during startup. Check <code>cloud-init status --long<\/code>, add the <code>99-preserve-hostname.cfg<\/code> drop-in from Step 4 when cloud-init is responsible, and update the provider&#8217;s provisioning hostname if it continues to overwrite the local file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>Failed to set hostname: Access denied<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The current account lacks permission to change the system name. Run the command with <code>sudo<\/code>, confirm that <code>sudo -v<\/code> succeeds, or reconnect as <code>root<\/code>; changing permissions on <code>\/etc\/hostname<\/code> is not a safe workaround.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Next steps<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>Add and test the public A or AAAA record from another network if the hostname will serve websites, mail, TLS, or monitoring.<\/li>\n\n\n<li>Request a matching PTR record through the system that manages your VPS IP when reverse DNS matters.<\/li>\n\n\n<li>Update SSH inventory, backups, monitoring, certificates, and configuration-management records that still reference the previous name.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use the server&#8217;s IP address as its hostname?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Use a short DNS-safe name such as <code>web01<\/code>, or an FQDN such as <code>web01.example.com<\/code>. An IP address belongs in DNS or <code>\/etc\/hosts<\/code>, not as the static hostname.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does changing the hostname update public DNS?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. <code>hostnamectl<\/code> changes Ubuntu&#8217;s local system identity and <code>\/etc\/hostname<\/code>. Public forward DNS and reverse DNS are separate records managed through your DNS provider or VPS control panel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Will changing the hostname disconnect my SSH session?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Normally it will not; the current session remains open. Its prompt may still show the old name, so open a new session after the change and close the original only after confirming access.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is a reboot required on Ubuntu 22.04?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. The new name becomes active immediately and persists through <code>\/etc\/hostname<\/code>. Reboot only when another maintenance task requires it, or restart a specific application that caches the hostname at startup.<\/p>\n\n\n\n<script type=\"application\/ld+json\">{\"@context\": \"https:\/\/schema.org\", \"@type\": \"FAQPage\", \"mainEntity\": [{\"@type\": \"Question\", \"name\": \"Can I use the server's IP address as its hostname?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. Use a short DNS-safe name such as `web01`, or an FQDN such as `web01.example.com`. An IP address belongs in DNS or `\/etc\/hosts`, not as the static hostname.\"}}, {\"@type\": \"Question\", \"name\": \"Does changing the hostname update public DNS?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. `hostnamectl` changes Ubuntu's local system identity and `\/etc\/hostname`. Public forward DNS and reverse DNS are separate records managed through your DNS provider or VPS control panel.\"}}, {\"@type\": \"Question\", \"name\": \"Will changing the hostname disconnect my SSH session?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Normally it will not; the current session remains open. Its prompt may still show the old name, so open a new session after the change and close the original only after confirming access.\"}}, {\"@type\": \"Question\", \"name\": \"Is a reboot required on Ubuntu 22.04?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. The new name becomes active immediately and persists through `\/etc\/hostname`. Reboot only when another maintenance task requires it, or restart a specific application that caches the hostname at startup.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Change hostname on Ubuntu 22.04 with hostnamectl, update \/etc\/hosts, handle cloud-init resets, and verify the new server name without rebooting.","protected":false},"author":1,"featured_media":926,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","footnotes":""},"categories":[5,9],"tags":[106,93,101],"class_list":["post-925","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-hostname","tag-linux-vps","tag-ubuntu-22-04","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/comments?post=925"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/925\/revisions"}],"predecessor-version":[{"id":3733,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/925\/revisions\/3733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/926"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}