How to Set a New Hostname on Raspberry Pi (4 ways)

1. Using the raspi-config Tool:
The raspi-config tool is a command-line utility that allows you to configure your Raspberry Pi. To set a new hostname using this tool, open a terminal window and type the following command:

sudo raspi-config

This will open the raspi-config menu. Select the “Network Options” option and then select “Hostname”. Enter the new hostname and then select “OK”.

2. Using the /etc/hostname File:
You can also set a new hostname by editing the /etc/hostname file. To do this, open a terminal window and type the following command:

sudo nano /etc/hostname

This will open the /etc/hostname file in the nano text editor. Replace the existing hostname with the new one and then save the file.

3. Using the /etc/hosts File:
You can also set a new hostname by editing the /etc/hosts file. To do this, open a terminal window and type the following command:

sudo nano /etc/hosts

This will open the /etc/hosts file in the nano text editor. Replace the existing hostname with the new one and then save the file.

4. Using the hostnamectl Command:
The hostnamectl command is a command-line utility that allows you to set a new hostname. To set a new hostname using this command, open a terminal window and type the following command:

sudo hostnamectl set-hostname

Replace with the new hostname you want to set.

You have probably already noticed that after the system installation, all Raspberry Pi on Raspberry Pi OS have the same name.
In this post, I will explain to you what it is and how to change it as you want.

The easiest way to set the hostname for a new installation, is to use the advanced options in Raspberry Pi Imager and fill the first field with the desired hostname. After the installation, using the configuration tool in a terminal allow you to change it too (sudo raspi-config).

That’s it for the short answer, I will now give you all the details and introduce other solutions.
I will also start with a quick reminder about the hostname in general, if you are not (yet) used to this term.

By the way, if you are just getting started on Raspberry Pi, I can help you learn much faster. My Raspberry Pi bootcamp course helps you step-by-step to discover the Raspberry Pi device and unlock its secrets. In a few hours, you’ll be ready to start any awesome project you are interested in.

Host name introduction on Raspberry Pi

What is a hostname?

On a network, devices will use a few identifiers, like the IP address or MAC address.
They also have a label, mainly to help us to identify which device has which IP (the hostname).

For example, when you do a network scan (as explained here), it will generally give you the IP and MAC address but also a name (the hostname):

You can choose what you want as a hostname, but you need to avoid using the same hostname on several devices.
Valid hostnames are between 1 and 63 characters, with only letters (a-z), digits (0-9) and hyphens (-).

What is the default value?

On Raspberry Pi, the default value is “raspberrypi”.
You can check that with the following command:
hostname

Any fresh new Raspberry Pi OS installation will get this default hostname, even if you already have another Raspberry Pi on your network.
Even if it’s not mandatory, it’s a good practice to change it after the installation (I’ll show you how to do this in the next section).

Are you a bit lost in the Linux command line? Check this article first, for the most important commands to remember, and a free downloadable cheat sheet so you can have the commands at your fingertips.

How to change the hostname on Raspberry Pi

Let’s look at how to change the default hostname to a custom name.
I will show you four different ways to do this now.

Solution 1: Use Raspberry Pi imager

If you use Raspberry Pi Imager to install a new system (especially for Raspberry Pi OS), you can now use the advanced options to set the new hostname directly. It will save the name you choose on the sd card, and use it instead of the default value on the first boot.

  • Start Raspberry Pi Imager.
  • Select the Raspberry Pi OS version you want to install.
  • Pick the media where you’ll install your system.
  • Then, you’ll see a button appeared in the bottom-right corner:
  • Click on it to open the advanced options, and fill the first field with the desired hostname.
  • Then, click on “Write” as usual to start the flashing process.

The hostname will be set as requested on the first boot.
Just remember to change it if you have several Raspberry Pi, as it will use it every time you flash a new SD card (if you chose “to always use” in the first list).

The only issue with this solution, is that it works on Raspberry Pi OS, should work with other Debian-based distributions (not all the time) and won’t work on custom images and other systems. So, check the following solutions if you are not using Raspberry Pi OS.

If you are not used to this tool, I have a complete guide for Raspberry Pi Imager here. Please don’t hesitate to check it before going further.

Solution 2: Edit the configuration file

Open and edit the /etc/hostname file

This solution is great because it works on almost any Linux distribution.
There is a file on most systems with the device hostname inside.
On Raspberry Pi OS (and any Linux system, I think), this file is /etc/hostname.

Here is how to change it:

  • Open the file with nano:
    sudo nano /etc/hostname
  • A file opens that only contains the default hostname: “raspberrypi”.
  • Remove the word and replace it with the one you want, for example:
    raspberrytips01
  • Save the file and exit (CTRL+O, CTRL+X).
  • If you use the hostname command now, the value will still be the same.
    You need to reboot the Raspberry Pi to apply changes:
    sudo reboot
  • Once restarted, the new hostname will appear in the command prompt.
    And, you can also check with the hostname command:

Network update

If you have some issues (or warnings at least) with some network services, you probably need to edit the /etc/hosts file to update the localhost name:

  • Open the /etc/hosts file the same way:
    sudo nano /etc/hosts
  • Edit the last line to update the Raspberry Pi name.
  • Save and exit (CTRL+O, CTRL+X).
  • Reboot:
    sudo reboot

That’s it.
You can use the same method on systems like Debian and Ubuntu.

Solution 3: Use raspi-config

The next two solutions are only available on Raspberry Pi OS.
It can be easier if you can’t remember the filename to edit, or are not comfortable with a terminal, especially with editing files.
You can also check the last one if you are on Raspberry Pi OS with Desktop.

The solution is to use raspi-config.
You probably already know this tool, as you can use it to change many things in your Raspberry Pi configuration.

Here is how to change the hostname in raspi-config:

  • Start raspi-config with:
    sudo raspi-config
  • On the wizard that shows up, click on “System options” (just press Enter).

    Note: if you are using an old version of Raspberry Pi OS, the hostname option might be under “Network options” instead of “System options”.
  • Then click on “Hostname” (press enter).
    Use the arrows to select the corresponding line and press enter to confirm.
  • You’ll get a message about the letters you can use (a-z, 0-9 and -).
  • Confirm (Enter) and type your new hostname.
  • After the confirmation, you will be back on the main menu.
  • Select the Finish option to save your changes (use Tab and press Enter).
  • Raspi-config will ask you to reboot.
    Confirm and wait a few seconds.
  • Once the system reboot is complete, you can see the new hostname in the command prompt:

    Same thing with the “hostname” command.

Note: you may also need to follow the “Network update” from the last part if you have errors like this:
sudo: unable to resolve host <hostname>: No address associated with hostname

Solution 4: Use the configuration tool on Raspberry Pi OS Desktop

The last option I want to show you is to use the Raspberry Pi Configuration Tool on Raspberry Pi OS Desktop.
This is probably the easiest method if you are on this Raspberry Pi OS version.

Here is how to do this:

  • In the main menu, go to Preferences > Raspberry Pi Configuration.
  • In the configuration tool, update the hostname field:
  • Click “OK” to save the changes.
  • And as with the other solutions, a reboot is needed to apply changes.
    Click on “Yes” to immediately reboot.

Note: In this case, there is no need to update the /etc/hosts file. The wizard will change it for you.

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

Conclusion

That’s it, you now know 4 ways to change the Raspberry Pi name, whatever the Raspberry Pi OS version you use.

I hope this post was useful for you.
If you have any remaining question or a suggestion, feel free to leave a comment in the community.

By the way, here are a few posts that might help you to customize your system after installation:

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Create, understand, and improve any Python script for your Raspberry Pi.
Learn the essentials step-by-step without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.

How to Set a New Hostname on Raspberry Pi (4 Ways)

The Raspberry Pi is a powerful and versatile device that can be used for a variety of projects. One of the most important things you can do with your Raspberry Pi is to set a new hostname. This will allow you to easily identify your device on a network and make it easier to manage. Here are four ways to set a new hostname on your Raspberry Pi.

1. Using the raspi-config Tool

The easiest way to set a new hostname on your Raspberry Pi is to use the raspi-config tool. This is a command-line tool that allows you to configure various settings on your Raspberry Pi. To use it, open a terminal window and type the following command:

sudo raspi-config

This will open the raspi-config tool. Select the “Network Options” option and then select “Hostname”. Enter the new hostname you want to use and then select “OK”. Your new hostname will be set.

2. Editing the /etc/hosts File

Another way to set a new hostname on your Raspberry Pi is to edit the /etc/hosts file. This file contains a list of hostnames and IP addresses that are used to identify devices on a network. To edit the file, open a terminal window and type the following command:

sudo nano /etc/hosts

This will open the /etc/hosts file in the nano text editor. Find the line that contains your current hostname and replace it with the new hostname you want to use. Save the file and exit the editor. Your new hostname will be set.

3. Editing the /etc/hostname File

Another way to set a new hostname on your Raspberry Pi is to edit the /etc/hostname file. This file contains the hostname of your Raspberry Pi. To edit the file, open a terminal window and type the following command:

sudo nano /etc/hostname

This will open the /etc/hostname file in the nano text editor. Replace the current hostname with the new hostname you want to use. Save the file and exit the editor. Your new hostname will be set.

4. Using the hostnamectl Command

The last way to set a new hostname on your Raspberry Pi is to use the hostnamectl command. This is a command-line tool that allows you to set the hostname of your Raspberry Pi. To use it, open a terminal window and type the following command:

sudo hostnamectl set-hostname [new_hostname]

Replace [new_hostname] with the new hostname you want to use. Your new hostname will be set.

These are four ways to set a new hostname on your Raspberry Pi. Using the raspi-config tool is the easiest way, but you can also edit the /etc/hosts and /etc/hostname files or use the hostnamectl command. Whichever method you choose, your new hostname will be set.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?