What a hostname is and why you might change it
Your hostname is the name your Linux computer uses to identify itself on a network. It appears in your terminal prompt, in file sharing dialogs, and when other computers try to connect to yours. By default, Linux assigns a generic name like "ubuntu-machine" or "localhost". You might want to change it to something more descriptive — like "home-server" or "work-laptop" — so you can recognize your machine in a list of networked devices, or to match a naming scheme you use across multiple computers.
Changing the hostname does not affect how your computer runs or its performance. It is purely a label. However, some network services and applications read the hostname during startup, so changing it usually requires a restart to take full effect everywhere on your system.
Key Takeaways
- Your hostname is stored in the file /etc/hostname and also in /etc/hosts, and you must edit both for the change to stick.
- The hostnamectl command is the fastest way to change your hostname on modern Linux systems — it updates both files at once.
- After changing your hostname, you should restart your computer so that all running services recognize the new name.
- Hostnames can contain letters, numbers, and hyphens, but cannot start or end with a hyphen and should not contain spaces or special characters.
Using hostnamectl on modern Linux systems
Most Linux distributions released in the last five years — including Ubuntu 18.04 and later, Fedora, Debian 9 and later, and CentOS 7 and later — include the hostnamectl command. This is the simplest method because it changes both required files in one step.
Open a terminal and type the following command, replacing "new-hostname" with the name you want:
sudo hostnamectl set-hostname new-hostname
The system will ask for your password. Type it and press Enter. The command produces no output if it succeeds — that is normal. You can verify the change took effect by typing hostnamectl with no arguments, which displays your current hostname and other system information.
After running this command, restart your computer so that all services and applications recognize the new hostname. You can restart when ready by typing sudo reboot, or you can wait and restart later through your system menu.
Editing the hostname files manually
If your Linux system does not have hostnamectl — which is rare but possible on very old distributions — you can edit the hostname files directly using a text editor. You will need to edit two files: /etc/hostname and /etc/hosts.
Open a terminal and type:
sudo nano /etc/hostname
This opens the file in the nano text editor. You will see the current hostname on a single line. Delete it entirely and type your new hostname. Press Ctrl+O to save, then press Enter to confirm the filename, then press Ctrl+X to exit the editor.
Now edit the second file:
sudo nano /etc/hosts
This file contains multiple lines. Look for a line that starts with 127.0.0.1 followed by your old hostname. It usually looks like this:
127.0.0.1 localhost old-hostname
Replace old-hostname with your new hostname, keeping the spacing the same. If you see a line starting with ::1 (which is the IPv6 version of localhost), do the same thing there. Save and exit using Ctrl+O, Enter, and Ctrl+X as before.
Restart your computer by typing sudo reboot so that all services recognize the new name.
Checking your hostname after the change
After you restart, open a terminal and type hostname to display your current hostname. It should show the new name you set. You can also type hostnamectl if that command is available on your system — it shows the hostname along with other details like your operating system and virtualization status.
If you are on a network with other computers, you can verify that other machines can see your new hostname by pinging your computer from another device. On Windows, open Command Prompt and type ping new-hostname. On another Linux or Mac computer, open a terminal and type the same command. If the ping succeeds, your hostname change is complete and visible across your network.
Hostname naming rules and restrictions
Linux hostnames must follow specific rules. They can contain letters (a–z, A–Z), numbers (0–9), and hyphens (-), but cannot start or end with a hyphen. Hostnames are not case-sensitive — "MyServer" and "myserver" are treated as the same name. Most systems accept hostnames up to 253 characters long, though shorter names (under 64 characters) are more practical and easier to type.
Avoid spaces, underscores, periods, and special characters like @, #, or $. These characters can confuse network services and cause connection problems. If you want to separate words in your hostname, use a hyphen instead: "home-server" instead of "home_server" or "home server".
What happens when you change your hostname
Changing your hostname does not affect your files, installed programs, or system settings. It only changes the label your computer uses to identify itself. However, some services may need to restart to recognize the new name. This is why restarting your computer after the change is recommended — it ensures that all running services, network daemons, and applications pick up the new hostname without errors.
If you have configured network shares, SSH access, or other services that reference your old hostname, those services will continue to work after the change. Other computers on your network may take a few minutes to update their records of your hostname, but this happens automatically through network discovery protocols.
Frequently Asked Questions
Do I have to restart my computer after changing the hostname?
Technically, some services will work with the new hostname when ready after you change the files. However, restarting ensures that every service, process, and network daemon recognizes the change. Without restarting, you may see the old hostname in some places and the new one in others, which creates confusion. A restart takes a few minutes and eliminates this inconsistency.
Can I change my hostname without using the terminal?
Most graphical Linux desktop environments do not provide a built-in settings panel for changing the hostname. Your best option is to use the terminal and hostnamectl, which takes one line of text. If you strongly prefer a graphical tool, some distributions include system settings apps that may offer a hostname option, but this varies by distribution and desktop environment.
What if I change my hostname and my network stops working?
A hostname change should not break your network connection. If your network stops working after the change, restart your computer — this usually resolves the issue. If the problem persists, check that you edited the /etc/hosts file correctly and that the 127.0.0.1 line still exists. You can also check your network settings by typing ip addr in a terminal to see if your network interface is still active.
Can I use spaces or special characters in my hostname?
No. Linux hostnames cannot contain spaces, periods, underscores, or special characters. They can only use letters, numbers, and hyphens. If you try to set a hostname with invalid characters, hostnamectl will reject it with an error message. Use hyphens to separate words instead: "web-server" instead of "web server".
Will changing my hostname affect my IP address?
No. Your IP address is separate from your hostname. Changing the hostname does not change your IP address, and other computers can still reach your machine using the IP address even if they do not know the new hostname. The hostname is just a human-readable label that makes it easier to remember and identify your computer.