Why do I get a “Connection to [host] closed by remote host” error over SFTP connection?

3

1

I'd like to connect as user to a remote host through SFTP.

Moreover, I'd like to land into a specific directory (say /home/user/dir). I've created user with default working directory as /home/user/dirand set a /home/user/.ssh/authorized_key file to host my public key.

However, upon connexion using sftp -P 7000 user@host.com and after typing user's password, I get the following error immediatly: Connection to host.com closed by remote host. Connection closed.

Any idea about where it might come from (or how to debug that kind of issue)?

UPDATE

I was finally to connect as user because authorized_key didn't have the proper rights. However, I got the exact same error message while trying to land in /home/user/dir with another user (say user2) which was granted group access to /home/user/dir. But as it turn out in this response, SFTP chroot jail does not allow that by design. I'll try to use symbolic links between /home/user/dir and /home/user2/dir to solve this issue.

Crolle

Posted 2018-11-09T13:08:56.367

Reputation: 131

1First add -vvv: sftp -P 7000 user@host.com -vvv and show output – Tomasz Jakub Rup – 2018-11-09T14:24:55.637

I get the exact same output : Connection to host.com closed by remote host. Connection closed. – Crolle – 2018-11-09T14:44:35.993

Did you set /home/user/dir as your user's $HOME ? or what else did you do to "land into a specific directory" ? Did logging in work before you changed that directory? – hyph – 2018-11-09T14:52:17.357

Sorry, my fault: sftp -P 7000 -vvv user@host.com – Tomasz Jakub Rup – 2018-11-09T14:59:26.307

@hyph I defined user default directory using usermod -m -d /home/user/dir user @Tomasz Jakub Rup The log is quite extensive : https://pastebin.com/0dRZBPUS

– Crolle – 2018-11-09T15:42:36.483

Please show Your .bashrc file (from host.com) – Tomasz Jakub Rup – 2018-11-09T19:31:04.570

so then you put your dotfiles and folders in /home/user/dir not in /home/user and it should work. – hyph – 2018-11-12T17:01:14.950

Answers

1

How to debug

First step is running command with verbose flag:

sftp -P 7000 -vvv user@host.com

Analyze of log

Connection are estabilished

debug1: Authentication succeeded (publickey).

but, ssh cannot set non-blocking mode:

debug3: fd 1 is not O_NONBLOCK
Connection to host.com closed by remote host.

Probably Your shell initialization file (.bashrc, .bash_profile, etc.) breaking ssh connection

Tomasz Jakub Rup

Posted 2018-11-09T13:08:56.367

Reputation: 598

I get the fd 1 is not 0_NONBLOCK what is wrong with my .zshrc? I don't see anything wrong with it. – Sephethus – 2020-02-18T21:20:34.150