Linux show open sockets

How to check open ports in Linux using the CLI

I need to list all open ports in Linux cloud server. How do I check open ports in Linux using the CLI? Can you give me the command to check open ports in Linux operating system?

To troubleshoot server problems and to avoid security issue, one needs to find out open TCP and UDP ports. In this tutorial, you will learn the different Linux commands to check open ports in Linux for auditing and securing the server.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux command line
Est. reading time 5 minutes

What the hell are a TCP and UDP ports?

A port is nothing but a 16-bit number between 0 to 65535. For example, TCP port number 22 may be forwarded to the OpenSSH server. Therefore, 22 port number is a way to identify the sshd (OpenSSH server) process.

Port numbers

  • The Well Known Ports are those from 0 through 1023.
  • The Registered Ports are those from 1024 through 49151.
  • The Dynamic and Private Ports are those from 49152 through 65535.

A registered port is a network port assigned by the Internet Assigned Numbers Authority (IANA) and stored in /etc/services file. Use the cat command or grep command/egrep command to view port numbers and service mappings:

Display a list of applications and their ports assigned by IANA

Check open ports in Linux

The procedure to monitor and display open ports in Linux is as follows:

  1. Open a Linux terminal application
  2. Use ss command to display all open TCP and UDP ports in Linux.
  3. Another option is to use the netstat command to list all ports in Linux.
  4. Apart from ss / netstat one can use the lsof command to list open files and ports on Linux based system.
  5. Finally, one can use nmap command to check TCP and UDP ports too.

Let us see all commands and examples in details.

Using netstat to list open ports

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

  • -t : All TCP ports
  • -u : All UDP ports
  • -l : Display listening server sockets
  • -p : Show the PID and name of the program to which each socket belongs
  • -n : Don’t resolve names
  • | grep LISTEN : Only display open ports by applying grep command filter.

Use ss to list open ports

The ss command is used to dump socket statistics. It allows showing information similar to netstat. It can display more TCP and state information than other tools. The syntax is:
sudo ss -tulpn
Sample outputs:

Listening ports and applications using lsof command

Let us run the following to check open TCP and UDP ports using the lsof command:
sudo lsof -i -P -n | grep LISTEN

Where,

  • -i : Look for listing ports
  • -P : Inhibits the conversion of port numbers to port names for network files. Inhibiting the conversion may make lsof run a little faster. It is also useful when port name lookup is not working properly.
  • -n : Do not use DNS name
  • | grep LISTEN : Again only show ports in LISTEN state using the grep command as filter.

nmap command

In addition, to above commands one can use the nmap command which is an open source tool for network exploration and security auditing. We are going to use nmap to find and list open ports in Linux:
$ sudo nmap -sT -O localhost
$ sudo nmap -sU -O 192.168.2.254 ##[ list open UDP ports ]##
$ sudo nmap -sT -O 127.0.0.1 ##[ list open TCP ports ]##
$ sudo nmap -sTU -O 192.168.2.24
Sample outputs:

The open port doesn’t mean anyone from outside can access those ports

So far, you know how to find and list open TCP and UDP ports on Linux. However, those ports can still be blocked by software, cloud, or hardware firewall. Hence, you need to verify that your corporate firewall is not blocking incoming or outgoing access. For instance on Linux server we list or dump firewall rules using the following syntax:
sudo iptables -S
# IPv6
sudo ip6tables -S

Conclusion

In conclusion, finding out open ports is one of the most fundamental duties of a Linux system administrator for security reasons. Therefore, close down all unwanted ports and configure firewall such as UFW and FirewallD to open or block ports as per your requirements. After reading this tutorial, you should have a good understanding of how to check for open ports in Linux. See IANA’s offical list of TCP, UDP and other ports here for more information.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Check Open Ports in Linux

During the troubleshooting of services running on a Linux system, checking open ports is one of the tasks any user or administrator should consider performing. If a service is expected to be running but for some reason it’s not, then most likely the port associated with that service is closed and should be opened.

In this tutorial, we will demonstrate how to check open ports in a Linux from the command line.

1) Check open ports using ss command

The Linux ss command gives you detailed insights on open ports and listening sockets. It draws information from the Linux kernel and is more preferred to the netstat command which has been deprecated.

To display listening TCP connections, run the command

Sample output

l — Shows listening sockets

t — Stands for TCP port

To display listening UDP connections, issue the command

Sample output

u — Stands for UDP port

To display both tcp and udp, process name

p — List process name that opened sockets

To print out all socket connections, simply use the ss command in its default format

Sample output

2) Check open ports using netstat command

The netstat command is a powerful command tool that is used for checking open TCP and UDP ports alongside other attributes. To check open ports, issue the command:

Sample output

Let’s take a closer look at the command options:

p — Displays the Procees ID associated with a service or Program name

n — Displays the numerical number of the port running e.g 3306 for mysqld, and 22 for sshd.

l — Shows listening sockets

t — Displays TCP connections

u — Displays UDP connections

3) Check open ports using the lsof command

The lsof command is a network command tool that can also be used to check open ports in a Linux system. To display open ports, issue the command

Sample output

If you wish to display open sockets, use the lsof command and pipe the output to grep as shown:

Sample output

To view all TCP connections execute :

Sample output

To display all UDP connections run the command:

Sample output

4) Check open ports using the Nmap utility

Nmap is a free and opensource network scanning tool usually used for reconnaissance in ethical hacking for discovering open ports of remote systems. By default, Nmap does not come installed on your system. To install Nmap, issue the command

To scan for open TCP ports, run the command

Sample output

To scan for open UDP ports, run the command:

Sample output

Wrapping up

Those are the linux commands and tools used for port scanning to check open ports in a Linux system. As always, your feedback is most welcome. If you have other ideas on how to check open ports, do get in touch with us.

Источник

List All Open Sockets Listening on a Linux Machine

A socket in networking serves as a communication endpoint. It provides a connection between two entities that wish to communicate with each other within a network. Sockets are used on the TCP/IP stack transport layer and are used to send and receive the data packets. In today’s article, we will explain to you the different methods of listing down all the open sockets that are listening on a Linux Mint 20 machine.

Methods of Listing Down all the Open Sockets Listening on a Linux Mint 20 Machine:

For listing down all the open sockets listening on a Linux Mint 20 machine, you can follow any of the following three methods:

Method # 1: Listing all the Open Sockets Listening on a Linux Mint 20 Machine with the “ss” Utility:

If you want to list down all the open sockets listening on a Linux Mint 20 machine regardless of which Internet protocol they are using, then you need to execute the command shown below in your terminal:

This command will list down all the open sockets that are listening on a Linux Mint 20 machine, as shown in the following image:

Method # 2: Listing all the Open TCP Sockets Listening on a Linux Mint 20 Machine:

If you want to list down all the open TCP sockets listening on a Linux Mint 20 machine, then you need to execute the command shown below in your terminal:

This command will list down all the open TCP sockets that are listening on a Linux Mint 20 machine, as shown in the following image:

Method # 3: Listing all the Open UDP Sockets Listening on a Linux Mint 20 Machine:

If you want to list down all the open UDP sockets listening on a Linux Mint 20 machine, then you need to execute the command shown below in your terminal:

This command will list down all the open UDP sockets that are listening on a Linux Mint 20 machine, as shown in the following image:

Conclusion:

In this guide, we shared three different methods of listing down the open sockets listening on a Linux Mint 20 machine. The first method lists down all the open and listening sockets regardless of the Internet protocol they are following. The other two methods are specific to the TCP and UDP protocols.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.

Источник

Find Out What Ports Are Listening / Open On My Linux & FreeBSD Server

netstat command to find open ports

The syntax is:
# netstat —listen
OR
# netstat -l
Sample outputs from my Debian 8.x Linux server:

To display open ports and established TCP connections, enter:
$ netstat -vatn
To display only open UDP ports try the following command:
$ netstat -vaun
If you want to see FQDN (full dns hostname), try removing the -n flag:
$ netstat -vat
FreeBSD/OS X Unix user try the following command:
$ netstat -na | grep -i LISTEN
$ netstat -f inet -na | grep -i LISTEN
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

lsof Command Examples

To display the list of open ports, enter:
# lsof -i
To display all open files, use:
# lsof
To display all open IPv4 network files in use by the process whose PID is 9255, use:
# lsof -i 4 -a -p 9255
Another example:
# lsof -iTCP -sTCP:LISTEN
Sample outputs:

A Note About FreeBSD Users

Fig.01: sockstat command on FreeBSD

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

I used lsof command in freebsd 5 i am not getting any output for checking tcp/udp ports which are listening or open on my server

Yes, you must be root and use sockstat command.

What is the equivalent port for com9 on linux?How do i find out com port equivalents in linux?Thanks

on FreeBSD you can also use sockstat to list open sockets

correction :- should be
netstat -l, –listening :- to get list the listening sockets.

lsof -i (to get an idea of ports out there)

netstat -a | grep
example: netstat -a | grep 4449

If nothing is returned like below the port is free
$ netstat -a | grep 4449
$

My name is Jacob and I’m running a hosting business. I have 2 dedicated servers at the moment and they’re both Linux Debian 6.0. I have some troubles with my dedicated IP’s. Some server on my dedicated server is having the port 0000 or 0. It takes all the dedicated IP’s and I don’t know how to find where its located or how to close it. I know the pid and I can see it’s running some where but the location I cant see. Please help me. It’s a big problem I’ve been trying to figure out.

there are some command to find out the all open port…bt there have any command to find out all the port,which are listing or not?that means have any command to show list of all ports.

Try this:
open tcp, udp, listening, program, numeric, ipv4
netstat -tulpn4

how do i find out what ports are listeningopen/off on my linuxfreebsd server?how do i find the port,which are not listening.i just want to see list of all port.

Источник

Читайте также:  Linux дистрибутив для инженера
Оцените статью