Renci.SshNet.SftpClient getting failed on Azure VM RRS feed

  • Question

  • User1037983650 posted

    Hi Folks, 

    I am using following code to connect SFTP. It is working fine on my local system. but getting failed on Azure VM and giving exception "An existing connection was forcibly closed by the remote host".

    I have already opened TCP 22 port as an outbound for that VM.

       using (Renci.SshNet.SftpClient client = new Renci.SshNet.SftpClient(host, port, user, password))
                        if (client.IsConnected)
                            Response.Write("OA Connected.");
                        //     var f = client.ListDirectory("");
                    catch (Renci.SshNet.Common.SshAuthenticationException ae)
                        Response.Write("Exception while connecting to OA (SSH): " + Convert.ToString(ae.Message));
                    catch (Exception ex)
                        Response.Write("Exception while connecting to OA: " + Convert.ToString(ex.Message));
                    return new EmptyResult();

    Any assistance would be much appreciated. Let me know if you need additional information.

    Thank you! 

    Thursday, August 6, 2020 7:36 AM

All replies

  • User1686398519 posted

    Hi Sukhdeep,

    1. If you have enabled port 22 on the “Networking” tab from the Azure portal, check if you have enabled port 22 through Windows firewall (using the control panel section from your virtual machine).
    2. By default, the SFTP port is 22. If you have modified it yourself, you need to set it according to the actual port.
    3. If your virtual machine is under a load balancer, you should:
      • Enable port 22 on the Windows firewall.
      • Use “Inbound NAT rules” tabs from your load balancer settings page through Azure web portal. From there, add a rule to enable port 22 and redirect queries to your virtual machine

    Best Regards,


    Friday, August 7, 2020 7:30 AM
  • User1037983650 posted

    Hi YihuiSun, 

    Thank you for your response. 

    I am not using Load Balancer for now. Will I need to use one? Can you please let me know steps to enable for outbound SFTP calls by adding load balancer settings?

    Thank you!

    Tuesday, August 11, 2020 1:30 PM
  • User-474980206 posted

    if you are not using a load balancer, then you just need to open the ports in two places:

    1) login to the VM and and change the firewall rules to open the port

    2) login to azure and open the port to the vm


    Tuesday, August 11, 2020 3:28 PM
  • User1037983650 posted

    Hi Bruce, 

    Thank you for your response. I tried it, unfortunately it did not work. 

    I would like to provide additional information - When I try same connection from FileZilla, it does not work there as well and throws following error:

    Status: Connecting to ftp.officeally.com...
    Response: fzSftp started, protocol_version=9
    Command: open "XXXX@ftp.officeally.com" 22
    Error: FATAL ERROR: Network error: Software caused connection abort
    Error: Could not connect to server

    Tuesday, August 11, 2020 7:09 PM
  • User-474980206 posted

    did you create outbound rules for both the VM's firewall and the azure hosting?

    Tuesday, August 11, 2020 8:21 PM
  • User1037983650 posted

    Yes, I created on both. 

    Wednesday, August 12, 2020 3:27 AM
  • User-474980206 posted

    then I'd check if the dns is configured correctly. use nslookup or ping to verify name lookup.

    Wednesday, August 12, 2020 3:56 AM
  • User1037983650 posted

    I have configured couple of web sites on same VM and they are working fine. So if I am not wrong, DNS is fine. I can also jump on Skype to connect and show you, if you have a moment now. I would really appreciate your help. Thank you!

    Wednesday, August 12, 2020 3:59 AM