38

I am very new to unix. How can I check that a particular port is free or used right now ?

Jubbles
  • 4,450
  • 8
  • 35
  • 47
arpanoid
  • 2,123
  • 4
  • 17
  • 15

2 Answers2

50
netstat -ano|grep 443|grep LISTEN

will tell you whether a process is listening on port 443 (you might have to replace LISTEN with a string in your language, though, depending on your system settings).

Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
  • 5
    note that after this call is made you can use $? to check if the grep matched... eg "echo $?" immediately after this will output 0 for the cases where the port was open and 1 for the case where the port wasn't open. you may find this value more usable as part of a larger script. – mat kelcey Nov 14 '13 at 19:43
  • 3
    When I need a clean 0/1 output, I use this: `netstat -ano | grep ':443\s' | if grep -q LISTEN; then echo 0; else echo 1; fi`. Here, `:` and `\s` are for separating port numbers such as 443 and 4430, and -q is for keeping grep quiet – srctaha Jul 02 '15 at 09:12
21

Try (maybe as root)

lsof -i -P

and grep the output for the port you are looking for.

For example to check for port 80 do

lsof -i -P | grep :80
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87