- How to check open ports in Linux using the CLI
- What the hell are a TCP and UDP ports?
- Port numbers
- Check open ports in Linux
- Using netstat to list open ports
- Use ss to list open ports
- Listening ports and applications using lsof command
- nmap command
- The open port doesn’t mean anyone from outside can access those ports
- Conclusion
- How to Check for Open Ports in Linux
- What is a Port?
- 1. Netstat
- 2. lsof
- 3. Network Mapped Command
- Conclusion
- 2 comments
- Popular Posts
- How to Find Out List of All Open Ports in Linux
- If You Appreciate What We Do Here On TecMint, You Should Consider:
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:
- Open a Linux terminal application
- Use ss command to display all open TCP and UDP ports in Linux.
- Another option is to use the netstat command to list all ports in Linux.
- Apart from ss / netstat one can use the lsof command to list open files and ports on Linux based system.
- 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 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.
- -a: shows all sockets
- -t: shows TCP connections
- -u: shows UDP connections
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.
2 comments
“the first thing to do is to close all unnecessary ports”
The $64,000 question is How do I determine whether a port is unnecessary?
Those services which are not really being used we should shut them down like say postfix, httpd if that machine is db server we intend to use only say mysql db on it then there is not need to keep other service on it.
Comments are closed.
Popular Posts
RedMagic 6S Pro Review: Gaming Is Serious Business.
How to Boot to Recovery Mode (Safe Mode) in Ubuntu
Ubuntu Software Center Not Working? Here Are the Fixes
How to Stress Test a Graphics Card on Linux
How to Mount a Windows Share Folder on Linux
How to Mount Your iPhone as an External Drive in Ubuntu
How to Fix Ubuntu Freezing in VirtualBox
How to Fix «Repository Does Not Have Release File» Error
How to Combine PDF Files on Windows and Linux
How to Reset the Root Password in Linux
8 Reasons to Switch from Windows to Linux
Affiliate Disclosure: Make Tech Easier may earn commission on products purchased through our links, which supports the work we do for our readers.
Источник
How to Find Out List of All Open Ports in Linux
In this article, we will briefly talk about ports in computer networking and move to how you can list all open ports in Linux.
In computer networking, and more definitely in software terms, a port is a logical entity which acts as a endpoint of communication to identify a given application or process on an Linux operating system. It is a 16-bit number (0 to 65535) which differentiates one application from another on end systems.
The two most popular Internet transport protocols, Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP) and other less known protocols use port numbers for communication sessions (source and destination port numbers in conjunction with the source and destination IP addresses).
In addition, a combination of an IP address, port and protocol such as TCP/UDP is known as a socket, and every service must have a unique socket.
Below are the different categories of ports:
- 0-1023 – the Well Known Ports, also referred to as System Ports.
- 1024-49151 – the Registered Ports, also known as User Ports.
- 49152-65535 – the Dynamic Ports, also referred to as the Private Ports.
You can view a list of different applications and port/protocol combination in /etc/services file in Linux using cat command:
To list all open ports or currently running ports including TCP and UDP in Linux, we will use netstat, is a powerful tool for monitoring network connections and statistics.
- -l – prints only listening sockets
- -n – shows port number
- -t – enables listing of tcp ports
- -u – enables listing of udp ports
You can also use ss command, a well known useful utility for examining sockets in a Linux system. Run the command below to list all your open TCP and UCP ports:
Make it a point to read through the man pages of the commands above for more usage information.
In summary, understanding the concept of ports in computer networking is very vital for system and network administrators. You can as well go through this netstat guide with simple, precise and well explained examples.
Last but not least, get in touch with us by sharing other methods for listing open ports in Linux or asking a question via the response form below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник