3

When the web server sends data back to the browser, the browser receives that data using a particular port on my system. How can I find out what that port is on a Linux machine?

1
  • Unfortunately, questions about applications and protocols above OSI layer-4 are explicitly off-topic here, as are host configurations.
    – Ron Maupin
    Dec 18 '16 at 15:54
1

Linux will choose a new local port number for each connection you open. To see what range of port numbers your kernel is configured to use you can type:

cat /proc/sys/net/ipv4/ip_local_port_range

For historical reasons that sysctl has ipv4 in its name, but the setting does in fact apply to both IPv4 and IPv6. The default range is 32768 - 61000.

If your question is not what it will use for new connections but rather what it is using for currently open connections, you can use the netstat command. For example I usually use:

netstat -ntpW

Here n means no reverse DNS is performed on the IP addresses. t means I want to know about TCP connections. p means I want to know which PID each connection is associated with (this information will be incomplete if I don't have privileges to see the information or if the socket is open by more than one PID). And W means to use wide format (without that argument the IP addresses in the output may be truncated).

3

There is no fixed port on the browser for receiving the data. The source port of a TCP connection is picked more or less random (depending on the system) and differs from the source port of all other TCP connections from the same host to the same target host+port. There are often multiple TCP connections open to the same target at the same time. To have a look what connections are currently open and which ports they use run netstat -npt.