8

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 ?

  • Did you install the ssh-server? Did you configure it to listen on port 2222? Normally it listens on port 22! – guntbert Sep 13 '15 at 11:12
  • Please show output of ssh -vvv user@127.0.0.1 -p 2222. – Łukasz Sep 13 '15 at 11:52
  • Did you configured sshd to listen on port 2222 ? if no then use ssh user@127.0.0.1 – heemayl Sep 13 '15 at 16:55
20

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
    
  • Hi, When I execute command. $ sudo service ssh start It throws error "service: no such service ssh " When I try to install ssh using command $sudo apt-get install ssh i throws error "No provider of 'ssh' found. " – Vinayak Dornala Mar 27 '17 at 13:10
  • Try sudo apt-get update prior to the sudo apt-get install ssh and see if that helps, if not you have sources.list problem. – thomasrutter Nov 9 '17 at 2:47
3

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

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.

  • Hi, When I execute command. $ sudo service ssh start It throws error "service: no such service ssh " When I try to install ssh using command $sudo apt-get install ssh i throws error "No provider of 'ssh' found. " – Vinayak Dornala Mar 27 '17 at 13:09
1

The following set of command will solve the problem for Ubuntu 18.04 Enter 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
  • Wow, this one is really weird, sad thing is, it worked ! I installed Ubuntu 18.04 using cloud-image on KVM using #cloud-init data. At first boot, SSH wouldn't work without doing this trick. – Sylvain Dec 26 '18 at 14:41
0

Using below command worked for me.

$systemctl start sshd.service
  • 1
    This answer would be more useful if you would indicate the version of Ubuntu that this worked on for you. It might seem trivial, but I have no doubt that there are readers that may not be aware of when Ubuntu changed from using upstart to systemd Do a good job expanding on this and ping me and I'll upvote. :) – Elder Geek Mar 29 '17 at 14:48
0

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.

0

run command

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

and check parameter

GSSAPICleanupCredentials yes

if it is no, then update as yes and retry

0

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

  • 1
    Welcome to Ask Ubuntu! Please don't add "thanks" as answers. Invest some time in the site and you will gain sufficient privileges to upvote answers you like, which is the Ask Ubuntu way of saying thank you. – Martin Thornton Oct 27 '18 at 22:35
-1

You should be sshing to the loopback address (127.0.1.1) for ubuntu not 127.0.0.1 IMO: https://unix.stackexchange.com/a/146028

Your Answer

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

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