{"id":21517,"date":"2023-09-30T12:44:21","date_gmt":"2023-09-30T12:44:21","guid":{"rendered":"https:\/\/linuxways.net\/?p=21517"},"modified":"2023-09-30T12:45:49","modified_gmt":"2023-09-30T12:45:49","slug":"how-do-i-know-if-a-tcp-port-is-open-or-closed-on-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-do-i-know-if-a-tcp-port-is-open-or-closed-on-ubuntu-22-04\/","title":{"rendered":"How Do I Know if a TCP Port is Open or Closed on Ubuntu 22.04?"},"content":{"rendered":"<p>As many functions rely on specific ports to function, it is essential to know the status of Ports. Port configuration can lead to Proper Functioning of Applications, and Remote Access as well. With the awareness of Open Ports, we can protect our system from unwanted and unauthorized access, we can allow or deny any type of traffic on a specific port, and it can also help us identify issues in our network configuration or connectivity.<\/p>\n<p>In this article, we will discuss the different methods used to check which TCP Ports are Open and which ones are Closed.<\/p>\n<h2><strong>How Do I know if a TCP Port is Open or Closed on Ubuntu 22.04?<\/strong><\/h2>\n<p>Several Methods can be used to find out if a TCP Port is opened or closed on Ubuntu 22.04. The methods are<\/p>\n<ul>\n<li>Using \u201clsof\u201d Command<\/li>\n<li>Viewing Network Service List<\/li>\n<li>Using \u201cnetstat\u201d Command<\/li>\n<li>Using \u201css\u201d Command<\/li>\n<li>Using \u201cnmap\u201d<\/li>\n<li>Using Bash Script<\/li>\n<li>Using \u201cnetcat\u201d Command<\/li>\n<\/ul>\n<p>Each method is explained in detail below.<\/p>\n<h2><strong>Using \u201clsof\u201d to Check Whether the Port is Opened or Closed<\/strong><\/h2>\n<p>The \u201clsof\u201d or \u201cList Open Files\u201d Command lists information about files currently opened by a process. Although it is used to list opened and in-process files, it can also be used to know whether a Network Port is Opened or Closed. The Syntax to use the \u201clsof\u201d command is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">lsof <span class=\"br0\">&#91;<\/span>options<span class=\"br0\">&#93;<\/span><\/div><\/div>\n<p>The \u201c[options]\u201d can be used to display the specific output you want. To display the list of open network connection, use the \u201c-i\u201d tag:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> lsof <span class=\"re5\">-i<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1035\" height=\"421\" class=\"wp-image-21518\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-1.png 1035w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-1-300x122.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-1-1024x417.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-1-768x312.png 768w\" sizes=\"auto, (max-width: 1035px) 100vw, 1035px\" \/><\/p>\n<p>Here, the \u201c<strong>LISTEN<\/strong>\u201d ones are those ports that are <strong>Open<\/strong>. To check whether the TCP ports are opened or not, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> lsof <span class=\"re5\">-i<\/span> <span class=\"re5\">-P<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> LISTEN<\/div><\/div>\n<p>The \u201cgrep\u201d will filter the ports that are in the \u201cLISTEN\u201d state, the \u201c-P\u201d is used to show the Port Number, and \u201c-i\u201d is used to show all the Open Sockets. The below TCP Ports are all open:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"405\" class=\"wp-image-21519\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-2.png 1030w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-2-300x118.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-2-1024x403.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-2-768x302.png 768w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/p>\n<p>With \u201clsof\u201d, a specific port can also be checked whether it is closed or open with the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> lsof <span class=\"re5\">-i<\/span> <span class=\"re5\">-P<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> :<span class=\"nu0\">22<\/span><\/div><\/div>\n<p>This command will check whether port 22 is opened or not:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1031\" height=\"97\" class=\"wp-image-21520\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-3.png 1031w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-3-300x28.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-3-1024x96.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-3-768x72.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-3-1020x97.png 1020w\" sizes=\"auto, (max-width: 1031px) 100vw, 1031px\" \/><\/p>\n<h2><strong>Using \u201cnetstat\u201d to Check Whether the Port is Opened or Closed<\/strong><\/h2>\n<p>The \u201cnetstat\u201d command is used to display Active Connections, Open Ports, and Network Statistics. To use netstat, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">netstat<\/span><\/div><\/div>\n<p>This will display all the Active Connections:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"400\" class=\"wp-image-21521\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-4.png 1030w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-4-300x117.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-4-1024x398.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-4-768x298.png 768w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/p>\n<p>Flags can be used with the \u201cnetstat\u201d command to display according to your needs. To get only the TCP Port, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">netstat<\/span> <span class=\"re5\">-tl<\/span><\/div><\/div>\n<p>The \u201c-t\u201d flag means only the TCP Connections and the \u201cl\u201d flag means list those connections. This will display the TCP Ports only:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"230\" class=\"wp-image-21523\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-5.png 1030w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-5-300x67.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-5-1024x229.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-5-768x171.png 768w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/p>\n<p>To display the Active TCP Ports using \u201cnetstat\u201d use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">netstat<\/span> <span class=\"re5\">-tl<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> LISTEN<\/div><\/div>\n<p>The \u201cgrep\u201d will filter the list and return only the TCP Ports that are open:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"203\" class=\"wp-image-21524\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-6.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-6-300x59.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-6-768x152.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-6-1020x203.png 1020w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>If you want to check whether a specific TCP Port is Opened or Closes, use the netstat command as<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">netstat<\/span> <span class=\"re5\">-tl<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> :<span class=\"nu0\">22<\/span><\/div><\/div>\n<p>The \u201cgrep\u201d command will filter and return only the Port 22 connections. In our case, the Port is Closed so no output will be displayed and the command line will move to the next line:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1023\" height=\"46\" class=\"wp-image-21525\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-7.png 1023w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-7-300x13.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-7-768x35.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-7-1020x46.png 1020w\" sizes=\"auto, (max-width: 1023px) 100vw, 1023px\" \/><\/p>\n<h2><strong>Using the \u201css\u201d Command to Check Whether the Port is Opened or Closed<\/strong><\/h2>\n<p>The \u201css\u201d command lists all the Sockets and Listening Sockets, and it also displays the Connections Established. It is simpler and faster than the \u201cnetstat\u201d command. To use the \u201css\u201d command, use the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">ss <span class=\"re5\">-tulpn<\/span><\/div><\/div>\n<p>The \u201c-tulpn\u201d will display all the TCP\/UDP Connections:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1038\" height=\"333\" class=\"wp-image-21526\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-8.png 1038w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-8-300x96.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-8-1024x329.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-8-768x246.png 768w\" sizes=\"auto, (max-width: 1038px) 100vw, 1038px\" \/><\/p>\n<p>To display only the TCP Ports, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">ss <span class=\"re5\">-tl<\/span><\/div><\/div>\n<p>The \u201c-tl\u201d command will display the TCP port connections only:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1037\" height=\"221\" class=\"wp-image-21527\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-9.png 1037w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-9-300x64.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-9-1024x218.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-9-768x164.png 768w\" sizes=\"auto, (max-width: 1037px) 100vw, 1037px\" \/><\/p>\n<p>Now to display the Open Ports only, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">ss <span class=\"re5\">-tl<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> LISTEN<\/div><\/div>\n<p>The \u201cgrep\u201d filters the list for Open Connections only:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1035\" height=\"192\" class=\"wp-image-21528\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-10.png 1035w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-10-300x56.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-10-1024x190.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-10-768x142.png 768w\" sizes=\"auto, (max-width: 1035px) 100vw, 1035px\" \/><\/p>\n<h2><strong>Using \u201cnmap\u201d to Check Whether the Port is Opened or Closed<\/strong><\/h2>\n<p>To check the Port Status on a Remote Host Machine, the \u201cnmap\u201d command is used. The syntax includes the Host Machine&#8217;s IP Address and the Port Number:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">nmap<\/span> <span class=\"re5\">-p<\/span> <span class=\"sy0\">&lt;<\/span>Port Number<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>Host IP Address<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>Considering my case where the Host System name is \u201ctahakhan\u201d and the Client is \u201cubuntu1\u201d. The IP address of my Host Machine is \u201c<strong>192.168.184.130<\/strong>\u201d. To check the status of all the TCP Ports on the Host Machine, the \u201cnmap\u201d command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">nmap<\/span> <span class=\"re5\">-p-<\/span> 192.168.184.130<\/div><\/div>\n<p>The \u201c-p-\u201d specifies to scan all the TCP Ports. The filtered TCP Ports are returned:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"809\" height=\"275\" class=\"wp-image-21529\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-11.png 809w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-11-300x102.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-11-768x261.png 768w\" sizes=\"auto, (max-width: 809px) 100vw, 809px\" \/><\/p>\n<h2><strong>Using \u201cnetcat\u201d to Check Whether the Port is Opened or Closed<\/strong><\/h2>\n<p>The \u201cnetcat\u201d command is used to analyze network hosts by connecting to a TCP port. The netcat command includes the hostname and the port number to analyze the network:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">nc <span class=\"re5\">-zv<\/span> <span class=\"sy0\">&lt;<\/span><span class=\"kw2\">hostname<\/span><span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>port number<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>The \u201c-zv\u201d flag scans for the Open Ports whereas the \u201cnc\u201d simply means netcat. To check the status of TCP Ports in some range using netcat, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">nc <span class=\"re5\">-zv<\/span> <span class=\"sy0\">&lt;<\/span><span class=\"kw2\">hostname<\/span><span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>start port<span class=\"sy0\">&gt;<\/span> - <span class=\"sy0\">&lt;<\/span>end port<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the host \u201ctahakhan\u201d will be scanned for Open Ports between 20-200:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">nc <span class=\"re5\">-zv<\/span> tahakhan <span class=\"nu0\">20<\/span>-<span class=\"nu0\">200<\/span><\/div><\/div>\n<p>This command will scan the Host Machine Ports one by one from Port 20 to Port 200 and return the status. If the \u201c<strong>Connection Succeeds<\/strong>\u201d it means the TCP Port is <strong>Open<\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1019\" height=\"530\" class=\"wp-image-21530\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-12.png 1019w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-12-300x156.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-12-768x399.png 768w\" sizes=\"auto, (max-width: 1019px) 100vw, 1019px\" \/><\/p>\n<p>You can also check a specific Port status using the netcat command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">nc <span class=\"re5\">-zv<\/span> tahakhan <span class=\"nu0\">22<\/span><\/div><\/div>\n<p>This will check the status of Port 22:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1023\" height=\"55\" class=\"wp-image-21531\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-13.png 1023w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-13-300x16.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-13-768x41.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-13-1020x55.png 1020w\" sizes=\"auto, (max-width: 1023px) 100vw, 1023px\" \/><\/p>\n<p>Similarly, to check the Status of Port 64, use the netcat command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">nc <span class=\"re5\">-zv<\/span> tahakhan <span class=\"nu0\">64<\/span><\/div><\/div>\n<p>If the Port is closed, the connection will be refused as seen in our case:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1028\" height=\"57\" class=\"wp-image-21532\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-14.png 1028w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-14-300x17.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-14-1024x57.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-14-768x43.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-14-1020x57.png 1020w\" sizes=\"auto, (max-width: 1028px) 100vw, 1028px\" \/><\/p>\n<h2><strong>Using Bash Script to Check Whether Port is Opened or Closed<\/strong><\/h2>\n<p>To check whether a TCP Port is Open or Closed using a Bash Script, use the following syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"sy0\">\/<\/span>dev<span class=\"sy0\">\/<\/span>tcp<span class=\"sy0\">\/&lt;<\/span>host name<span class=\"sy0\">&gt;\/&lt;<\/span>port number<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>This syntax can be modified to be used in the terminal. To check whether the Port 22 is Open or Closed, use the Bash Script as:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"br0\">&#40;<\/span><span class=\"kw3\">echo<\/span> <span class=\"sy0\">&gt;\/<\/span>dev<span class=\"sy0\">\/<\/span>tcp<span class=\"sy0\">\/<\/span>tahakhan<span class=\"sy0\">\/<\/span><span class=\"nu0\">22<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">&amp;&gt;\/<\/span>dev<span class=\"sy0\">\/<\/span>null <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Port Open&quot;<\/span> <span class=\"sy0\">||<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Port Closed&quot;<\/span><\/div><\/div>\n<p>If the TCP Port is Open, the echo command will display \u201cPort Open\u201d, as seen in our case:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1032\" height=\"67\" class=\"wp-image-21533\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-15.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-15.png 1032w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-15-300x19.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-15-1024x66.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-15-768x50.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-15-1020x67.png 1020w\" sizes=\"auto, (max-width: 1032px) 100vw, 1032px\" \/><\/p>\n<p>To check if the Port 64 is Opened, modify the Bash Script as:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"br0\">&#40;<\/span><span class=\"kw3\">echo<\/span> <span class=\"sy0\">&gt;\/<\/span>dev<span class=\"sy0\">\/<\/span>tcp<span class=\"sy0\">\/<\/span>tahakhan<span class=\"sy0\">\/<\/span><span class=\"nu0\">64<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">&amp;&gt;\/<\/span>dev<span class=\"sy0\">\/<\/span>null <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Port Open&quot;<\/span> <span class=\"sy0\">||<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Port Closed&quot;<\/span><\/div><\/div>\n<p>Now if the TCP Port is closed, the echo command will display \u201cPort Closed\u201d in the terminal, as seen in our case:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1031\" height=\"61\" class=\"wp-image-21534\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-16.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-16.png 1031w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-16-300x18.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-16-1024x61.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-16-768x45.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-16-1020x61.png 1020w\" sizes=\"auto, (max-width: 1031px) 100vw, 1031px\" \/><\/p>\n<p><strong>Viewing Network Service List to Check Whether Port is Opened or Closed<\/strong><\/p>\n<p>The \u201c\/etc\/services\u201d text file consists of the names of the services along with the assigned port numbers. Thus, by viewing the file, you can see which Ports are Open. To open the file use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">cat<\/span> <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>services<\/div><\/div>\n<p>The highlighted \u201cuse of each TCP Protocol\u201d means those Ports are Open:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1033\" height=\"489\" class=\"wp-image-21535\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-17.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-17.png 1033w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-17-300x142.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-17-1024x485.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21517-17-768x364.png 768w\" sizes=\"auto, (max-width: 1033px) 100vw, 1033px\" \/><\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>To check if the TCP port is open or closed on Ubuntu 22.04, the \u201c<strong>lsof<\/strong>\u201d, \u201c<strong>ss<\/strong>\u201d, \u201c<strong>netstat<\/strong>\u201d, \u201c<strong>nmap<\/strong>\u201d, \u201c<strong>netcat<\/strong>\u201d, and \u201c<strong>Bash<\/strong>\u201d commands are used. Finding out Open Ports is among the crucial concerns of a System Administrator as leaving them unchecked will lead to potential security concerns. This article explained the step-by-step procedure of different methods to check active or open TCP Ports.<\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>Check the step-by-step procedure of finding Open TCP Ports using \u201clsof\u201d, \u201css\u201d, \u201cnetstat\u201d, \u201cnmap\u201d, \u201cnetcat\u201d, \u201cBash\u201d Commands and Methods. <\/p>","protected":false},"author":110,"featured_media":21522,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-21517","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=21517"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21517\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/21522"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=21517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=21517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=21517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}