Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Windows 7 and have multiple FTP client applications working without any problem. However, when I use command-line FTP, I can get connected, log in (after entering user name and password), but it always gets disconnected when the dir or ls command is used. The response is:

Connection closed by remote host.

How can I fix this problem?

share|improve this question
Open up ports 20, 21 in your firewall. – Sean Perkins Nov 19 '15 at 19:15
up vote 10 down vote accepted

You'll usually see this type of thing happen when the server tries to connect back to you but can't. FTP originally used two TCP channels. One was for commands, and the other for transferring data. When you run the dir command it uses a data channel. The data channel is established on demand and results in the server trying to connect back to you.

That's most likely being blocked by your router or Windows firewall. You need a client that supports PASV mode.

You can enable PASV mode in Internet Explorer. Or use a different client. I'm not sure if the command-line version can use PASV mode. It's a very basic client.

share|improve this answer
Thanks, Matt. I thought the command-line FTP client used passive mode by default but I have just found multiple posts saying Windows command-line FTP actually excludes passive mode. IE always works fine on my machine for FTP. I just want to use command-line to examine some raw data from an FTP server. – Hong Nov 28 '10 at 4:53
As an experiment, you could see if enabling or disabling PASV in internet explorer makes a difference.. or gives similar or the same error. – barlop Nov 28 '10 at 23:15
ftp> PASV - Invalid command. (Windows 7 Ent.). This answer is exactly what happens at work. – Bratch Jun 28 '12 at 17:10
it needs to be QUOTE PASV – JosephStyons Jul 10 '12 at 5:29

I encountered this problem on a Windows 2008 server.

This article suggests enabling the Application Layer Gateway Service. This did not fix the problem for me.

What did fix the problem was adding an exception in the Windows Firewall for C:\Windows\system32\ftp.exe

share|improve this answer
By default windows firewall include ftp.exe in its exception but only for home/work network. Make sure you are not on public network else allow ftp for public network as well. – Jags Mar 5 '14 at 15:12

I had an issue with transmitting files via FTP once I was logged into the box in question. It would create the file name, but the file was empty. I resolved it by doing the following:

  • Go to Control Panel → Windows Firewall → Advanced Settings → Inbound Rules
  • Double-click the Name column to sort by name
  • Scroll down until you see two rules called "File Transfer Program"; these were set to Block on my PC (big red circle with a line through it)
  • Double-click on the rule
  • On the General tab, under Action, set to Allow the connection
  • Click OK
  • Do this for the other rule

It should be good to go now.

share|improve this answer
What are the security implications of doing this? – Peter Mortensen May 9 at 17:37

Windows FTP command-line client ftp.exe supports an active FTP mode only.

Setting up the FTP in the active mode can be cumbersome nowadays due to firewalls and NATs.

It's likely because of your local firewall or NAT that the server was not able to connect back to your client to establish data transfer connection for the directory listing (ls or dir commands).

Or your client is not aware of its external IP address and provides an internal address instead to the server (in PORT command), which the server is obviously not able to use.

See my article Network Configuration for Active Mode.

If possible, you should use a passive mode as it typically requires no additional setup on a client-side.

Unfortunately Windows FTP command-line client (the ftp.exe) does not support passive mode at all. It makes it pretty useless nowadays.

Use any other Windows FTP command-line client instead. Most other support passive mode.

For example WinSCP defaults to passive mode and there's a guide available for converting Windows FTP script to WinSCP script.

(I'm the author of WinSCP)

share|improve this answer

I was experiencing the same issue with third-party POS software on a Windows 7 machine that batches client transaction data over back to an SQL server.

It worked fine until the client location changed ISPs and, therefore, static IP addresses. Command-line FTP commands did not work beyond the initial login and none of the remedies I found online or this thread worked.

Just for kicks, I re-created the FTP rule in the firewall, a cheapo D-Link router/firewall, and viola, it seems to be working again.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .