By default, a fresh Raspbian has the hostname or computer name “raspberrypi”. Maybe you want to change raspberry pi hostname. For example, if you operate several. Then you want them to differ in name.

Changing the hostname is not a problem in itself and usually done with a command. Unfortunately, most systems have a whole rat tail of changes. Unfortunately, you forget that or do not know it as a beginner yet. The changes are still necessary because otherwise it can eventually cause errors.
It is important that if you have changed the hostname, you check where the hostname is still fixed everywhere. These include configuration files from web server, mail server and also a manually maintained hosts file. In addition, after a change, you should also recreate the keys for the SSH server.
task
- Change hostname on Raspberry Pi.
- Check and change configuration files.
- Recreate SSH key.
Solution: Change hostname / computer name on Raspberry Pi
Before we change the hostname, we look at what the old name is.
hostname
Then we set a new hostname.
sudo hostname -b {NEWER_NAME}
Then briefly check:
hostname
Now the new hostname should be set.
Solution: Check and change configuration files
It may not be enough. It is important to check if the old hostname is still in any configurations.
sudo grep -lr "{ALTER_NAME}" /etc/
If so, then you should change that. It may be that the old hostname is somewhere in a string. You then have to look carefully, whether it is the old hostname, or if it is just a string. For example, in “/etc/apt/sources.list.d / …”. There you should not change anything.
But if the old hostname is in the “/ etc / hosts” for example, then you have to change that urgently.
sudo nano /etc/hosts
Then save and close: Ctrl + O, Return, Ctrl + X.
In general, a system restart is required.
sudo reboot
Only then is it ensured that Raspberry Pi and its network services can be reached via the new hostname.
Solution: Recreate SSH key
It is important that if you have changed the hostname, you also recreate the SSH keys.
First we delete all the files in which the keys are located. There are several of them. Nevertheless, a command is sufficient for deletion.
sudo rm /etc/ssh/ssh_host_
Afterwards, we perform a reconfiguration of the SSH server:
sudo dpkg-reconfigure openssh-server
The creation of the key files is automatic. Normally, the SSH server should also be restarted automatically. If not, then you have to do it manually.
sudo service ssh restart
A restart of Raspberry Pi is not required. However, one must note that after a new login via SSH, the identity of Raspberry Pi must be reconfirmed.
Note: Changing the hostname
Changing the hostname on a running system can have far-reaching consequences. For example, if other systems in the network with the old hostname want to connect to the system with a new hostname. This can mean that these connections are no longer possible.
You have to know that logical network connections are not based on the hostname, but on the IP address. Now, if the hostname changes, then the mapping between the hostname and IP address is no longer correct. For this reason it is recommended to consider a hostname right at the beginning and then to consider the further configuration. Once the hostname has been set, it should not be changed anymore.
Alternatively, there might be the possibility of maintaining a DNS server on the local network, where the hostname and IP address mappings are maintained, or a publisher service such as Avahi. However, it must be activated on all hosts.
Leave a Reply