143

I have an Ubuntu Server 10.10 32-bit in my home. I'm making SSH connections to it from my PC via Putty.

The problem is, sometimes I'm able to login seamlessly. However, sometimes it gives me an error like this: Network error: Connection refused.

Then, I dont't change anything, try to login a few times more, wait a while and try again. Sometimes I can log in, sometimes I cannot. It seems pretty random to me.

What can I do to solve this?

Edit:

And Sometimes, Putty gives Network error: Software caused connection abort error after displaying login as: text.

Here is the ping -t output:

Pinging 192.168.2.254 with 32 bytes of data:
Reply from 192.168.2.254: bytes=32 time=6ms TTL=64
Reply from 192.168.2.254: bytes=32 time=65ms TTL=6
Reply from 192.168.2.254: bytes=32 time=88ms TTL=6
Reply from 192.168.2.254: bytes=32 time=1ms TTL=64
Reply from 192.168.2.254: bytes=32 time=3ms TTL=64
Reply from 192.168.2.254: bytes=32 time=1ms TTL=64
Reply from 192.168.2.254: bytes=32 time=1ms TTL=64
Reply from 192.168.2.254: bytes=32 time=1ms TTL=64
Reply from 192.168.2.254: bytes=32 time=1ms TTL=64

I turned off firewall of router, and everything seems to work now. Except for that, I still can't enter my web server by typing external IP from my PC.

5
  • next time it craps out like that; run a ping on it again, does it still work? how many people ssh to this machine?
    – myusuf3
    Commented Mar 13, 2011 at 4:33
  • Only I ssh this server.
    – frbry
    Commented Mar 13, 2011 at 12:03
  • Exactly the same problem here, also getting a mix of successful connects (which then work for hours), 'connection refused' and 'software caused connection abort' after the login or password entry. To provide some additional details: in my case it's a 64-bit virtual machine (running Ubuntu Server 10.10) on a Microsoft Hyper-V host, using the "old network card" emulation. Commented Mar 31, 2011 at 12:24
  • 1
    SSH is installed on your server? Commented Jan 8, 2014 at 18:27
  • use the command below to restart ssh service service sshd restart
    – Joaquim
    Commented Jan 31, 2022 at 14:59

14 Answers 14

119

You must install openssh-server on Linux before this will work. Get the internal IP address of Ubuntu and use that IP to setup port forwarding on port 22 (SSH) section of your router. Although if you don't intend to SSH from outside your network, this is not required.

Here's how to install openssh-server:

sudo apt-get install openssh-server openssh-client
[ENTER PASSWORD]
4
  • 18
    Does not answer the question directly but this is useful as Ubuntu does not come pre-installed with SSH server.
    – Ostati
    Commented Dec 30, 2015 at 17:50
  • 2
    I recently got a new laptop and totally forgot it's not installed by default =D
    – Izkata
    Commented Apr 5, 2016 at 14:31
  • 2
    This not only doesn't answer the question directly, it's just plain wrong. The OP has an intermittent problem using SSH, so obviously the server is installed.
    – Auspex
    Commented Jun 1, 2022 at 9:53
  • In our case, I used the following to solve the "unmet dependencies" errors: sudo aptitude install openssh-server Commented Dec 9, 2023 at 1:15
57

IP address conflict turned out to be the root cause when I was seeing this SSH error message.

After reading the answers above I suspected an IP address conflict, but needed to prove that address conflict was or was not the problem.

This great article showed how to use arp-scan to see if two pieces of hardware were claiming the same IP address.

In my case the server machine had a static ip address of 192.168.1.42, I used arp-scan to check that address:

$ sudo apt-get install arp-scan
$ arp-scan -I eth0 -l | grep 192.168.1.42
  192.168.1.42 d4:eb:9a:f2:11:a1 (Unknown)
  192.168.1.42 f4:23:a4:38:b5:76 (Unknown) (DUP: 2)

Sure enough there was a conflict, as shown above. Then I ran arp-scan without the grep, found that .43 was available, went and edited /etc/network/interfaces and changed the static ip from .42 to .43

7
  • Actually running that command showed only one line on my box, except that the name was that of another device.
    – assylias
    Commented Jul 25, 2012 at 23:26
  • 1
    Thank you so much for the point in the right direction about IP address conflict. Had a similar problem! +1 Commented Jan 10, 2015 at 2:47
  • Just saved me so much time... kudos... Commented Jul 8, 2015 at 19:31
  • Sometimes my router just needs to be restarted to solve this issue. Commented Sep 11, 2016 at 22:47
  • yep. Had to tell my wife to restart wifi on her iphone. Restarting a router would fix it too Commented May 24, 2020 at 2:26
21

open the file of ssh config:

sudo nano /etc/ssh/ssh_config

find the port

#port 22

uncomment (remove the #) the port option

then save the file by ctrl + x and restart your service

sudo service sshd restart
4
  • 2
    This was the problem for me on a freshly installed Raspbian on Raspberry Pi 3 B+ Commented May 20, 2020 at 7:13
  • Did not help me. Commented Feb 18, 2021 at 18:40
  • "Failed to restart ssh.service. Unit ssh.service not found" Commented Aug 2, 2021 at 3:20
  • @KansaiRobot Should be sshd.service. sshd is the server, ssh is the client
    – Auspex
    Commented Jun 1, 2022 at 10:33
20

Use arping on the IP address that is having connection issues. That will show the MAC address for each ping reply, and hopefully will unmask the rogue MAC address.

arping 192.168.2.254

You should check the DHCP IP address pool on the DHCP server, make sure no devices have static IPs that collide with the DHCP pool.

These clues point to duplicate IP:

  • ping ttl and round trip time looks like 2 distinct servers
  • intermittent disconnects without rebooting
1
  • 3
    I needed to add -I wlan0 (wlan0 being my wifi device)
    – guhur
    Commented Jan 15, 2018 at 14:32
10

I had the same problem, even though I had a static IP address. Turns out another server on my network had been assigned the same (static) IP address. (Duh.) So it does appear that the problem is caused by IP address conflicts, but there may be various possible ways they can happen. If you set yours to static and still have a problem, try shutting down your machine and pinging the address. If you get any replies, start looking for what else could have the same address.

0
8

What do you get if you do this?

grep 192.168.2.25 /etc/hosts.deny

If that returns anything then you need to remove it.

3
  • 11
    That's one of the infamous "useless uses of cat" - you can simply grep 192.168.2.25 /etc/hosts.deny Commented Feb 26, 2014 at 8:19
  • 1
    @TobiasKienzler Fixed it
    – wjandrea
    Commented May 23, 2017 at 20:09
  • Opening the file in an editor is likely to be easier to see what's going on in there. Also in my case the content was: ALL: ALL EXCEPT localhost (GParted default setup). All I had to do is add my IP address to the list of exceptions. Commented Jan 13, 2022 at 17:59
4

This looks more of a problem of your network equipment than the server itself.

Check /var/log/messages for ethernet link up/downs (or wlan in case of wireless). If so try changing the cables.

6
  • Yes, i tried and succeeded in making an ssh connection from outside of my network (which my server and my PC are located in). It seems working well. My server is also a web server. When I try to connect over port 80 from my PC, i see my ADSL modem's login page. But when I try the same thing from another computer which is outside my network, I see Apache's It Works page.
    – frbry
    Commented Mar 12, 2011 at 19:08
  • What is you PC ip, your server ip and your internal adsl router ip?
    – forcefsck
    Commented Mar 12, 2011 at 19:22
  • PC: 192.168.2.2 (Received from DHCP) Server: 192.168.2.254 (Static, out of DHCP range) ADSL Router: 192.168.2.1
    – frbry
    Commented Mar 12, 2011 at 19:24
  • I'm a bit confused, when you connect from 192.168.2.2 to 192.168.2.254 port 80 you get the adsl modem's login?
    – forcefsck
    Commented Mar 12, 2011 at 19:54
  • 1
    Unless you have setup some kind of web proxy to your server, then clearly your adsl modem is responding to requests made to 192.168.2.254 and probably that's the case when you get the Connection refused error with ssh. So you have to recheck the settings of your adsl modem.
    – forcefsck
    Commented Mar 12, 2011 at 20:27
4

I can confirm this exact issue: It's not a simple connectivity issue. The ethernet link does not change state; server is reachable via ping; ssh connects flawlessly occasionally, then seemingly randomly does not connect or existing ssh session drops. This occurs on Ubuntu 10.04 and 11.04. Following hheimbuerger's suggestion I gave the server a static IP, this seemed to fix it.

Workaround: Change adapter from DHCP to static.

4

It's basically because of any one of the following reasons:

  1. Too many users on the network trying to access the server
  2. More than one computer on the network has the same IP as the server causing an IP conflict
  3. Wrong username or your credentials have been revoked
3

On my case, fail2ban had banned my IP:

  1. iptables -n -L (use this command to see if you were banned by this ****** or something else)
  2. https://bobcares.com/blog/fail2ban-unban-ip/
2

You might want to check your iptables rules on your server. It sounds like you've a rule for limiting new SSH connections.

The next rule allows 5 new connections per minute, if you exceed this limit, your new connections will be rejected after some time has passed:

-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m limit --limit 5/min -j ACCEPT

See the IPTables / Netfilter documentation, scroll a bit till limit.

1
  • Related, I found my ufw was blocking incoming data on port 22 by sudo ufw disable. By default ufw blocks all incoming ports. Solution was sudo ufw allow ssh.
    – rdtsc
    Commented Nov 19, 2019 at 13:39
2

I had exactly the same issue, and in my case it turned out to be an IP address conflict. My Ubuntu VM had a dynamic address, but another machine (in this case a phone) had a static IP assigned that the DHCP server did not know about.

Just changing the IP that was assigned by the DHCP server to the Ubuntu VM fixed all connection issues.

2

arp-scan is showing two duplicate devices, but when I run Advanced IP scanner on Win8 they all look fine. So they don't always agree.

I set the router to revoke all the leases by telling it to give only 1-hour leases. Will give it time to see if it clears up.

0

On Manjaro, while sshd is installed by default, the server daemon was not actice.

sudo systemctl start sshd.service opened it immediately, and works out of the box.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .