3

I use ssh tunneling with : ssh -i <my_file> -R *:0:localhost:80 <my_name>@<host>.

With the port = 0, the server assign me a port. How does the server choose and how can I config the range ?

Thomas
  • 33
  • 1
  • 3

2 Answers2

2

When the OS assigns a port to a TCP or UDP endpoint, it uses the ephemeral port range. Operating systems will allocate an unused port number from this range in some arbitrary fashion. It may choose a number randomly. Or it might just start with the low end of the range and assign the first number which isn't in use.

This page describes how to view and change the range on several different operating systems. On Linux, you can view and change the range by accessing /proc/sys/net/ipv4/ip_local_port_range:

$ cat /proc/sys/net/ipv4/ip_local_port_range 
1024 4999

# echo "49152 65535" > /proc/sys/net/ipv4/ip_local_port_range 

Alternately, you can use sysctl to view or set the key net.ipv4.ip_local_port_range.

Kenster
  • 7,483
  • 2
  • 32
  • 44
1

I'm pretty sure you can't specify to ssh the server side port range to pick from, certainly not on the client side. Assuming some *nix like system on the server, you should be able to set the ephemeral port range in /proc/sys/net/ipv4/ip_local_port_range.

Perhaps more usefully, you might just want to get the port assigned on the server side, which has been discussed here and here.

crimson-egret
  • 3,276
  • 17
  • 20