Search code examples
javaspring-boothttpserver

What is a default port range for server.port=0 in spring boot application?


What is a default port range for server.port=0 in Spring Boot application?

I'm using spring-boot 2.7 (with default tomcat server) and working with java 11.

Or the range differs depending on OS?


Solution

  • The range for random ports depends on a couple of factors:

    • All port numbers are 16-bit unsigned integers, so the highest possible value is 65535.
    • Ports below 1024 are considered privileged ports on Unix-based systems (including macOS and Linux). Normal users can't open sockets on these ports, so typically, Spring Boot will not use them.
    • Ports from 1024 to 49151 are known as the User or Registered Ports, and ports from 49152 to 65535 are the Dynamic or Private Ports. These are typically the ports Spring Boot might choose from.

    If you are not running Spring Boot with root or administrator privilege, then the random port is likely to be between 1024 and 65535 (this is not a strict rule and can be different based on your OS configuration).

    However, Spring Boot doesn't set this range - the operating system's networking subsystem does. Spring Boot simply asks the OS to give it a available port, and the OS determines what port to hand out based on its own configuration.