Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 9Network · Linux

How to test if a remote port is open

"The app can not reach the database." Is the problem the network, a firewall, or the service itself? Testing the TCP port from the client side answers it in seconds. This how-to shows several ways (some tool is always installed), how to read the three possible answers, how to test UDP, TLS and from inside Kubernetes.

  • 5 Lessen
  • 13 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

9.1

The quick answer

nc -zv db1.example.com 5432
Connection to db1.example.com 5432 port [tcp/postgresql] succeeded!
timeout 3 bash -c '</dev/tcp/db1.example.com/5432' && echo open || echo closed
curl -v telnet://db1.example.com:5432
nmap -p 22,80,443,5432 db1.example.com

nc comes from nmap-ncat (RHEL) or netcat-openbsd (Ubuntu); the bash line needs no extra tool at all.

9.2

Step by step

  1. Test from the machine that has the problem (the app server), not from your laptop — firewalls are different for each source.
  2. Read the answer — there are only three:
AnswerMeaningLook at
succeeded / openNetwork and firewall are fine; something listens.The app's config, credentials, TLS.
Connection refusedThe host answered "nothing listens on this port".The service on the target: running? right port? bound to 127.0.0.1 only?
timed out / filteredPackets are dropped on the way.Firewalls (target host, cloud security group, network), routing, VPN.
  1. Check the name resolves to the right IP first:
getent hosts db1.example.com
10.0.2.21       db1.example.com
  1. More detail with nmap (also shows several ports at once):
nmap -Pn -p 5432 db1.example.com
PORT     STATE    SERVICE
5432/tcp filtered postgresql
  1. TLS ports — check the certificate and the handshake, not only the port:
openssl s_client -connect shop.example.com:443 -servername shop.example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -enddate
curl -vI https://shop.example.com 2>&1 | grep -E 'Connected|SSL connection|HTTP/'
  1. UDP (DNS, NTP, WireGuard) has no handshake — a "no answer" can mean open or filtered:
nc -zvu 10.0.0.53 53
dig @10.0.0.53 example.com +short
nmap -sU -p 53,123 10.0.0.53

The best UDP test is a real request (dig for DNS, chronyc for NTP).

  1. From inside Kubernetes — start a throwaway Pod in the same namespace:
kubectl run nettest -n shop --rm -it --image=docker.io/library/busybox:1.37 --restart=Never -- nc -zv -w 3 postgres 5432

9.3

Check that it worked

After a fix (firewall rule, service started), the same test turns from timed out/refused to succeeded — then test the real client (for example psql -h db1 -U app -c 'select 1').

Commando's in deze les

9.4

Common problems

ProblemCauseFix
nc: command not foundNot installed.dnf install nmap-ncat / apt install netcat-openbsd, or use the bash /dev/tcp line.
refused although the service runsIt listens on 127.0.0.1 only, or on another port.On the target: ss -ltnp; change the bind address.
timed outA firewall drops the traffic.Open the port on the host and in the cloud firewall (only for the client network).
Works by IP, not by nameDNS points to another IP.getent hosts NAME; fix DNS or /etc/hosts.
Port open, app still failsTLS, credentials or app-level rules (for example pg_hba.conf).Read the client error; test with the real client.
Different results from different serversRules allow only some sources.Test from the real client host.

9.5

Go further

1What is the difference between "connection refused" and "timed out"?

Refused = the host answered but nothing listens; timed out = a firewall or the network dropped the packets.

2How can you test a TCP port without installing any tool?

timeout 3 bash -c '</dev/tcp/HOST/PORT' && echo open.

Alle commando's in dit hoofdstuk