Test connectivity between two machines with netcat

Test connectivity between two machines with netcat

To test TCP connectivity

In the server machine

nc -lvk 8989

In client machine ( example.com is the server address)

nc example.com 8989 -v

Flag explanation

  • -l: listening instead of request
  • -v: verbose
  • -k: keep connection after one client exits. Note: in a UDP connection, this flag implies listening to multiple clients.

After connected, type some text from the client or server to send data. The connection can be terminated with the EOF character via Ctrl-D keystroke.


To test a UDP connection

Add -u flag to both commands.


Allow/disallow a port with ufw tool

Check status: sudo ufw status

If the status is inactive, you need to activate it with sudo ufw enable. To disable it after the experiment, use sudo ufw disable.

To open a TCP port: sudo ufw allow 8989/tcp. Similarly, to open a UDP port sudo ufw allow 1191/dcp.

To close a TCP/UDP port, change the action word from allow to disallow.

sudo ufw disallow 8989/tcp
sudo ufw disallow 8989/udp

Happy code <3

Buy Me A Coffee