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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • 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. sudo Command Examples in Linux
  2. Upgrading from CentOS/RHEL 7 to CentOS/RHEL 8 using Leapp
  3. Linux OS Service ‘sshd’
  4. How to Install Mokutil package on CentOS/RHEL 7 and 8
  5. nohup: command not found
  6. whiptail: command not found
  7. amass db – Interact with an Amass database (Command Examples)
  8. getopt Command Examples in Linux
  9. CentOS / RHEL 7 : How to Modify GRUB2 Arguments with grubby
  10. How to disable a specific command for a specific user in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • gixy Command Examples
  • gitsome Command Examples
  • gitmoji Command Examples
  • gitlint Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright