Connection refused on SSH after changing hostname

I have my Rpi4 connected to ethernet headless. It was working fine, but then I notcied it was using default hostname DietPi, hostnamectl gave me errors, so I changed the hostname in /etc/hostname and rebooted the system. Now I cannot get access to it. Getting

Network error: Connection refused

when trying to connect in putty. I am using the correct IP address as confirmed on the router side.

I suspect I did something wrong, but can someone please explain how this could cause the issue? And, more importantly, what is the correct way to change the system hostname?

Samba shares are not working either, so it’s not just SSH issue.

Easiest would be using dietpi-config.

If possible try to connect screen and keyboard to gain local access

That was the first place I looked in.

Just googled, and according to this page it is located under “Security” section which I didn’t think to check.

Yeah, I’ll need to go to work and get my “investigation toolkit“ which has a micro-HDMI cable.

I’ll change the /etc/hostname back to DietPi and hope it appears in the network.

Yes, security is not the most obvious category for this. :sweat_smile:

Btw this is what the dietpi scripts does:

G_EXEC hostname "$*"
		G_EXEC eval "echo '$*' > /etc/hostname"
		G_CONFIG_INJECT '127.0.1.1[[:blank:]]' "127.0.1.1 $*" /etc/hosts
		G_CONFIG_INJECT 'AUTO_SETUP_NET_HOSTNAME=' "AUTO_SETUP_NET_HOSTNAME=$*" /boot/dietpi.txt

So it set’s the desired hostname in /etc/hostname and sets the hostname for the loopback address in /etc/hosts

1 Like

So not doing one of those things puts the system down? I thought it was more of a display thing.

Yes and no, it depends.
For example samba and SSH try to resolve their own hostnames when starting, but your new hostname is not mapped in /etc/hosts so they refuse to start or refuse connection.

1 Like

Small hint, see System Configuration - DietPi.com Docs
You can use dietpi-config 5 to directly jump to the security dialog. :slight_smile:

2 Likes