ssh: connect to host 127.0.0.1 port 2222: Connection refused

Hello I've created a user in Ubuntu and I want to connect to it using ssh using this command

ssh user@127.0.0.1 -p 2222

and I got this error

ssh: connect to host 127.0.0.1 port 2222: Connection refused

I tried

ssh -vvv user@127.0.0.1 -p 2222

and got

OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to 127.0.0.1 [127.0.0.1] port 2222.
debug1: connect to address 127.0.0.1 port 2222: Connection refused
sssh: connect to host 127.0.0.1 port 2222: Connection refused

any help ?

Note : I am using virtualbox

I didn't install ssh, how to install it and configure it ?

3

9 Answers

Note, that 127.0.0.1 aka localhost is your local machine. Usually at this point you use the IP address or hostname of the remote host.

  • First install the ssh server and client on your target host and your local host

    sudo apt-get install ssh

    A configuration isn't necessary.

  • Per default SSH is listening on port 22, therefore use

    ssh user@127.0.0.1 -p 22

    or

    ssh user@127.0.0.1
  • Or reconfigure the port for the ssh server (target host)

    sudo nano /etc/ssh/sshd_config

    and change

    Port 22

    to

    Port 2222

    reload the configuration

    sudo service ssh force-reload

    and connect via

    ssh user@127.0.0.1 -p 2222
2

Couple of points here

  • By default Ubuntu has ssh client (which is for out-going connection from yours to somewhere else) but no ssh server (to allow in-coming connections from other computers to yours). That means, if you wanna ssh into our computer, you need the server, which you can get with sudo apt-get install openssh-server.
  • ssh by default runs on port 22. If you try any other port, connection will be refused. So once you have ssh server, you can just ssh username@localhost, and that will direct you to port 22 by default. Now, if you want to enable ssh login on port 2222, you will need to enable port forwarding. Especially since you are using virtualbox.
1

It looks that you don't have either:

  1. sshd daemon running inside a VM, or

  2. port forwarding configured correctly

For 1. you need to check whether sshd server is running on a vm by for example checking with VB console that service ssh status returns that is active and running. If not run sudo service ssh start.

For 2. you can check if VB is listening on that port. You can do that by checking all open ports in LISTENING mode. sudo netstat -anp | grep -w LISTEN should include port 2222 (assuming you're using linux as a host OS).

You should have something like this in output:

tcp 0 0 0.0.0.0:**2222** 0.0.0.0:* LISTEN 1564/sshd 

(in this case the sshd process is listening on port 2222)

Additionally you should tell us what is your network configuration inside VB. Without it it's hard to tell whether command you're using is right.

1

The following set of command will solve the problem for Ubuntu 18.04Enter the following commands in the terminal:

sudo rm /etc/ssh/ssh_host_*
sudo dpkg-reconfigure openssh-server

Now check with:

ssh localhost

This will work. And if you want then again install openssh with the following command:

sudo apt-get install open-ssh
2

Using below command worked for me.

$systemctl start sshd.service
1

It's all because of ssh error so I suggest you to reboot the machine and log-in again.

After log-in just open

cmd =$vi /etc/crontab 

and add

*/5 * * * * root service ssh restart

Now for every five minutes ssh service will restart and would troubleshoot all the ssh errors.

run command

grep "^[^#;]" /etc/ssh/sshd_config

and check parameter

GSSAPICleanupCredentials yes

if it is no, then update as yes and retry

The default port is 22. Using ssh xxxt@127.0.0.1 -p 22 -i ~/.ssh/xxx should work.

1

You should be sshing to the loopback address (127.0.1.1) for ubuntu not 127.0.0.1 IMO:

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like