We have already learned what a hostname is, but how to set or change such a hostname?
For the following steps you will need:
- Linux operating system
- The root user or a user with sudo command authority
Terminal #
In the directory structure, we go to the /etc folder
cd /etc/
Editing the hostname #
Let's open the hostname file in the editor. In our case, we will use the nano editor.
sudo nano hostname
note if the logged in user is root, we don't type sudo before the command.
When using the sudo command, the terminal will ask us for a password.
We delete the old one and write a new hostname.
We save the changes with the keyboard shortcut Ctrl + O and Enter, and close the editor with Ctrl + X.
If the old hostname is also listed in /etc/hosts (usually on the 127.0.1.1 line), we change it there the same way. Otherwise sudo will report unable to resolve host.
sudo reboot
After the restart, we have a changed hostname.
Done.