Ways to check for open ports on RHEL

Latest response

Hi, what are some of the ways to check for open ports on RHEL 7.5? I use lsof -i usually are there other ways also? Thanks! :)

Responses

try

netstat -taupel

Thanks, this works great! :)

Hi,

Many ways, depending on what you want to achieve. Some common tools on Linux:

nmap
ss (included in iproute2 package and substitute of the netstat)
nc (netcat)

Here are few more unusual or undocumented ways:

BASH

# cat < /dev/tcp/127.0.0.1/22
SSH-2.0-OpenSSH_5.3

cURL

# curl -v telnet://127.0.0.1:22
* About to connect() to 127.0.0.1 port 22 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 22 (#0)
SSH-2.0-OpenSSH_5.3

Python

# python
Python 2.6.6 (r266:84292, Aug 9 2016, 06:11:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 22))
>>> clientsocket.send('\n')

Perl

#!/usr/bin/perl
use IO::Socket::INET;
$| = 1;
my $mysocket = new IO::Socket::INET(
  PeerHost => '127.0.0.1',
  PeerPort => '22',
  Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $mysocket;
print "connected to the server\n";

Regards,

Dusan Baljevic (amateur radio VK2COT)

Thanks, this was very helpful! :)

Echoing to the /dev/tcp/<HOSTNAME|HOSTADDR>/<PORT> method is the most Linux-portable method. You don't have to worry "is / installed on the host I want to my script on". It's a big part of why I use it as a generic diagnostic method: I never have those "shit: / so I can't use my habitual method to troubleshoot" moments.

That said, a "better" way of using the /dev/tcp method is:

timeout 1 bash -c " echo > /dev/tcp/${HOST}/${PORT} " > /dev/null 2>&1 && echo "Port ${PORT} is open" || echo "Port ${PORT} is closed"

I say "better" because you the method shown can "hang" (pending a -C or similar action): not good when you're automating stuff.

I deal with a lot of @Core RHEL systems, so, not needing to rely on "extra" tools is key.

That's really cool. The bash packet interface is such a handy addition, I use it a bit too.

When some applications are not yet configured or running, we can set up TCP/UDP listeners and even test firewall rules well.

TCP port 1. Start Netcat server listener on a given server:

# nc -l localhost 5454
  1. Test from remote server (Netcat client):
# nc -v remoteserver 5454
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Connected to remoteserver:5454.

UDP port 3. Start Netcat server listener on a given server:

# nc -ul localhost 2115
  1. Test from remote server (Netcat client):
# nc -u remoteserver 2115

Because UDP is a connection-less protocol, the only real way to check it is to send some data, so from the client side type any string, it should show on the remote server.

Regards,

Dusan Baljevic (amateur radio VK2COT)

Thanks!

Hello Team,

I've installed RHEL7.2 64 bit with Minimum Setting option at my server HP Proliant DL20 Gen 9 with Intel Processor and have public address and a virtual internal network.

I've done port forwarded to my server internal private IP Address to public address so that i can ssh from outside.

After fresh installation i've setup internet connection at very begnnning at the time of installation & after installation i was able to ping google.com on console and was able to ssh internally, but as soon as i tried to ssh from outside network my internet connection got lost and even after rebooting and restarting network service i tried ping google.com its says unable to resolve host.

So after looking many link for solution at web i ended up in reinstalled my Operating system and then tried ping google.com surprisingly it was working and tried again ssh internally and after that still ping google.com it was working, but as soon as i tried ssh from outside network the internet connection got lost again.

NOTE : Early i was using RHEL6 and there was no problem even after ssh from outside network it was working just fine and the reason the switch from RHEL6 to RHEL7 is docker have support for RHEL7 and above.

I've a strong feeling that something is getting updated in network script or somewhere else at my server machine.

I'm lost here, can you please help me, need assistance.

Appericate your help.

Thank You.

Regards, Amit

Hi

You can aslo use nmap -O localhost to see the open ports

Thanks Sadiq

Thanks for your help!

Yes, that is nice info by Dusan. Another utility that we could use is "ss" (socket statistics) which is also good (#ss -tln)

Thanks! :)

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.