Skip to content

Instantly share code, notes, and snippets.

@nethoncho
Last active February 22, 2023 14:29
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nethoncho/161330e9538bbcfae783666c7eeaf230 to your computer and use it in GitHub Desktop.
Save nethoncho/161330e9538bbcfae783666c7eeaf230 to your computer and use it in GitHub Desktop.
Raspberry Pi Raspbian list open ports
netstat -lnt | grep LISTEN | awk '{ print ( $4 ) }' | awk 'BEGIN{FS=":"} { print $(NF) }' | sort -n | uniq
@nethoncho
Copy link
Author

$ netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:42217           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 :::5000                 :::*                    LISTEN
tcp6       0      0 :::42217                :::*                    LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
tcp6       0      0 :::9171                 :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN

Filtered

$ netstat -lnt | grep LISTEN | awk '{ print ( $4 ) }' | awk 'BEGIN{FS=":"} { print $(NF) }' | sort -n | uniq
22
80
5000
9171
42217

@deepakkoirala
Copy link

This only works for tcp ports. What about udp ports ?

@Fr3d59180
Copy link

Fr3d59180 commented Jan 6, 2021

for udp ports :
netstat -lnu | grep udp | awk '{ print ( $4 ) }' | awk 'BEGIN{FS=":"} { print $(NF) }' | sort -n | uniq

@anurag01a
Copy link

$ netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:42217           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp6       0      0 :::5000                 :::*                    LISTEN
tcp6       0      0 :::42217                :::*                    LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
tcp6       0      0 :::9171                 :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN

Filtered

$ netstat -lnt | grep LISTEN | awk '{ print ( $4 ) }' | awk 'BEGIN{FS=":"} { print $(NF) }' | sort -n | uniq
22
80
5000
9171
42217

🧡👍👏

@hoang-himself
Copy link

-l already filters listening ports, so we can omit grep LISTEN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment