Troubleshooting Hugo's 'Port 1313 already in use' error

Resolving a "Port 1313 already in use..." error

Today I:
… started my computer, as usual
… checked for patches, as usual
… checked for updated software (via chocolatey!), as usual
… started Hugo’s local server… as not usual!

C:\Users\gbeifuss\blog>hugo server -D -v -p 1313
port 1313 already in use, attempting to use an available port
Start building sites …
hugo v0.87.0-B0C541E4+extended windows/amd64 BuildDate=2021-08-03T10:57:28Z VendorInfo=gohugoio
INFO 2021/08/20 11:20:57 syncing static files to C:\Users\gbeifuss\blog\
WARN 2021/08/20 11:20:57 Search page not found. Create a page with layout: search.
WARN 2021/08/20 11:20:57 Archives page not found. Create a page with layout: archives.

                   | EN
-------------------+-----
  Pages            | 45
  Paginator pages  |  5
  Non-page files   |  0
  Static files     |  5
  Processed images |  5
  Aliases          | 12
  Sitemaps         |  1
  Cleaned          |  0

Built in 135 ms
Watching for changes in C:\Users\gbeifuss\blog\{archetypes,content,data,layouts,static,themes}
Watching for config changes in C:\Users\gbeifuss\blog\config.yaml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:15823/ (bind address 127.0.0.1)
Press Ctrl+C to stop

This has never been a problem before. How can port 1313 already be in use? There’s nothing else on my system configured to use that port!

I pulled up netstat to confirm:

C:\Users\gbeifuss>netstat -ano | find "1313"
C:\Users\gbeifuss>

Well, if nothing is using the port, then why can’t Hugo use it? If there’s nothing actively blocking Hugo, then Hugo must be prevented from using that port by another mechanism.

I checked the reservations that Windows knows about with netsh:

C:\Users\gbeifuss\blog>netsh int ipv4 show excludedportrange protocol=tcp

Protocol tcp Port Exclusion Ranges

Start Port    End Port
----------    --------
      1214        1313
      8004        8004
     11801       11900
     29480       29579
     30221       30320
     32822       32921
     49680       49779
     50000       50059     *
     50160       50259
     50261       50360
     50378       50477

* - Administered port exclusions.

Well, there we go! What’s reserved that port?
I stopped Docker Desktop Service and made sure all the Hyper-V services weren’t running either, but that reservation was still there.

It turns out that the range 1214-1313 was reserved by the Windows NAT Driver (WinNat) service.

I stopped the winnat service via. the command line (as Administrator):
net stop winnat

Then I started Hugo, which happily grabbed port 1313:

C:\Users\gbeifuss\blog>hugo server -D
Start building sites …
hugo v0.87.0-B0C541E4+extended windows/amd64 BuildDate=2021-08-03T10:57:28Z VendorInfo=gohugoio
WARN 2021/08/20 14:12:16 Search page not found. Create a page with layout: search.
WARN 2021/08/20 14:12:16 Archives page not found. Create a page with layout: archives.

                   | EN
-------------------+-----
  Pages            | 46
  Paginator pages  |  5
  Non-page files   |  0
  Static files     |  5
  Processed images |  8
  Aliases          | 12
  Sitemaps         |  1
  Cleaned          |  0

Built in 481 ms
Watching for changes in C:\Users\gbeifuss\blog\{archetypes,content,data,layouts,static,themes}
Watching for config changes in C:\Users\gbeifuss\blog\config.yaml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop

Then I started the winnat service again:
net start winnat

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy