6 Networking Uses for the Linux nc Command

The Netcat tool is very popular among network administrators. However, it can also serve you different objectives as a casual Linux user in your daily life. By researching, I found large use cases to play with the NC command.
6
Establish network connections
I will start with the simplest and most fundamental thing that NC can do. That is to say create a direct TCP or UDP connection between two machines. This is the construction block of many most advanced uses.
Suppose you have two Linux systems. For the demonstration, I use an Ubuntu machine and a Linux mint machine. Ubuntu will serve as a server or earphone while Linux Mint will act as a customer or connector.
First, start the listener.
nc -l -p 12345
The indicator -L is for the earpiece mode. The -P indicator is to define the port. Here, we use Port 12345, but you can use any other you want, unless it is already used. After executing this order, your terminal will just be seated there, waiting for a connection.
Now from the connector machine, run:
nc ip_address port_number
Replace ip_address with the IP address of your server. The port number will be the one you have used in your listener. So in my case:
nc 192.168.100.4 12345
This establishes a TCP connection between the two machines. In addition, it is not a unidirectional communication where only the server can send. On the contrary, both parties can send and receive.
If you want to establish a UDP connection instead, you can do so using the -u flag. On the auditor’s machine, run:
nc -u -l -p 12345
On the customer machine, run:
nc -u 192.168.100.4 12345
UDP connections are less reliable than TCP. However, they are preferable when speed is critical and that a little data loss is acceptable.
5
Port
In addition to connecting to a single port, NC can check a whole range of ports to see which are open. This can help to help out or list services. This is also why NC is sometimes called the “Swiss army knife” of networking.
Let’s say you want to find the TCP ports open to your server machine from your customer machine. You create a earpiece on the server as before.
nc -l -p 12345
Then the customer, execute:
nc -z -v 192.168.100.4 20-130
The indicator -Z indicates NetCat to use zero -i / o mode. This means that NetCat should simply look for demons to listen to instead of sending data. The -V indicator allows verbose mode, which simply displays the results in a better way. We also define the range of 20 to 130 ports that interest us.
Only the digitization systems you have or have an explicit authorization to scan. Unauthorized digitization can be illegal or considered hostile.
In the demonstration, almost all the ports refused the connection because they were closed, with the exception of Port 80. In the same way, you can also scan the UDP ports.
4
File transfer
Netcat can send files directly to TCP or UDP without the need for FTP, SCP or shared folders. This makes it ideal for fast transfers, especially when other methods are not available. Let’s send a file from my Linux Mint to my Ubuntu machine.
I already have a file called test.txt, which reads “file to test the transfer of NC files”. First of all, create a earpiece on the machine where you want to transfer the file. In doing so, redirect the connection output to a file, like this.
nc -l -p 12345 > received.txt
Then, on the sender’s machine, redirect the content of the file to your NC connection like this.
nc 192.168.100.4 12345
To check if the transfer has succeeded, use the LS command to see if the file is there, then the CAT command to read its content. You can also do the opposite and send files to the goal. All you have to do is reverse the roles using the orders I have shown above.
3
Create a web server
Netcat can act as a simple HTTP server by listening to a port and sending raw HTML to anyone who connects. This will not replace Apache or Nginx, of course. However, for fast demos, debugging or learning HTTP requests, it’s perfect.
First of all, create a static HTML file (I call it page.html) on your server machine. For me, it’s Ubuntu.
echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" > page.html
In this file, I included an HTTP status line, a content type of content and a simple HTML body. THE \r\n The sequences are essential in HTTP to separate the headers from the body.
Now start a netcat web server on the same machine:
while true; do nc -l -p 8080
We use a time loop here so that even after each request, the server remains standing. We listen to the 8080 port and send the HTML file in response. Now open a web browser from the customer machine (Linux Mint in my case) and visit the HTML static page by specifying the IP and the port.
http://192.168.100.4:8080
You can also use the Curl command to recover the HTML page.
curl http://192.168.100.4:8080
2
Create a simple cat application
Netcat can connect two terminals, so everything you type on one appears instantly on the other. With a small shell deception, you can make it a cat tool in real time. No need for third -party chat applications.
To do this, you must create a network connection as indicated in the first section. Create a listener on a single machine:
nc -l -p 12345
Then connect from another machine.
nc 192.168.100.4 12345
Once you have established the connection, you can type anything on a terminal. It will appear on the other terminal. Then you can type anything on the second terminal, and it will appear in the first.
You can now type back and forth in the terminal to send messages from one computer to another. I tried to become a little creative and see if I could also add the sender names with the corresponding messages. In this way, it is easier to follow who sent what message.
You can do this by adding the shipper name using Echo and a time loop. Here is the configuration. Perform this on the server machine:
while read msg; do echo "Ubuntu: $msg"; done | nc -l -p 12345
And this on the connector machine:
while read msg; do echo "LM: $msg"; done | nc 192.168.100.4 12345
The above command awaits the entry, prefix and sends it. Upon reception, the prefix appears exactly as sent. Even if there are more than two users, you can follow the messages this way.
1
Network troubleshooting
Finally, you can use the NC command to solve certain common networking problems. When something is wrong on a network, it can help confirm whether it is a connectivity problem, a service problem or a firewall problem. He can test the ports, simulate services and check the raw responses.
Suppose a request does not respond. You can use NC to see if you can even connect to its port.
nc -zv 192.168.100.4 22
If it is hung or said “the connection refused”, the port is closed or filtered.
You can speak manually to a service to see if it responds as expected. For example, check if a local HTTP service responds.
nc 192.168.100.4 8080
If the service works, it will send an HTTP response header and maybe an HTML. It is ideal for debugging poorly compliant web applications without browser.
If you think that an application can connect to a port but do not send the right data, you can pretend to be the service and capture what the application sends. This is useful for debugging personalized customer / server protocols. With Netcat, you can quickly do these little tests and know where your network fails and how to fix it.
You can do much more with Netcat. If you are interested, you can consult the official manual page of NC. There are many more Linux networking orders you need to explore.


