20

I have a requirement to know which all ports in my Solaris machine are free to be used for any kind of network communication.

I tried the netstat -a command. But the information returned didn't give a convincing result of which all ports I can use for a new application that I am writing. Appreciate any assistance provided in this regard.

3
  • Within what numerical range? There are always ephemeral ports in use and these constantly change.
    – mdpc
    Nov 19, 2014 at 7:22
  • What about the result of netstat -an was not convincing to you? netstat should list everything that's being used. Everything else (normally many thousands of ports!) is available.
    – Celada
    Nov 19, 2014 at 8:21
  • 1
    Also, it may help to know why you need this information. When binding a socket you can always choose to let the kernel assign a port for you, so if you don't care which port you get as long as you get one, then you don't need to find one manually.
    – Celada
    Nov 19, 2014 at 8:22

2 Answers 2

24

1: You should't use anonymous ports (a.k.a. ephemeral ports) to implement a UDP or TCP service.

By default, these ports are in the range 32768 - 65535.

# ndd /dev/tcp tcp_smallest_anon_port
32768
# ndd /dev/tcp tcp_largest_anon_port 
65535

2: Unless your service is running as root or has the required RBAC privilege, you shouldn't use a privileged port. By default all ports from 1 to 1024 are privileged:

# ndd /dev/tcp tcp_smallest_nonpriv_port
1024

3: Unless you know what you are doing, you shouldn't use a well known port that is expected to run a different service than the one you plan to. Well known ports are listed in /etc/services. eg:

# tail /etc/services
dtspc           6112/tcp                        # CDE subprocess control
fs              7100/tcp                        # Font server
solaris-audit   16162/tcp                       # Secure remote audit logging
servicetag      6481/udp
servicetag      6481/tcp
wnn6            22273/tcp                       # Wnn6 jserver
wnn6            22273/udp                       # Wnn6 jserver
rdc             121/tcp                         # SNDR server daemon
snmpd           161/udp        snmp             # Net-SNMP snmp daemon
swat            901/tcp                         # Samba Web Adm.Tool

4: You shouldn't use a port that is already used by an existing service. You can get these with running:

# netstat -an | grep LISTEN | nawk '{$0=$1;gsub(".*\\.","");print}' | sort -un

5: Finally, you shouldn't use a port used by a service currently disabled but that might be enabled in the future. This is the trickiest part to comply with...

13

You can use 1-65,535 ports on your system in which first 1024 are root privileged.

So Instead of finding the free port, you can get list of used ports using below command

netstat -tunlep | grep LISTEN | awk '{print $4}'

Then you can use any port from 1-65535 except those ports.

1
  • 3
    Some of the netstat options you use are not supported by Solaris netstat implementation.
    – jlliagre
    Nov 19, 2014 at 22:02

You must log in to answer this question.

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