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.
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 :-)
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
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