The Raspbian SD image gives your Raspberry Pi a default name of ‘raspberrypi’. This name appears in the command prompt. Many people will leave it at the default but you may wish to change it to something more unique.
It’s quick and easy to change by following the steps below.
Note: A hostname can only contain the letters ‘a’ through ‘z’, the digits ‘0’ through ‘9’, and the hyphen (‘-‘).
Step 1 – Edit Hostname File
The first thing to do is edit the /etc/hostname file. You can do this by entering the following command at the command prompt :
sudo nano /etc/hostname
This will lauch the hostname file in the Nano text editor. Change raspberrypi to the name of your choice. In the example below I changed it to mypi.
Press CTRL-X, then Y and finally Enter to save the changes.
You can type the following command to check the file contains the new name :
cat /etc/hostname
Step 2 – Edit Hosts File
The next file that needs to be changed is the /etc/hosts file. We can use the same technique to edit this file.
sudo nano /etc/hosts
Using the cursor keys locate and update the line that says
127.0.1.1 raspberry
to
127.0.1.1 mypi
Here is a screenshot :
Leave the other lines unchanged. Press CTRL-X, then Y and finally Enter to save the changes.
You can type the following command to check the file contains the new name :
cat /etc/hosts
Step 3 – Reboot and Test
All the changes are made but we must reboot for those changes to take effect. Use the following command to reboot the Pi :
sudo reboot
Once the Pi has rebooted your command prompt should now include the new name. You can also type hostname at the command prompt to get your Pi to report its name.
2 Comments
Hi Matt
May I suggest a few simplifications to this guide, the method I regularly use is the following:-
sudo su
echo mypi > /etc/hostname
/etc/init.d/hostname.sh start
reboot
the setting of 127.0.1.1 in /etc/hosts is a bug workaround for gnome from as far back as 2005 – So I leave it well alone – other unix systems do not use this method. I’ve left my host name referring 127.0.1.1 to raspberrypi with no ill effects so far..
Keep up the good work Matt.
G.
Pingback: 5 Raspberry Pi Projects For Kids | Initial StateInitial State |