How to Change Hostname in Ubuntu 24.04
A hostname in Ubuntu 24.04 is the network name that identifies your PC to routers and other devices on a local network. Changing this name helps you spot your machine quickly when you share files, set up a home server, or troubleshoot network problems.
You can update your system name in less than 60 seconds by running a single command in the Terminal. The change takes effect immediately, with no reinstall and no hardware restart. You only need an account with sudo rights. If you also want the new name to show up cleanly in your prompt and in every program, a quick edit of one more file and a restart finish the job. Both are covered below.
To change the hostname on Ubuntu 24.04, open Terminal with Ctrl+Alt+T and run sudo hostnamectl set-hostname followed by your new name, such as sudo hostnamectl set-hostname office-pc. Enter your password when asked. The command prints nothing on success, and the new name applies immediately with no restart needed.
Show the current hostname
Check your current hostname before you change anything, so you know what you are replacing. You need this old name later when you edit the hosts file.
- Press Ctrl+Alt+T to open the Terminal.
- Type the command shown below and press Enter.
The Terminal prints your computer’s active network name on one line. Write it down.
You will run it again after the change to confirm that the new name was applied. If you want more detail, running `hostnamectl` with no arguments also shows the static hostname, the operating system and the kernel version.
Now change the name. You use the `hostnamectl` command followed by the new network name you choose for your computer. This gives the machine a clear identity on the network.
Pick the name first. Stick to lowercase letters, numbers and hyphens, and avoid spaces and underscores. Keep it under 64 characters. A name such as office-pc or web-server-01 is easy to recognize in a router's device list.
To change the hostname on Ubuntu, use the `hostnamectl` command. Run it with sudo in front, because only an administrator can change the system name. Terminal asks for your password, and nothing shows on screen while you type it. That is normal.
For example, to change the device hostname to srv1.ubuntu.local, run the command below.
If the command works, it prints nothing and returns you to the prompt. Silence means success here. Note that a name ending in .local can clash with mDNS, the service that handles local name discovery on many home networks. If you see odd name-lookup problems, try a plain name without the dots.
The `hostnamectl set-hostname` command updates the system name in the `/etc/hostname` file. This step ensures proper network identification across Ubuntu 24.04. To confirm, run the `hostname` command again. It should now print the new name. Your terminal prompt keeps showing the old name until you open a new terminal window or log in again.
/etc/hostname
The [/etc/hosts] file won't be updated automatically to match the new hostname. You might need to edit it manually to replace the old hostname with the new one.
Do not skip this on a machine you use every day. If the old name stays in the file, sudo can print a warning such as "unable to resolve host" and run slowly, and some local services may fail to find the machine by its own name. The old name is usually on a line that starts with 127.0.1.1, though a fresh install may not have that line.
Open the file in a text editor with root rights. The command is in the block below.
sudo nano /etc/hosts
Then, replace the old name with the new one, save the file, and exit.
- Find the line that contains the old hostname you wrote down earlier.
- Replace the old name with the new one. Leave the IP address at the start of the line as it is.
- Save the file. In the nano editor, press Ctrl+O, then Enter.
- Exit the editor. In nano, press Ctrl+X.
You are back at the prompt. If you made a typo, open the file again and fix it. A mistake in this file affects name lookups only, and you can always correct it with the same steps.
127.0.0.1 localhost
127.0.1.1 srv1.ubuntu.local
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
After changing the hostname, you may need to restart the device. Run the command below to do that.
It does make sure that every running program, the prompt and the login screen pick up the change. Save your work and close open applications first, since the machine restarts right away. If you are connected over SSH, you will be disconnected and need to reconnect.
sudo reboot
When you log back in, the system should have the new name you assigned. Open the Terminal and run the `hostname` command once more to check. The prompt should now show the new name too.
To undo the change, repeat the same steps with the old name. Run the set-hostname command with the old name, then edit the hosts file back to the old name.
That completes the process.
Updating your Ubuntu 24.04 hostname takes a moment in the terminal. The short version is below.
- Changing the hostname in Ubuntu 24.04 is a straightforward process using the
hostnamectlcommand, run with sudo. - Always check your current hostname using
hostnamectlbefore making changes, and note the old name for the hosts file. - Update both the
/etc/hostnameand/etc/hostsfiles to ensure the hostname is configured correctly. The first is changed for you by the command, while the second needs a manual edit. - Restart your device to apply the changes everywhere, including the prompt and running programs.
- Using clear and distinct hostnames, with lowercase letters, numbers and hyphens, is essential for better network management and identification of devices.
Have a question about this guide?
Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.
Did this guide fix your problem?
About the Author
Richard
Tech Writer, IT Professional
Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.
No comments yet — be the first to share your thoughts!