Is it possible to change the hostname of my Raspberry Pi permanently, but also have the change take effect without rebooting?
5 Answers
Yes, it is possible using the following steps. Replace "mynewhostname" with whatever hostname you are changing to.
1) First change the hostname read on bootup, to make the change permanent.
echo "mynewhostname" | sudo tee /etc/hostname
2) Edit the hosts-file so that the new name can be resolved.
Replace the line 127.0.1.1 raspberrypi
(raspberrypi
being the old hostname) with 127.0.1.1 mynewhostname
.
sudo nano /etc/hosts
3) Change the currently used hostname.
This is the trick to skip rebooting, issue the following command to change the currently used hostname:
sudo hostnamectl set-hostname "mynewhostname"
4) Restart the mDNS daemon
To be able to use mynewhostname.local
from other machines, we need to restart the mDNS daemon to respond to the new hostname.
sudo systemctl restart avahi-daemon
-
1If you're logged in and your bash prompt includes the host name, the prompt is set when bash starts. So using hostnamectl won't change your prompt. To check the new prompt, enter "bash" to start a subordinate bash process". Or you can log out and log in. Commented Jan 25, 2018 at 18:44
-
To be strictly accurate: In
/etc/hosts
, the line127.0.0.1
is forlocalhost
. The line that should actually be changed is127.0.1.1
(as p8me's answer illustrates). For more details, see What is difference between localhost address 127.0.0.1 and 127.0.1.1 Commented Oct 17, 2022 at 22:05 -
Here is a repeat of Linus's reply into a shell script (with non-manual change of /etc/hosts).
setHostName:
host_name=$1
echo $host_name | tee /etc/hostname
sed -i -E 's/^127.0.1.1.*/127.0.1.1\t'"$host_name"'/' /etc/hosts
hostnamectl set-hostname $host_name
systemctl restart avahi-daemon
Usage:
./setHostName NEW_NAME
-
I just wrote an almost identical script. Even without the "without rebooting" requirement, this is a glaring omission from Raspbian! I just wanted to programmatically set the hostname on multiple devices. This does the trick. Commented Aug 7, 2019 at 0:42
Added a few sudo in and a re-login to get the prompt updated assuming the user is pi
change host name
host_name='AtHome'
echo $host_name | sudo tee /etc/hostname
sudo sed -i -E 's/^127.0.1.1.*/127.0.1.1\t'"$host_name"'/' /etc/hosts
sudo hostnamectl set-hostname $host_name
sudo systemctl restart avahi-daemon
sudo su -l pi
To follow on from Linus's answer...
In /etc/hosts
, the line 127.0.0.1
is for localhost
(the loopback interface lo
) and should not be changed.
The line that should actually be changed is 127.0.1.1
(as both p8me's answer and Paul's answer illustrate).
From What is difference between localhost address 127.0.0.1 and 127.0.1.1:
The reason it's used as the IP for your hostname is explained in Section 10.4 of the Debian Reference Manual.
Quote:
Some software (e.g., GNOME) expects the system hostname to be resolvable to an IP address with a canonical fully qualified domain name. This is really improper because system hostnames and domain names are two very different things; but there you have it. In order to support that software, it is necessary to ensure that the system hostname can be resolved. Most often this is done by putting a line in /etc/hosts containing some IP address and the system hostname. If your system has a permanent IP address then use that; otherwise use the address 127.0.1.1.
[SOLVED]
Building upon MichielB's answer here, the code for changing it dynamically through a new_hostname file after the pi has been set up is here, no modification to the command line is necessary and you will have NOT have to reboot the pi after its done. This code is for the situation where you wanna mass produce an OS image from a base image and your greater shell script modifies the hostname file to create its own unique OS image, thus the whole hostname changing process is automated and no reboot is necessary. The code can be used with custom front-end services to change the hostname dynamically
More details in the github link: https://github.com/techscapades/change-raspberrypi-hostname-from-file-no-reboot
#!/bin/bash
set +e
CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
NEW_HOSTNAME=`cat /path_to_new_hostname_file | tr -d " \t\n\r"`
echo `cat /path_to_new_hostname_file | tr -d "\t\n\r"` >/etc/hostname
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$NEW_HOSTNAME/g" /etc/hosts
echo `cat /etc/new_hostname | tr -d "\t\n\r"` >/proc/sys/kernel/hostname
exit 0
you can throw in systemctl restart avahi-daemon
before it exits but you might have to wait a while before you can access your_new_hostname.local again