Please I need help. I'm using Lubuntu 17.04, I tried to copy a file using "scp" remotely through ssh but logged in the host computer, not local. Ater that when I try to login via ssh I get this error "connect to host foobar port 22: Connection refused".

Things that work: ssh localhost indeed works, ssh host in another PC through the same router works.

Things that haven't worked:

sudo apt-get remove openssh-client openssh-server
sudo apt-get install openssh-client openssh-server
sudo ufw allow 22
ssh -p 22 foo@bar
ssh foo@IP
Going to /etc/ssh/sshd_config and setting "PermitRootLogin yes"
sudo rm /etc/ssh/sshd_config > sudo apt-get purge openssh-server > sudo apt-get install openssh-server
sudo iptables -L > sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT
/etc/init.d/ssh restart and service ssh restart
rm -rf /root/.ssh/*
/etc/init.d/ssh restart
sudo service ssh restart

Changed /etc/ssh/sshd_config, change Port 22 to Port 2222. Please anybody can help me?, I've donde almost everything I found on google.

Thank you

  • 1
    If you've changed port from 22 to 2222 you need to run ssh -p 2222 user@example.org. Furthermore, the rows of commands you ran (edit sshd config, then purge?!) doesn't really make sense. What are you trying to do?
    – vidarlo
    Oct 4, 2017 at 4:44
  • This is confusing. You are having issues logging in to bar host from your local PC - why would you change anything with openssh/sshd service? Apparently destination host is rejecting the connection. Try running ssh -v foo@bar to get verbose/debug output (or -vvv to get more) and see if there's anything helpful. If there's nothing you'll need to contact remote host admin to look into sshd logs.
    – michal
    Oct 4, 2017 at 6:02

1 Answer 1


Don't permit root login. That is like wearing a 'kick me' sign. Transfer files as a regular user.

Appears to me your code has some gibberish, you are pasting together advice from different sources. The last 2 lines betray you there. These are old style service launchers. In current Ubuntu, you'd run

sudo systemctl start ssh

Since you mention ufw, it makes me wonder if you are also running other security. If you have fail2ban, then your SSH login might be blocked by your security because you have too many failed tries. If you did some cutting and pasting in /etc/hosts.deny, that could cause same effect.

Here is what I would do. Suppose you are logged in as user@home.com and you have account same user name on other system. Maybe if you are playing in 2 systems at home, you have IP numbers like instead of system names. I guess the numbers are more likely.

On other system, Turn off UFW and other blockages like fail2ban, then go to your user@home.com system. Try

ping other.system

To make sure connects are ok

ssh user@other.system

To prove login works. Then log out, try

scp file.txt user@other.system:

That will copy file.txt to other system. Then login again over there an make sure file is there.

Keep it simple, don't copy/paste code you don't understand from websites.

While on the other system, do the UFW config. Do not run iptables directly. UFW is a simple interface to iptables firewalls. Turn on the UFW firewall again. Log out. If you strangled yourself with errors in iptables, perhaps this is bad enough to warrant a restart.

Try to log in again. If rejected, then your UFW setup is bad, have to walk over to other system, try to fix over there, you are not allowed to log in.

Please note here that the ssh programs on home.com are needed, but at this stage you do not need change the config. Outgoing ssh is generally configured sufficiently. On other system, config generally does not need alteration. You make a disastrous mistake by allowing root login, don't cause trouble for yourself.

I realize that if you have account named "user" on both accounts, it is not needed to type "user@" in these commands. I write this for clarity to new users.

If problem is that fail2ban is blocking you and your user is in jail, I wrote notes on how to clear it a few years ago (http://pj.freefaculty.org/blog).

I suppose my 'old man' message is try to understand lines you run, and be more careful about copying instructions for other distributions or times.

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.