How to set hostname on Raspberry Pi

The purpose of this tutorial is to show how to change the system hostname on a Raspberry Pi. The hostname is an important part of your device, as it allows it to be easily identified on the local network. The hostname is also shown in other prominent places throughout the system, such as in the prompt of the command line terminal. This helps make it easy to know which system you are working on if you frequently SSH into your Raspberry Pi and other systems simultaneously.

Hostnames give us a way to know which device we are interacting with either on the network or physically, without remembering a bunch of IP addresses that are subject to change. You should pick a descriptive hostname like “rasp-pi” or “backup-server” to make it easily identifiable to local server administrators, rather than something ambiguous like “server2” which could be just about any system on your network.

In this tutorial you will learn:

  • How to change the hostname on a Raspberry Pi via raspi-config
  • How to change the hostname on a Raspberry Pi via hostnamectl (systemd)
How to set hostname on Raspberry Pi
How to set hostname on Raspberry Pi
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software raspi-config, hostnamectl (systemd)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Set hostname on Raspberry Pi step by step instructions




We will cover two methods of changing the Raspberry Pi’s system hostname below. Ordinarily, the raspi-config utility proves easiest to use and is simple for all users to understand, but if you do not have this utility available for some reason, you can always set the hostname manually with the hostnamectl command. Choose whichever method you prefer.

NOTE
Upon changing your Raspberry Pi’s hostname, you will not be required to reboot the system in order for the new hostname to be applied, although you may need to reopen your terminal to see the change there.

Using raspi-config to change hostname

  1. Open the raspi-config utility with root permissions by executing the command below:
    $ sudo raspi-config
    
  2. Select the System Options menu from the raspi-config tool.
    Start by opening the System Options menu
    Start by opening the System Options menu
  3. Scroll down and select the Hostname option in this menu.
    Opening the Hostname menu inside of raspi-config
    Opening the Hostname menu inside of raspi-config
  4. The next screen contains a helpful reminder that the criteria for a hostname is quite strict. We can only use ASCII characters in the name, with a through z, all digits, and the hyphen being the only allowed characters. Just press Enter to get past this warning.

    Remember that hostnames must abide by the criteria shown here
    Remember that hostnames must abide by the criteria shown here



  5. Finally, enter the desired hostname you want to use, and then press Enter for the changes to take effect.
    Entering the desired hostname in the raspi-config menu
    Entering the desired hostname in the raspi-config menu

Using hostnamectl to change hostname

  1. First, check your current hostname. To do so use either the hostnamectl or hostname command:
    $ hostname
    raspberry
    

    Your output of the hostnamectl command may look similar to the one below:

    $ hostnamectl
     Static hostname: raspberry
    ...
    

    In either case the current hostname of our system is raspberry.

  2. Next, use the hostnamectl command to change the hostname of our Raspberry Pi system. For an example, we will switch our hostname to linuxconfig.
    $ sudo hostnamectl set-hostname linuxconfig
    

    The above command will change the hostname of our Raspberry Pi system to linuxconfig.

  3. Next, edit the /etc/hosts file to reflect the change by executing the command sudoedit /etc/hosts. For example, change:



    FROM:

    127.0.0.1 localhost
    127.0.1.1 raspberry
    

    TO:

    127.0.0.1 localhost
    127.0.1.1 linuxconfig
    
  4. Execute the hostnamectl command to confirm the hostname change:
    $ hostnamectl
     Static hostname: linuxconfig
    ...
    

Closing Thoughts




In this tutorial, we saw two methods for changing the hostname on a Raspberry Pi system using raspi-config and hostnamectl. We also learned about the importance of picking an applicable hostname to assist in the easy identification of a system. Administrators can use these methods to ensure that they have a network of appropriately named devices.



Comments and Discussions
Linux Forum