11

In FTP passive mode, I read that the server sends a random port number to the client where it can establish a data channel.
Then client establishes a data channel from its random port number to this port number sent by the server.

My question is why does the server send a random port number to the client? Why can't the client directly establish a data channel to port number 20 on the server side?

1
  • Unfortunately, questions about protocols above OSI layer-4 are off-topic here.
    – Ron Maupin
    Commented Aug 29, 2017 at 19:45

2 Answers 2

15

That's how the FTP protocol was designed to work in the passive mode. It was probably not a good idea, as I do not think that this model was ever repeated again in any other protocol (and that's true even more so about the FTP active mode). Wikipedia FTP article mentions that FTP was designed this way because originally it was not intended to operate over TCP/IP (FTP originated in 1971).


On the data connection port, there's no protocol. All that the server knows – the only thing that carries any information in that connection – is the port number you connect to.

If you were to connect to the same port every time, the server would not be able to tell what file you are connecting for. The port number serves as a link between a transfer request on the control connection and a data connection – the port number is contained in the response to the PASV command.

If two clients were to request a transfer at the same time, when the server accepts a connection on a single port, the server would not be able to tell what file to transfer. Of course, the server could use a client IP for the decision (actually many FTP servers do validate that the client IP matches the IP used on the control connection, for security).

But this would not work for:

  • Multiple connections from the same machine (most FTP clients do support parallel transfers/queues and you can actually run multiple different FTP clients on one machine);
  • Connection from different machines within the same (corporate) network, as those have the same external IP.

Partially copied from my answer to Why does FTP passive mode require a port range as opposed to only one port? on Server Fault.

0
5

Usually, the server doesn't send a random port but a free one from a defined (by installation) range/pool - for the client this looks random. This port needs to be forwarded at the firewall which requires defining a range.

Unfortunately, FTP is ancient. I guess, the ancient servers couldn't distinguish multiple clients' data sessions except by port. Generally, it's better to move on to more up-to-date protocols where everything is neatly packetized within a single socket session.

3
  • So it can be 20 as well right ? In every website the server port number for data is other than 20.
    – Zephyr
    Commented Aug 29, 2017 at 11:11
  • 20 is the outgoing port from the server for active FTP (which isn't in much use any more).
    – Zac67
    Commented Aug 29, 2017 at 11:12
  • It wasn't that ancient servers had trouble, it's that protocol designers were still trying to figure out the best way to do things more complicated than primitive request-response.
    – Mark
    Commented Aug 30, 2017 at 3:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.