I have dnsmasq configured on a DNS/DHCP server, which is set up so that the domain name mycompany.com is automatically appended to the hostname of each device connected over DHCP. e.g. the computer with the hostname derek will be assigned the FQDN derek.mycompany.com. This is working as expected for the other devices connected to the network (one is able to ssh into them using their long hostname), however the majority of them are Macs and have automatically configured network settings.

The issue: I have a Raspberry Pi with Raspbian installed on it (let's say the IP address is and its short hostname is rpi) that I'm trying to get set up so that I can SSH into it. It's connected to the network over wifi and I'd like for it to have a static IP and also be able to assign a FQDN to it (i.e. rpi.mycompany.com). To this end I've configured dnsmasq to identify it by its hardware address and to assign it a domain and IP. However after trying a lot of different configurations on both the rpi and the dns server, I keep getting the ssh: connect to host ... port 22: Connection refused error.

This is what I have set up:

/etc/network/interfaces on the pi:

auto lo                                                                                                                                                                                                                                                                                                                     
iface lo inet loopback              

iface eth0 inet dhcp                                   

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

/etc/resolv.conf on the pi:

domain mycompany.com
search mycompany.com
nameserver <ip address of dns server>

/etc/dnsmasq.d/hosts.conf on the DNS/DHCP server:

dhcp-host=<mac address of the rpi>,rpi,

Here are some facts for troubleshooting purposes:

  • When I run sudo service ssh status, it says that sshd is running ok.
  • I haven't touched /etc/ssh/sshd_config, i.e. Port 22, PasswordAuthentication yes, and is set in it.
  • I am able to ping the machine from another network device, both by its IP address, short hostname, and long hostname.
  • I can ping external addresses/domains (e.g. google.com) from the machine (which is why I didn't include /etc/wpa_supplicant/wpa_supplicant.conf)
  • The pi is assigned the correct IP address from what I can see in the ifconfig output, as well as the correct long hostname from the hostname -A.

But no matter what I do I can't seem to ssh in.

One thing I did notice was that at some point, I did manage to ssh in, and shortly after I attempted to scp over my public key to use pubkey authentication instead, and I got the following error message:

ssh: connect to host port 22: Connection refused
lost connection

And right after that I was suddenly unable to ssh in again, getting the same Connection refused message above.

What am I doing wrong? Is it something to do with scp or is that just a fluke?

  • connection refused sounds like either the ssh daemon isn't running, or the IP address is still wrong. Have you checked on the Pi to make sure the Ip address has been assigned correctly ?
    – Lawrence
    Jul 24, 2014 at 10:03
  • Check your machine have openssh-server and openssh-client installed??
    – RahulAN
    Jan 6, 2015 at 10:07
  • 2
    On the Pi can you ssh localhost and login successfully?
    – Fred
    Jan 25, 2015 at 11:31

3 Answers 3


I just had this issue, I was able to fix it by running sudo raspi-config, then going to Advanced, then Change Hostname, then Reboot.

Give it a try.

  • 2
    Or you may have a duplicate IP address on your network... Dec 5, 2014 at 20:37

I just wanted to point out. The SD format tool wasn't working for me. The new Pi 2 needs an SD with 2 partitions. FIRST is a fat32, this is your boot and SECOND a Linux ext3. The tools create 2 fat32 partitions, which will work until you try to enable ssh, as fat doesn't handle permissions properly and the ssh rsa keys will have 755 as their permissions and you cannot change them. Make sure you have your second partition as ext2, 3 or 4.


I found that restarting my router fixed the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.