How to change hostname permanently in ubuntu?

Member

by alivia , in category: General Help , 2 years ago

How to change hostname permanently in ubuntu?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by georgiana.senger , 2 years 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.

Member

by brock , 7 months ago

@alivia 

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

  1. Open a terminal by pressing Ctrl+Alt+T.
  2. Use the following command to change the hostname (replace "newhostname" with your desired hostname):
1
sudo hostnamectl set-hostname newhostname


  1. Edit the hostname file using a text editor:
1
sudo nano /etc/hostname


  1. Replace the existing hostname with your new hostname, then save and exit the file.
  2. Edit the hosts file:
1
sudo nano /etc/hosts


  1. Replace the old hostname with the new one in the file, then save and exit.
  2. Reboot your system for the changes to take effect:
1
sudo reboot


Once the system boots up again, your Ubuntu system will have the new hostname applied permanently.