Changing the hostname in Ubuntu is a straightforward process that involves modifying a few configuration files and, in some cases, updating related network settings. The hostname is a unique name assigned to a computer on a network, and it helps in identifying the machine within the network. Here’s how you can change it:
Step 1: Check the Current Hostname
Before renaming the hostname, it's good practice to check the current hostname of your Ubuntu system. You can do this by running:
hostnamectl
This command will display various details about your system, including the current hostname.
Step 2: Change the Hostname
To rename the hostname, you'll need to update the following files and settings:
1. Temporary Change (For the Current Session Only)
If you want to change the hostname temporarily (until the next reboot), you can use the hostname
command:
sudo hostname NEW_HOSTNAME
Replace NEW_HOSTNAME
with your desired hostname. This change will only last until the system is rebooted.
2. Permanent Change
To permanently change the hostname, you need to modify two files: /etc/hostname
and /etc/hosts
.
a. Edit /etc/hostname
This file contains the current hostname. Use your favorite editor to edit it:
sudo vi /etc/hostname
Replace the existing hostname with your new hostname. Save the file and exit the editor.
b. Edit /etc/hosts
The /etc/hosts
file maps hostnames to IP addresses. You'll need to update this file to reflect the new hostname:
sudo vi /etc/hosts
Look for a line that looks something like this:
127.0.1.1 old_hostname
Replace old_hostname
with your new hostname. Save the file and exit the editor.
Step 3: Apply the Changes
After making these changes, you need to apply them by restarting the hostname
service or simply rebooting your machine.
Option 1: Restart the hostname
Service
sudo systemctl restart systemd-logind.service
Option 2: Reboot the System
sudo reboot
After the reboot, your Ubuntu system will start with the new hostname.
Step 4: Verify the Change
Once the system has rebooted or the service has restarted, you can verify the new hostname by running:
hostnamectl
The output should reflect the new hostname you set.
Additional Considerations
- Network Services: If your machine is part of a network and provides services such as a web server, file server, or domain name services, make sure to update the hostname in any relevant configuration files for those services.
- SSH Connections: If you connect to your machine via SSH, the known hosts file on the client machine might flag the hostname change as suspicious. You may need to update or clear the relevant entry in
~/.ssh/known_hosts
.
Conclusion
Changing the hostname on an Ubuntu system is a simple process that involves editing a couple of configuration files and restarting your machine or the relevant services. Whether you're setting up a new server, renaming a machine for clarity, or just experimenting, these steps will guide you through the process efficiently.