0
0
Iot-protocolsHow-ToBeginner · 3 min read

How to Change Hostname on Raspberry Pi Quickly and Easily

To change the hostname on a Raspberry Pi, edit the /etc/hostname and /etc/hosts files with your new name, then reboot the device. Alternatively, use the raspi-config tool to update the hostname interactively.
📐

Syntax

Changing the hostname involves editing two files and rebooting your Raspberry Pi:

  • /etc/hostname: Contains the current hostname.
  • /etc/hosts: Maps hostnames to IP addresses; update the old hostname here.

Alternatively, use the raspi-config tool to change the hostname via a menu.

bash
sudo nano /etc/hostname
sudo nano /etc/hosts
sudo reboot
💻

Example

This example shows how to change the hostname from raspberrypi to my-pi by editing files and rebooting.

bash
# Open and edit /etc/hostname
sudo nano /etc/hostname
# Replace 'raspberrypi' with 'my-pi'

# Open and edit /etc/hosts
sudo nano /etc/hosts
# Replace 'raspberrypi' with 'my-pi' in the line:
# 127.0.1.1    raspberrypi

# Reboot to apply changes
sudo reboot
Output
pi@my-pi:~ $ hostname my-pi
⚠️

Common Pitfalls

Common mistakes when changing the hostname include:

  • Not updating both /etc/hostname and /etc/hosts, causing network issues.
  • Forgetting to reboot, so the change does not take effect.
  • Using invalid characters or spaces in the hostname; only letters, numbers, and hyphens are allowed.
bash
Wrong way:
sudo nano /etc/hostname
# Changed hostname but forgot to update /etc/hosts
sudo reboot

Right way:
sudo nano /etc/hostname
sudo nano /etc/hosts
sudo reboot
📊

Quick Reference

Summary tips for changing Raspberry Pi hostname:

  • Always edit both /etc/hostname and /etc/hosts.
  • Use only letters, numbers, and hyphens in the hostname.
  • Reboot the Raspberry Pi to apply changes.
  • Use raspi-config for an easier interactive method.

Key Takeaways

Edit both /etc/hostname and /etc/hosts files to change the Raspberry Pi hostname correctly.
Use only valid characters: letters, numbers, and hyphens in your hostname.
Reboot the Raspberry Pi after changes to apply the new hostname.
The raspi-config tool offers a simple interactive way to change the hostname.
Failing to update both files or reboot can cause network and identification issues.