CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Network Transfer & Tools / nc

nc

Test ports and move raw bytes over TCP/UDP

Intermediatenetworktcptroubleshooting

nc (netcat) opens a raw TCP or UDP connection, or listens for one, and connects it to your terminal or a pipe. DevOps engineers use it daily to answer 'can this host reach that port?', to speak a protocol by hand, and to test firewalls between two machines.

Syntaxnc [-zv] [-w SEC] [-u] HOST PORT[-PORT] | nc -l [-k] PORT

Watch it run

The app can't reach its database and cache. Test the exact ports from the app server and tell the three outcomes apart.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
nc -zv -w 3 10.0.3.20 5432

The TCP handshake completed: the network path and the security groups allow it, and Postgres is listening. If the app still fails, look at credentials or pg_hba.conf, not the network.

Anatomy of a command

$
commandnc

netcat. On Ubuntu it's the OpenBSD variant (netcat-openbsd); options differ slightly from ncat and traditional nc.

How it works

In client mode nc creates a socket and calls connect() to the host and port, which for TCP sends a SYN. A SYN-ACK means something is listening: nc reports success and, unless -z, copies stdin to the socket and the socket to stdout until one side closes. A TCP RST back means 'Connection refused': the host is reachable but nothing listens there (or a firewall rejects). No answer at all means the packets are being dropped, typically by a security group or firewall, and nc waits until -w expires: 'timed out'. With -l it calls bind() and listen() and waits for one client (-k to keep accepting more). UDP is connectionless, so nc -u can't really prove a port is open: it only notices 'closed' if an ICMP port-unreachable comes back. nc's -v messages go to stderr, so pipe with 2>&1 if you want to grep them.

Options you'll actually use

-zScan mode: connect and close, send no data
-vVerbose: report success or the error (on stderr)
-w SECConnection and idle timeout
-uUDP instead of TCP
-lListen for an incoming connection on a port
-kWith -l, keep listening after a client disconnects
-NShut down the socket after EOF on stdin (so the other side sees the end)
-q SECQuit SEC seconds after EOF on stdin (Debian/Ubuntu)
-nDon't resolve names
-4 / -6Force IPv4 or IPv6
-s ADDRUse this local source address
-UUse a Unix domain socket path instead of host and port

Common mistakes

nc -z host 443 && echo open without -w

If the port is filtered, nc waits for the kernel's TCP timeout (over two minutes) before failing.

nc -z -w 3 host 443 && echo open
nc -zv host 1-1000 | grep succeeded

-v messages go to stderr, so grep sees nothing and every line is printed anyway.

nc -zv host 1-1000 2>&1 | grep succeeded
Trusting nc -zu host 53 to prove a UDP service is up

UDP has no handshake. Silence might mean open or might mean dropped; only an ICMP 'port unreachable' gives a definite answer.

Test with the real protocol: dig @host example.com
Copying flags from ncat or traditional nc docs (-e, -c, -p with -l)

Variants differ: OpenBSD nc has no -e and listens with nc -l PORT.

Check nc -h; on Ubuntu it's 'OpenBSD netcat'.

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Check whether anything listens on TCP port 22 of localhost, with a 2-second timeout and a verbose message.

  2. 2

    Start a listener on port 9000 in the background writing to /tmp/got.txt, send it the text 'ping' from a second nc, then show the file.

  3. 3

    Scan ports 20 to 30 on localhost and show only the open ones.

Related commands

telnetQuick TCP port check (never for logins)nmapScan hosts and ports, detect servicesssSee listening ports and open connectionscurlMake HTTP requests and debug APIs
PreviousrsyncNextnmap
Linux / Network Transfer & Tools / nc

nc

Test ports and move raw bytes over TCP/UDP

Intermediatenetworktcptroubleshooting

nc (netcat) opens a raw TCP or UDP connection, or listens for one, and connects it to your terminal or a pipe. DevOps engineers use it daily to answer 'can this host reach that port?', to speak a protocol by hand, and to test firewalls between two machines.

Syntaxnc [-zv] [-w SEC] [-u] HOST PORT[-PORT] | nc -l [-k] PORT

Watch it run

The app can't reach its database and cache. Test the exact ports from the app server and tell the three outcomes apart.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
nc -zv -w 3 10.0.3.20 5432

The TCP handshake completed: the network path and the security groups allow it, and Postgres is listening. If the app still fails, look at credentials or pg_hba.conf, not the network.

Anatomy of a command

$
commandnc

netcat. On Ubuntu it's the OpenBSD variant (netcat-openbsd); options differ slightly from ncat and traditional nc.

How it works

In client mode nc creates a socket and calls connect() to the host and port, which for TCP sends a SYN. A SYN-ACK means something is listening: nc reports success and, unless -z, copies stdin to the socket and the socket to stdout until one side closes. A TCP RST back means 'Connection refused': the host is reachable but nothing listens there (or a firewall rejects). No answer at all means the packets are being dropped, typically by a security group or firewall, and nc waits until -w expires: 'timed out'. With -l it calls bind() and listen() and waits for one client (-k to keep accepting more). UDP is connectionless, so nc -u can't really prove a port is open: it only notices 'closed' if an ICMP port-unreachable comes back. nc's -v messages go to stderr, so pipe with 2>&1 if you want to grep them.

Options you'll actually use

-zScan mode: connect and close, send no data
-vVerbose: report success or the error (on stderr)
-w SECConnection and idle timeout
-uUDP instead of TCP
-lListen for an incoming connection on a port
-kWith -l, keep listening after a client disconnects
-NShut down the socket after EOF on stdin (so the other side sees the end)
-q SECQuit SEC seconds after EOF on stdin (Debian/Ubuntu)
-nDon't resolve names
-4 / -6Force IPv4 or IPv6
-s ADDRUse this local source address
-UUse a Unix domain socket path instead of host and port

Common mistakes

nc -z host 443 && echo open without -w

If the port is filtered, nc waits for the kernel's TCP timeout (over two minutes) before failing.

nc -z -w 3 host 443 && echo open
nc -zv host 1-1000 | grep succeeded

-v messages go to stderr, so grep sees nothing and every line is printed anyway.

nc -zv host 1-1000 2>&1 | grep succeeded
Trusting nc -zu host 53 to prove a UDP service is up

UDP has no handshake. Silence might mean open or might mean dropped; only an ICMP 'port unreachable' gives a definite answer.

Test with the real protocol: dig @host example.com
Copying flags from ncat or traditional nc docs (-e, -c, -p with -l)

Variants differ: OpenBSD nc has no -e and listens with nc -l PORT.

Check nc -h; on Ubuntu it's 'OpenBSD netcat'.

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Check whether anything listens on TCP port 22 of localhost, with a 2-second timeout and a verbose message.

  2. 2

    Start a listener on port 9000 in the background writing to /tmp/got.txt, send it the text 'ping' from a second nc, then show the file.

  3. 3

    Scan ports 20 to 30 on localhost and show only the open ones.

Related commands

telnetQuick TCP port check (never for logins)nmapScan hosts and ports, detect servicesssSee listening ports and open connectionscurlMake HTTP requests and debug APIs
PreviousrsyncNextnmap