Linux change hostname
To change the hostname in Linux, you can use the
hostnamectl command. Here’s how:
- Open the Terminal.
- Check the current hostname by running the following command:
This will display the current hostname, as well as other system information.
- Change the hostname by running the following command:
sudo hostnamectl set-hostname new-hostname
Replace “new-hostname” with the new hostname you want to set. Note that the new hostname must be a valid hostname, consisting of letters, digits, and hyphens.
- Update the /etc/hosts file by running the following command:
sudo nano /etc/hosts
This will open the hosts file in the Nano text editor. Replace the old hostname with the new hostname in the line that starts with “127.0.0.1”, so it looks like this:
127.0.0.1 new-hostname localhost
Save and close the file by pressing
Y, and then
- Log out and log back in to apply the changes.
Note that changing the hostname may affect certain network services and applications that rely on the hostname. You may need to restart the affected services or applications after changing the hostname.