• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

netcat Command Examples in Linux

by admin

The netcat command can be used to test connectivity and send data across network connections. The command may be spelled out as “netcat” or abbreviated as “nc” depending on the distribution. Systems may be identified by IP address or by hostname. When troubleshooting, use netcat to listen on the destination computer and attempt a connection from the source computer in order to verify network functionality.

Syntax

The syntax of the netcat command is:

# netcat [options]

If you need more flexibility in connecting to a remote host than a command like telnet host port allows, use netcat (or nc). netcat can connect to remote TCP/UDP ports, specify a local port, listen on ports, scan ports, redirect standard I/O to and from network connections, and more. To open a TCP connection to a port with netcat, run

# netcat host port

netcat only terminates when the other side of the connection ends the connection, which can confuse things if you redirect standard input to netcat. You can end the connection at any time by pressing CTRL-C. (If you’d like the program and network connection to terminate based on the standard input stream, try the sock program instead.)

To listen on a particular port, run

# netcat -l -p port_number

netcat Command Examples

1. Connect two computers for the purpose of transferring information:

On comp1 (listen on port):

# netcat -l 4242

On comp2 (connect to listener):

# netcat comp1

2. Transfer file content between two computers:

On comp1 (listen on port):

# netcat -l 4242 > received.file

On comp2 (connect to listener):

# netcat comp1 < original.file

3. Port scan a computer

# netcat -z -v domain.tld 1-1000       ### scans ports 1 to 1000

Conclusion

The netcat utility can read from and write to any network port, making it a virtual Swiss army knife for the networking world. You can use netcat to test just about any type of network situation, including building your very own client/server test tool. What makes netcat even more versatile is that it accepts input from redirection and piping, so you can easily use it in scripts to create simple ad hoc servers and clients for just about any network testing you need to do.

Filed Under: Linux

Some more articles you might also be interested in …

  1. exec Command Examples in Linux
  2. flashrom: command not found
  3. firejail Command Examples in Linux
  4. How to Save Command history of Selected Users in Linux
  5. How to use auditd to monitor a specific SYSCALL
  6. SSH Connection Refused by TCP Wrapper
  7. CentOS / RHEL : How to add, delete and display LVM tags
  8. CentOS / RHEL 5 : How to password-protect single user mode
  9. env Command Examples in Linux
  10. “userdel: user xxx is currently used by process yyy” – Unable to delete an User

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright