- How can I see what ports are open on my machine?
- 10 Answers 10
- nmap (install)
- How to Check for Listening Ports in Linux (Ports in use)
- What is Listening Port #
- Check Listening Ports with netstat #
- Check Listening Ports with ss #
- Check Listening Ports with lsof #
- Conclusion #
- How to Check for Open Ports in Linux
- What is a Port?
- 1. Netstat
- 2. lsof
- 3. Network Mapped Command
- Conclusion
How can I see what ports are open on my machine?
I would like to see what ports are open on my machine, e.g. what ports my machine is listening on. E.g. port 80 if I have installed a web server, and so on. Is there any command for this?
10 Answers 10
If the netstat command is not available, install it with:
sudo apt install net-tools
-l already filters for listening. grep LISTEN won’t help beyond hiding 2 lines of header information.
-t : tcp, -l : listening socket, -p : show pid and program name, -n : print 127.0.0.1:80 instead of localhost:http . Reference: linux.die.net/man/8/netstat
The expanded command is sudo netstat —tcp —listening —programs —numeric . There’s no need to use grep unless you want to eliminate column headers.
nmap (install)
Nmap («Network Mapper») is a free and open source utility for network exploration or security auditing.
Use nmap 192.168.1.33 for internal PC or nmap external IP address .
More information man nmap .
Zenmap is the official GUI frontend.
Remember that there is a difference between nmap localhost and nmap 192.168.0.3 (or what ever you machine IP is)
I think netstat is a better answer to this. netstat will list what the system is listening on directly, and without using an additional application or doing unnecessary calls over localhost or thought the network.
This is stupid. If you have access to the computer, just use netstat -ln . You’ll instantly see all the open ports.
nmap localhost didn’t find services that were bound only to localhost. For example, I run influxd with bind-address:localhost:8086 . That didn’t show up in sudo nmap localhost , but did show up in sudo netstat -tulpn .
Other good ways to find out what ports are listenting and what your firewall rules are:
To list open ports use the netstat command.
$ sudo netstat -tulpn | grep LISTEN tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 5452/dnsmasq tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1037/cupsd tcp6 0 0 ::1:631 . * LISTEN 1037/cupsd
In the above example three services are bound to the loopback address.
IPv4 services bound to the loopback address «127.0.0.1» are only available on the local machine. The equivalent loopback address for IPv6 is «::1». The IPv4 address «0.0.0.0» means «any IP address», which would mean that other machines could potentially connect to any of the locally configured network interfaces on the specific port.
Another method is to use the lsof command:
$ sudo lsof -nP -i | grep LISTEN cupsd 1037 root 9u IPv6 11276 0t0 TCP [::1]:631 (LISTEN) cupsd 1037 root 10u IPv4 11277 0t0 TCP 127.0.0.1:631 (LISTEN) dnsmasq 5452 nobody 5u IPv4 212707 0t0 TCP 127.0.0.1:53 (LISTEN)
For more details see man netstat or man lsof .
How to Check for Listening Ports in Linux (Ports in use)
When troubleshooting network connectivity or application-specific issues, one of the first things to check should be what ports are actually in use on your system and which application is listening on a specific port.
This article explains how to use the netstat , ss and lsof commands to find out which services are listening on which ports. The instructions are applicable for all Linux and Unix-based operating systems like macOS.
What is Listening Port #
Network port is identified by its number, the associated IP address, and type of the communication protocol, such as TCP or UDP.
Listening port is a network port on which an application or process listens on, acting as a communication endpoint.
Each listening port can be open or closed (filtered) using a firewall. In general terms, an open port is a network port that accepts incoming packets from remote locations.
You can’t have two services listening to the same port on the same IP address.
For example, if you are running an Apache web server that listens on ports 80 and 443 and you try to install Nginx , the later will fail to start because the HTTP and HTTPS ports are already in use.
Check Listening Ports with netstat #
netstat is a command-line tool that can provide information about network connections.
To list all TCP or UDP ports that are being listened on, including the services using the ports and the socket status use the following command:
The options used in this command have the following meaning:
- -t — Show TCP ports.
- -u — Show UDP ports.
- -n — Show numerical addresses instead of resolving hosts.
- -l — Show only listening ports.
- -p — Show the PID and name of the listener’s process. This information is shown only if you run the command as root or sudo user.
The output will look something like this:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 445/sshd tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 929/master tcp6 0 0 . 3306 . * LISTEN 534/mysqld tcp6 0 0 . 80 . * LISTEN 515/apache2 tcp6 0 0 . 22 . * LISTEN 445/sshd tcp6 0 0 . 25 . * LISTEN 929/master tcp6 0 0 . 33060 . * LISTEN 534/mysqld udp 0 0 0.0.0.0:68 0.0.0.0:* 966/dhclient
The important columns in our case are:
- Proto — The protocol used by the socket.
- Local Address — The IP Address and port number on which the process listen to.
- PID/Program name — The PID and the name of the process.
If you want to filter the results, use the grep command . For example, to find what process listens on TCP port 22 you would type:
sudo netstat -tnlp | grep :22
The output shows that on this machine port 22 is used by the SSH server:
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 445/sshd tcp6 0 0 . 22 . * LISTEN 445/sshd
If the output is empty it means that nothing is listening on the port.
You can also filter the list based on criteria, for example, PID, protocol, state, and so on.
netstat is obsolete and replaced with ss and ip , but still it is of the most used commands to check network connections.
Check Listening Ports with ss #
ss is the new netstat . It lacks some of the netstat features, but exposes more TCP states and it is slightly faster. The command options are mostly the same, so the transition from netstat to ss is not difficult.
To get a list of all listening ports with ss you would type:
The output is almost the same as the one reported by netstat :
State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=445,fd=3)) LISTEN 0 100 0.0.0.0:25 0.0.0.0:* users:(("master",pid=929,fd=13)) LISTEN 0 128 *:3306 *:* users:(("mysqld",pid=534,fd=30)) LISTEN 0 128 *:80 *:* users:(("apache2",pid=765,fd=4),("apache2",pid=764,fd=4),("apache2",pid=515,fd=4)) LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=445,fd=4)) LISTEN 0 100 [::]:25 [::]:* users:(("master",pid=929,fd=14)) LISTEN 0 70 *:33060 *:* users:(("mysqld",pid=534,fd=33))
Check Listening Ports with lsof #
lsof is a powerful command-line utility that provides information about files opened by processes.
In Linux, everything is a file. You can think of a socket as a file that writes to the network.
To get a list of all listening TCP ports with lsof type:
sudo lsof -nP -iTCP -sTCP:LISTEN
The options used are as follows:
- -n — Do not convert port numbers to port names.
- -p — Do not resolve hostnames, show numerical addresses.
- -iTCP -sTCP:LISTEN — Show only network files with TCP state LISTEN.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 445 root 3u IPv4 16434 0t0 TCP *:22 (LISTEN) sshd 445 root 4u IPv6 16445 0t0 TCP *:22 (LISTEN) apache2 515 root 4u IPv6 16590 0t0 TCP *:80 (LISTEN) mysqld 534 mysql 30u IPv6 17636 0t0 TCP *:3306 (LISTEN) mysqld 534 mysql 33u IPv6 19973 0t0 TCP *:33060 (LISTEN) apache2 764 www-data 4u IPv6 16590 0t0 TCP *:80 (LISTEN) apache2 765 www-data 4u IPv6 16590 0t0 TCP *:80 (LISTEN) master 929 root 13u IPv4 19637 0t0 TCP *:25 (LISTEN) master 929 root 14u IPv6 19638 0t0 TCP *:25 (LISTEN)
Most of the output columns names are self-explanatory:
- COMMAND , PID , USER — The name, the pid and the user running the program associated with the port.
- NAME — The port number.
To find what process is listening on a particular port, for example, port 3306 you would use:
sudo lsof -nP -iTCP:3306 -sTCP:LISTEN
The output shows that MySQL server uses port 3306 :
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 534 mysql 30u IPv6 17636 0t0 TCP *:3306 (LISTEN)
For more information, visit the lsof man page and read about all other powerful options of this tool.
Conclusion #
We have shown you several commands that you can use to check what ports are in use on your system, and how to find what process listens on a specific port.
If you have any questions or remarks, please leave a comment below.
How to Check for Open Ports in Linux
When you are troubleshooting networking issues in Linux or are looking for ways to improve the security of your Linux machine, you will need to know if and which ports are open. In this article we will look at different ways to list or display open ports in Linux.
What is a Port?
A port is a 16-bit number (0 to 65535) to help identify a given application or process on a Linux (Unix) operating system. Port differentiates one application from another on a Linux system.
Below are the different categories of ports:
- 0 – 1023 – Referred to as Well Known Ports
- 1024 – 49151 –Referred to as Registered Ports
- 49152 – 65535 – Referred to as Dynamic Ports
Using the following command, a list of applications and ports is displayed on your terminal:
TCP: TCP stands for Transmission Control Protocol. It is the most commonly-used protocol on the Internet. TCP is not just one-way communication, rather it sends packets back to acknowledge it’s received your packets.
UDP: Also known as User Datagram Protocol. It is an alternative communications protocol to TCP. The UDP protocol works similar to TCP. However, it ignores all error-checking stuff. UDP is necessary when speed is desirable and error correction is not needed.
SOCKETS: Socket allows communication from two different processes on the same or different machines.
Let’s look at different ways to list an open port in Linux.
1. Netstat
In this method we will use the command netstat -atu to check for open ports in Linux.
We used the -a , -t and -u flags for netstat.
You can also add the -p flag to show related PID of the process or program name.
To display only UDP ports, you can use the following command:
Also, you can use the following command to search for TCP ports:
2. lsof
Instead of using netstat, we can use the lsof command to display open ports in Linux:
The following command can also help to display open sockets:
Also, you can use the command below to list all TCP connections:
Moreover, you can use the following command for UDP connections:
3. Network Mapped Command
In this method we will use nmap to detect the open port on your system. We can use the following command to show tcp port connections:
Finally, to show udp port connections, we can use the following command:
Conclusion
When it comes to the security of your Linux PC, the first thing to do is to close all unnecessary ports to prevent external access. With the methods listed above, you will be able to easily check for open ports on a Linux system and determine which ports should be closed or remain open.
Michael wears many hat in the opensource industry. He is based in Accra, Ghana. He revels in anything Linux and Devops.
Our latest tutorials delivered straight to your inbox