Why do I get a “Connection to [host] closed by remote host” error over SFTP connection?
Solution 1
I was stumbling on this question when I had a similar issue, I followed this guide and that worked for me. In essence using user's home directories will not work since the directory that you want to lock the user to must be owned by root and cannot be writable by anyone else.
So try to:
-
create a folder like
/var/sftp
that is owned by root -
add a sub-folder like
/var/sftp/user1
that is owned by user1 and where they can write to -
then modify
/etc/ssh/sshd_config
like following, so that the user can only access their folder for file access/uploadMatch User user1 ForceCommand internal-sftp ChrootDirectory /var/sftp PermitTunnel no AllowAgentForwarding no AllowTcpForwarding no X11Forwarding no
-
finally restart ssh service
sudo systemctl restart sshd
-
access to other user's folders can be restricted by
sudo chmod 700 /var/sftp/*
Solution 2
How to debug
First step is running command with verbose flag:
sftp -P 7000 -vvv [email protected]
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
Related videos on Youtube
Crolle
Updated on September 18, 2022Comments
-
Crolle over 1 year
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/dir
and set a/home/user/.ssh/authorized_key
file to host my public key.However, upon connexion using
sftp -P 7000 [email protected]
and after typinguser
'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
becauseauthorized_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 (sayuser2
) 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.-
Tomasz Jakub Rup over 5 yearsFirst add
-vvv
:sftp -P 7000 [email protected] -vvv
and show output -
Crolle over 5 yearsI get the exact same output :
Connection to host.com closed by remote host. Connection closed.
-
hyph over 5 yearsDid 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?
-
Tomasz Jakub Rup over 5 yearsSorry, my fault:
sftp -P 7000 -vvv [email protected]
-
Crolle over 5 years@hyph I defined
user
default directory usingusermod -m -d /home/user/dir user
@Tomasz Jakub Rup The log is quite extensive : pastebin.com/0dRZBPUS -
Tomasz Jakub Rup over 5 yearsPlease show Your
.bashrc
file (fromhost.com
) -
hyph over 5 yearsso then you put your dotfiles and folders in /home/user/dir not in /home/user and it should work.
-
-
Sephethus about 4 yearsI get the fd 1 is not 0_NONBLOCK what is wrong with my .zshrc? I don't see anything wrong with it.