ncTest ports and move raw bytes over TCP/UDP
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.
nc [-zv] [-w SEC] [-u] HOST PORT[-PORT] | nc -l [-k] PORTThe app can't reach its database and cache. Test the exact ports from the app server and tell the three outcomes apart.
ncnetcat. On Ubuntu it's the OpenBSD variant (netcat-openbsd); options differ slightly from ncat and traditional nc.
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.
-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 portnc -z host 443 && echo open without -wIf the port is filtered, nc waits for the kernel's TCP timeout (over two minutes) before failing.
nc -z -w 3 host 443 && echo opennc -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 succeededTrusting nc -zu host 53 to prove a UDP service is upUDP 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.comCopying 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'.Try these on a Linux machine of your own. Each task has a hint and a solution.
Check whether anything listens on TCP port 22 of localhost, with a 2-second timeout and a verbose message.
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.
Scan ports 20 to 30 on localhost and show only the open ones.
ncTest ports and move raw bytes over TCP/UDP
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.
nc [-zv] [-w SEC] [-u] HOST PORT[-PORT] | nc -l [-k] PORTThe app can't reach its database and cache. Test the exact ports from the app server and tell the three outcomes apart.
ncnetcat. On Ubuntu it's the OpenBSD variant (netcat-openbsd); options differ slightly from ncat and traditional nc.
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.
-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 portnc -z host 443 && echo open without -wIf the port is filtered, nc waits for the kernel's TCP timeout (over two minutes) before failing.
nc -z -w 3 host 443 && echo opennc -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 succeededTrusting nc -zu host 53 to prove a UDP service is upUDP 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.comCopying 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'.Try these on a Linux machine of your own. Each task has a hint and a solution.
Check whether anything listens on TCP port 22 of localhost, with a 2-second timeout and a verbose message.
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.
Scan ports 20 to 30 on localhost and show only the open ones.