A hostname in a Unix-based operating system (such as Ubuntu, Kali Linux, etc.) is a label that uniquely identifies a device on a network. When you install Kali Linux, the default hostname is ‘Kali’. However, you might want to change the hostname according to your own preference for customization, security, or simply for identification purposes. Here are the steps to permanently change the hostname in Kali Linux using the terminal only:
1. Open the terminal window by pressing ‘ctrl + alt + t’ at the same time on your keyboard.
2. Check your existing hostname. When you open the terminal, the hostname is written after ‘@’. You can also verify the hostname with the following command:
hostname
* I can confirm that my hostname is “kali”
3. Next, open the ‘/etc/hostname’ file with your favorite text editor. In this example, we will use the nano text editor. To open the file, use the following command:
sudo nano /etc/hostname
*Make sure to use sudo to ensure you have the necessary permissions to edit the file. The ‘/etc/hostname’ file contains the permanent hostname of the machine.
4. Replace the existing hostname with your new hostname. I will replace my existing hostname ‘kali’ with my new hostname ‘youtube’
*Save and exit the file. In nano, press ctrl+o to save the changes, then press Enter to confirm. After that, press ctrl+x to exit out of the file.
5. Now, open the ‘/etc/hosts’ file with your favorite text editor. I will again be using the nano text editor. So, the command for me is:
sudo nano /etc/hosts
*Make sure to use sudo to ensure you have the necessary permissions to edit the file. The ‘/etc/hosts’ file is used to map hostnames to IP addresses.
6. Find your existing hostname and replace it with the new one. I can see that my current hostname, “kali,” is listed in line 2, which starts with 127.0.1.1. I will replace ‘kali’ with my new hostname ‘youtube’.
*Save and exit the file. If you are using nano, press ctrl+o to save the changes, then press Enter to confirm. After that, press ctrl+x to exit out of the file.
7. Finally, restart your machine for the changes to take place. If you want to reboot the machine from your terminal, type the following command:
sudo reboot
8. After your machine boots up, open your terminal, and you can now confirm that the new hostname (‘youtube’ in my case) is being displayed.
This is how you change and make your hostname persistent across reboots in Kali Linux.