4

This question already has an answer here:

How to change machine name in Ubuntu; as in my case it is written as Lenov@G500, and I want to change both names i. Lenovo and G500. how it can be done through Terminal.

marked as duplicate by αғsнιη, Pilot6, karel, Eric Carvalho, Maythux Jul 21 '15 at 8:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

What you need to do is as following:

Open a terminal (hotkey is Ctrl + Alt + T).

Next, use any editor you favor to edit the following files (I use vi because I like it, but you can use anything else, such as gedit):

sudo vi /etc/hostname

There you will see your current hostname, just change it to whatever you like the new machine name to be, save the file and quit.

Next, you will also need to edit /etc/hosts file, as following:

sudo vi /etc/hosts

Locate the entry in the file that contains your current hostname and change it to the new hostname. The IP address should remain unchanged!

Don't forget to save your changes. Note that in some cases, especially when you're using a static IP address, you might have more than one entry containing your machine hostname. In such case make sure to change all relevant entries.

And last but not least, you must immediately change your hostname itself since your machine is loading the hostname only during boot time from the /etc/hostname file. You can do this by typing:

sudo hostname <New Hostname>

Hit Enter and you're done!


Lenov is your username. To change this, you should follow this answer.

Hope this helps :-)

1

Use the command

hostnamectl -P set-hostname new.hostname.here

to write to the /etc/hostname file and update the stored in-memory hostname at the same time.

(Note: If you put sudo in front of that command, you can omit the -P flag. The -P option tells hostnamectl to use PolicyKit to elevate privileges, which can automatically choose between prompting your password in the terminal or using a GUI.)

Then, you need to add it to /etc/hosts, with:

sudoedit /etc/hosts

adding (or editing) an entry from the new hostname to 127.0.0.1:

127.0.0.1     localhost
127.0.0.1     new.hostname.here
0

Try sudo gedit /etc/hostname and you can edit it from there, if you don't want to leave the terminal window you can change it with sudo nano /etc/hostname You will also need to modify sudo gedit /etc/hosts to get rid of "@G500"

Not the answer you're looking for? Browse other questions tagged or ask your own question.