nc Command in Linux

Learn via video courses
Topics Covered

Overview

The nc command in Linux, often referred to as Netcat, is a versatile tool utilized for reading, writing, redirecting data across networks. It creates both TCP and UDP sockets for network communication. It is often termed as 'Swiss-army knife' for TCP/IP due to its flexibility and capability to perform various network related tasks.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of nc Command in Linux

The basic syntax of the nc command in Linux is as follows:

Where:

  • options: These are flags that modify the behavior of the nc command.
  • hostname: This refers to the network device on which the connection is to be established.
  • port[s]: This is the network port number where nc should connect to or listen at.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Options in nc Command in Linux

  1. -l: Used to specify that nc should listen for an incoming connection rather than initiate a connection.

    For example -

    Output:

    This command makes nc listen on port 1234 for incoming connections.

  2. -v: Used to make the operation more talkative or verbose.

    For example -

    Output:

    This command attempts to connect to 'localhost' on port 1234, and outputs verbose information about the connection.

  3. -z: Makes nc to scan for listening daemons, without sending any data to them.

    For example -

    Output:

    This command checks if the ports from 20 to 25 on 'localhost' are open.

Example Usages

  • Transferring files between machines:

    Output:

    Explanation: The nc command in Linux is used to send 'file_to_send.txt' from the sender machine to the receiver machine.

  • Chatting between two systems:

    Output:

    Explanation: The nc command creates a simple chat system between two hosts.

Tips

  • The nc command can be combined with other commands using pipes for powerful results.

  • Always ensure that the port you are trying to use is not blocked by any firewall rules.

  • For security purposes, avoid running nc with root privileges unless absolutely necessary.

Advanced Use Cases of nc Command in Linux

  • Performing a port scanning:

    Output:

    Explanation: This advanced use of the nc command in Linux scans ports from 20 to 80 on the specified IP address, informing which ones are open and which ones are closed.

  • Setting up a reverse shell:

    Output:

    Explanation: This advanced use of the nc command sets up a reverse shell, allowing the listener to execute commands on the victim's machine. Use with caution and only for legitimate purposes.

  • Performing a banner grabbing:

    Output:

    Explanation: This advanced use of the nc command in Linux grabs the HTTP banner of 'www.google.com'. This technique is often used in reconnaissance stages of penetration testing.

Conclusion

  • The nc command in Linux is a powerful tool for network diagnostics and data transfer.

  • NC can operate in two modes: listen mode and connect mode.

  • NC can be used for file transfers, port scanning, banner grabbing, and even setting up a chat system or a reverse shell.

  • Despite its power, nc should be used responsibly and ethically due to its potential in network exploitation.

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more