Cyberithub

17 Useful nc command examples in Linux (RedHat/CentOS 7/8)

In this article, I will take you through 17 Useful nc command examples in Linux. nc command is a widely used tool in Linux based systems to manage and troubleshoot local and remote network devices and services. It is also used by Linux Professionals, Network Administrators and Penetration Testors to perform Port Scanning, reading/writing data to/from network.

There are other Open source tools available along with nc tool in Linux Based Systems but we will keep our focus on nc command in this session and will cover other utilities in future articles.

Syntax

ncat/nc [OPTIONS…] [hostname] [port]

17 Useful nc command examples in Linux (RedHat/CentOS 7/8) 1

nc command examples

Also Read: How to Install netcat command in Linux (RedHat/CentOS 7/8)

Example 1. Check nc command version

You need to use nc --version command to find the nc command version as shown below. As you can check from below output, current version is 7.50.

[root@localhost ~]# nc --version
Ncat: Version 7.50 ( https://nmap.org/ncat )

–version : Display Ncat’s version information and exit

Example 2. Check Google Port 443

If you want to connect Google Port 443 using nc command then you need to use nc -v google.com 443 command as shown below.

[root@localhost ~]# nc -v google.com 443
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 216.58.196.174:443.

-v : Set verbosity level (can be used several times)

Example 3. Use only IPV4 to check Google Port 443

If you want to force only IPV4 address to connect remote URL then you need to use -4 option with nc command as shown below.

[root@localhost ~]# nc -v -4 google.com 443
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 216.58.196.174:443.

-4 : Force the use of IPv4 only.

Example 4. Check UDP Ports

By default nc command will check for remote TCP Ports. If you want to check UDP Ports instead then you need to use -u option with nc command as shown below.

[root@localhost ~]# nc -v -u google.com 443
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 216.58.196.174:443.

-u : Use UDP instead of default TCP

Example 5. Run a Node in Server mode using nc command

If you want the Server to start listening on some port for incoming connections say on Port 12000 in this example, then you need to use nc -l 12000 command as shown below.

[root@localhost ~]# nc -l 12000

-l : Bind and listen for incoming connections

Example 6. Run a Node in Client mode using nc command

If you want to send data to the Server on Port 12000 from localhost then you need to use nc localhost 12000 command as shown below. After running below command you can write your message and press enter. You can see the data getting received on Server end where you started Listening on Port 12000 as shown in above example.

[root@localhost ~]# nc localhost 12000
Hi,This is from CyberITHub

After writing the messages, press Enter. Now go back to Server and check the output. You will see below output at the Server end.

[root@localhost ~]# nc -l 12000
Hi,This is from CyberITHub

Example 7. Keep Server Listener Open using nc command

You might have observed that once client is disconnected by pressing Ctrl+C, server connection also disconnects. So to avoid this server disconnection you can use -k flag at the server end to force it to remain up even after client disconnection.

[root@localhost ~]# nc localhost 12000
^C

We have used -k flag here to force the server to listen to Port 12000 even when client connection got disconnected.

[root@localhost ~]# nc -l -k localhost 12000

-k : Accept multiple connections in listen mode

Example 8: Configure Unidirectional Proxy

If you want to redirect all the incoming traffic from Port 443 of local system to another server 192.168.0.104 Port 443 then you need to use below nc command.

[root@localhost ~]# nc -l 443 | nc 192.168.0.104 443

Example 9: Configure Bi-directional Proxy

In the above example, you can only enable the proxy for Incoming traffic, it will not work for outgoing traffic. To enable proxy for both incoming as well as for outgoing traffic we need to use the pipe concept. First we need to create a pipe using mkfifo command and then use it redirect incoming and outgoing traffic through proxy using nc command as shown below.

[root@localhost ~]# mkfifo pipe
[root@localhost ~]# nc -l -p 443 <pipe | nc 192.168.0.104 80 >pipe

-p : Specify source port to use

Example 10: Send data from a file using nc command

You also have an option to send data through a file instead through STDIN where you need to use the --send-only option to redirect the file.txt contents to listening server on Port 443 as shown below.

[root@localhost ~]# nc -l 192.168.0.104 443 --send-only < file.txt

–send-only : Only send data, ignoring received; quit on EOF

Example 11: Bind a bash shell to a Port(Open a Backdoor Connection)

When you want to open a backdoor connection to some port then you can use nc command as mentioned below. Here we are binding /bin/bash shell to Port 12000.

[root@localhost ~]# nc -l 12000 -e /bin/bash

-e : Execute the specified command after a connection has been established.

Example 12: Enable Port Forwarding using nc command

Another useful example of nc command is for Port Forwarding. You can use below command to forward all the incoming traffic from Port 443 to Port 80.

[root@localhost ~]# nc -u -l 443 -c 'nc -u -l 80'

-c : Same as -e, except it tries to execute the command via /bin/sh

Example 13: Terminate Connection with the Server after 10 seconds

If you want to terminate client connection with the server after n secs then you need to use the time(in secs) as an argument with -w option to terminate the connection after that much time.

[root@localhost ~]# nc -w 10 localhost 12000

-w : Set a fixed timeout for connection attempts.

NOTE:

Please do not use -w flag with -l option on server end. It has to be used on client end.

Example 14: Delay the messages from client end

If you want to delay the sent messages from the client by few seconds then you need to use -d option with time(in secs) specified as an argument as shown below..

[root@localhost ~]# nc -d 10 localhost 12000
this is from cyberithub

-d : Set the delay interval for lines sent.

Example 15: Receiving Incoming Data to a File

Sometimes instead of receiving all the data to STDOUT you might want all the output in a file. This can be easily achieved by redirection operator to redirect all the output to a file as shown below.

[root@localhost ~]# nc -l localhost 12000 > output.txt

Example 16: Prevent DNS Lookups

If you want to prevent DNS looksup for a particular hostname then you need to use -n option with nc command as shown below. In this example we are preventing google.com DNS lookup by using nc -n google.com 443 command.

[root@localhost ~]# nc -n google.com 443
Ncat: Could not resolve hostname "google.com": Name or service not known. QUITTING.

-n : Do not resolve hostnames via DNS

Example 17: Check Other nc command options

If you want to check other options that can be used with nc command in Linux then you need to use nc -h command to check that as specified below.

[root@localhost ~]# nc -h
Ncat 7.50 ( https://nmap.org/ncat )
Usage: ncat [options] [hostname] [port]

Options taking a time assume seconds. Append 'ms' for milliseconds,
's' for seconds, 'm' for minutes, or 'h' for hours (e.g. 500ms).
-4 Use IPv4 only
-6 Use IPv6 only
-U, --unixsock Use Unix domain sockets only
-C, --crlf Use CRLF for EOL sequence
-c, --sh-exec <command> Executes the given command via /bin/sh
-e, --exec <command> Executes the given command
--lua-exec <filename> Executes the given Lua script
-g hop1[,hop2,...] Loose source routing hop points (8 max)
-G <n> Loose source routing hop pointer (4, 8, 12, ...)
-m, --max-conns <n> Maximum <n> simultaneous connections
-h, --help Display this help screen
-d, --delay <time> Wait between read/writes

 

Popular Recommendations:-

Create SAN Certificate

How to Enable or Disable SELinux Temporarily or Permanently on RedHat/CentOS 7/8

10 Popular Examples of sudo command in Linux(RedHat/CentOS 7/8)

9 useful w command in Linux with Examples

12 Most Popular rm command in Linux with Examples

Create a Self Signed Certificate using OpenSSL

Leave a Comment