- 6 ways to Check a remote port is open in Linux
- Methods to check if a remote port is open in Linux
- Use nc command to check the remote port is open in Linux
- Use nmap to check the remote port is open in Linux
- Use telnet to check the remote port is open in Linux
- Use python telnet to check remote port is open in Linux
- Use python socket to check remote port is open in Linux
- Use curl to check remote port is open in Linux
- How to check opened/closed ports on my computer?
- 8 Answers 8
- How to check if a certain port is open and unused?
- 9 Answers 9
- Eg 1:
- Eg 2:
6 ways to Check a remote port is open in Linux
Checking remote port status is a common task for Linux admin. Now we collect 6 different ways for this task. We don’t need to install any package if we use the following two python commands. We need to install the package if we choose nc, nmap,telnet.
Methods to check if a remote port is open in Linux
The following commands can be used to check if a port is open on the remote server in Linux.
- Use nc command nc -zvw10 192.168.0.1 22
- Use nmap command nmap 192.168.0.1 -p 22
- Use telnet command telnet 192.168.0.1 22
- Use python telnet module
- Use python socket module
- Use curl command
Use nc command to check the remote port is open in Linux
$ nc [-options] [HostName or IP] [PortNumber]
- z: zero-I/O mode which is used for scanning
- v: for verbose output
- w10: timeout wait 10 seconds
The “nc” command stands for “netcat”. The “nc” command is a very versatile command that can be used for a variety of purposes, including network administration and data transmission.
For example, the “nc” command can be used to create a simple TCP connection between two computers. The “nc” command can be used to connect to a remote server on a given port and send/receive data.
For example, if you want to connect to a remote server on port xx, you would use the following command: nc -zv port
In this example, “” is the IP address or hostname of the remote server, and “” is the port that you want to connect to.
I needed to see if the port 22 (SSH) on a remote machine was open, so I opened a terminal and ran the following command:
The -v option enabled verbose output, and the -z option instructed nc to only scan for open ports, without actually establishing a connection.
The output showed me the results of the port scan:
Connection to hostname.com 22 port [tcp/ssh] succeeded!
This told me that the port 22 was open and that I could connect to the remote machine using SSH.
In another scenario, if the port was not open, the output would look something like this:
nc: connect to hostname.com port 22 (tcp) failed: Connection refused
You can also use the “nc” command to open a port in Linux. To do this, you would use the following command: nc -l -p 1234
In this example, “-l” is used to listen for a connection on port 1234
Use nmap to check the remote port is open in Linux
$ nmap [-options] [HostName or IP] [-p] [PortNumber]
The “nmap” command is a command-line tool used for network exploration and security auditing. The “nmap” command can be used to scan for open ports on a remote server, as well as to identify the operating system and services running on that server.
For example, if you want to scan for open ports on a remote server, you would use the following command:
In this example, “” is the IP address or hostname of the remote server, and “” is the port that you want to scan.
Use telnet to check the remote port is open in Linux
$ telnet [HostName or IP] [PortNumber]
The telnet command is a command-line tool used for network communication. The telnet command can be used to connect to a remote server on a given port.
For example, if you want to connect to a remote server on port, you would use the following command: telnet port
In this example, “” is the IP address or hostname of the remote server, and “” is the port that you want to connect to.
Use python telnet to check remote port is open in Linux
python -c «import telnetlib; tel=telnetlib.Telnet(‘192.168.0.1′,’22’,10); print tel; tel.close()»
If you are using Python3, using the following command:
python3 -c «import telnetlib; tel=telnetlib.Telnet(‘10.248.169.140′,’5432’,10); print(tel); tel.close()»
Telnetlib is a module in Python that allows you to communicate with remote servers using the Telnet protocol. The Telnet protocol is a text-based protocol used for communicating with remote servers.
To use the Telnetlib module, you first need to import it into your Python program: import telnetlib
Next, you need to create an instance of the Telnet object: telnet = telnetlib.Telnet()
The Telnet object has a number of methods that allow you to send and receive data. For example, the send() method allows you to send text data to the remote server, and the recv() method allows you to receive text data from the remote server.
Use python socket to check remote port is open in Linux
Python -c «import socket; s = socket.socket(); s.settimeout(10); s.connect((‘192.168.0.1’, 22)); «
The “socket” module is a module in Python that allows you to create and use sockets. A socket is a communication channel that allows two processes to connect and send/receive data.
The “socket” module has a number of functions that allow you to do a variety of things, including creating sockets, binding sockets to addresses, and sending/receiving data.
In order to use the “socket” module, you first need to import it into your Python program. You can do this by using the following command: import socket
Once you have imported the “socket” module, you can then use its functions to create sockets and communicate with other processes.
Use curl to check remote port is open in Linux
We have another solution for this with the curl command. curl -v telnet://192.168.0.1:22
The “curl” command is a tool used for transferring data with URL syntax. The “curl” command can be used to send data to a remote server, or it can be used to download data from a remote server.
If you want to download data from a remote server, you can use the following command: curl port -o filename.txt
In this example, “” is the IP address or hostname of the remote server, and “” is the port that you want to download data from.
The “curl” command can also be used to check whether a port is open or not. To do this, you would use the following command:
In this example, “” is the IP address or hostname of the remote server, and “” is the port that you want to check.
How to check opened/closed ports on my computer?
@Justgivemeaname: nmap is a tool to check for open ports on another host. If you can run netstat on a machine, it’s much faster and reliable to use it.
@DavidFoerster: Didn’t know about netstat , so I learned that. It says in the link that it should be used from another host, though. Thanks!
8 Answers 8
There’s a few parameters to netstat that are useful for this :
- -l or —listening shows only the sockets currently listening for incoming connection.
- -a or —all shows all sockets currently in use.
- -t or —tcp shows the tcp sockets.
- -u or —udp shows the udp sockets.
- -n or —numeric shows the hosts and ports as numbers, instead of resolving in dns and looking in /etc/services.
You use a mix of these to get what you want. To know which port numbers are currently in use, use one of these:
netstat -atn # For tcp netstat -aun # For udp netstat -atun # For both
In the output all port mentioned are in use either listening for incoming connection or connected to a peer** all others are closed. TCP and UDP ports are 16 bits wide (they go from 1-65535)
** They can also be connecting/disconnecting from the peer.
If it shows some process its used. Its closed(not used) if there is no output.
Another alternative command line easy to use to find out which process is using a port:
lsof -n -i4TCP:$PORT | grep LISTEN
I added the next function in my .bash_profile,
and now run «pslisten 5060» to see who is grabing my SIP port.
It’s work with Apple Mac OS X too.
Is the port status «LISTENING» indicated that the port is opened?
Yes. It means that some service is listening to that port on your computer for incoming connection i.e. this port is open for establishing new connections.
Any port that are not shown in the output indicated that it’s closed?
Yes. Remember netstat -a will show all active (listening) and passive (non-listening) connections i.e. the ports that are acting as both server (some services are listening to these ports for connections from a different machine/process) and established (connections are established on these ports regardless of the fact the host/a service can be a server or client)
All TCP and UDP ports belong to a category called sockets and there are a whole lot of those. To view socket info you can check man ss .
Thanks. you wrote that -a means server and established. Does «server» means ports that are being listened at by some services? Does «established» mean ports where there are existing connections regardless of it is a client or server’s port? Then what kinds of ports does -a not show?
I don’t think the -a option means «all active» sockets; it just means «all». netstat shows all active sockets by default, but leaves out the passive sockets (open, listening). By using the -a option both active and passive sockets are shown.
@heemayl The second part of your answer is still not correct. A TCP socket in the «listening» state can never be a connection; it is not connected to anything, it is only listening. Listening TCP sockets are also called passive sockets because of this. If a client attempts to connect to a (listening) socket on a server, a new socket will be created on the server to establish a connection with the client. A socket which is part of an established connection is called an active socket.
Another option is ss. It’s much easier to use.
The below command will only output a list of current listening sockets.
root@server:~# ss -l Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port u_dgr UNCONN 0 0 * 23353 * 23352 u_dgr UNCONN 0 0 * 568 * 362 u_dgr UNCONN 0 0 * 14836 * 14837 u_dgr UNCONN 0 0 * 20446 * 369 u_dgr UNCONN 0 0 * 22877 * 369 u_dgr UNCONN 0 0 * 504 * 347 u_dgr UNCONN 0 0 * 16298 * 369 u_dgr UNCONN 0 0 * 23343 * 369 u_dgr UNCONN 0 0 * 24125 * 369 u_dgr UNCONN 0 0 * 24617 * 369 u_dgr UNCONN 0 0 * 23352 * 23353 u_dgr UNCONN 0 0 * 23334 * 369 u_dgr UNCONN 0 0 * 17113 * 369 u_dgr UNCONN 0 0 * 16957 * 369 u_dgr UNCONN 0 0 * 14793 * 362 u_dgr UNCONN 0 0 * 23345 * 362 u_dgr UNCONN 0 0 * 24070 * 369 udp UNCONN 0 0 *:sunrpc *:* udp UNCONN 0 0 *:981 *:* udp UNCONN 0 0 . sunrpc . * udp UNCONN 0 0 . 981 . * tcp LISTEN 0 128 127.0.0.1:85 *:* tcp LISTEN 0 128 *:ssh *:* tcp LISTEN 0 128 *:3128 *:* tcp LISTEN 0 100 127.0.0.1:smtp *:* tcp LISTEN 0 128 *:8006 *:* tcp LISTEN 0 128 *:sunrpc *:* tcp LISTEN 0 128 . ssh . * tcp LISTEN 0 100 ::1:smtp . * tcp LISTEN 0 128 . sunrpc . *
How to check if a certain port is open and unused?
Could you please help and tell how can i find out if port 80 is open and unused so that I can start installation.
For what it’s worth, /etc/services is completely static. Grepping it can tell you if a port is officially designated by IANA or some such, but does not tell whether or not it’s in local use.
9 Answers 9
sudo netstat -anp | grep ':80 '
That should give you pid & name of the process that holds port 80
This can be achieved using the nc command as follows:
It will return TRUE if the port is already in use, or FALSE is it (i.e, available not listening currently).
I don’t recommend lsof or netstat method as it first try to scan all running PIDs to get all bounded ports:
# time lsof -i:8888 real 0m1.194s user 0m0.137s sys 0m1.056s``` # time nc -z 127.0.0.1 8888 real 0m0.014s user 0m0.011s sys 0m0.004s
Here 8888 is an unused port. The nc command is ~85 times faster in the above example.
Eg 1:
$ nc -z 127.0.0.1 80 && echo "IN USE" || echo "FREE" IN USE $ nc -z 127.0.0.1 81 && echo "IN USE" || echo "FREE" FREE
Eg 2:
If you are trying with a remote IP, it is better to add a timeout to auto-exit if it is not accepting connection for the specified time.
Its Google’s IP which is not used, so it will timeout after trying for 2 seconds.
This also works greatly when running inside the Docker image that uses host network. Inside the image, lsof incorrectly reports the port is not in use when it actually is.
The traditional version of nc does not include the -z option. See the differences between traditional and openbsd.
netstat -tln | tail -n +3 | awk '< print $4 >'
This one displays bind addresses of TCP listening endpoints. All other endpoints are free; Also if on Unix and you are not root, then you can’t bind to a ‘privileged’ port number (port number lower than 1024).
Explained in more details:
- netstat -tln — all listening tcp ports
- tail -n +3 — cut off the header of netstat command
- awk ‘< print $4 >‘ — print the fourth column that consists of [ip]:[port]
For the general case you still need to care to cut out all irrelevant interfaces; a listening address 0.0.0.0 is listening on all network cards, if there is an IP address then that’s the specific IP of the network card/network interface.