To change the hostname of a Linux system, you can follow these steps:
- Log in to the system with root or sudo access.
- Open the
/etc/hostnamefile with a text editor such asnanoorvi.sudo nano /etc/hostname
- In the file, replace the current hostname with the new hostname that you want to set. Save and close the file.
- Open the
/etc/hostsfile with a text editor.sudo nano /etc/hosts
- In the file, look for the line that contains the current hostname and update it with the new hostname. For example:
127.0.0.1 localhost old-hostname
should be updated to:
127.0.0.1 localhost new-hostname
- Save and close the file.
- Restart the networking service to apply the changes.
sudo systemctl restart network
If the above command does not work, you may need to use the appropriate command for your distribution, such as
sudo service network restart. - Verify the new hostname by running the
hostnamecommand.hostname
This should output the new hostname that you just set.