How to Change Hostname on Raspberry Pi OS
This article will introduce several methods to change the hostname on Raspberry Pi OS.
hostnamectl
Command to Change Raspberry Pi Hostname
Generally, there are three classes of hostnames: static, pretty, and transient.
We can manipulate each of them using the hostnamectl
command. Executing the hostnamectl
command without any arguments will print the system hostname and related information.
Also, it will produce the same output if the status
argument is specified:
hostnamectl
#OR
hostnamectl status
Output:
Static hostname: raspberrypi
Icon name: computer-vm
Chassis: vm
Machine ID: cbd927c65cc948a5b9e4384f2740aadf
Boot ID: 8c63bdba77f548c5930cdf3b70eb3630
Virtualization: oracle
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-13-amd64
Architecture: x86-64
If you want to change the current hostname, you can execute the hostnamectl
command with the set-hostname
option and specify a new name as the second argument.
Notice that this requires sudo
privileges if you are not logged in as a root
user.
sudo hostnamectl set-hostname pi
Output:
Static hostname: pi
Icon name: computer-vm
Chassis: vm
Machine ID: cbd927c65cc948a5b9e4384f2740aadf
Boot ID: 8c63bdba77f548c5930cdf3b70eb3630
Virtualization: oracle
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-13-amd64
Architecture: x86-64
The previous command will set both static and transient hostnames. Alternatively, you can change a specific hostname class by appending corresponding options --static
, --pretty
or --transient
to the hostnamectl
command.
The following command will set the pretty hostname to Pi
:
sudo hostnamectl set-hostname Pi --pretty
Now, if we print the hostname-related information, a new line will be included starting with Pretty hostname:
.
hostnamectl
Output:
Static hostname: pi
Pretty hostname: Pi
Icon name: computer-vm
Chassis: vm
Machine ID: cbd927c65cc948a5b9e4384f2740aadf
Boot ID: 8c63bdba77f548c5930cdf3b70eb3630
Virtualization: oracle
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-13-amd64
Architecture: x86-64
hostname
Command to Change Raspberry Pi Hostname
Another helpful command for modifying the Raspberry PI transient hostname is hostname
. This command also prints the hostname if executed without any arguments.
In case you want to alter the existing transient name, include a new hostname as the only argument:
sudo hostname pios
The previous command sets the pios
as a new system hostname. Note that the sudo
prefix is only mandatory when executing the command from a non-root user.
sysctl
Command to Change Raspberry Pi Hostname
The sysctl
command can generally configure kernel parameters at run-time.
Now, we will utilize it to set a new transient hostname. The command can accept the variable name, and its value pairs with an equal sign.
So, we modify the kernel.hostname
variable to have the value pico
.
sudo sysctl kernel.hostname=pico
Modify /etc/hostname
File to Change Raspberry Pi Hostname
Finally, you can edit the system file /etc/hostname
where the current static hostname is stored. This file only contains one line, and you can modify it using any preferable text editor.
In this case, we demonstrate a command-line solution using echo
and tee
commands. Note that the /etc/hostname
file needs sudo
privileges for non-root users.
echo "raspi" | sudo tee /etc/hostname
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook