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 [email protected] -p 2222

and I got this error

ssh: connect to host 127.0.0.1 port 2222: Connection refused

I tried

ssh -vvv [email protected] -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 ?


Solution 1:

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 [email protected] -p 22
    

    or

    ssh [email protected]
    
  • 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 [email protected] -p 2222
    

Solution 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.