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

11,652

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/upload

      Match 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

Share:
11,652

Related videos on Youtube

Crolle
Author by

Crolle

Updated on September 18, 2022

Comments

  • Crolle
    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/dirand set a /home/user/.ssh/authorized_key file to host my public key.

    However, upon connexion using sftp -P 7000 [email protected] 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.

    • Tomasz Jakub Rup
      Tomasz Jakub Rup over 5 years
      First add -vvv: sftp -P 7000 [email protected] -vvv and show output
    • Crolle
      Crolle over 5 years
      I get the exact same output : Connection to host.com closed by remote host. Connection closed.
    • hyph
      hyph over 5 years
      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?
    • Tomasz Jakub Rup
      Tomasz Jakub Rup over 5 years
      Sorry, my fault: sftp -P 7000 -vvv [email protected]
    • Crolle
      Crolle over 5 years
      @hyph I defined user default directory using usermod -m -d /home/user/dir user @Tomasz Jakub Rup The log is quite extensive : pastebin.com/0dRZBPUS
    • Tomasz Jakub Rup
      Tomasz Jakub Rup over 5 years
      Please show Your .bashrc file (from host.com)
    • hyph
      hyph over 5 years
      so then you put your dotfiles and folders in /home/user/dir not in /home/user and it should work.
  • Sephethus
    Sephethus about 4 years
    I get the fd 1 is not 0_NONBLOCK what is wrong with my .zshrc? I don't see anything wrong with it.