Changing the hostname in Debian 12 is a straightforward process, but it’s essential to ensure all steps are done correctly to avoid network or system configuration issues. This guide will walk you through both temporary and permanent hostname changes.
1. Check the Current Hostname
Before making any changes, you might want to see the current hostname of your system. To do this, run:
1 |
hostnamectl |
This command will display information about the system’s hostname and other related metadata.
2. Set a New Hostname Temporarily
If you want to change the hostname temporarily (only for the current session), use the following command:
1 |
sudo hostnamectl set-hostname <new-hostname> |
Replace <new-hostname>
with the name you want to assign to your system. Keep in mind this change will revert after a system reboot.
3. Set a New Hostname Permanently
To make the hostname change permanent, you need to edit a couple of configuration files in addition to setting the hostname.
a. Update /etc/hostname
- Open the file with a text editor:
1 |
sudo vi /etc/hostname |
- Replace the existing hostname with your new desired hostname.
- Save the file and exit the editor by pressing CTRL+O, Enter, and then CTRL+X.
b. Update /etc/hosts
- Open the
/etc/hosts
file:
1 |
sudo vi /etc/hosts |
- Find the line containing the old hostname. It might look something like this:
1 |
127.0.1.1 old-hostname |
- Replace
old-hostname
with your new hostname. For example:
1 |
127.0.1.1 new-hostname |
- Save the changes and exit the editor.
4. Apply the Changes
Once you’ve updated the files, you can apply the changes without needing a full reboot by restarting the hostname service:
1 |
sudo systemctl restart systemd-hostnamed |
Alternatively, you can reboot the system to ensure the changes take effect:
1 |
sudo reboot |
5. Verify the Changes
After applying the changes, you can confirm the new hostname by running:
1 |
hostnamectl |
This will display the updated hostname along with other system information.
Final Words …
Changing the hostname in Debian 12 is a simple yet essential task for personalizing or reconfiguring your system.
By following this guide, you can ensure the hostname is updated correctly without disrupting your system’s functionality.
Whether you’re setting it temporarily or permanently, these steps will help you achieve your goal seamlessly.