How to make sure that a certain Port is not occupied by any other process
Asked Answered
N

8

79

I am working on a Java EE Application in a Windows environment. (I am using Windows 7)

I am using Tomcat Server, unfortunately port number 8080 is busy (used by Oracle). Now I want to assign a different port to Tomcat.

So before changing inside conf/server.xml file, I want to make sure that a certain port is not occupied by any other process and it's free.

Novobiocin answered 11/1, 2012 at 10:11 Comment(0)
S
92

You can use "netstat" to check whether a port is available or not.

Use the netstat -anp | find "port number" command to find whether a port is occupied by an another process or not. If it is occupied by an another process, it will show the process id of that process.

You have to put : before port number to get the actual output

Ex netstat -anp | find ":8080"

Sloan answered 11/1, 2012 at 10:20 Comment(12)
Thanks for the answer , as said i executed the following command on Windows Command Prompt C:\Users\kiran>netstat -anp | find 8086 It showed FIND: Parameter format not correctNovobiocin
You should cover the port number with in quotes. For example, netstat -ano | find "8086"Sloan
Aravind , after issuing that command C:\Softwares\apache-tomcat-6.0.33\bin>netstat -anp | find "8080" Nothing came . Thank you .Novobiocin
I copy pasted the command and executed the same under Command Prompt , it displayed nothing .Novobiocin
Means no process occupied that port number. If u are not sure, try the TCP viewer from SysInternals and check whether the port is listed in it or not.Sloan
Try this one in windows: netstat -ano | find "7"Maintenon
not working in windows 10. "netstat -ano|findstr port no" is working fineBrachium
-an works but -anp does not work. FYI I am using win 10 Pro 64 bitIntrusion
I am trying to check if the exact port is in free or not. I used netstat -an | find ":566". It is returning info that the port 56687 is in use. I tried with an,ano, find and findstr. I used : before the port number. But all returning the same results. OS am trying is Windows 10.Spohr
I tried all modifications like find/findstr/-an/-ano/:9200/9200 but non of them give a result. It silently ends up. Max I get an error "wrong parameter" if port number is not enclosed in ""Archaism
for windows 10 netstat -ano|find ":port_no" , below answer is workingSherleysherline
shoud use command prompt, not PowerShellGoalie
S
54

It's netstat -ano|findstr port no

Result would show process id in last column

Shinny answered 27/1, 2014 at 2:53 Comment(5)
netstat -ano|findstr 3306 Nothing happened when i run this commandHyohyoid
@sam Based on answers netstat -ano | find ":3306" Vanadinite
@MostafaFallah answer worked for me. Anyone, remember to try other ports so you better see how the command works.Depew
Better answer in my opinion because it also works in PowerShell. Accepted answer is only for CMDRune
downvoted because you used an unnecessary acronym, there's no "o" in numberBasanite
S
21

netstat -ano|find ":port_no" will give you the list.
a: Displays all connections and listening ports.
n: Displays addresses and port numbers in numerical form.
o: Displays the owning process ID associated with each connection .

example : netstat -ano | find ":1900" This gives you the result like this.

UDP    107.109.121.196:1900   *:*                                    1324  
UDP    127.0.0.1:1900         *:*                                    1324  
UDP    [::1]:1900             *:*                                    1324  
UDP    [fe80::8db8:d9cc:12a8:2262%13]:1900  *:*                      1324
Snowden answered 12/9, 2017 at 4:41 Comment(2)
> nestat -ano | find ":443" gives netstat: illegal option -- oSpecter
This is the command that worked on Windows 7-64. Tested with XAMPP Apache server down, nothing came up on port 80. With same up, port 80 displayed an entry, as expected.Planer
N
11

If you prefer Powershell, use this. You will get the name of the process.

PS C:\Users\Administrator> Get-Process -Id (Get-NetTCPConnection -LocalPort 9093).OwningProcess

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
   6021    1464  2760976    2131548     290.39  25512   2 java

The PID is in Id column, and it provides process name, as well.

If no process is using this port, you get a red error message.

If you want to kill the process with PID 25512, use

taskkill /PID 25512 /F

/F means force, some processes cannot be killed without /F

Nidorf answered 3/9, 2021 at 6:13 Comment(0)
S
3

If someone is trying in windows 10 pro, netstat -anp might not list any connection, so try netstat -an and add | find to find your desire port

for example I want to check in port 65432 is being currently used then I do

netstat -an|find "65432"
Scribe answered 20/4, 2023 at 19:4 Comment(0)
A
2

It's (Get-NetTCPConnection -LocalPort "port no.").OwningProcess

Autocephalous answered 10/7, 2017 at 11:14 Comment(1)
its show this error Get-NetTCPConnection : No MSFT_NetTCPConnection objects found with property 'LocalPort' equal to '55283'. Verify the value of the property and retry. At line:1 char:2 + (Get-NetTCPConnection -LocalPort "55283").OwningProcess + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (55283:UInt16) [Get-NetTCPConnection], CimJobException + FullyQualifiedErrorId : CmdletizationQuery_NotFound_LocalPort,Get-NetTCPConnectionNeel
S
1

If this is a purely local concern e.g. you want to run tomcat locally to test the application you're working on, something which often works is to configure port 0. In that case when the application port-binds it will be allocated a random "ephemeral" port by the OS, which hopefully it logs out.

You'll have to check if that's supported by tomcat, but I would expect it is given past answers mentioning it.

It avoids having to hardcode ports and issues of TOCTOU, though it is obviously somewhat less convenient as you need to get the port you need to connect to every time.

The alternative is to just try out a bunch of free ports e.g. 8000[0] and 8888 are common alternate ports for HTTP servers. 8008 is also an official IANA alternate port though I can't remember ever seing it used.

[0] though officially assigned to iRDMI

Statampere answered 3/9, 2021 at 6:23 Comment(0)
M
0

netstat -ano| grep

this will give status of the port if being used or not

Motorman answered 23/4, 2022 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.