Showing posts with label nmap. Show all posts
Showing posts with label nmap. Show all posts

Tuesday, September 3, 2019

How to check if port is in use on Linux or Unix

How to check if port is in use on Linux or Unix


sudo lsof -i -P -n | grep LISTEN
sudo netstat -tulpn | grep LISTEN
sudo lsof -i:22 ## see a specific port such as 22 ##
sudo nmap -sTU -O IP-address-Here

Option #1: lsof command
$ sudo lsof -i -P -n
$ sudo lsof -i -P -n | grep LISTEN
$ doas lsof -i -P -n | grep LISTEN ### [OpenBSD] ###

Linux netstat syntax
netstat -tulpn | grep LISTEN
watch netstat -tulpn
sudo ss -tulw
sudo ss -tulwn


netstat -tulpn |grep tcp |grep -v 127.0.0.1 |grep -v 192.168.101.36
-v is invert matching (every thing except 127.0.0.1 and 192.168.101.36)


Where ss command options are as follows:
  • -t : Show only TCP sockets on Linux
  • -u : Display only UDP sockets on Linux
  • -l : Show listening sockets. For example, TCP port 22 is opened by SSHD server.
  • -p : List process name that opened sockets
  • -n : Don’t resolve service names i.e. don’t use DNS

FreeBSD/MacOS X netstat syntax

$ netstat -anp tcp | grep LISTEN
$ netstat -anp udp | grep LISTEN

OpenBSD netstat syntax

$ netstat -na -f inet | grep LISTEN
$ netstat -nat | grep LISTEN

Option #3: nmap command

The syntax is:
$ sudo nmap -sT -O localhost
$ sudo nmap -sU -O 192.168.2.13 ##[ list open UDP ports ]##
$ sudo nmap -sT -O 192.168.2.13 ##[ list open TCP ports ]##
You can combine TCP/UDP scan in a single command:
$ sudo nmap -sTU -O 192.168.2.13

A note about Windows users

You can check port usage from Windows operating system using following command:
netstat -bano | more
netstat -bano | grep LISTENING
netstat -bano | findstr /R /C:"[LISTEING]"

Ref:
unix-linux-check-if-port-is-in-use-command