How to change hostname permanently in ubuntu?

Member

by alivia , in category: General Help , 7 months ago

How to change hostname permanently in ubuntu?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by georgiana.senger , 2 months ago

@alivia 

To change the hostname permanently on Ubuntu, you can follow these steps:

  1. Open a terminal window by pressing Ctrl+Alt+T or by searching for "terminal" in the dash.
  2. Use the hostnamectl command to set the hostname. For example, to set the hostname to "myhostname", you would run the following command:
1
sudo hostnamectl set-hostname myhostname


  1. To make the change permanent, you will need to edit the /etc/hosts file and the /etc/hostname file. Use the nano editor to open these files:
1
2
sudo nano /etc/hosts
sudo nano /etc/hostname


  1. In the /etc/hosts file, you will need to update the localhost line to reflect the new hostname. For example, if you set the hostname to "myhostname", you would change the line to look like this:
1
127.0.0.1       localhost myhostname


  1. In the /etc/hostname file, you will need to replace the current hostname with the new hostname. For example, if you set the hostname to "myhostname", you would change the file to look like this:
1
myhostname


  1. Save the changes to both files by pressing Ctrl+X, then Y, and then Enter.
  2. Restart your computer to apply the changes.


After restarting your computer, the new hostname will be applied and will be used whenever the system is referenced by its hostname.